THP AST
Created during the syntax analysis phase, from the stream of tokens produced by the lexic analysis phase.
File and modules
Every file has its own AST, and every file is a module.
AST = Module
Module = (Statement | Expression)*
Statement
A statement is either a variable binding, a function declaration, a conditional (for now, until those becom expressions), for loop, while loop, or an assignment.
Assignment includes the operators = += -= *= /=
, etc. Those operators
cannot be used elsewhere, only as part of an Assignment.
Statement = VariableBinding
| FunctionDeclaration
| Conditional
| ForLoop
| WhileLoop
| Assignment
Expression
See the Expression section
VariableBinding
VariableBinding = ImplicitBinding
| ExplicitBinding
ImplicitBinding = Datatype, Identifier, "=", Expression
ExplicitBinding = ("var" | "val"), Datatype?, Identifier, "=", Expression
FunctionDeclaration
FunctionDeclaration = "fun", Identifier, ParameterList, ("->", Datatype)?, Block
ParameterList = "(", (Parameter, ",")*, ")"
Parameter = Datatype, Identifier
Block
Block = "{", BlockMember*, "}"
BlockMember = Statement
| Expression
Assignment
The target of an assignment can only be an identifier for now. In the future this will include other things like maps, arrays, pattern matching, destructuring, etc.
Assignment = AssignmentTarget, AssignmentOperator, Expression
AssignmentTarget = Identifier
AssignmentOperator = "="
| "+="
| "-="
| "*/"
| "/="
| "%="