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

NodeTypeDescription
ProgramRootTop-level container
VarStatementStatementtrust_me_bro x = ...
AssignStatementStatementx = ... / x += ...
PrintStatementStatementsay_my_name(...)
IfStatementStatementif / else-if / else branches
WhileStatementStatementon_repeat loop
ForStatementStatementrun_it_back loop
FuncDeclStatementFunction definition
ReturnStatementStatementtake_this
BreakStatementStatementmission_abort
ContinueStatementStatementskip_this_one
IntegerLiteralExpression42
FloatLiteralExpression3.14
StringLiteralExpression"hello"
BooleanLiteralExpressionfr_fr / cap
NilLiteralExpressionghosted
ArrayLiteralExpression[1, 2, 3]
IdentifierExpressionVariable reference
InfixExpressionExpressionBinary operator
PrefixExpressionExpressionUnary ! / -
CallExpressionExpressionFunction call
IndexExpressionExpressionArray 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)