ast

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package ast defines the Abstract Syntax Tree (AST) for the Monke programming language.

The AST represents the structure of a Monke program after it has been parsed. It consists of nodes that represent different language constructs such as expressions, statements, and literals. The AST is used by the evaluator to execute the program.

Key components:

  • Node: The base interface for all AST nodes
  • Statement: Interface for nodes that represent statements (e.g., let, return)
  • Expression: Interface for nodes that represent expressions (e.g., literals, function calls)
  • Program: The root node of the AST, containing a list of statements

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayLiteral

type ArrayLiteral struct {
	Token    token.Token  // The '[' token
	Elements []Expression // The elements of the array
}

ArrayLiteral represents an array literal expression in the AST. For example, "[1, 2 * 2, 3 + 3]".

func (*ArrayLiteral) String

func (al *ArrayLiteral) String() string

String returns a string representation of the array literal. Format: "[<elements>]"

func (*ArrayLiteral) TokenLiteral

func (al *ArrayLiteral) TokenLiteral() string

TokenLiteral returns the literal value of the token associated with this array.

type BlockStatement

type BlockStatement struct {
	Token      token.Token // The '{' token
	Statements []Statement // The statements within the block
}

BlockStatement represents a block of statements enclosed in braces. For example, "{ statement1; statement2; }".

func (*BlockStatement) String

func (bs *BlockStatement) String() string

String returns a string representation of the block statement. It concatenates the string representations of all statements in the block.

func (*BlockStatement) TokenLiteral

func (bs *BlockStatement) TokenLiteral() string

TokenLiteral returns the literal value of the token associated with this block.

type Boolean

type Boolean struct {
	Token token.Token // The token containing the boolean literal
	Value bool        // The actual boolean value
}

Boolean represents a boolean literal expression in the AST. For example, "true" or "false".

func (*Boolean) String

func (b *Boolean) String() string

String returns a string representation of the boolean literal.

func (*Boolean) TokenLiteral

func (b *Boolean) TokenLiteral() string

TokenLiteral returns the literal value of the token associated with this boolean.

type CallExpression

type CallExpression struct {
	Token     token.Token  // The '(' token
	Function  Expression   // The function being called (can be an identifier or function literal)
	Arguments []Expression // The arguments passed to the function
}

CallExpression represents a function call in the AST. For example, "add(1, 2)" or "fn(x, y){ x + y }(1, 2)".

func (*CallExpression) String

func (ce *CallExpression) String() string

String returns a string representation of the function call. Format: "<function>(<arguments>)"

func (*CallExpression) TokenLiteral

func (ce *CallExpression) TokenLiteral() string

TokenLiteral returns the literal value of the token associated with this call.

type Expression

type Expression interface {
	Node
	// contains filtered or unexported methods
}

Expression is the interface for all expression nodes in the AST. Expressions are language constructs that produce values. Examples include literals, identifiers, function calls, and operators.

type ExpressionStatement

type ExpressionStatement struct {
	Token      token.Token // The first token of the expression
	Expression Expression  // The expression itself
}

ExpressionStatement represents a statement consisting of a single expression. For example, function calls can be used as statements.

func (*ExpressionStatement) String

func (exp *ExpressionStatement) String() string

String returns a string representation of the expression statement. It delegates to the String method of the underlying expression.

func (*ExpressionStatement) TokenLiteral

func (exp *ExpressionStatement) TokenLiteral() string

TokenLiteral returns the literal value of the token associated with this statement.

type FunctionLiteral

type FunctionLiteral struct {
	Token      token.Token     // The 'fn' token
	Parameters []*Identifier   // The function parameters
	Body       *BlockStatement // The function body
}

FunctionLiteral represents a function definition in the AST. For example, "fn(x, y) { return x + y; }".

func (*FunctionLiteral) String

func (fl *FunctionLiteral) String() string

String returns a string representation of the function literal. Format: "fn(<parameters>) <body>"

func (*FunctionLiteral) TokenLiteral

func (fl *FunctionLiteral) TokenLiteral() string

TokenLiteral returns the literal value of the token associated with this function.

type HashLiteral

type HashLiteral struct {
	Token token.Token               // The '{' token
	Pairs map[Expression]Expression // The key-value pairs in the hash
}

HashLiteral represents a hash literal expression in the AST. For example, "{key1: value1, key2: value2}".

func (*HashLiteral) String

func (hl *HashLiteral) String() string

String returns a string representation of the hash literal. Format: "{<key1>:<value1>, <key2>:<value2>, ...}"

func (*HashLiteral) TokenLiteral

func (hl *HashLiteral) TokenLiteral() string

TokenLiteral returns the literal value of the token associated with this hash.

type Identifier

type Identifier struct {
	Token token.Token // The token containing the identifier
	Value string      // The value (name) of the identifier
}

An Identifier represents a name in the program, such as a variable or function name.

func (*Identifier) String

func (id *Identifier) String() string

String returns the value (name) of the identifier.

func (*Identifier) TokenLiteral

func (id *Identifier) TokenLiteral() string

TokenLiteral returns the literal value of the identifier token.

type IfExpression

type IfExpression struct {
	Token       token.Token     // The 'if' token
	Condition   Expression      // The condition expression
	Consequence *BlockStatement // The block to execute if condition is true
	Alternative *BlockStatement // The block to execute if condition is false (optional)
}

