2022-08-12 15:11:43 +00:00
|
|
|
package parser
|
|
|
|
|
2022-08-30 05:11:10 +00:00
|
|
|
import "git.tebibyte.media/arf/arf/file"
|
2022-09-04 18:04:48 +00:00
|
|
|
import "git.tebibyte.media/arf/arf/types"
|
2022-09-27 18:48:05 +00:00
|
|
|
import "git.tebibyte.media/arf/arf/lexer"
|
2022-09-04 23:30:59 +00:00
|
|
|
import "git.tebibyte.media/arf/arf/infoerr"
|
2022-08-15 05:47:42 +00:00
|
|
|
|
2022-08-12 15:11:43 +00: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 16:55:17 +00:00
|
|
|
license string
|
|
|
|
author string
|
|
|
|
|
2022-09-07 21:12:46 +00:00
|
|
|
requires map[string] string
|
2022-09-04 21:13:49 +00:00
|
|
|
sections map[string] Section
|
2022-08-12 15:11:43 +00:00
|
|
|
}
|
2022-08-15 05:47:42 +00:00
|
|
|
|
2022-09-04 18:02:48 +00:00
|
|
|
// 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 18:04:48 +00:00
|
|
|
Location () (location file.Location)
|
|
|
|
Permission () (permission types.Permission)
|
|
|
|
Name () (name string)
|
2022-09-04 23:30:59 +00:00
|
|
|
NewError (message string, kind infoerr.ErrorKind) (err error)
|
|
|
|
ToString (indent int) (output string)
|
2022-09-04 18:02:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 02:27:06 +00:00
|
|
|
// Identifier represents a chain of names separated by a dot.
|
2022-08-15 05:47:42 +00:00
|
|
|
type Identifier struct {
|
2022-09-04 02:17:05 +00:00
|
|
|
locatable
|
|
|
|
trail []string
|
2022-08-15 05:47:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 02:30:14 +00:00
|
|
|
// TypeKind represents what kind of type a type is.
|
2022-08-15 05:47:42 +00:00
|
|
|
type TypeKind int
|
|
|
|
|
|
|
|
const (
|
2022-09-29 06:01:31 +00:00
|
|
|
// TypeKindNil means that the type is unspecified.
|
|
|
|
TypeKindNil TypeKind = iota
|
|
|
|
|
2022-09-12 19:27:29 +00:00
|
|
|
// TypeKindBasic means its a normal type and inherits from something.
|
|
|
|
// Basic types can define new members on their parent types.
|
2022-09-29 06:01:31 +00:00
|
|
|
TypeKindBasic
|
2022-08-15 05:47:42 +00:00
|
|
|
|
2022-09-12 19:27:29 +00:00
|
|
|
// TypeKindPointer means it's a pointer.
|
2022-08-15 05:47:42 +00:00
|
|
|
TypeKindPointer
|
|
|
|
|
2022-09-05 02:27:06 +00:00
|
|
|
// TypeKindVariableArray means it's an array of variable length.
|
|
|
|
TypeKindVariableArray
|
2022-08-15 05:47:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Type represents a type specifier
|
|
|
|
type Type struct {
|
2022-09-04 02:17:05 +00:00
|
|
|
locatable
|
2022-08-15 05:47:42 +00:00
|
|
|
|
|
|
|
mutable bool
|
|
|
|
kind TypeKind
|
2022-08-16 14:44:02 +00:00
|
|
|
length uint64
|
2022-08-15 05:47:42 +00:00
|
|
|
|
2022-08-15 18:04:57 +00:00
|
|
|
// only applicable for basic.
|
2022-08-15 05:47:42 +00:00
|
|
|
name Identifier
|
|
|
|
|
2022-08-15 18:04:57 +00:00
|
|
|
// not applicable for basic.
|
2022-08-15 05:47:42 +00:00
|
|
|
points *Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// Declaration represents a variable declaration.
|
|
|
|
type Declaration struct {
|
2022-09-04 02:17:05 +00:00
|
|
|
locatable
|
2022-09-04 02:33:34 +00:00
|
|
|
nameable
|
2022-09-04 02:56:08 +00:00
|
|
|
typeable
|
2022-08-15 05:47:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 18:17:03 +00:00
|
|
|
// List represents an array or object literal.
|
|
|
|
type List struct {
|
|
|
|
locatable
|
2022-09-27 22:03:27 +00:00
|
|
|
|
|
|
|
// TODO: have an array of unnamed arguments, and a map of named
|
|
|
|
// arguments
|
2022-09-27 18:17:03 +00:00
|
|
|
multiValuable
|
|
|
|
}
|
|
|
|
|
2022-08-15 05:47:42 +00:00
|
|
|
// ArgumentKind specifies the type of thing the value of an argument should be
|
|
|
|
// cast to.
|
|
|
|
type ArgumentKind int
|
|
|
|
|
|
|
|
const (
|
2022-08-17 15:30:17 +00:00
|
|
|
ArgumentKindNil ArgumentKind = iota
|
|
|
|
|
2022-08-15 05:47:42 +00:00
|
|
|
// [name argument]
|
|
|
|
// [name argument argument]
|
|
|
|
// etc...
|
2022-09-03 16:54:41 +00:00
|
|
|
ArgumentKindPhrase
|
2022-08-15 05:47:42 +00:00
|
|
|
|
2022-09-27 18:17:03 +00:00
|
|
|
// (argument argument argument)
|
|
|
|
ArgumentKindList
|
|
|
|
|
2022-08-16 17:43:36 +00:00
|
|
|
// {name}
|
|
|
|
ArgumentKindDereference
|
|
|
|
|
|
|
|
// {name 23}
|
|
|
|
ArgumentKindSubscript
|
|
|
|
|
2022-08-15 05:47:42 +00:00
|
|
|
// name.name
|
|
|
|
// name.name.name
|
|
|
|
// etc...
|
|
|
|
ArgumentKindIdentifier
|
|
|
|
|
|
|
|
// name:Type
|
|
|
|
// name:{Type}
|
2022-08-17 03:45:25 +00:00
|
|
|
// name:{Type ..}
|
2022-08-15 05:47:42 +00:00
|
|
|
// name:{Type 23}
|
|
|
|
// etc...
|
|
|
|
ArgumentKindDeclaration
|
|
|
|
|
|
|
|
// -1337
|
|
|
|
ArgumentKindInt
|
|
|
|
|
|
|
|
// 1337
|
|
|
|
ArgumentKindUInt
|
|
|
|
|
|
|
|
// 0.44
|
|
|
|
ArgumentKindFloat
|
|
|
|
|
|
|
|
// "hello world"
|
|
|
|
ArgumentKindString
|
|
|
|
|
|
|
|
// 'S'
|
|
|
|
ArgumentKindRune
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-04 02:17:05 +00:00
|
|
|
locatable
|
|
|
|
kind ArgumentKind
|
|
|
|
value any
|
2022-08-15 05:47:42 +00: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-04 02:17:05 +00:00
|
|
|
locatable
|
2022-09-04 02:33:34 +00:00
|
|
|
nameable
|
2022-09-04 02:56:08 +00:00
|
|
|
typeable
|
2022-09-04 03:03:09 +00:00
|
|
|
permissionable
|
2022-09-27 20:13:02 +00:00
|
|
|
valuable
|
2022-09-05 18:09:47 +00:00
|
|
|
|
|
|
|
external bool
|
2022-08-18 20:56:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 15:07:39 +00:00
|
|
|
// TypeSectionMember represents a member variable of a type section.
|
|
|
|
type TypeSectionMember struct {
|
|
|
|
locatable
|
|
|
|
nameable
|
|
|
|
typeable
|
|
|
|
permissionable
|
|
|
|
valuable
|
|
|
|
|
|
|
|
bitWidth uint64
|
|
|
|
}
|
|
|
|
|
2022-09-11 20:15:02 +00:00
|
|
|
// TypeSection represents a type definition.
|
2022-08-20 06:46:40 +00:00
|
|
|
type TypeSection struct {
|
2022-09-04 02:17:05 +00:00
|
|
|
locatable
|
2022-09-04 02:33:34 +00:00
|
|
|
nameable
|
2022-09-04 02:56:08 +00:00
|
|
|
typeable
|
2022-09-04 03:03:09 +00:00
|
|
|
permissionable
|
2022-09-27 20:13:02 +00:00
|
|
|
valuable
|
2022-09-28 15:07:39 +00:00
|
|
|
|
|
|
|
// if non-nil, this type defines new members.
|
|
|
|
members []TypeSectionMember
|
2022-08-18 20:56:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 21:13:40 +00:00
|
|
|
// EnumMember represents a member of an enum section.
|
2022-08-24 05:16:44 +00:00
|
|
|
type EnumMember struct {
|
2022-09-04 02:17:05 +00:00
|
|
|
locatable
|
2022-09-04 02:33:34 +00:00
|
|
|
nameable
|
2022-09-04 07:31:35 +00:00
|
|
|
valuable
|
2022-08-24 05:16:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-21 06:40:04 +00:00
|
|
|
// EnumSection represents an enumerated type section.
|
|
|
|
type EnumSection struct {
|
2022-09-04 02:17:05 +00:00
|
|
|
locatable
|
2022-09-04 02:33:34 +00:00
|
|
|
nameable
|
2022-09-04 02:56:08 +00:00
|
|
|
typeable
|
2022-09-04 03:03:09 +00:00
|
|
|
permissionable
|
2022-09-04 02:56:08 +00:00
|
|
|
|
2022-09-04 03:03:09 +00:00
|
|
|
members []EnumMember
|
2022-08-21 06:40:04 +00:00
|
|
|
}
|
2022-08-23 14:56:37 +00:00
|
|
|
|
2022-09-29 19:45:25 +00:00
|
|
|
// FaceKind determines if an interface is a type interface or an function
|
|
|
|
// interface.
|
|
|
|
type FaceKind int
|
|
|
|
|
|
|
|
const (
|
2022-09-29 19:52:14 +00:00
|
|
|
FaceKindEmpty FaceKind = iota
|
|
|
|
FaceKindType
|
2022-09-29 19:45:25 +00:00
|
|
|
FaceKindFunc
|
|
|
|
)
|
|
|
|
|
2022-08-23 14:56:37 +00:00
|
|
|
// FaceBehavior represents a behavior of an interface section.
|
|
|
|
type FaceBehavior struct {
|
2022-09-04 02:17:05 +00:00
|
|
|
locatable
|
2022-09-04 02:33:34 +00:00
|
|
|
nameable
|
2022-08-23 14:56:37 +00:00
|
|
|
|
|
|
|
inputs []Declaration
|
|
|
|
outputs []Declaration
|
|
|
|
}
|
|
|
|
|
|
|
|
// FaceSection represents an interface type section.
|
|
|
|
type FaceSection struct {
|
2022-09-04 02:17:05 +00:00
|
|
|
locatable
|
2022-09-04 02:33:34 +00:00
|
|
|
nameable
|
2022-09-04 03:03:09 +00:00
|
|
|
permissionable
|
2022-08-24 22:57:07 +00:00
|
|
|
inherits Identifier
|
2022-09-29 19:45:25 +00:00
|
|
|
|
|
|
|
kind FaceKind
|
|
|
|
|
2022-09-05 06:04:37 +00:00
|
|
|
behaviors map[string] FaceBehavior
|
2022-09-29 19:45:25 +00:00
|
|
|
FaceBehavior
|
2022-08-23 14:56:37 +00:00
|
|
|
}
|
2022-08-25 16:02:43 +00:00
|
|
|
|
2022-09-03 19:49:47 +00:00
|
|
|
// PhraseKind determines what semantic role a phrase plays.
|
|
|
|
type PhraseKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
PhraseKindCall = iota
|
|
|
|
PhraseKindCallExternal
|
|
|
|
PhraseKindOperator
|
2022-09-04 00:32:27 +00:00
|
|
|
PhraseKindAssign
|
2022-09-03 20:24:13 +00:00
|
|
|
PhraseKindReference
|
2022-09-03 19:49:47 +00:00
|
|
|
PhraseKindDefer
|
|
|
|
PhraseKindIf
|
|
|
|
PhraseKindElseIf
|
|
|
|
PhraseKindElse
|
|
|
|
PhraseKindSwitch
|
|
|
|
PhraseKindCase
|
|
|
|
PhraseKindWhile
|
|
|
|
PhraseKindFor
|
|
|
|
)
|
|
|
|
|
2022-09-03 19:22:18 +00:00
|
|
|
// Phrase represents a function call or operator. In ARF they are the same
|
|
|
|
// syntactical concept.
|
|
|
|
type Phrase struct {
|
|
|
|
location file.Location
|
2022-09-05 15:49:19 +00:00
|
|
|
returnees []Argument
|
2022-09-27 18:17:03 +00:00
|
|
|
multiValuable
|
2022-09-27 18:48:05 +00:00
|
|
|
|
2022-09-03 19:49:47 +00:00
|
|
|
kind PhraseKind
|
2022-09-27 19:48:47 +00:00
|
|
|
|
|
|
|
// TODO: do not have this be an argument. make a string version, and
|
|
|
|
// and identifier version.
|
2022-09-27 18:48:05 +00:00
|
|
|
command Argument
|
|
|
|
|
|
|
|
// only applicable for PhraseKindOperator
|
|
|
|
operator lexer.TokenKind
|
2022-09-03 19:49:47 +00:00
|
|
|
|
|
|
|
// only applicable for control flow phrases
|
2022-09-03 19:22:18 +00:00
|
|
|
block Block
|
|
|
|
}
|
|
|
|
|
2022-08-25 16:02:43 +00:00
|
|
|
// Block represents a scoped/indented block of code.
|
|
|
|
type Block []Phrase
|
|
|
|
|
2022-09-26 22:28:21 +00:00
|
|
|
// FuncOutput represents a function output declaration. It allows for a default
|
|
|
|
// value.
|
|
|
|
type FuncOutput struct {
|
|
|
|
Declaration
|
|
|
|
valuable
|
|
|
|
}
|
|
|
|
|
2022-08-25 16:02:43 +00:00
|
|
|
// FuncSection represents a function section.
|
|
|
|
type FuncSection struct {
|
2022-09-04 02:17:05 +00:00
|
|
|
locatable
|
2022-09-04 02:33:34 +00:00
|
|
|
nameable
|
2022-09-04 03:03:09 +00:00
|
|
|
permissionable
|
2022-08-25 16:02:43 +00:00
|
|
|
|
|
|
|
receiver *Declaration
|
|
|
|
inputs []Declaration
|
2022-09-29 06:29:35 +00:00
|
|
|
outputs []FuncOutput
|
2022-09-02 05:25:22 +00:00
|
|
|
root Block
|
|
|
|
|
|
|
|
external bool
|
2022-08-25 16:02:43 +00:00
|
|
|
}
|