AST
The Abstract Syntax Tree (AST) is the data structure produced by the Parser. It captures the complete structure of a BrainRot program as a tree of typed nodes. The Interpreter then walks this tree to execute the program.
AST Overview
BrainRot's AST is defined in parser/ast.go and has 20+ node types. Every node implements either the Expression or Statement interface.
ast.go (excerpt)
// Every AST node implements one of two interfaces:
type Expression interface {
expressionNode()
TokenLiteral() string
}
type Statement interface {
statementNode()
TokenLiteral() string
}
// Example node — VarStatement
type VarStatement struct {
Token token.Token // the trust_me_bro token
Name *Identifier
Value Expression
}Node Types
| Node | Type | Description |
|---|---|---|
Program | Root | Top-level container |
VarStatement | Statement | trust_me_bro x = ... |
AssignStatement | Statement | x = ... / x += ... |
PrintStatement | Statement | say_my_name(...) |
IfStatement | Statement | if / else-if / else branches |
WhileStatement | Statement | on_repeat loop |
ForStatement | Statement | run_it_back loop |
FuncDecl | Statement | Function definition |
ReturnStatement | Statement | take_this |
BreakStatement | Statement | mission_abort |
ContinueStatement | Statement | skip_this_one |
IntegerLiteral | Expression | 42 |
FloatLiteral | Expression | 3.14 |
StringLiteral | Expression | "hello" |
BooleanLiteral | Expression | fr_fr / cap |
NilLiteral | Expression | ghosted |
ArrayLiteral | Expression | [1, 2, 3] |
Identifier | Expression | Variable reference |
InfixExpression | Expression | Binary operator |
PrefixExpression | Expression | Unary ! / - |
CallExpression | Expression | Function call |
IndexExpression | Expression | Array index arr[i] |
Viewing the AST
Use the ast command to print the full tree for any source file:
terminal
# Given this source:
# trust_me_bro x = 42
# say_my_name(x + 1)
# brainrot ast example.brt outputs:
Program
├── VarStatement
│ ├── Name: Identifier("x")
│ └── Value: IntegerLiteral(42)
└── PrintStatement
└── InfixExpression(+)
├── Left: Identifier("x")
└── Right: IntegerLiteral(1)