bunch of changes
Some checks failed
Build / build (push) Failing after 5m54s
Publish Image / publish (push) Failing after 35s
Test / build (push) Failing after 5m46s

This commit is contained in:
Chuck Smith
2024-03-28 17:19:23 -04:00
parent 244b71d245
commit fb0cebaf56
16 changed files with 174 additions and 320 deletions

29
repl.go
View File

@@ -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