62 lines
889 B
Go
62 lines
889 B
Go
//go:build ignore
|
|
|
|
package main
|
|
|
|
import (
|
|
"monkey/internal/compiler"
|
|
"monkey/internal/parser"
|
|
"monkey/internal/vm"
|
|
"os"
|
|
"runtime/pprof"
|
|
)
|
|
|
|
const fib = `
|
|
fib := fn(n) {
|
|
if (n < 2) {
|
|
return n
|
|
}
|
|
return fib(n-1) + fib(n-2)
|
|
}
|
|
|
|
print(fib(35))`
|
|
|
|
func check(err error) {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func code(path string) string {
|
|
b, err := os.ReadFile(path)
|
|
check(err)
|
|
|
|
return string(b)
|
|
}
|
|
|
|
func fileOrDefault() string {
|
|
if len(os.Args) > 1 {
|
|
return code(os.Args[1])
|
|
}
|
|
return fib
|
|
}
|
|
|
|
func main() {
|
|
cpuf, err := os.Create("cpu.prof")
|
|
check(err)
|
|
|
|
code := fileOrDefault()
|
|
tree, errs := parser.Parse("<profiler>", code)
|
|
if len(errs) > 0 {
|
|
panic("parser errors")
|
|
}
|
|
|
|
c := compiler.New()
|
|
c.SetFileInfo("<profiler>", code)
|
|
check(c.Compile(tree))
|
|
|
|
check(pprof.StartCPUProfile(cpuf))
|
|
defer pprof.StopCPUProfile()
|
|
mvm := vm.New("<profiler>", c.Bytecode())
|
|
mvm.Run()
|
|
}
|