forked from mattn/anko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anko.go
129 lines (108 loc) · 2.74 KB
/
anko.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
// +build js,wasm
package main
import (
"fmt"
"html"
"strings"
"syscall/js"
"github.com/mattn/anko/core"
"github.com/mattn/anko/packages"
"github.com/mattn/anko/parser"
"github.com/mattn/anko/vm"
)
var (
result = js.Global.Get("document").Call("getElementById", "result")
input = js.Global.Get("document").Call("getElementById", "input")
)
func writeCommand(s string) {
result.Set("innerHTML", result.Get("innerHTML").String()+"<p class='command'>"+html.EscapeString(s)+"</p>")
result.Set("scrollTop", result.Get("scrollHeight").Int())
}
func writeStdout(s string) {
result.Set("innerHTML", result.Get("innerHTML").String()+"<p class='stdout'>"+html.EscapeString(s)+"</p>")
result.Set("scrollTop", result.Get("scrollHeight").Int())
}
func writeStderr(s string) {
result.Set("innerHTML", result.Get("innerHTML").String()+"<p class='stderr'>"+html.EscapeString(s)+"</p>")
result.Set("scrollTop", result.Get("scrollHeight").Int())
}
func main() {
env := vm.NewEnv()
core.Import(env)
packages.DefineImport(env)
env.Define("print", func(a ...interface{}) {
writeStdout(fmt.Sprint(a...))
})
env.Define("printf", func(a string, b ...interface{}) {
writeStdout(fmt.Sprintf(a, b...))
})
var following bool
var source string
parser.EnableErrorVerbose()
ch := make(chan string)
input.Call("addEventListener", "keypress", js.NewCallback(func(args []js.Value) {
e := args[0]
if e.Get("keyCode").Int() != 13 {
return
}
s := e.Get("target").Get("value").String()
e.Get("target").Set("value", "")
writeCommand(s)
ch <- s
}))
input.Set("disabled", false)
result.Set("innerHTML", "")
go func() {
for {
text, ok := <-ch
if !ok {
break
}
source += text
if source == "" {
continue
}
if source == "quit()" {
break
}
stmts, err := parser.ParseSrc(source)
if e, ok := err.(*parser.Error); ok {
es := e.Error()
if strings.HasPrefix(es, "syntax error: unexpected") {
if strings.HasPrefix(es, "syntax error: unexpected $end,") {
following = true
continue
}
} else {
if e.Pos.Column == len(source) && !e.Fatal {
writeStderr(e.Error())
following = true
continue
}
if e.Error() == "unexpected EOF" {
following = true
continue
}
}
}
following = false
source = ""
var v interface{}
if err == nil {
v, err = vm.Run(stmts, env)
}
if err != nil {
if e, ok := err.(*vm.Error); ok {
writeStderr(fmt.Sprintf("%d:%d %s\n", e.Pos.Line, e.Pos.Column, err))
} else if e, ok := err.(*parser.Error); ok {
writeStderr(fmt.Sprintf("%d:%d %s\n", e.Pos.Line, e.Pos.Column, err))
} else {
writeStderr(err.Error())
}
continue
}
writeStdout(fmt.Sprintf("%#v\n", v))
}
}()
select {}
}