Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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.
var BuiltinLookupFn func(name string, env *object.Environment) (object.Object, bool)
BuiltinLookupFn looks up a builtin by name. Returns (builtin, ok).
var EvalInfixExpressionFn func(operator string, left, right object.Object) object.Object
EvalInfixExpressionFn evaluates an infix expression. Must be set before running the bytecode VM.
EvalPrefixExpressionFn evaluates a prefix expression. Must be set before running the bytecode VM.
var EvalProgramFn func(program any, env *object.Environment) object.Object
EvalProgramFn evaluates an AST program. Used by RunProgram.
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 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 )