lots o fixes
This commit is contained in:
@@ -17,9 +17,11 @@ import (
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const StackSize = 2048
|
||||
const GlobalsSize = 65536
|
||||
const MaxFrames = 1024
|
||||
const (
|
||||
maxStackSize = 2048
|
||||
maxFrames = 1024
|
||||
maxGlobals = 65536
|
||||
)
|
||||
|
||||
func isTruthy(obj object.Object) bool {
|
||||
switch obj := obj.(type) {
|
||||
@@ -36,7 +38,7 @@ func isTruthy(obj object.Object) bool {
|
||||
}
|
||||
|
||||
// executeModule compiles the named module and returns a object.Module object
|
||||
func executeModule(name string, state *VMState) (object.Object, error) {
|
||||
func executeModule(name string, state *State) (object.Object, error) {
|
||||
filename := utils.FindModule(name)
|
||||
if filename == "" {
|
||||
return nil, fmt.Errorf("ImportError: no module named '%s'", name)
|
||||
@@ -73,21 +75,22 @@ func executeModule(name string, state *VMState) (object.Object, error) {
|
||||
return state.ExportedHash(), nil
|
||||
}
|
||||
|
||||
type VMState struct {
|
||||
// State is the state of the virtual machine.
|
||||
type State struct {
|
||||
Constants []object.Object
|
||||
Globals []object.Object
|
||||
Symbols *compiler.SymbolTable
|
||||
}
|
||||
|
||||
func NewVMState() *VMState {
|
||||
func NewState() *State {
|
||||
symbolTable := compiler.NewSymbolTable()
|
||||
for i, builtin := range builtins.BuiltinsIndex {
|
||||
symbolTable.DefineBuiltin(i, builtin.Name)
|
||||
}
|
||||
|
||||
return &VMState{
|
||||
return &State{
|
||||
Constants: []object.Object{},
|
||||
Globals: make([]object.Object, GlobalsSize),
|
||||
Globals: make([]object.Object, maxGlobals),
|
||||
Symbols: symbolTable,
|
||||
}
|
||||
}
|
||||
@@ -95,7 +98,7 @@ func NewVMState() *VMState {
|
||||
// exported binding in the vm state. That is every binding that starts with a
|
||||
// capital letter. This is used by the module import system to wrap up the
|
||||
// compiled and evaulated module into an object.
|
||||
func (s *VMState) ExportedHash() *object.Hash {
|
||||
func (s *State) ExportedHash() *object.Hash {
|
||||
pairs := make(map[object.HashKey]object.HashPair)
|
||||
for name, symbol := range s.Symbols.Store {
|
||||
if unicode.IsUpper(rune(name[0])) {
|
||||
@@ -113,7 +116,7 @@ type VM struct {
|
||||
Debug bool
|
||||
Trace bool
|
||||
|
||||
state *VMState
|
||||
state *State
|
||||
|
||||
dir string
|
||||
file string
|
||||
@@ -121,16 +124,16 @@ type VM struct {
|
||||
stack []object.Object
|
||||
sp int // Always points to the next value. Top of stack is stack[sp-1]
|
||||
|
||||
frames []Frame
|
||||
frames []frame
|
||||
fp int // Always points to the current frame. Current frame is frames[fp-1]
|
||||
}
|
||||
|
||||
func (vm *VM) currentFrame() *Frame {
|
||||
func (vm *VM) currentFrame() *frame {
|
||||
return &vm.frames[vm.fp-1]
|
||||
}
|
||||
|
||||
func (vm *VM) pushFrame(f Frame) {
|
||||
if vm.fp >= MaxFrames {
|
||||
func (vm *VM) pushFrame(f frame) {
|
||||
if vm.fp >= maxFrames {
|
||||
panic("frame overflow")
|
||||
}
|
||||
|
||||
@@ -138,7 +141,7 @@ func (vm *VM) pushFrame(f Frame) {
|
||||
vm.fp++
|
||||
}
|
||||
|
||||
func (vm *VM) popFrame() Frame {
|
||||
func (vm *VM) popFrame() frame {
|
||||
if vm.fp == 0 {
|
||||
panic("fame underflow")
|
||||
}
|
||||
@@ -151,18 +154,18 @@ func (vm *VM) popFrame() Frame {
|
||||
func New(fn string, bytecode *compiler.Bytecode) *VM {
|
||||
mainFn := object.CompiledFunction{Instructions: bytecode.Instructions}
|
||||
mainClosure := object.Closure{Fn: &mainFn}
|
||||
mainFrame := NewFrame(&mainClosure, 0)
|
||||
mainFrame := newFrame(&mainClosure, 0)
|
||||
|
||||
frames := make([]Frame, MaxFrames)
|
||||
frames := make([]frame, maxFrames)
|
||||
frames[0] = mainFrame
|
||||
|
||||
state := NewVMState()
|
||||
state := NewState()
|
||||
state.Constants = bytecode.Constants
|
||||
|
||||
vm := &VM{
|
||||
state: state,
|
||||
|
||||
stack: make([]object.Object, StackSize),
|
||||
stack: make([]object.Object, maxStackSize),
|
||||
sp: 0,
|
||||
|
||||
frames: frames,
|
||||
@@ -174,12 +177,12 @@ func New(fn string, bytecode *compiler.Bytecode) *VM {
|
||||
return vm
|
||||
}
|
||||
|
||||
func NewWithState(fn string, bytecode *compiler.Bytecode, state *VMState) *VM {
|
||||
func NewWithState(fn string, bytecode *compiler.Bytecode, state *State) *VM {
|
||||
mainFn := object.CompiledFunction{Instructions: bytecode.Instructions}
|
||||
mainClosure := object.Closure{Fn: &mainFn}
|
||||
mainFrame := NewFrame(&mainClosure, 0)
|
||||
mainFrame := newFrame(&mainClosure, 0)
|
||||
|
||||
frames := make([]Frame, MaxFrames)
|
||||
frames := make([]frame, maxFrames)
|
||||
frames[0] = mainFrame
|
||||
|
||||
vm := &VM{
|
||||
@@ -188,7 +191,7 @@ func NewWithState(fn string, bytecode *compiler.Bytecode, state *VMState) *VM {
|
||||
frames: frames,
|
||||
fp: 1,
|
||||
|
||||
stack: make([]object.Object, StackSize),
|
||||
stack: make([]object.Object, maxStackSize),
|
||||
sp: 0,
|
||||
}
|
||||
|
||||
@@ -198,7 +201,7 @@ func NewWithState(fn string, bytecode *compiler.Bytecode, state *VMState) *VM {
|
||||
}
|
||||
|
||||
func (vm *VM) push(o object.Object) error {
|
||||
if vm.sp >= StackSize {
|
||||
if vm.sp >= maxStackSize {
|
||||
return fmt.Errorf("stack overflow")
|
||||
}
|
||||
|
||||
@@ -712,7 +715,7 @@ func (vm *VM) callClosure(cl *object.Closure, numArgs int) error {
|
||||
}
|
||||
}
|
||||
|
||||
frame := NewFrame(cl, vm.sp-numArgs)
|
||||
frame := newFrame(cl, vm.sp-numArgs)
|
||||
vm.pushFrame(frame)
|
||||
vm.sp = frame.basePointer + cl.Fn.NumLocals
|
||||
|
||||
|
||||
Reference in New Issue
Block a user