-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprofile.go
64 lines (50 loc) · 985 Bytes
/
profile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//go:build ignore
package main
import (
"os"
"runtime/pprof"
"github.com/NicoNex/tau/internal/compiler"
"github.com/NicoNex/tau/internal/parser"
"github.com/NicoNex/tau/internal/vm"
_ "github.com/ianlancetaylor/cgosymbolizer"
)
const fib = `
fib = fn(n) {
if n < 2 {
return n
}
fib(n-1) + fib(n-2)
}
println(fib(40))`
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)
tauCode := fileOrDefault()
tree, errs := parser.Parse("<profiler>", tauCode)
if len(errs) > 0 {
panic("parser errors")
}
c := compiler.New()
c.SetFileInfo("<profiler>", tauCode)
check(c.Compile(tree))
check(pprof.StartCPUProfile(cpuf))
defer pprof.StopCPUProfile()
tvm := vm.New("<profiler>", c.Bytecode())
tvm.Run()
}