server
Some checks failed
Build / build (push) Failing after 6m4s
Test / build (push) Failing after 6m33s

This commit is contained in:
Chuck Smith
2024-04-02 12:21:41 -04:00
parent 862119e90e
commit 4c9ec5aaaa
77 changed files with 1181 additions and 244 deletions

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"log"
"monkey/internal/object"
"os"
"path/filepath"
"runtime"
@@ -18,7 +17,7 @@ import (
)
// MonkeyVersion is the version number for the monkey language virtual machine.
const MonkeyVersion = "v0.0.1"
var MonkeyVersion = "v2.0.1"
func fileExists(filename string) bool {
info, err := os.Stat(filename)
@@ -94,7 +93,7 @@ func compileString(input string, debug bool) (bc *compiler.Bytecode, err error)
}
// ExecFile executes a Monkey program from a file on disk.
func ExecFile(fn string, args []string, debug, trace bool) error {
func ExecFile(fn string, opts *Options) error {
var (
bytecode *compiler.Bytecode
err error
@@ -112,13 +111,13 @@ func ExecFile(fn string, args []string, debug, trace bool) error {
if err != nil {
return err
}
bytecode, err = compileString(string(input), debug)
bytecode, err = compileString(string(input), opts.Debug)
}
if err != nil {
return err
}
if debug {
if opts.Debug {
log.Printf("Bytecode:\n%s\n", bytecode)
}
@@ -129,11 +128,7 @@ func ExecFile(fn string, args []string, debug, trace bool) error {
}
}
object.Args = args
mvm := vm.New(fn, bytecode)
mvm.Debug = debug
mvm.Trace = trace
mvm := vm.New(fn, bytecode, vm.WithDebug(opts.Debug), vm.WithTrace(opts.Trace))
if err := mvm.Run(); err != nil {
return err
}
@@ -142,19 +137,17 @@ func ExecFile(fn string, args []string, debug, trace bool) error {
}
// ExecString executes a Monkey program from a string.
func ExecString(input string, debug, trace bool) error {
bytecode, err := compileString(input, debug)
func ExecString(input string, opts *Options) error {
bytecode, err := compileString(input, opts.Debug)
if err != nil {
return err
}
if debug {
if opts.Debug {
log.Printf("Bytecode:\n%s\n", bytecode)
}
mvm := vm.New("<stdin>", bytecode)
mvm.Debug = debug
mvm.Trace = trace
mvm := vm.New("<stdin>", bytecode, opts.ToVMOptions()...)
if err = mvm.Run(); err != nil {
return err
}
@@ -163,7 +156,7 @@ func ExecString(input string, debug, trace bool) error {
}
// CompileFiles compiles multiple Monkey files and returns any errors encountered during compilation.
func CompileFiles(fns []string, debug bool) error {
func CompileFiles(fns []string, opts *Options) error {
for _, fn := range fns {
ext := filepath.Ext(fn)
mc := fn[:len(fn)-len(ext)] + ".mc"
@@ -184,17 +177,17 @@ func CompileFiles(fns []string, debug bool) error {
return errors.New(buf.String())
}
if debug {
if opts.Debug {
log.Printf("AST:\n%s\n", res)
}
c := compiler.New()
c.Debug = debug
c.Debug = opts.Debug
if err := c.Compile(res); err != nil {
return err
}
if debug {
if opts.Debug {
log.Printf("Bytecode:\n%s\n", c.Bytecode())
}