- Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
81 lines (70 loc) · 2.05 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package main
import (
"fmt"
"html"
"github.com/gopherjs/jquery"
"github.com/asciian/iris/runtime"
"github.com/asciian/iris/runtime/ilos"
"github.com/asciian/iris/runtime/ilos/class"
"github.com/asciian/iris/runtime/ilos/instance"
)
constversion="a0f4f8d"
varstream=make(chanstring)
typeDomstruct{}
func (Dom) Write(p []byte) (nint, errerror) {
jQuery("#output").Append(string(p))
returnlen(p), nil
}
func (domDom) Read(p []byte) (nint, errerror) {
s:=<-stream
fmt.Fprint(dom, s, "\n")
copy(p, []byte(s))
returnlen(p), nil
}
varjQuery=jquery.NewJQuery
funcmain() {
prompt:=true
dom:=new(Dom)
runtime.TopLevel.StandardInput=instance.NewStream(dom, nil)
runtime.TopLevel.StandardOutput=instance.NewStream(nil, dom)
runtime.TopLevel.ErrorOutput=instance.NewStream(nil, dom)
fmt.Fprintf(dom, `Welcome to Iris (%v). Iris is an ISLisp implementation on Go.
This REPL works on JavaScript with gopherjs.
Copyright © 2017 asciian All Rights Reserved.`, version)
jQuery("#version").SetHtml(version)
jQuery("#input").On(jquery.KEYDOWN, func(e jquery.Event) {
if!e.ShiftKey&&e.KeyCode==13 {
ifprompt {
fmt.Fprint(dom, "\n> ")
}
gofunc() {
input:=jQuery("#input").Html()
jQuery("#input").SetHtml("")
stream<-jQuery(`<span>`+input+`</span>`).Text()
}()
}
})
for {
prompt=true
jQuery("#prompt").Show()
runtime.TopLevel.StandardInput=instance.NewStream(dom, nil)
exp, err:=runtime.Read(runtime.TopLevel)
iferr!=nil {
if!ilos.InstanceOf(class.EndOfStream, err) {
fmt.Fprint(dom, html.EscapeString(err.String()))
}
continue
}
prompt=false
jQuery("#prompt").Hide()
ret, err:=runtime.Eval(runtime.TopLevel, exp)
iferr!=nil {
fmt.Fprint(dom, html.EscapeString(err.String()))
continue
}
fmt.Fprint(dom, html.EscapeString(ret.String()))
}
}