Documentation
¶
Index ¶
- Constants
- type AssignStatement
- type BinaryExpr
- type BoolLiteral
- type BreakStatement
- type CallExpr
- type DimStatement
- type ElseIfClause
- type Expression
- type ExpressionStatement
- type ExternalFunc
- type FloatLiteral
- type ForStatement
- type FunctionStatement
- type Identifier
- type IfStatement
- type IndexExpr
- type IntLiteral
- type Interpreter
- func (i *Interpreter) Call(funcName string, args ...interface{}) (interface{}, error)
- func (i *Interpreter) HasFunction(funcName string) bool
- func (i *Interpreter) Interpret(code string) error
- func (i *Interpreter) Load(code string) error
- func (i *Interpreter) RegisterFunction(name string, function ExternalFunc)
- func (i *Interpreter) SetMaxIterations(max int)
- func (i *Interpreter) SetPrintFunc(fn PrintFunc)
- func (i *Interpreter) Validate(code string) error
- type LetStatement
- type Node
- type Parser
- type Pos
- type PrintFunc
- type PrintStatement
- type Program
- type ReturnStatement
- type Statement
- type StringLiteral
- type Token
- type TokenType
- type Tokenizer
- type UnaryExpr
Constants ¶
const MaxIterations = 100000
MaxIterations is the default limit for loop iterations to prevent infinite loops
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AssignStatement ¶
type AssignStatement struct {
Pos
Name string
Index Expression // non-nil when assigning to an array slot: A[i] = ...
Operator TokenType // TOKEN_EQ, TOKEN_PLUS_EQ, TOKEN_MINUS_EQ, TOKEN_PLUS_PLUS, TOKEN_MINUS_MINUS
Value Expression // nil for ++ and --
}
AssignStatement represents: x = expr, x += 1, x++, x--, A[i] = expr
type BinaryExpr ¶
type BinaryExpr struct {
Pos
Left Expression
Operator TokenType
Right Expression
}
BinaryExpr represents: left op right (e.g., x + y, a < b, x AND y)
type CallExpr ¶
type CallExpr struct {
Pos
Name string
Args []Expression
}
CallExpr represents a function call: pow(2, 3), getX()
type DimStatement ¶
type DimStatement struct {
Pos
Name string
Size Expression
}
DimStatement represents: DIM A(size) — allocates an array with size+1 slots (indices 0..size)
type ElseIfClause ¶
type ElseIfClause struct {
Pos
Condition Expression
Block []Statement
}
ElseIfClause represents a single ELSEIF branch
type Expression ¶
type Expression interface {
Node
// contains filtered or unexported methods
}
Expression represents a value-producing expression
type ExpressionStatement ¶
type ExpressionStatement struct {
Pos
Expr Expression
}
ExpressionStatement wraps an expression used as a statement (e.g., function call)
type ExternalFunc ¶
type ExternalFunc func(args ...interface{}) (interface{}, error)
ExternalFunc is the signature for registered external functions
type FloatLiteral ¶
FloatLiteral represents a float literal: 3.14
type ForStatement ¶
type ForStatement struct {
Pos
Variable string
Start Expression
End Expression
Body []Statement
}
ForStatement represents: FOR i = start TO end ... NEXT i
type FunctionStatement ¶
FunctionStatement represents: FUNCTION name(params): ... ENDFUNCTION
type Identifier ¶
Identifier represents a variable reference: x, foo
type IfStatement ¶
type IfStatement struct {
Pos
Condition Expression
ThenBlock []Statement
ElseIfClauses []ElseIfClause
ElseBlock []Statement // nil if no ELSE
}
IfStatement represents: IF cond THEN ... [ELSEIF cond THEN ...] [ELSE ...] ENDIF
type IndexExpr ¶
type IndexExpr struct {
Pos
Name string
Index Expression
}
IndexExpr represents an array index read: A[i]
type IntLiteral ¶
IntLiteral represents an integer literal: 42
type Interpreter ¶
type Interpreter struct {
// contains filtered or unexported fields
}
Interpreter executes MechanicalBasic programs
func NewInterpreter ¶
func NewInterpreter() *Interpreter
NewInterpreter creates a new interpreter instance
func (*Interpreter) Call ¶
func (i *Interpreter) Call(funcName string, args ...interface{}) (interface{}, error)
Call invokes a script-defined function by name with the provided arguments. Global variables from top-level code persist between calls. Function-local variables do not persist between calls.
func (*Interpreter) HasFunction ¶
func (i *Interpreter) HasFunction(funcName string) bool
HasFunction checks if a function with the given name exists
func (*Interpreter) Interpret ¶
func (i *Interpreter) Interpret(code string) error
Interpret executes the given code string
func (*Interpreter) Load ¶
func (i *Interpreter) Load(code string) error
Load parses the code, registers function definitions, and executes top-level code. Top-level variables are stored in global scope and persist between function calls.
func (*Interpreter) RegisterFunction ¶
func (i *Interpreter) RegisterFunction(name string, function ExternalFunc)
RegisterFunction registers an external function that can be called from scripts
func (*Interpreter) SetMaxIterations ¶
func (i *Interpreter) SetMaxIterations(max int)
SetMaxIterations sets the maximum loop iterations allowed
func (*Interpreter) SetPrintFunc ¶
func (i *Interpreter) SetPrintFunc(fn PrintFunc)
SetPrintFunc sets a custom handler for PRINT statements
func (*Interpreter) Validate ¶
func (i *Interpreter) Validate(code string) error
Validate checks the given code for syntax errors without executing it.
type LetStatement ¶
type LetStatement struct {
Pos
Name string
Value Expression
}
LetStatement represents: LET x = expr
type Node ¶
type Node interface {
Position() (line, column int)
// contains filtered or unexported methods
}
Node is the base interface for all AST nodes
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser converts tokens into an AST
func (*Parser) ParseProgram ¶
ParseProgram parses the entire program
type PrintFunc ¶
type PrintFunc func(value interface{})
PrintFunc is the signature for custom print handlers
type PrintStatement ¶
type PrintStatement struct {
Pos
Value Expression
}
PrintStatement represents: PRINT expr
type Program ¶
type Program struct {
Statements []Statement
}
Program is the root node containing all statements
type ReturnStatement ¶
type ReturnStatement struct {
Pos
Value Expression // nil for bare RETURN
}
ReturnStatement represents: RETURN expr
type Statement ¶
type Statement interface {
Node
// contains filtered or unexported methods
}
Statement represents an executable statement
type StringLiteral ¶
StringLiteral represents a string literal: "hello"
type Token ¶
type Token struct {
Type TokenType
Value string // The literal value of the token
Line int // Line number (1-indexed)
Column int // Column number (1-indexed)
}
Token represents a lexical token with its type, value, and position
type TokenType ¶
type TokenType int
TokenType represents the type of a token
const ( // Special tokens TOKEN_EOF TokenType = iota TOKEN_NEWLINE TOKEN_COMMENT // Literals TOKEN_IDENTIFIER TOKEN_INT TOKEN_FLOAT TOKEN_STRING TOKEN_TRUE TOKEN_FALSE // Keywords TOKEN_LET TOKEN_IF TOKEN_THEN TOKEN_ELSE TOKEN_ELSEIF TOKEN_ENDIF TOKEN_FOR TOKEN_TO TOKEN_NEXT TOKEN_BREAK TOKEN_FUNCTION TOKEN_ENDFUNCTION TOKEN_RETURN TOKEN_PRINT TOKEN_AND TOKEN_OR TOKEN_NOT TOKEN_DIM // Operators TOKEN_PLUS // + TOKEN_MINUS // - TOKEN_STAR // * TOKEN_SLASH // / TOKEN_EQ // = TOKEN_NEQ // <> or != TOKEN_LT // < TOKEN_GT // > TOKEN_LTE // <= TOKEN_GTE // >= TOKEN_PLUS_EQ // += TOKEN_MINUS_EQ // -= TOKEN_PLUS_PLUS // ++ TOKEN_MINUS_MINUS // -- // Delimiters TOKEN_LPAREN // ( TOKEN_RPAREN // ) TOKEN_LBRACKET // [ TOKEN_RBRACKET // ] TOKEN_COMMA // , TOKEN_COLON // : )
func LookupKeyword ¶
LookupKeyword checks if an identifier is a keyword and returns the appropriate token type
type Tokenizer ¶
type Tokenizer struct {
// contains filtered or unexported fields
}
Tokenizer holds the state for lexical analysis
func NewTokenizer ¶
NewTokenizer creates a new tokenizer for the given input
type UnaryExpr ¶
type UnaryExpr struct {
Pos
Operator TokenType
Operand Expression
}
UnaryExpr represents: op expr (e.g., NOT x, -5)