Switching to wsl
Some checks failed
Build / build (push) Successful in 10m47s
Publish Image / publish (push) Failing after 30s
Test / build (push) Successful in 11m0s

This commit is contained in:
Charles Smith
2024-03-31 19:35:04 -04:00
parent 7a32cce8a1
commit a85dc73954
15 changed files with 91 additions and 85 deletions

View File

@@ -3,14 +3,21 @@
package main
import (
"monkey/internal/compiler"
"monkey/internal/parser"
"monkey/internal/vm"
"flag"
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
"strings"
"git.mills.io/prologic/monkey-lang/internal/compiler"
"git.mills.io/prologic/monkey-lang/internal/object"
"git.mills.io/prologic/monkey-lang/internal/parser"
"git.mills.io/prologic/monkey-lang/internal/vm"
)
const fib = `
const defaultCode = `
fib := fn(n) {
if (n < 2) {
return n
@@ -18,44 +25,64 @@ fib := fn(n) {
return fib(n-1) + fib(n-2)
}
print(fib(35))`
print(fib(%d))`
func check(err error) {
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func code(path string) string {
func readFile(path string) string {
b, err := os.ReadFile(path)
check(err)
checkErr(err)
return string(b)
}
func fileOrDefault() string {
if len(os.Args) > 1 {
return code(os.Args[1])
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 fib
return defaultCode
}
func main() {
n := flag.Int("n", 35, "Set n for fib(n) [Default: 35]")
flag.Parse()
cpuf, err := os.Create("cpu.prof")
check(err)
checkErr(err)
heapf, err := os.Create("heap.prof")
checkErr(err)
log.Printf("Using %d as n", *n)
code := fileOrDefault()
if strings.Count(code, "%d") > 0 {
code = fmt.Sprintf(code, *n)
}
log.Printf("Code:\n%s\n", code)
tree, errs := parser.Parse("<profiler>", code)
if len(errs) > 0 {
for _, err := range errs {
log.Println(err)
}
panic("parser errors")
}
c := compiler.New()
c.SetFileInfo("<profiler>", code)
check(c.Compile(tree))
check(pprof.StartCPUProfile(cpuf))
checkErr(c.Compile(tree))
checkErr(pprof.StartCPUProfile(cpuf))
defer pprof.StopCPUProfile()
mvm := vm.New("<profiler>", c.Bytecode())
mvm.Run()
checkErr(mvm.Run())
runtime.GC()
checkErr(pprof.WriteHeapProfile(heapf))
log.Println("CPU Profile written to cpu.prof")
log.Println("Heap Profile written to heap.prof")
}