parser

package
v0.0.0-...-d6b0b83 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddImport

func AddImport(p *Program, path string)

AddImport appends an import path to the program's import list.

func CheckTypes

func CheckTypes(p *Program) error

CheckTypes performs a very small type-check pass validating declared types used in variable declarations and function return types. It's intentionally conservative and only recognizes "int" and "string" for now.

func CountNodes

func CountNodes(n Node) int

CountNodes returns the total number of nodes in the subtree rooted at n.

func PrettyPrint

func PrettyPrint(p *Program) string

PrettyPrint produces a simple human-readable representation of the AST.

func Walk

func Walk(n Node, v Visitor)

Walk performs a simple preorder traversal calling v.VisitNode on each Node.

Types

type BlockStatement

type BlockStatement struct {
	Statements []Statement
}

func (*BlockStatement) Children

func (b *BlockStatement) Children() []Node

type CallExpression

type CallExpression struct {
	Function Expression
	Args     []Expression
}

func (*CallExpression) Children

func (c *CallExpression) Children() []Node

type Expression

type Expression interface{}

type ExpressionStatement

type ExpressionStatement struct {
	Expr Expression
}

func (*ExpressionStatement) Children

func (e *ExpressionStatement) Children() []Node

type Function

type Function struct {
	Name       string
	ReturnType string
	Body       *BlockStatement
}

func (*Function) Children

func (f *Function) Children() []Node

type Identifier

type Identifier struct {
	Value string
}

func (*Identifier) Children

func (id *Identifier) Children() []Node

type IfStatement

type IfStatement struct {
	Condition   Expression
	Consequent  *BlockStatement
	Alternative *BlockStatement
}

IfStatement represents a simple if/else statement.

type Import

type Import struct {
	Path string
}

Import represents a simple import directive in the source.

type InfixExpression

type InfixExpression struct {
	Left     Expression
	Operator string
	Right    Expression
}

func (*InfixExpression) Children

func (in *InfixExpression) Children() []Node

type IntegerLiteral

type IntegerLiteral struct {
	Value string
}

func (*IntegerLiteral) Children

func (i *IntegerLiteral) Children() []Node

type Module

type Module struct {
	Name  string
	Files []Node
}

Module groups multiple AST files into a single compilation unit.

func NewModule

func NewModule(name string) *Module

func (*Module) AddFile

func (m *Module) AddFile(n Node)

func (*Module) TotalNodes

func (m *Module) TotalNodes() int

TotalNodes returns total node count across all files in the module.

type Node

type Node interface {
	Children() []Node
}

Node is a generic AST node used by helpers and visitors.

type Parameter

type Parameter struct {
	Name string
	Type string
}

Parameter represents a function parameter (name and type) in the AST.

type ParseError

type ParseError struct {
	Msg string
	Pos *lexer.Position
}

ParseError represents a parser error with optional position information.

func (*ParseError) Error

func (e *ParseError) Error() string

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

func New

func New(tokens []lexer.Token) *Parser

func (*Parser) ParseProgram

func (p *Parser) ParseProgram() (*Program, error)

type Program

type Program struct {
	Functions []*Function
	Imports   []string
}

func (*Program) Children

func (p *Program) Children() []Node

type ReturnStatement

type ReturnStatement struct {
	Value Expression
}

func (*ReturnStatement) Children

func (r *ReturnStatement) Children() []Node

type Statement

type Statement interface{}

type StringLiteral

type StringLiteral struct {
	Value string
}

func (*StringLiteral) Children

func (s *StringLiteral) Children() []Node

type SymbolTable

type SymbolTable struct {
	Funcs map[string]*Function
	Vars  map[string]string // var name -> type
}

SymbolTable is a minimal symbol table tracking functions and variables.

func NewSymbolTable

func NewSymbolTable() *SymbolTable

func (*SymbolTable) LookupFunc

func (s *SymbolTable) LookupFunc(name string) (*Function, bool)

func (*SymbolTable) LookupVar

func (s *SymbolTable) LookupVar(name string) (string, bool)

func (*SymbolTable) RegisterFunc

func (s *SymbolTable) RegisterFunc(f *Function)

func (*SymbolTable) RegisterVar

func (s *SymbolTable) RegisterVar(name, typ string)

type VarStatement

type VarStatement struct {
	Name  string
	Type  string
	Value Expression
}

func (*VarStatement) Children

func (v *VarStatement) Children() []Node

type Visitor

type Visitor interface {
	VisitNode(n Node)
}

Visitor allows walking the AST nodes.

type WhileStatement

type WhileStatement struct {
	Condition Expression
	Body      *BlockStatement
}

WhileStatement represents a while loop.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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