module support
Some checks failed
Build / build (push) Successful in 10m22s
Test / build (push) Failing after 15m54s

This commit is contained in:
Chuck Smith
2024-03-26 16:49:38 -04:00
parent 6d234099d1
commit 110152a139
21 changed files with 541 additions and 100 deletions

View File

@@ -8,7 +8,6 @@ import (
"fmt"
"io"
"log"
"monkey/builtins"
"monkey/compiler"
"monkey/evaluator"
"monkey/lexer"
@@ -42,25 +41,6 @@ type Options struct {
Interactive bool
}
type VMState struct {
constants []object.Object
globals []object.Object
symbols *compiler.SymbolTable
}
func NewVMState() *VMState {
symbolTable := compiler.NewSymbolTable()
for i, builtin := range builtins.BuiltinsIndex {
symbolTable.DefineBuiltin(i, builtin.Name)
}
return &VMState{
constants: []object.Object{},
globals: make([]object.Object, vm.GlobalsSize),
symbols: symbolTable,
}
}
type REPL struct {
user string
args []string
@@ -101,14 +81,14 @@ func (r *REPL) Eval(f io.Reader) (env *object.Environment) {
// Exec parses, compiles and executes the program given by f and returns
// the resulting virtual machine, any errors are printed to stderr
func (r *REPL) Exec(f io.Reader) (state *VMState) {
func (r *REPL) Exec(f io.Reader) (state *vm.VMState) {
b, err := io.ReadAll(f)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading source file: %s", err)
return
}
state = NewVMState()
state = vm.NewVMState()
l := lexer.New(string(b))
p := parser.New(l)
@@ -119,7 +99,7 @@ func (r *REPL) Exec(f io.Reader) (state *VMState) {
return
}
c := compiler.NewWithState(state.symbols, state.constants)
c := compiler.NewWithState(state.Symbols, state.Constants)
c.Debug = r.opts.Debug
err = c.Compile(program)
if err != nil {
@@ -128,9 +108,9 @@ func (r *REPL) Exec(f io.Reader) (state *VMState) {
}
code := c.Bytecode()
state.constants = code.Constants
state.Constants = code.Constants
machine := vm.NewWithGlobalState(code, state.globals)
machine := vm.NewWithState(code, state)
machine.Debug = r.opts.Debug
err = machine.Run()
if err != nil {
@@ -176,11 +156,11 @@ func (r *REPL) StartEvalLoop(in io.Reader, out io.Writer, env *object.Environmen
}
// StartExecLoop starts the REPL in a continious exec loop
func (r *REPL) StartExecLoop(in io.Reader, out io.Writer, state *VMState) {
func (r *REPL) StartExecLoop(in io.Reader, out io.Writer, state *vm.VMState) {
scanner := bufio.NewScanner(in)
if state == nil {
state = NewVMState()
state = vm.NewVMState()
}
for {
@@ -201,7 +181,7 @@ func (r *REPL) StartExecLoop(in io.Reader, out io.Writer, state *VMState) {
continue
}
c := compiler.NewWithState(state.symbols, state.constants)
c := compiler.NewWithState(state.Symbols, state.Constants)
c.Debug = r.opts.Debug
err := c.Compile(program)
if err != nil {
@@ -210,9 +190,9 @@ func (r *REPL) StartExecLoop(in io.Reader, out io.Writer, state *VMState) {
}
code := c.Bytecode()
state.constants = code.Constants
state.Constants = code.Constants
machine := vm.NewWithGlobalState(code, state.globals)
machine := vm.NewWithState(code, state)
machine.Debug = r.opts.Debug
err = machine.Run()
if err != nil {