ast

package
v0.3.4-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Argument

type Argument struct {
	Name string
	Type ValueType
}

Represents an argument for a function

type BlockStatement

type BlockStatement struct {
	Token      lexer.Token
	Statements []Statement
}

Represents a block of code contained within {}

func (*BlockStatement) String

func (bs *BlockStatement) String() string

func (*BlockStatement) TokenLiteral

func (bs *BlockStatement) TokenLiteral() string

func (*BlockStatement) ValueType

func (bs *BlockStatement) ValueType() ValueType

type CharLiteral

type CharLiteral struct {
	Token lexer.Token
	Value byte
}

Represents a character literal (e.g. 'a', 'b', '\n')

func (*CharLiteral) String

func (cl *CharLiteral) String() string

func (*CharLiteral) TokenLiteral

func (cl *CharLiteral) TokenLiteral() string

func (*CharLiteral) ValueType

func (fl *CharLiteral) ValueType() ValueType

type Expression

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

Represents an expression (e.g. 5 < 2, 6 + 7, 5 * (3 + 5))

type ExpressionStatement

type ExpressionStatement struct {
	Token      lexer.Token
	Expression Expression
}

Represents a standalone expression statement

func (*ExpressionStatement) String

func (es *ExpressionStatement) String() string

func (*ExpressionStatement) TokenLiteral

func (es *ExpressionStatement) TokenLiteral() string

func (*ExpressionStatement) ValueType

func (es *ExpressionStatement) ValueType() ValueType

type FloatLiteral

type FloatLiteral struct {
	Token lexer.Token
	Value float64
	Type  ValueType
}

Represents a literal float (e.g. 5.0, 3.14159, -0.56791)

func (*FloatLiteral) String

func (fl *FloatLiteral) String() string

func (*FloatLiteral) TokenLiteral

func (fl *FloatLiteral) TokenLiteral() string

func (*FloatLiteral) ValueType

func (fl *FloatLiteral) ValueType() ValueType

type ForLoopStatement

type ForLoopStatement struct {
	Token     lexer.Token
	Initial   Statement  // initial statement (e.g. int i = 0;)
	Condition Expression // stopping condition (e.g. i < 10;)
	Increment Expression // code to run every iteration (e.g. i++)
	Statement Statement  // actual code to run throughout the loop
}

Represents a for loop

func (*ForLoopStatement) String

func (fs *ForLoopStatement) String() string

func (*ForLoopStatement) TokenLiteral

func (fs *ForLoopStatement) TokenLiteral() string

func (*ForLoopStatement) ValueType

func (fs *ForLoopStatement) ValueType() ValueType

type Function

type Function struct {
	Name            string
	ReturnType      ValueType
	Statement       Statement
	SymbolIndex     map[string]int
	Symbols         []*Symbol
	NextSymbolIndex int
	Arguments       []Argument
}

Represents a function in the program

func NewFunction

func NewFunction(name string, returnType ValueType) *Function

func (*Function) GetSymbol

func (f *Function) GetSymbol(name string) *Symbol

Retrieves the symbol for the given variable name

func (*Function) GetVariableCounts

func (f *Function) GetVariableCounts() (integerCount, floatCount int)

Gets the number of variables based on each main type, used to generate WASM locals to store the variables in

func (*Function) SetSymbol

func (f *Function) SetSymbol(name string, t ValueType, constant bool) *Symbol

Registers a variable within the program

func (*Function) String

func (f *Function) String() string

func (*Function) TokenLiteral

func (f *Function) TokenLiteral() string

func (*Function) ValueType

func (f *Function) ValueType() ValueType

type FunctionCallExpression

type FunctionCallExpression struct {
	Token      lexer.Token
	Name       string
	Args       []Expression
	ReturnType ValueType
	Index      int
}

Represents a call to a function in an expression

func (*FunctionCallExpression) String

func (fce *FunctionCallExpression) String() string

func (*FunctionCallExpression) TokenLiteral

func (fce *FunctionCallExpression) TokenLiteral() string

func (*FunctionCallExpression) ValueType

func (fce *FunctionCallExpression) ValueType() ValueType

type Identifier

type Identifier struct {
	Token  lexer.Token
	Value  string
	Symbol *Symbol
}

Represents an identifier expression (e.g. using a variable in an expression)

func (*Identifier) String

func (i *Identifier) String() string

func (*Identifier) TokenLiteral

func (i *Identifier) TokenLiteral() string

func (*Identifier) ValueType

func (i *Identifier) ValueType() ValueType

type IfStatement

type IfStatement struct {
	Token       lexer.Token
	Condition   Expression
	Consequence Statement
	Alternative Statement
}

Represents an if statement in the AST

func (*IfStatement) String

func (i *IfStatement) String() string

func (*IfStatement) TokenLiteral

func (i *IfStatement) TokenLiteral() string

func (*IfStatement) ValueType

func (i *IfStatement) ValueType() ValueType

type InfixExpression

type InfixExpression struct {
	Token    lexer.Token
	Left     Expression
	Operator string
	Right    Expression
}

Represents an expression with an operator and two values (e.g. 5 + 6)

