-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
45 lines (37 loc) · 842 Bytes
/
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
package main
import (
"bytes"
_ "fmt"
_ "strings"
"syscall/js"
"magpie/eval"
"magpie/lexer"
"magpie/parser"
)
func runCode(this js.Value, i []js.Value) interface{} {
m := make(map[string]interface{})
var buf bytes.Buffer
l := lexer.NewLexer(i[0].String())
p := parser.NewParser(l)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
for _, msg := range p.Errors() {
buf.WriteString(msg + "\n")
}
m["output"] = buf.String()
return m
}
scope := eval.NewScope(nil, &buf)
result := eval.Eval(program, scope)
if (string(result.Type()) == eval.ERROR_OBJ) {
m["output"] = buf.String() + result.Inspect()
} else {
m["output"] = buf.String()
}
return m
}
func main() {
c := make(chan struct{}, 0)
js.Global().Set("magpie_run_code", js.FuncOf(runCode))
<-c
}