This repository has been archived on 2024-02-27. You can view files and clone it, but cannot push or open issues or pull requests.
arf/parser/tree.go

280 lines
5.1 KiB
Go
Raw Normal View History

package parser
2022-08-29 23:11:10 -06:00
import "git.tebibyte.media/arf/arf/file"
// 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
requires []string
2022-08-18 15:39:19 -06:00
typeSections map[string] *TypeSection
objtSections map[string] *ObjtSection
2022-08-21 00:40:04 -06:00
enumSections map[string] *EnumSection
2022-08-23 08:56:37 -06:00
faceSections map[string] *FaceSection
2022-08-15 13:09:07 -06:00
dataSections map[string] *DataSection
2022-08-25 10:02:43 -06:00
funcSections map[string] *FuncSection
}
// Identifier represents a chain of arguments separated by a dot.
type Identifier struct {
locatable
trail []string
}
// TypeKind represents what kind of type a type is
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
// TypeKindArray means it's an array.
TypeKindArray
)
// Type represents a type specifier
type Type struct {
locatable
mutable bool
kind TypeKind
// only applicable for arrays. a value of zero means it has an
// undefined/dynamic length.
length uint64
// only applicable for basic.
name Identifier
// not applicable for basic.
points *Type
}
// Declaration represents a variable declaration.
type Declaration struct {
locatable
nameable
typeable
}
// ObjectInitializationValues represents a list of object member initialization
// attributes.
type ObjectInitializationValues struct {
locatable
attributes map[string] Argument
}
// ArrayInitializationValues represents a list of attributes initializing an
// array.
type ArrayInitializationValues struct {
locatable
values []Argument
}
// ArgumentKind specifies the type of thing the value of an argument should be
// cast to.
type ArgumentKind int
const (
ArgumentKindNil ArgumentKind = iota
// [name argument]
// [name argument argument]
// etc...
ArgumentKindPhrase
// {name}
ArgumentKindDereference
// {name 23}
ArgumentKindSubscript
// .name value
// but like, a lot of them
ArgumentKindObjectInitializationValues
// value value...
ArgumentKindArrayInitializationValues
// name.name
// name.name.name
// etc...
ArgumentKindIdentifier
// name:Type
// name:{Type}
// name:{Type ..}
// name:{Type 23}
// etc...
ArgumentKindDeclaration
// -1337
ArgumentKindInt
// 1337
ArgumentKindUInt
// 0.44
ArgumentKindFloat
// "hello world"
ArgumentKindString
// 'S'
ArgumentKindRune
// + - * / etc...
// this is only used as a phrase command
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 {
locatable
kind ArgumentKind
value any
// 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 {
locatable
nameable
typeable
permissionable
value Argument
2022-08-18 14:56:42 -06:00
}
// TypeSection represents a blind type definition.
type TypeSection struct {
locatable
nameable
typeable
permissionable
2022-09-04 01:31:35 -06:00
valuable
2022-08-18 14:56:42 -06:00
}
// ObjtMember represents a part of an object type definition.
type ObjtMember struct {
locatable
nameable
typeable
permissionable
2022-09-04 01:31:35 -06:00
valuable
2022-08-24 16:29:15 -06:00
bitWidth uint64
}
// ObjtSection represents an object type definition.
type ObjtSection struct {
locatable
nameable
permissionable
inherits Identifier
members []ObjtMember
}
2022-08-21 00:40:04 -06:00
// EnumMember represents a member of an enum section.
2022-08-23 23:16:44 -06:00
type EnumMember struct {
locatable
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 {
locatable
nameable
typeable
permissionable
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 {
locatable
nameable
2022-08-23 08:56:37 -06:00
inputs []Declaration
outputs []Declaration
}
// FaceSection represents an interface type section.
type FaceSection struct {
locatable
nameable
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
// 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
PhraseKindReference
PhraseKindDefer
PhraseKindIf
PhraseKindElseIf
PhraseKindElse
PhraseKindSwitch
PhraseKindCase
PhraseKindWhile
PhraseKindFor
)
// 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
kind PhraseKind
// only applicable for control flow phrases
block Block
}
2022-08-25 10:02:43 -06:00
// Block represents a scoped/indented block of code.
type Block []Phrase
// 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-08-25 10:02:43 -06:00
// FuncSection represents a function section.
type FuncSection struct {
locatable
nameable
permissionable
2022-08-25 10:02:43 -06:00
receiver *Declaration
inputs []Declaration
outputs []FuncOutput
2022-09-01 23:25:22 -06:00
root Block
external bool
2022-08-25 10:02:43 -06:00
}