func (*InfixExpression) String

func (ie *InfixExpression) String() string

func (*InfixExpression) TokenLiteral

func (ie *InfixExpression) TokenLiteral() string

func (*InfixExpression) ValueType

func (ie *InfixExpression) ValueType() ValueType

type IntegerLiteral

type IntegerLiteral struct {
	Token lexer.Token
	Value int64
	Type  ValueType
}

Represents a literal integer (e.g. 5, 682329, -2198)

func (*IntegerLiteral) String

func (il *IntegerLiteral) String() string

func (*IntegerLiteral) TokenLiteral

func (il *IntegerLiteral) TokenLiteral() string

func (*IntegerLiteral) ValueType

func (il *IntegerLiteral) ValueType() ValueType

type Node

type Node interface {
	// Returns the TokenLiteral of the AST Node
	TokenLiteral() string
	// Returns a string representation of the AST Node
	String() string
	// Returns the type of the AST Node
	ValueType() ValueType
}

Represents a Node in the Abstract Syntax Tree

type PostfixExpression

type PostfixExpression struct {
	Token    lexer.Token
	Left     Expression
	Operator string
}

Represents an expression with it's operator on the right side (e.g. 5++, i--)

func (*PostfixExpression) String

func (pe *PostfixExpression) String() string

func (*PostfixExpression) TokenLiteral

func (pe *PostfixExpression) TokenLiteral() string

func (*PostfixExpression) ValueType

func (pe *PostfixExpression) ValueType() ValueType

type PrefixExpression

type PrefixExpression struct {
	Token    lexer.Token
	Operator string
	Right    Expression
}

Represents an expression with a prefix operator (e.g. -5, +6)

func (*PrefixExpression) String

func (pe *PrefixExpression) String() string

func (*PrefixExpression) TokenLiteral

func (pe *PrefixExpression) TokenLiteral() string

func (*PrefixExpression) ValueType

func (pe *PrefixExpression) ValueType() ValueType

type Program

type Program struct {
	// Every function declared
	Functions []*Function

	// Functions to be imported from JS
	ExternalFunctions []*Function

	// global statements
	Statements []Statement
}

Stores the entire program and it's AST

func (*Program) FunctionExists

func (p *Program) FunctionExists(name string) int

checks if a function exists, if so returns it's index. otherwise the function returns -1

func (*Program) String

func (p *Program) String() string

func (*Program) TokenLiteral

func (p *Program) TokenLiteral() string

func (*Program) Type

func (p *Program) Type() ValueType

type ReturnStatement

type ReturnStatement struct {
	Token       lexer.Token
	ReturnValue Expression
}

Represents a statement that returns a value from a function

func (*ReturnStatement) String

func (rs *ReturnStatement) String() string

func (*ReturnStatement) TokenLiteral

func (rs *ReturnStatement) TokenLiteral() string

func (*ReturnStatement) ValueType

func (rs *ReturnStatement) ValueType() ValueType

type Statement

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

Represents a statement node (e.g. variable declaration, if statement etc.) in the AST

type Symbol

type Symbol struct {
	Index    int
	Type     ValueType
	Constant bool
}

Represents a defiend symbol such as a variable name

type ValueType

type ValueType string

Represents a type within the C programming language Used mainly for storing return types of functions and statements within the AST

const (
	ValueTypeInt   ValueType = "int"
	ValueTypeFloat ValueType = "float"
	ValueTypeChar  ValueType = "char"
	ValueTypeVoid  ValueType = "void"
)

type VariableDefineStatement

type VariableDefineStatement struct {
	Token lexer.Token
	Name  *Identifier
	Value Expression
	Type  ValueType
}

Represents a statement that defines a variable (e.g. int x = 0;)

func (*VariableDefineStatement) String

func (vds *VariableDefineStatement) String() string

func (*VariableDefineStatement) TokenLiteral

func (vds *VariableDefineStatement) TokenLiteral() string

func (*VariableDefineStatement) ValueType

func (vds *VariableDefineStatement) ValueType() ValueType

type VariableUpdateStatement

type VariableUpdateStatement struct {
	Name      *Identifier
	Token     lexer.Token
	NewValue  Expression
	Operation string
}

Represents a statement that defines a variable (e.g. x = 5; x += 6;)

func (*VariableUpdateStatement) String

func (vus *VariableUpdateStatement) String() string

func (*VariableUpdateStatement) TokenLiteral

func (vus *VariableUpdateStatement) TokenLiteral() string

func (*VariableUpdateStatement) ValueType

func (vus *VariableUpdateStatement) ValueType() ValueType

type WhileLoopStatement

type WhileLoopStatement struct {
	Token     lexer.Token
	Condition Expression
	Statement Statement
}

Represents a while loop

func (*WhileLoopStatement) String

func (ws *WhileLoopStatement) String() string

func (*WhileLoopStatement) TokenLiteral

func (ws *WhileLoopStatement) TokenLiteral() string

func (*WhileLoopStatement) ValueType

func (ws *WhileLoopStatement) ValueType() ValueType

Jump to

Keyboard shortcuts

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