further improvements
Some checks failed
Build / build (push) Successful in 10m40s
Publish Image / publish (push) Failing after 34s
Test / build (push) Has been cancelled

This commit is contained in:
2024-04-01 17:02:44 -04:00
parent aebbe43999
commit f9e6e164b0
26 changed files with 168 additions and 138 deletions

View File

@@ -12,6 +12,7 @@ import (
"monkey/internal/utils"
"os"
"path/filepath"
"sort"
"time"
"unicode"
)
@@ -20,10 +21,6 @@ const StackSize = 2048
const GlobalsSize = 65536
const MaxFrames = 1024
var Null = object.Null{}
var True = object.Boolean{Value: true}
var False = object.Boolean{Value: false}
func isTruthy(obj object.Object) bool {
switch obj := obj.(type) {
@@ -114,6 +111,7 @@ func (s *VMState) ExportedHash() *object.Hash {
type VM struct {
Debug bool
Trace bool
state *VMState
@@ -143,9 +141,9 @@ func (vm *VM) popFrame() Frame {
// New constructs a new monkey-lang bytecode virtual machine
func New(fn string, bytecode *compiler.Bytecode) *VM {
mainFn := &object.CompiledFunction{Instructions: bytecode.Instructions}
mainClosure := &object.Closure{Fn: mainFn}
mainFrame := NewFrame(mainClosure, 0)
mainFn := object.CompiledFunction{Instructions: bytecode.Instructions}
mainClosure := object.Closure{Fn: &mainFn}
mainFrame := NewFrame(&mainClosure, 0)
frames := make([]Frame, MaxFrames)
frames[0] = mainFrame
@@ -169,9 +167,9 @@ func New(fn string, bytecode *compiler.Bytecode) *VM {
}
func NewWithState(fn string, bytecode *compiler.Bytecode, state *VMState) *VM {
mainFn := &object.CompiledFunction{Instructions: bytecode.Instructions}
mainClosure := &object.Closure{Fn: mainFn}
mainFrame := NewFrame(mainClosure, 0)
mainFn := object.CompiledFunction{Instructions: bytecode.Instructions}
mainClosure := object.Closure{Fn: &mainFn}
mainFrame := NewFrame(&mainClosure, 0)
frames := make([]Frame, MaxFrames)
frames[0] = mainFrame
@@ -222,13 +220,13 @@ func (vm *VM) executeConstant() error {
func (vm *VM) executeAssignGlobal() error {
globalIndex := vm.currentFrame().ReadUint16()
vm.state.Globals[globalIndex] = vm.pop()
return vm.push(Null)
return vm.push(object.NULL)
}
func (vm *VM) executeAssignLocal() error {
localIndex := vm.currentFrame().ReadUint8()
vm.stack[vm.currentFrame().basePointer+int(localIndex)] = vm.pop()
return vm.push(Null)
return vm.push(object.NULL)
}
func (vm *VM) executeSetGlobal() error {
@@ -241,7 +239,7 @@ func (vm *VM) executeSetGlobal() error {
vm.state.Globals[globalIndex] = ref
}
return vm.push(Null)
return vm.push(object.NULL)
}
func (vm *VM) executeGetGlobal() error {
@@ -259,7 +257,7 @@ func (vm *VM) executeSetLocal() error {
vm.stack[vm.currentFrame().basePointer+int(localIndex)] = ref
}
return vm.push(Null)
return vm.push(object.NULL)
}
func (vm *VM) executeGetLocal() error {
@@ -283,15 +281,15 @@ func (vm *VM) executeCurrentClosure() error {
}
func (vm *VM) executeTrue() error {
return vm.push(True)
return vm.push(object.TRUE)
}
func (vm *VM) executeFalse() error {
return vm.push(False)
return vm.push(object.FALSE)
}
func (vm *VM) executeNull() error {
return vm.push(Null)
return vm.push(object.NULL)
}
func (vm *VM) buildHash(startIndex, endIndex int) (object.Object, error) {
@@ -547,9 +545,9 @@ func (vm *VM) executeEqual() error {
if obj, ok := left.(object.Comparable); ok {
val := obj.Compare(right)
if val == 0 {
return vm.push(True)
return vm.push(object.TRUE)
}
return vm.push(False)
return vm.push(object.FALSE)
}
return object.NewBinaryOpError(left, right, "==")
@@ -562,9 +560,9 @@ func (vm *VM) executeNotEqual() error {
if obj, ok := left.(object.Comparable); ok {
val := obj.Compare(right)
if val != 0 {
return vm.push(True)
return vm.push(object.TRUE)
}
return vm.push(False)
return vm.push(object.FALSE)
}
return object.NewBinaryOpError(left, right, "!=")
@@ -577,9 +575,9 @@ func (vm *VM) executeGreaterThan() error {
if obj, ok := left.(object.Comparable); ok {
val := obj.Compare(right)
if val == 1 {
return vm.push(True)
return vm.push(object.TRUE)
}
return vm.push(False)
return vm.push(object.FALSE)
}
return object.NewBinaryOpError(left, right, ">")
@@ -592,9 +590,9 @@ func (vm *VM) executeGreaterThanOrEqual() error {
if obj, ok := left.(object.Comparable); ok {
val := obj.Compare(right)
if val >= 0 {
return vm.push(True)
return vm.push(object.TRUE)
}
return vm.push(False)
return vm.push(object.FALSE)
}
return object.NewBinaryOpError(left, right, ">")
@@ -630,7 +628,7 @@ func (vm *VM) executeSetItem() error {
if err != nil {
return err
}
return vm.push(Null)
return vm.push(object.NULL)
}
return fmt.Errorf(
@@ -664,7 +662,7 @@ func (vm *VM) executeCall() error {
switch callee := callee.(type) {
case *object.Closure:
return vm.callClosure(callee, args)
case *object.Builtin:
case object.Builtin:
return vm.callBuiltin(callee, args)
default:
return fmt.Errorf(
@@ -727,7 +725,7 @@ func (vm *VM) callClosure(cl *object.Closure, numArgs int) error {
return nil
}
func (vm *VM) callBuiltin(builtin *object.Builtin, numArgs int) error {
func (vm *VM) callBuiltin(builtin object.Builtin, numArgs int) error {
args := vm.stack[vm.sp-numArgs : vm.sp]
result := builtin.Fn(args...)
@@ -739,7 +737,7 @@ func (vm *VM) callBuiltin(builtin *object.Builtin, numArgs int) error {
return err
}
} else {
err := vm.push(Null)
err := vm.push(object.NULL)
if err != nil {
return err
}
@@ -761,8 +759,8 @@ func (vm *VM) pushClosure(constIndex, numFree int) error {
}
vm.sp = vm.sp - numFree
closure := &object.Closure{Fn: function, Free: free}
return vm.push(closure)
closure := object.Closure{Fn: function, Free: free}
return vm.push(&closure)
}
func (vm *VM) loadModule(name object.Object) error {
@@ -788,20 +786,38 @@ func (vm *VM) LastPoppedStackElem() object.Object {
}
func (vm *VM) Run() (err error) {
var n int
var (
op code.Opcode
opcodeFreqs = make(map[code.Opcode]int)
)
if vm.Debug {
start := time.Now()
defer func() {
log.Printf("%d instructions executeuted in %s", n, time.Now().Sub(start))
total := 0
opcodes := make([]code.Opcode, 0, len(opcodeFreqs))
for opcode := range opcodeFreqs {
opcodes = append(opcodes, opcode)
}
sort.SliceStable(opcodes, func(i, j int) bool {
return opcodeFreqs[opcodes[i]] > opcodeFreqs[opcodes[j]]
})
log.Printf("%d instructions executed in %s", total, time.Now().Sub(start))
log.Print("Top 10 instructions:")
for _, opcode := range opcodes[:10] {
log.Printf("%10d %s", opcodeFreqs[opcode], opcode)
}
}()
}
for err == nil {
op := vm.currentFrame().ReadNextOp()
op = vm.currentFrame().ReadNextOp()
if vm.Debug {
n++
opcodeFreqs[op]++
log.Printf(
"%-25s %-20s\n",
fmt.Sprintf("%04d %s", vm.currentFrame().ip, op),
@@ -949,7 +965,7 @@ func (vm *VM) Run() (err error) {
err = fmt.Errorf("unhandled opcode: %s", op)
}
if vm.Debug {
if vm.Trace {
log.Printf(
"%-25s [ip=%02d fp=%02d, sp=%02d]",
"", vm.currentFrame().ip, vm.fp-1, vm.sp,