-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
81 lines (70 loc) · 2.05 KB
/
main.go
File metadata and controls
81 lines (70 loc) · 2.05 KB
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"
)
const version = "a0f4f8d"
var stream = make(chan string)
type Dom struct{}
func (Dom) Write(p []byte) (n int, err error) {
jQuery("#output").Append(string(p))
return len(p), nil
}
func (dom Dom) Read(p []byte) (n int, err error) {
s := <-stream
fmt.Fprint(dom, s, "\n")
copy(p, []byte(s))
return len(p), nil
}
var jQuery = jquery.NewJQuery
func main() {
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 {
if prompt {
fmt.Fprint(dom, "\n> ")
}
go func() {
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)
if err != 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)
if err != nil {
fmt.Fprint(dom, html.EscapeString(err.String()))
continue
}
fmt.Fprint(dom, html.EscapeString(ret.String()))
}
}