From b02ff6cda69ae1be6f214c7b7e24f6cc30884320 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 15 Aug 2022 01:47:42 -0400 Subject: [PATCH] Created structs needed to represent a data section --- parser/body.go | 3 +- parser/tree.go | 132 +++++++++++++++++++++++++++++++++++++ tests/parser/data/main.arf | 26 +++++--- 3 files changed, 152 insertions(+), 9 deletions(-) diff --git a/parser/body.go b/parser/body.go index cd05305..a7d77c1 100644 --- a/parser/body.go +++ b/parser/body.go @@ -10,8 +10,9 @@ func (parser *ParsingOperation) parseBody () (err error) { switch parser.token.Value().(string) { case "data": case "type": - case "func": case "face": + case "enum": + case "func": } return diff --git a/parser/tree.go b/parser/tree.go index 4b95d9d..d93bf11 100644 --- a/parser/tree.go +++ b/parser/tree.go @@ -1,5 +1,8 @@ package parser +import "git.tebibyte.media/sashakoshka/arf/file" +import "git.tebibyte.media/sashakoshka/arf/types" + // 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.) @@ -9,3 +12,132 @@ type SyntaxTree struct { requires []string } + +// Identifier represents a chain of arguments separated by a dot. +type Identifier struct { + location file.Location + trail []Argument +} + +// 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 { + location file.Location + + mutable bool + kind TypeKind + + // only applicable for arrays. a value of zero means it has an + // undefined/dynamic length. + length uint64 + + // not applicable for pointers. + name Identifier + + // only applicable for pointers. + points *Type +} + +// Declaration represents a variable declaration. +type Declaration struct { + location file.Location + name string + + what Type + value []Argument +} + +// ObjectAttribute represents a notation to initialize object attributes. It +// contains a name, and the value that the attribute should be initialized to. +type ObjectAttribute struct { + location file.Location + name string + value Argument +} + +// 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 +} + +// ArgumentKind specifies the type of thing the value of an argument should be +// cast to. +type ArgumentKind int + +const ( + // [name argument] + // [name argument argument] + // etc... + ArgumentKindPhrase ArgumentKind = iota + + // , name value + ArgumentKindObjectAttribute + + // 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... + 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 { + location file.Location + what 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 { + location file.Location + name string + + what Type + value []Argument + permission types.Permission +} diff --git a/tests/parser/data/main.arf b/tests/parser/data/main.arf index d3419cd..f781595 100644 --- a/tests/parser/data/main.arf +++ b/tests/parser/data/main.arf @@ -15,13 +15,23 @@ data wr integerArrayInitialized:{Int 16} 340 0 2304 0 4785 92 data wr object:Obj - : this 324 - : that 2139 + , this 324 + , that 2139 data wr nestedObject:Obj - : this - : bird0 324 - : bird1 "hello world" - : that - : bird2 123.8439 - : bird3 9328.21348239 + , this + , bird0 324 + , bird1 "hello world" + , that + , bird2 123.8439 + , bird3 9328.21348239 + + +# parsing an object literal +func rr main + --- + [let object:Obj + , this 324 + , that 2139] + + let object:Obj , this 324 , that 2139