/
main.go
173 lines (153 loc) · 3.35 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"time"
"github.com/coyove/nj"
"github.com/coyove/nj/bas"
)
var (
output = flag.String("o", "ret", "separated by comma: (none|compileonly|opcode|bytes|ret|timing)+")
input = flag.String("i", "f", "input source, 'f': file, '-': stdin, others: string")
version = flag.Bool("v", false, "print version and usage")
repl = flag.Bool("repl", false, "repl mode")
timeout = flag.Int("t", 0, "max execution time in ms")
stackSize = flag.Int("ss", 1e6, "max stack size, 1 stack size is 16-byte on 64bit platform")
apiServer = flag.String("serve", "", "start as language playground")
)
func main() {
source := ""
for i, arg := range os.Args {
if _, err := os.Stat(arg); err == nil && i > 0 {
source = arg
os.Args = append(os.Args[:i], os.Args[i+1:]...)
break
}
}
flag.Parse()
if *apiServer != "" {
http.HandleFunc("/", nj.PlaygroundHandler("", nil))
log.Println("listen", *apiServer)
http.ListenAndServe(*apiServer, nil)
return
}
if *repl {
runRepl()
}
log.SetFlags(0)
if *version {
fmt.Println("nj virtual machine v" + strconv.FormatInt(bas.Version, 10) + " (" + runtime.GOOS + "/" + runtime.GOARCH + ")")
flag.Usage()
return
}
if p, _ := os.Getwd(); !strings.Contains(p, "/cmd/nj") {
f, err := os.Create("cpuprofile")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
switch *input {
case "f":
if source == "" {
log.Fatalln("Please specify the input file: ./nj <filename>")
}
case "-":
buf, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalln(err)
}
source = string(buf)
default:
if _, err := os.Stat(*input); err == nil {
source = *input
*input = "f"
} else {
source = *input
}
}
var _opcode, _timing, _ret, _compileonly bool
for _, a := range strings.Split(*output, ",") {
switch a {
case "o", "opcode", "op":
_opcode = true
case "r", "ret", "return":
_ret = true
case "t", "time", "timing":
_timing = true
case "co", "compile", "compileonly":
_compileonly = true
}
}
runtime.GOMAXPROCS(runtime.NumCPU() * 2)
start := time.Now()
var b *bas.Program
var err error
defer func() {
if _opcode {
log.Println(b.GoString())
}
if _timing {
log.Printf("Time elapsed: %v\n", time.Since(start))
}
}()
if *input == "f" {
b, err = nj.LoadFile(source, nil)
} else {
b, err = nj.LoadString(source, nil)
}
if err != nil {
log.Fatalln(err)
}
b.MaxStackSize = int64(*stackSize)
if _compileonly {
return
}
if *timeout > 0 {
go func() {
time.Sleep(time.Millisecond * time.Duration(*timeout))
b.Stop()
}()
}
i := b.Run()
if _ret {
fmt.Println(i)
}
}
func runRepl() {
var code []string
var globals *bas.Object
rd := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ")
s, err := rd.ReadString('\n')
if err != nil {
fmt.Println("Exit")
break
}
s = strings.TrimSuffix(s, "\n")
code = append(code, s)
if s == "" || strings.HasSuffix(s, ";") {
text := strings.TrimSuffix(strings.Join(code, "\n"), ";")
p, err := nj.LoadString(text, &nj.LoadOptions{Globals: globals.ToMap()})
if err != nil {
fmt.Println("x", err)
} else {
res := p.Run()
fmt.Println("=>", res)
globals = p.LocalsObject()
}
code = code[:0]
}
}
}