VM globals
Some checks failed
Build / build (push) Failing after 1m50s
Test / build (push) Failing after 1m25s

This commit is contained in:
Chuck Smith
2024-02-20 16:24:59 -05:00
parent e8254fc996
commit 8caeaca559
4 changed files with 54 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ import (
"io"
"monkey/compiler"
"monkey/lexer"
"monkey/object"
"monkey/parser"
"monkey/vm"
)
@@ -15,6 +16,10 @@ const PROMPT = ">> "
func Start(in io.Reader, out io.Writer) {
scanner := bufio.NewScanner(in)
constants := []object.Object{}
globals := make([]object.Object, vm.GlobalsSize)
symbolTable := compiler.NewSymbolTable()
for {
fmt.Fprintf(out, PROMPT)
scanned := scanner.Scan()
@@ -32,14 +37,17 @@ func Start(in io.Reader, out io.Writer) {
continue
}
comp := compiler.New()
comp := compiler.NewWithState(symbolTable, constants)
err := comp.Compile(program)
if err != nil {
fmt.Fprintf(out, "Woops! Compilation failed:\n %s\n", err)
continue
}
machine := vm.New(comp.Bytecode())
code := comp.Bytecode()
constants = code.Constants
machine := vm.NewWithGlobalState(comp.Bytecode(), globals)
err = machine.Run()
if err != nil {
fmt.Fprintf(out, "Woops! Executing bytecode failed:\n %s\n", err)