forked from refaktor/rye
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_wasm.go
executable file
·236 lines (178 loc) · 4.58 KB
/
main_wasm.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//go:build wasm
// +build wasm
package main
import (
"fmt"
"regexp"
"strings"
"syscall/js"
"github.com/refaktor/rye/contrib"
"github.com/refaktor/rye/env"
"github.com/refaktor/rye/evaldo"
"github.com/refaktor/rye/loader"
"github.com/refaktor/rye/term"
"github.com/refaktor/rye/util"
)
type TagType int
type RjType int
type Series []any
type anyword struct {
kind RjType
idx int
}
type node struct {
kind RjType
value any
}
var ES *env.ProgramState
var CODE []any
var PREV_LINES string
var prevResult env.Object
var ml *util.MLState
//
// main function. Dispatches to appropriate mode function
//
func main1() {
evaldo.ShowResults = true
// main_rye_string("print $Hello world$", false, false)
}
//
// main for awk like functionality with rye language
//
var (
jsCallback js.Value
jsCallback2 js.Value
)
func sendMessageToJS(message string) {
jsCallback.Invoke(message)
}
func sendLineToJS(line string) string {
ret := jsCallback2.Invoke(line)
return ret.String()
}
func main() {
term.SetSB(sendMessageToJS)
c := make(chan util.KeyEvent)
ml = util.NewMicroLiner(c, sendMessageToJS, sendLineToJS)
js.Global().Set("RyeEvalString", js.FuncOf(RyeEvalString))
js.Global().Set("RyeEvalString2", js.FuncOf(RyeEvalString))
js.Global().Set("RyeEvalShellLine", js.FuncOf(RyeEvalShellLine))
js.Global().Set("InitRyeShell", js.FuncOf(InitRyeShell))
js.Global().Set("SendKeypress", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if len(args) > 0 {
cc := util.NewKeyEvent(args[0].String(), args[1].Int(), args[2].Bool(), args[3].Bool(), args[4].Bool())
c <- cc
}
return nil
}))
// Get the JavaScript function to call back
jsCallback = js.Global().Get("receiveMessageFromGo")
jsCallback2 = js.Global().Get("receiveLineFromGo")
ml.MicroPrompt("x> ", "", 0)
/* for {
key := <-c
// Process the keypress and then send a message back to JavaScript
response := key
if key == "A" {
response = "\x1B[1;3;31mA\x1B[0m"
}
sendMessageToJS(response)
} */
}
func InitRyeShell(this js.Value, args []js.Value) any {
// subc := false
// fmt.Println("INITIALIZATION")
ps := env.NewProgramStateNEW()
evaldo.RegisterBuiltins(ps)
contrib.RegisterBuiltins(ps, &evaldo.BuiltinNames)
ctx := ps.Ctx
ps.Ctx = env.NewEnv(ctx)
ES = ps
evaldo.ShowResults = false
/* bloc k := loader.LoadString(" ", false)
switch val := block.(type) {
case env.Block:
if subc {
}
evaldo.EvalBlock(es)
evaldo.MaybeDisplayFailureOrErrorWASM(es, genv, sendMessageToJS)
ES = es
prevResult = env.Void{}
case env.Error:
fmt.Println(val.Message)
return "Error"
}
return "Other"*/
return "Initalized"
}
func RyeEvalShellLine(this js.Value, args []js.Value) any {
sig := false
subc := false
evaldo.ShowResults = false
code := args[0].String()
multiline := len(code) > 1 && code[len(code)-1:] == " "
comment := regexp.MustCompile(`\s*;`)
codes := comment.Split(code, 2) //--- just very temporary solution for some comments in repl. Later should probably be part of loader ... maybe?
code1 := strings.Trim(codes[0], "\t")
if multiline {
PREV_LINES += code1
return "next line"
}
code1 = PREV_LINES + "\n" + code1
PREV_LINES = ""
if ES == nil {
return "Error: Rye is not initialized"
}
ps := ES
block := loader.LoadStringNEW(" "+code1+" ", sig, ps)
switch val := block.(type) {
case env.Block:
ps = env.AddToProgramState(ps, val.Series, ps.Idx)
if subc {
ctx := ps.Ctx
ps.Ctx = env.NewEnv(ctx)
}
evaldo.EvalBlockInj(ps, prevResult, true)
evaldo.MaybeDisplayFailureOrErrorWASM(ps, ps.Idx, sendMessageToJS)
prevResult = ps.Res
if !ps.ErrorFlag && ps.Res != nil {
sendMessageToJS("\033[38;5;37m" + ps.Res.Inspect(*ps.Idx) + "\x1b[0m")
}
ps.ReturnFlag = false
ps.ErrorFlag = false
ps.FailureFlag = false
ml.AppendHistory(code)
return ""
case env.Error:
fmt.Println(val.Message)
return "Error"
}
return "Other"
}
func RyeEvalString(this js.Value, args []js.Value) any {
sig := false
subc := false
code := args[0].String()
//fmt.Println("RYE EVAL STRING")
// fmt.Println(code)
//util.PrintHeader()
//defer profile.Start(profile.CPUProfile).Stop()
block, genv := loader.LoadString(code, sig)
switch val := block.(type) {
case env.Block:
es := env.NewProgramState(block.(env.Block).Series, genv)
evaldo.RegisterBuiltins(es)
contrib.RegisterBuiltins(es, &evaldo.BuiltinNames)
if subc {
ctx := es.Ctx
es.Ctx = env.NewEnv(ctx)
}
evaldo.EvalBlock(es)
evaldo.MaybeDisplayFailureOrError(es, genv)
return es.Res.Print(*es.Idx)
case env.Error:
fmt.Println(val.Message)
return "Error"
}
return "Other"
}