bunch of changes
This commit is contained in:
29
repl.go
29
repl.go
@@ -3,7 +3,9 @@ package monkey
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/tebeka/atexit"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
@@ -18,19 +20,25 @@ import (
|
||||
// Package repl implements the Read-Eval-Print-Loop or interactive console
|
||||
// by lexing, parsing and evaluating the input in the interpreter
|
||||
|
||||
func VmREPL() error {
|
||||
func VmREPL(args []string, debug bool) error {
|
||||
var state = vm.NewVMState()
|
||||
|
||||
object.Args = args
|
||||
|
||||
initState, err := term.MakeRaw(0)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return fmt.Errorf("error opening terminal: %w", err)
|
||||
}
|
||||
defer term.Restore(0, initState)
|
||||
atexit.Register(func() {
|
||||
term.Restore(0, initState)
|
||||
})
|
||||
|
||||
t := term.NewTerminal(os.Stdin, ">>> ")
|
||||
t.AutoCompleteCallback = autoComplete
|
||||
object.Stdout = t
|
||||
object.ExitFunction = atexit.Exit
|
||||
|
||||
PrintVersionInfo(t)
|
||||
for {
|
||||
@@ -51,13 +59,20 @@ func VmREPL() error {
|
||||
continue
|
||||
}
|
||||
|
||||
if debug {
|
||||
log.Printf("AST:\n%s\n", res)
|
||||
}
|
||||
|
||||
c := compiler.NewWithState(state.Symbols, &state.Constants)
|
||||
c.Debug = debug
|
||||
c.SetFileInfo("<stdin>", input)
|
||||
if err := c.Compile(res); err != nil {
|
||||
fmt.Fprintln(t, err)
|
||||
continue
|
||||
}
|
||||
|
||||
mvm := vm.NewWithState("<stdin>", c.Bytecode(), state)
|
||||
mvm.Debug = debug
|
||||
if err := mvm.Run(); err != nil {
|
||||
fmt.Fprintf(t, "runtime error: %v\n", err)
|
||||
continue
|
||||
@@ -114,12 +129,14 @@ func acceptUntil(t *term.Terminal, start, end string) (string, error) {
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
func SimpleVmREPL() {
|
||||
func SimpleVmREPL(args []string, debug bool) {
|
||||
var (
|
||||
state = vm.NewVMState()
|
||||
reader = bufio.NewReader(os.Stdin)
|
||||
)
|
||||
|
||||
object.Args = args
|
||||
|
||||
PrintVersionInfo(os.Stdout)
|
||||
for {
|
||||
fmt.Print(">>> ")
|
||||
@@ -140,14 +157,20 @@ func SimpleVmREPL() {
|
||||
continue
|
||||
}
|
||||
|
||||
if debug {
|
||||
log.Printf("AST:\n%s\n", res)
|
||||
}
|
||||
|
||||
c := compiler.NewWithState(state.Symbols, &state.Constants)
|
||||
c.Debug = debug
|
||||
c.SetFileInfo("<stdin>", input)
|
||||
if err := c.Compile(res); err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
mvm := vm.NewWithState("<stdin>", c.Bytecode(), state)
|
||||
|
||||
mvm := vm.NewWithState("<stdin>", c.Bytecode(), state)
|
||||
mvm.Debug = debug
|
||||
if err := mvm.Run(); err != nil {
|
||||
fmt.Printf("runtime error: %v\n", err)
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user