IfExpression represents an if-else expression in the AST. For example, "if (x > y) { x } else { y }".

func (*IfExpression) String

func (ie *IfExpression) String() string

String returns a string representation of the `if expression`. Format: "if <condition> <consequence> else <alternative>"

func (*IfExpression) TokenLiteral

func (ie *IfExpression) TokenLiteral() string

TokenLiteral returns the literal value of the token associated with this expression.

type IndexExpression

type IndexExpression struct {
	Token token.Token // The '[' token
	Left  Expression  // The expression being indexed (array or hash)
	Index Expression  // The index expression
}

IndexExpression represents an index expression in the AST. For example, "myArray[1]" or "myHash["key"]".

func (*IndexExpression) String

func (ie *IndexExpression) String() string

String returns a string representation of the index expression. Format: "(<left-expression>[<index-expression>])"

func (*IndexExpression) TokenLiteral

func (ie *IndexExpression) TokenLiteral() string

TokenLiteral returns the literal value of the token associated with this expression.

type InfixExpression

type InfixExpression struct {
	Token    token.Token // The operator token (e.g., "+")
	Left     Expression  // The expression to the left of the operator
	Operator string      // The operator (e.g., "+")
	Right    Expression  // The expression to the right of the operator
}

InfixExpression represents an infix operator expression in the AST. For example, "5 + 5" or "x == y" where "+" and "==" are infix operators.

func (*InfixExpression) String

func (ie *InfixExpression) String() string

String returns a string representation of the infix expression. Format: "(<left-expression> <operator> <right-expression>)"

func (*InfixExpression) TokenLiteral

func (ie *InfixExpression) TokenLiteral() string

TokenLiteral returns the literal value of the token associated with this expression.

type IntegerLiteral

type IntegerLiteral struct {
	Token token.Token // The token containing the integer literal
	Value int64       // The actual integer value
}

IntegerLiteral represents an integer literal expression in the AST. For example, the literal "5" in the expression "x + 5".

func (*IntegerLiteral) String

func (il *IntegerLiteral) String() string

String returns a string representation of the integer literal.

func (*IntegerLiteral) TokenLiteral

func (il *IntegerLiteral) TokenLiteral() string

TokenLiteral returns the literal value of the token associated with this integer.

type LetStatement

type LetStatement struct {
	Token token.Token // The 'let' token
	Name  *Identifier // The identifier being bound
	Value Expression  // The expression that produces the value to bind
}

LetStatement represents a variable binding statement (e.g., "let x = 5;").

func (*LetStatement) String

func (ls *LetStatement) String() string

String returns a string representation of the let statement. Format: "let <identifier> = <expression>;"

func (*LetStatement) TokenLiteral

func (ls *LetStatement) TokenLiteral() string

TokenLiteral returns the literal value of the 'let' token.

type Node

type Node interface {
	// TokenLiteral returns the literal value of the token associated with this node.
	TokenLiteral() string
	// String returns a string representation of the node for debugging and testing.
	String() string
}

Node is the base interface for all AST nodes. Every node in the AST must implement this interface.

type PrefixExpression

type PrefixExpression struct {
	Token    token.Token // The prefix operator token (e.g., "!")
	Operator string      // The operator (e.g., "!")
	Right    Expression  // The expression to the right of the operator
}

PrefixExpression represents a prefix operator expression in the AST. For example, "-5" or "!true" where "-" and "!" are prefix operators.

func (*PrefixExpression) String

func (pe *PrefixExpression) String() string

String returns a string representation of the prefix expression. Format: "(<operator><expression>)"

func (*PrefixExpression) TokenLiteral

func (pe *PrefixExpression) TokenLiteral() string

TokenLiteral returns the literal value of the token associated with this expression.

type Program

type Program struct {
	Statements []Statement // The list of statements in the program
}

Program is the root node of the AST. It represents a complete Monke program and contains a list of statements.

func (*Program) String

func (p *Program) String() string

String returns a string representation of the program. It concatenates the string representations of all statements in the program.

func (*Program) TokenLiteral

func (p *Program) TokenLiteral() string

TokenLiteral returns the literal value of the first token in the program. If the program has no statements, it returns an empty string.

type ReturnStatement

type ReturnStatement struct {
	Token       token.Token // The 'return' token
	ReturnValue Expression  // The expression that produces the return value
}

ReturnStatement represents a return statement (e.g., "return 5;").

func (*ReturnStatement) String

func (rs *ReturnStatement) String() string

String returns a string representation of the return statement. Format: "return <expression>;"

func (*ReturnStatement) TokenLiteral

func (rs *ReturnStatement) TokenLiteral() string

TokenLiteral returns the literal value of the 'return' token.

type Statement

type Statement interface {
	Node
	// contains filtered or unexported methods
}

Statement is the interface for all statement nodes in the AST. Statements are language constructs that perform actions but don't produce values. Examples include let statements, return statements, and expression statements.

type StringLiteral

type StringLiteral struct {
	Token token.Token // The token containing the string literal
	Value string      // The actual string value
}

StringLiteral represents a string literal expression in the AST. For example, "hello world".

func (*StringLiteral) String

func (sl *StringLiteral) String() string

String returns a string representation of the string literal.

func (*StringLiteral) TokenLiteral

func (sl *StringLiteral) TokenLiteral() string

TokenLiteral returns the literal value of the token associated with this string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL