VM globals
This commit is contained in:
12
repl/repl.go
12
repl/repl.go
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user