optimizations
Some checks failed
Build / build (push) Successful in 10m29s
Publish Image / publish (push) Failing after 31s
Test / build (push) Failing after 6m34s

This commit is contained in:
Chuck Smith
2024-04-02 14:08:08 -04:00
parent 4c9ec5aaaa
commit 07fd82b261
23 changed files with 296 additions and 265 deletions

46
repl.go
View File

@@ -6,7 +6,6 @@ import (
"github.com/tebeka/atexit"
"io"
"log"
"monkey/internal/context"
"os"
"strings"
@@ -22,7 +21,10 @@ import (
// by lexing, parsing and evaluating the input in the interpreter
// REPL provides a read-eval-print loop for the monkey virtual machine.
func REPL(args []string, opts *Options) error {
func REPL(opts *Options) error {
if opts == nil {
opts = &Options{}
}
initState, err := term.MakeRaw(0)
if err != nil {
@@ -42,12 +44,7 @@ func REPL(args []string, opts *Options) error {
t := term.NewTerminal(os.Stdin, ">>> ")
t.AutoCompleteCallback = autoComplete
ctx := context.New(
context.WithArgs(args),
context.WithStdout(t),
context.WithStderr(t),
context.WithExit(atexit.Exit),
)
opts.Exit = atexit.Exit
state := vm.NewState()
@@ -86,14 +83,7 @@ func REPL(args []string, opts *Options) error {
log.Printf("Bytecode:\n%s\n", c.Bytecode())
}
opts := []vm.Option{
vm.WithContext(ctx),
vm.WithDebug(opts.Debug),
vm.WithTrace(opts.Trace),
vm.WithState(state),
}
mvm := vm.New("<stdin>", c.Bytecode(), opts...)
mvm := vm.New("<stdin>", c.Bytecode(), opts.ToVMOptions()...)
if err := mvm.Run(); err != nil {
fmt.Fprintf(t, "runtime error: %v\n", err)
@@ -152,8 +142,12 @@ func acceptUntil(t *term.Terminal, start, end string) (string, error) {
}
// SimpleREPL provides a simple read-eval-print loop for the monkey virtual machine.
func SimpleREPL(args []string, opts *Options) {
var reader = bufio.NewReader(os.Stdin)
func SimpleREPL(opts *Options) {
if opts == nil {
opts = &Options{}
}
reader := bufio.NewReader(os.Stdin)
defer func() {
if err := recover(); err != nil {
@@ -164,13 +158,6 @@ func SimpleREPL(args []string, opts *Options) {
t := term.NewTerminal(os.Stdin, ">>> ")
t.AutoCompleteCallback = autoComplete
ctx := context.New(
context.WithArgs(args),
context.WithStdout(t),
context.WithStderr(t),
context.WithExit(atexit.Exit),
)
state := vm.NewState()
PrintVersionInfo(os.Stdout)
@@ -209,14 +196,7 @@ func SimpleREPL(args []string, opts *Options) {
log.Printf("Bytecode:\n%s\n", c.Bytecode())
}
opts := []vm.Option{
vm.WithContext(ctx),
vm.WithDebug(opts.Debug),
vm.WithTrace(opts.Trace),
vm.WithState(state),
}
mvm := vm.New("<stdin>", c.Bytecode(), opts...)
mvm := vm.New("<stdin>", c.Bytecode(), opts.ToVMOptions()...)
if err := mvm.Run(); err != nil {
fmt.Printf("runtime error: %v\n", err)