lots o fixes
Some checks failed
Build / build (push) Successful in 9m50s
Publish Image / publish (push) Failing after 49s
Test / build (push) Successful in 10m55s

This commit is contained in:
2024-04-01 18:18:45 -04:00
parent fe33fda0ab
commit 862119e90e
44 changed files with 326 additions and 170 deletions

View File

@@ -5,57 +5,57 @@ import (
"monkey/internal/object"
)
type Frame struct {
type frame struct {
cl *object.Closure
ip int
basePointer int
}
func NewFrame(cl *object.Closure, basePointer int) Frame {
return Frame{
func newFrame(cl *object.Closure, basePointer int) frame {
return frame{
cl: cl,
basePointer: basePointer,
}
}
func (f *Frame) Closure() *object.Closure {
func (f *frame) Closure() *object.Closure {
return f.cl
}
func (f *Frame) GetFree(idx uint8) object.Object {
func (f *frame) GetFree(idx uint8) object.Object {
return f.cl.Free[idx]
}
func (f *Frame) SetFree(idx uint8, obj object.Object) {
func (f *frame) SetFree(idx uint8, obj object.Object) {
f.cl.Free[idx] = obj
}
func (f *Frame) SetIP(ip int) {
func (f *frame) SetIP(ip int) {
f.ip = ip
}
func (f Frame) PeekNextOp() code.Opcode {
func (f frame) PeekNextOp() code.Opcode {
return code.Opcode(f.cl.Fn.Instructions[f.ip])
}
func (f *Frame) ReadNextOp() code.Opcode {
func (f *frame) ReadNextOp() code.Opcode {
op := code.Opcode(f.cl.Fn.Instructions[f.ip])
f.ip++
return op
}
func (f *Frame) ReadUint8() uint8 {
func (f *frame) ReadUint8() uint8 {
n := code.ReadUint8(f.cl.Fn.Instructions[f.ip:])
f.ip++
return n
}
func (f *Frame) ReadUint16() uint16 {
func (f *frame) ReadUint16() uint16 {
n := code.ReadUint16(f.cl.Fn.Instructions[f.ip:])
f.ip += 2
return n
}
func (f Frame) Instructions() code.Instructions {
func (f frame) Instructions() code.Instructions {
return f.cl.Fn.Instructions
}

View File

@@ -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

View File

@@ -1015,7 +1015,7 @@ func TestTailCalls(t *testing.T) {
}
func TestIntegration(t *testing.T) {
matches, err := filepath.Glob("../testdata/*.monkey")
matches, err := filepath.Glob("../testdata/*.m")
if err != nil {
t.Error(err)
}
@@ -1060,7 +1060,7 @@ func TestExamples(t *testing.T) {
t.Skip("skipping test in short mode.")
}
matches, err := filepath.Glob("../examples/*.monkey")
matches, err := filepath.Glob("../examples/*.m")
if err != nil {
t.Error(err)
}