2022-08-12 09:11:43 -06:00
|
|
|
package parser
|
|
|
|
|
2022-08-29 23:11:10 -06:00
|
|
|
import "git.tebibyte.media/arf/arf/file"
|
2022-09-04 12:04:48 -06:00
|
|
|
import "git.tebibyte.media/arf/arf/types"
|
2022-09-04 17:30:59 -06:00
|
|
|
import "git.tebibyte.media/arf/arf/infoerr"
|
2022-08-14 23:47:42 -06:00
|
|
|
|
2022-08-12 09:11:43 -06:00
|
|
|
// SyntaxTree represents an abstract syntax tree. It covers an entire module. It
|
|
|
|
// can be expected to be syntactically correct, but it might not be semantically
|
|
|
|
// correct (because it has not been analyzed yet.)
|
|
|
|
type SyntaxTree struct {
|
2022-08-12 10:55:17 -06:00
|
|
|
license string
|
|
|
|
author string
|
|
|
|
|
2022-09-04 15:13:49 -06:00
|
|
|
requires []string
|
|
|
|
sections map[string] Section
|
2022-08-12 09:11:43 -06:00
|
|
|
}
|
2022-08-14 23:47:42 -06:00
|
|
|
|
2022-09-04 12:02:48 -06:00
|
|
|
// SectionKind differentiates Section interfaces.
|
|
|
|
type SectionKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
SectionKindType = iota
|
|
|
|
SectionKindObjt
|
|
|
|
SectionKindEnum
|
|
|
|
SectionKindFace
|
|
|
|
SectionKindData
|
|
|
|
SectionKindFunc
|
|
|
|
)
|
|
|
|
|
|
|
|
// Section can be any kind of section. You can find out what type of section it
|
|
|
|
// is with the Kind method.
|
|
|
|
type Section interface {
|
2022-09-04 12:04:48 -06:00
|
|
|
Location () (location file.Location)
|
|
|
|
Kind () (kind SectionKind)
|
|
|
|
Permission () (permission types.Permission)
|
|
|
|
Name () (name string)
|
2022-09-04 17:30:59 -06:00
|
|
|
NewError (message string, kind infoerr.ErrorKind) (err error)
|
|
|
|
ToString (indent int) (output string)
|
2022-09-04 12:02:48 -06:00
|
|
|
}
|
|
|
|
|
2022-09-04 20:27:06 -06:00
|
|
|
// Identifier represents a chain of names separated by a dot.
|
2022-08-14 23:47:42 -06:00
|
|
|
type Identifier struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
|
|
|
trail []string
|
2022-08-14 23:47:42 -06:00
|
|
|
}
|
|
|
|
|
2022-09-04 20:30:14 -06:00
|
|
|
// TypeKind represents what kind of type a type is.
|
2022-08-14 23:47:42 -06:00
|
|
|
type TypeKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TypeKindBasic either means it's a primitive, or it inherits from
|
|
|
|
// something.
|
|
|
|
TypeKindBasic TypeKind = iota
|
|
|
|
|
|
|
|
// TypeKindPointer means it's a pointer
|
|
|
|
TypeKindPointer
|
|
|
|
|
2022-09-04 20:27:06 -06:00
|
|
|
// TypeKindArray means it's a fixed length array.
|
2022-08-14 23:47:42 -06:00
|
|
|
TypeKindArray
|
2022-09-04 20:19:19 -06:00
|
|
|
|
2022-09-04 20:27:06 -06:00
|
|
|
// TypeKindVariableArray means it's an array of variable length.
|
|
|
|
TypeKindVariableArray
|
2022-08-14 23:47:42 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Type represents a type specifier
|
|
|
|
type Type struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
2022-08-14 23:47:42 -06:00
|
|
|
|
|
|
|
mutable bool
|
|
|
|
kind TypeKind
|
|
|
|
|
2022-09-04 20:27:06 -06:00
|
|
|
// only applicable for fixed length arrays.
|
2022-08-16 08:44:02 -06:00
|
|
|
length uint64
|
2022-08-14 23:47:42 -06:00
|
|
|
|
2022-08-15 12:04:57 -06:00
|
|
|
// only applicable for basic.
|
2022-08-14 23:47:42 -06:00
|
|
|
name Identifier
|
|
|
|
|
2022-08-15 12:04:57 -06:00
|
|
|
// not applicable for basic.
|
2022-08-14 23:47:42 -06:00
|
|
|
points *Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// Declaration represents a variable declaration.
|
|
|
|
type Declaration struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
2022-09-03 20:33:34 -06:00
|
|
|
nameable
|
2022-09-03 20:56:08 -06:00
|
|
|
typeable
|
2022-08-14 23:47:42 -06:00
|
|
|
}
|
|
|
|
|
2022-08-16 22:49:49 -06:00
|
|
|
// ObjectInitializationValues represents a list of object member initialization
|
2022-08-16 21:45:25 -06:00
|
|
|
// attributes.
|
2022-08-16 22:49:49 -06:00
|
|
|
type ObjectInitializationValues struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
2022-08-16 21:45:25 -06:00
|
|
|
attributes map[string] Argument
|
2022-08-14 23:47:42 -06:00
|
|
|
}
|
|
|
|
|
2022-08-16 22:49:49 -06:00
|
|
|
// ArrayInitializationValues represents a list of attributes initializing an
|
|
|
|
// array.
|
|
|
|
type ArrayInitializationValues struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
|
|
|
values []Argument
|
2022-08-16 22:49:49 -06:00
|
|
|
}
|
|
|
|
|
2022-08-14 23:47:42 -06:00
|
|
|
// ArgumentKind specifies the type of thing the value of an argument should be
|
|
|
|
// cast to.
|
|
|
|
type ArgumentKind int
|
|
|
|
|
|
|
|
const (
|
2022-08-17 09:30:17 -06:00
|
|
|
ArgumentKindNil ArgumentKind = iota
|
|
|
|
|
2022-08-14 23:47:42 -06:00
|
|
|
// [name argument]
|
|
|
|
// [name argument argument]
|
|
|
|
// etc...
|
2022-09-03 10:54:41 -06:00
|
|
|
ArgumentKindPhrase
|
2022-08-14 23:47:42 -06:00
|
|
|
|
2022-08-16 11:43:36 -06:00
|
|
|
// {name}
|
|
|
|
ArgumentKindDereference
|
|
|
|
|
|
|
|
// {name 23}
|
|
|
|
ArgumentKindSubscript
|
|
|
|
|
2022-08-16 21:45:25 -06:00
|
|
|
// .name value
|
|
|
|
// but like, a lot of them
|
2022-08-16 22:49:49 -06:00
|
|
|
ArgumentKindObjectInitializationValues
|
|
|
|
|
|
|
|
// value value...
|
|
|
|
ArgumentKindArrayInitializationValues
|
2022-08-14 23:47:42 -06:00
|
|
|
|
|
|
|
// name.name
|
|
|
|
// name.name.name
|
|
|
|
// etc...
|
|
|
|
ArgumentKindIdentifier
|
|
|
|
|
|
|
|
// name:Type
|
|
|
|
// name:{Type}
|
2022-08-16 21:45:25 -06:00
|
|
|
// name:{Type ..}
|
2022-08-14 23:47:42 -06:00
|
|
|
// name:{Type 23}
|
|
|
|
// etc...
|
|
|
|
ArgumentKindDeclaration
|
|
|
|
|
|
|
|
// -1337
|
|
|
|
ArgumentKindInt
|
|
|
|
|
|
|
|
// 1337
|
|
|
|
ArgumentKindUInt
|
|
|
|
|
|
|
|
// 0.44
|
|
|
|
ArgumentKindFloat
|
|
|
|
|
|
|
|
// "hello world"
|
|
|
|
ArgumentKindString
|
|
|
|
|
|
|
|
// 'S'
|
|
|
|
ArgumentKindRune
|
|
|
|
|
|
|
|
// + - * / etc...
|
2022-08-15 12:04:57 -06:00
|
|
|
// this is only used as a phrase command
|
2022-08-14 23:47:42 -06:00
|
|
|
ArgumentKindOperator
|
|
|
|
)
|
|
|
|
|
|
|
|
// Argument represents a value that can be placed anywhere a value goes. This
|
|
|
|
// allows things like phrases being arguments to other phrases.
|
|
|
|
type Argument struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
|
|
|
kind ArgumentKind
|
|
|
|
value any
|
2022-08-14 23:47:42 -06:00
|
|
|
// TODO: if there is an argument expansion operator its existence should
|
|
|
|
// be stored here in a boolean.
|
|
|
|
}
|
|
|
|
|
|
|
|
// DataSection represents a global variable.
|
|
|
|
type DataSection struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
2022-09-03 20:33:34 -06:00
|
|
|
nameable
|
2022-09-03 20:56:08 -06:00
|
|
|
typeable
|
2022-09-03 21:03:09 -06:00
|
|
|
permissionable
|
2022-09-04 17:55:47 -06:00
|
|
|
valuable
|
2022-08-18 14:56:42 -06:00
|
|
|
}
|
|
|
|
|
2022-08-20 00:46:40 -06:00
|
|
|
// TypeSection represents a blind type definition.
|
|
|
|
type TypeSection struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
2022-09-03 20:33:34 -06:00
|
|
|
nameable
|
2022-09-03 20:56:08 -06:00
|
|
|
typeable
|
2022-09-03 21:03:09 -06:00
|
|
|
permissionable
|
2022-09-04 01:31:35 -06:00
|
|
|
valuable
|
2022-08-18 14:56:42 -06:00
|
|
|
}
|
|
|
|
|
2022-08-20 10:40:44 -06:00
|
|
|
// ObjtMember represents a part of an object type definition.
|
|
|
|
type ObjtMember struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
2022-09-03 20:33:34 -06:00
|
|
|
nameable
|
2022-09-03 20:56:08 -06:00
|
|
|
typeable
|
2022-09-03 21:03:09 -06:00
|
|
|
permissionable
|
2022-09-04 01:31:35 -06:00
|
|
|
valuable
|
2022-09-03 20:56:08 -06:00
|
|
|
|
2022-08-24 16:29:15 -06:00
|
|
|
bitWidth uint64
|
2022-08-20 00:46:40 -06:00
|
|
|
}
|
|
|
|
|
2022-09-01 15:13:40 -06:00
|
|
|
// ObjtSection represents an object type definition.
|
2022-08-20 10:40:44 -06:00
|
|
|
type ObjtSection struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
2022-09-03 20:33:34 -06:00
|
|
|
nameable
|
2022-09-03 21:03:09 -06:00
|
|
|
permissionable
|
2022-09-03 20:56:08 -06:00
|
|
|
inherits Identifier
|
2022-08-24 16:22:47 -06:00
|
|
|
|
2022-09-04 17:55:47 -06:00
|
|
|
members []ObjtMember
|
2022-08-14 23:47:42 -06:00
|
|
|
}
|
2022-08-21 00:40:04 -06:00
|
|
|
|
2022-09-01 15:13:40 -06:00
|
|
|
// EnumMember represents a member of an enum section.
|
2022-08-23 23:16:44 -06:00
|
|
|
type EnumMember struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
2022-09-03 20:33:34 -06:00
|
|
|
nameable
|
2022-09-04 01:31:35 -06:00
|
|
|
valuable
|
2022-08-23 23:16:44 -06:00
|
|
|
}
|
|
|
|
|
2022-08-21 00:40:04 -06:00
|
|
|
// EnumSection represents an enumerated type section.
|
|
|
|
type EnumSection struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
2022-09-03 20:33:34 -06:00
|
|
|
nameable
|
2022-09-03 20:56:08 -06:00
|
|
|
typeable
|
2022-09-03 21:03:09 -06:00
|
|
|
permissionable
|
2022-09-03 20:56:08 -06:00
|
|
|
|
2022-09-03 21:03:09 -06:00
|
|
|
members []EnumMember
|
2022-08-21 00:40:04 -06:00
|
|
|
}
|
2022-08-23 08:56:37 -06:00
|
|
|
|
|
|
|
// FaceBehavior represents a behavior of an interface section.
|
|
|
|
type FaceBehavior struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
2022-09-03 20:33:34 -06:00
|
|
|
nameable
|
2022-08-23 08:56:37 -06:00
|
|
|
|
|
|
|
inputs []Declaration
|
|
|
|
outputs []Declaration
|
|
|
|
}
|
|
|
|
|
|
|
|
// FaceSection represents an interface type section.
|
|
|
|
type FaceSection struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
2022-09-03 20:33:34 -06:00
|
|
|
nameable
|
2022-09-03 21:03:09 -06:00
|
|
|
permissionable
|
2022-08-24 16:57:07 -06:00
|
|
|
inherits Identifier
|
2022-08-23 08:56:37 -06:00
|
|
|
|
|
|
|
behaviors map[string] FaceBehavior
|
|
|
|
}
|
2022-08-25 10:02:43 -06:00
|
|
|
|
2022-09-03 13:49:47 -06:00
|
|
|
// PhraseKind determines what semantic role a phrase plays.
|
|
|
|
type PhraseKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
PhraseKindCall = iota
|
|
|
|
PhraseKindCallExternal
|
|
|
|
PhraseKindOperator
|
2022-09-03 18:32:27 -06:00
|
|
|
PhraseKindAssign
|
2022-09-03 14:24:13 -06:00
|
|
|
PhraseKindReference
|
2022-09-03 13:49:47 -06:00
|
|
|
PhraseKindDefer
|
|
|
|
PhraseKindIf
|
|
|
|
PhraseKindElseIf
|
|
|
|
PhraseKindElse
|
|
|
|
PhraseKindSwitch
|
|
|
|
PhraseKindCase
|
|
|
|
PhraseKindWhile
|
|
|
|
PhraseKindFor
|
|
|
|
)
|
|
|
|
|
2022-09-03 13:22:18 -06:00
|
|
|
// Phrase represents a function call or operator. In ARF they are the same
|
|
|
|
// syntactical concept.
|
|
|
|
type Phrase struct {
|
|
|
|
location file.Location
|
|
|
|
command Argument
|
|
|
|
arguments []Argument
|
|
|
|
returnsTo []Argument
|
|
|
|
|
2022-09-03 13:49:47 -06:00
|
|
|
kind PhraseKind
|
|
|
|
|
|
|
|
// only applicable for control flow phrases
|
2022-09-03 13:22:18 -06:00
|
|
|
block Block
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:02:43 -06:00
|
|
|
// Block represents a scoped/indented block of code.
|
|
|
|
type Block []Phrase
|
|
|
|
|
2022-09-01 15:13:40 -06:00
|
|
|
// FuncOutput represents an input a function section. It is unlike an input in
|
|
|
|
// that it can have a default value.
|
|
|
|
type FuncOutput struct {
|
|
|
|
Declaration
|
2022-09-04 01:31:35 -06:00
|
|
|
valuable
|
2022-09-01 15:13:40 -06:00
|
|
|
}
|
|
|
|
|
2022-08-25 10:02:43 -06:00
|
|
|
// FuncSection represents a function section.
|
|
|
|
type FuncSection struct {
|
2022-09-03 20:17:05 -06:00
|
|
|
locatable
|
2022-09-03 20:33:34 -06:00
|
|
|
nameable
|
2022-09-03 21:03:09 -06:00
|
|
|
permissionable
|
2022-08-25 10:02:43 -06:00
|
|
|
|
|
|
|
receiver *Declaration
|
|
|
|
inputs []Declaration
|
2022-09-01 15:13:40 -06:00
|
|
|
outputs []FuncOutput
|
2022-09-01 23:25:22 -06:00
|
|
|
root Block
|
|
|
|
|
|
|
|
external bool
|
2022-08-25 10:02:43 -06:00
|
|
|
}
|