Documentation
¶
Index ¶
Constants ¶
const GlobalsSize = 65536 // upper limit on the number of global bindings since operands are 16 bits wide.
const MaxFrames = 1024 // arbitrary number
const StackSize = 2048 // arbitrary number
Variables ¶
var False = &object.Boolean{Value: false}
var Null = &object.Null{}
var True = &object.Boolean{Value: true}
Functions ¶
This section is empty.
Types ¶
type Frame ¶
type Frame struct {
// contains filtered or unexported fields
}
Frame is the struct that holds the execution-relevant information for a function. It is effectively like the inner-environment of a function, allowing the VM to execute its instructions and update the ip (instruction-pointer) without entangling them with the outer scopes. cl points to the closure referenced by the frame. ip is the instruction pointer in this frame for this function. basePointer is a stack pointer value to indicate where in the stack to start allocating memory when executing the function, it is used to create a "hole" on the stack to store all the local bindings of the function. Below the "hole" is the lower boundary which contains all the values on the stack before calling the function. If sp is 3 before calling the function, the lower boundary contains indices [0, 1, 2]. The "hole" will be n-length deep where n is the number of local-bindings. Above the "hole" is the function's workspace, where it will push and pop values, if sp is 3 it should use the indices that are greater than 3+n. When the function exits, we can restore the stack, removing all values after the initial basePointer, thus giving us the stack before the function was called.
func (*Frame) Instructions ¶
func (f *Frame) Instructions() code.Instructions
Instructions simply returns the instructions of the function enclosed by the Closure
type VM ¶
type VM struct {
// contains filtered or unexported fields
}
VM is the struct for our virtual-machine. It holds the bytecode instructions and constants-pool generated by the compiler. A VM implements a stack, as it executes the bytecode, it organizes (push, pop, etc) the evaluated constants on the stack. The field sp helps keep track of the position of the next item in the stack (top to bottom).
func New ¶
New initializes a new VM using the bytecode generated by the compiler. VMs are initialized with an sp of 0 (the initial top). The stack will have a preallocated number of elements (StackSize).
func NewWithGlobalStore ¶
NewWithGlobalStore keeps global state in the REPL so the VM can execute with the byteode and global store from a previous compilation.
func (*VM) LastPoppedStackElem ¶
LastPoppedStackElem helps identify the last element that was popped from the stack as the VM executes through it. If a stack had two elements [a, b], sp would be at index 2. If the vm pops an element, it would pop the element at [sp-1], so index 1, and then sp is moved to index 1. Leaving b to be the last popped stack element.