basic

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
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 BoolLiteral

type BoolLiteral struct {
	Pos
	Value bool
}

BoolLiteral represents: true, false

type BreakStatement

type BreakStatement struct {
	Pos
}

BreakStatement represents: BREAK

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

type FloatLiteral struct {
	Pos
	Value float64
}

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

type FunctionStatement struct {
	Pos
	Name   string
	Params []string
	Body   []Statement
}

FunctionStatement represents: FUNCTION name(params): ... ENDFUNCTION

type Identifier

type Identifier struct {
	Pos
	Name string
}

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

type IntLiteral struct {
	Pos
	Value int
}

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 NewParser

func NewParser(tokens []Token) *Parser

NewParser creates a new parser for the given tokens

func (*Parser) ParseProgram

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

ParseProgram parses the entire program

type Pos

type Pos struct {
	Line   int
	Column int
}

Pos holds line and column information for error reporting

func (Pos) Position

func (p Pos) Position() (line, column int)

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

func Parse

func Parse(tokens []Token) (*Program, error)

Parse parses the tokens into a Program AST

func (*Program) Position

func (p *Program) Position() (line, column int)

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

type StringLiteral struct {
	Pos
	Value string
}

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

func Tokenize

func Tokenize(input string) ([]Token, error)

Tokenize converts an input string into a slice of tokens

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

func LookupKeyword(ident string) TokenType

LookupKeyword checks if an identifier is a keyword and returns the appropriate token type

func (TokenType) String

func (t TokenType) String() string

String returns a human-readable name for the token type

type Tokenizer

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

Tokenizer holds the state for lexical analysis

func NewTokenizer

func NewTokenizer(input string) *Tokenizer

NewTokenizer creates a new tokenizer for the given input

func (*Tokenizer) NextToken

func (t *Tokenizer) NextToken() (Token, error)

NextToken scans and returns the next token

func (*Tokenizer) ScanAll

func (t *Tokenizer) ScanAll() ([]Token, error)

ScanAll scans all tokens from the input

type UnaryExpr

type UnaryExpr struct {
	Pos
	Operator TokenType
	Operand  Expression
}

UnaryExpr represents: op expr (e.g., NOT x, -5)

Jump to

Keyboard shortcuts

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