optimizations
Some checks failed
Build / build (push) Successful in 9m47s
Publish Image / publish (push) Failing after 49s
Test / build (push) Failing after 6m19s

This commit is contained in:
Chuck Smith
2024-04-02 14:32:03 -04:00
parent 07fd82b261
commit 88e3330856
18 changed files with 510 additions and 31 deletions

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"log"
"monkey/internal/compiler"
"monkey/internal/object"
"monkey/internal/parser"
"monkey/internal/vm"
"os"
@@ -24,7 +23,7 @@ fib := fn(n) {
return fib(n-1) + fib(n-2)
}
print(fib(%d))`
println(fib(%d))`
func checkErr(err error) {
if err != nil {
@@ -39,13 +38,12 @@ func readFile(path string) string {
return string(b)
}
func fileOrDefault() string {
func fileOrDefault() (string, []string) {
if flag.NArg() > 0 {
log.Printf("Using %s as program input...", flag.Arg(0))
object.Args = flag.Args()[1:]
return readFile(flag.Arg(0))
return readFile(flag.Arg(0)), flag.Args()[1:]
}
return defaultCode
return defaultCode, flag.Args()[:]
}
func main() {
@@ -60,7 +58,7 @@ func main() {
log.Printf("Using %d as n", *n)
code := fileOrDefault()
code, args := fileOrDefault()
if strings.Count(code, "%d") > 0 {
code = fmt.Sprintf(code, *n)
}
@@ -78,7 +76,7 @@ func main() {
checkErr(c.Compile(tree))
checkErr(pprof.StartCPUProfile(cpuf))
defer pprof.StopCPUProfile()
mvm := vm.New("<profiler>", c.Bytecode())
mvm := vm.New("<profiler>", c.Bytecode(), vm.WithContext(context.New(context.WithArgs(args))))
checkErr(mvm.Run())
runtime.GC()
checkErr(pprof.WriteHeapProfile(heapf))