-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
main_wasm.go
executable file
·249 lines (188 loc) · 4.89 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
237
238
239
240
241
242
243
244
245
246
247
248
249
//go:build wasm
// +build wasm
package main
import (
"context"
"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"
)
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 *term.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 sendMessageToJSNL(message string) {
jsCallback.Invoke(message + "\n")
}
func sendLineToJS(line string) string {
ret := jsCallback2.Invoke(line)
return ret.String()
}
func main() {
term.SetSB(sendMessageToJS)
c := make(chan term.KeyEvent)
ml = term.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("SetTerminalSize", js.FuncOf(SetTerminalSize))
js.Global().Set("SendKeypress", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if len(args) > 0 {
cc := term.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")
ctx := context.Background()
ml.MicroPrompt("x> ", "", 0, ctx)
/* 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 SetTerminalSize(this js.Value, args []js.Value) any {
term.SetTerminalColumns(args[0].Int())
ml.SetColumns(term.GetTerminalColumns())
return "Ok"
}
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, sendMessageToJSNL)
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"
}