bytecode

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ApplyFunctionFn func(fn object.Object, args []object.Object, env *object.Environment, call *ast.CallExpression) object.Object

ApplyFunctionFn calls a function object with arguments. call is the originating *ast.CallExpression (nil if the caller has none), forwarded so call-stack frames can be built the same way the tree walker builds them. Must be set before running the bytecode VM.

View Source
var BuiltinLookupFn func(name string, env *object.Environment) (object.Object, bool)

BuiltinLookupFn looks up a builtin by name. Returns (builtin, ok).

View Source
var EvalInfixExpressionFn func(operator string, left, right object.Object) object.Object

EvalInfixExpressionFn evaluates an infix expression. Must be set before running the bytecode VM.

View Source
var EvalPrefixExpressionFn func(operator string, right object.Object) object.Object

EvalPrefixExpressionFn evaluates a prefix expression. Must be set before running the bytecode VM.

View Source
var EvalProgramFn func(program any, env *object.Environment) object.Object

EvalProgramFn evaluates an AST program. Used by RunProgram.

View Source
var FinalizeCallResultFn func(result object.Object, call *ast.CallExpression, env *object.Environment) object.Object

FinalizeCallResultFn wraps a call's result the same way the tree walker's evalCallExpression does via finalizeCallResult: if the result is an error, it appends a call-stack frame for `call` (deduplicating a frame ApplyFunctionFn may already have added for a validation failure). Without this, a bytecode-compiled call loses one level of stack-trace detail compared to the same call evaluated by the tree walker. May be nil, in which case OpCall skips this step.

Functions

func RunOnVM

func RunOnVM(program *BytecodeProgram, env *object.Environment) object.Object

RunOnVM executes a compiled BytecodeProgram in the given environment.

func RunProgram

func RunProgram(program any, env *object.Environment) object.Object

RunProgram evaluates a program via the configured EvalProgramFn. This is the bytecode package's equivalent of the root-package runProgram.

Types

type BytecodeProgram

type BytecodeProgram struct {
	Instructions []Instruction
	Constants    []object.Object
}

func CompileToBytecode

func CompileToBytecode(program *ast.Program) (*BytecodeProgram, error)

CompileToBytecode compiles an ast.Program into bytecode instructions.

type Compiler

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

type ErrUnsupportedNode

type ErrUnsupportedNode struct {
	Node string
}

func (*ErrUnsupportedNode) Error

func (e *ErrUnsupportedNode) Error() string

type Instruction

type Instruction struct {
	Op  OpCode
	Arg int
	S   string
	// Call carries the original *ast.CallExpression for OpCall instructions,
	// so ApplyFunctionFn can build the same call-stack frame the tree walker
	// would (see extendFunctionEnv/callFrameFromExpression in
	// pkg/eval/apply.go) - without it, errors from a bytecode-compiled call
	// silently lose one level of stack-trace detail. Unused by every other
	// opcode.
	Call *ast.CallExpression
}

type OpCode

type OpCode byte
const (
	OpConstant OpCode = iota
	OpGetVar
	OpSetVar
	OpPop
	OpUnary
	OpBinary
	OpCall
	OpReturn
	OpNull
	OpPrint
	// OpJump unconditionally sets ip to Arg (an instruction index).
	OpJump
	// OpJumpIfFalse pops the top of stack and, if it is falsy (per
	// object.IsTruthy), sets ip to Arg; otherwise falls through. Used to
	// compile `if` expressions and short-circuiting `&&`/`||`.
	OpJumpIfFalse
	// OpJumpIfTrue pops the top of stack and, if it is truthy, sets ip to
	// Arg; otherwise falls through. Used for `||`'s short-circuit path.
	OpJumpIfTrue
	// OpToBool pops the top of stack and pushes object.TRUE/object.FALSE per
	// object.IsTruthy, matching the boolean-coercing (not value-returning)
	// semantics of SPL's `&&`/`||` (see pkg/eval/infix.go).
	OpToBool
)

type VM

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

func (*VM) Run

func (v *VM) Run() object.Object

Run executes the bytecode program and returns the result.

Jump to

Keyboard shortcuts

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