-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb-cli.mooc
More file actions
178 lines (155 loc) · 4.32 KB
/
Copy pathweb-cli.mooc
File metadata and controls
178 lines (155 loc) · 4.32 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
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
import Core from "moocscript.core"
import Window from "js" { global }
-- Save references to lua baselib functions used
local _G = _G
local load = load
local pack, unpack, tinsert, tremove = table.pack, table.unpack, table.insert, table.remove
local tostring = tostring
local traceback = debug.traceback
local xpcall = xpcall
document = Window.document
output = document:getElementById("moocscript-console")
prompt = document:getElementById("moocscript-prompt")
input = document:getElementById("moocscript-input")
assert(output and prompt and input)
fn triggerEvent(el, type) {
e = document:createEvent("HTMLEvents")
e:initEvent(type, false, true)
el:dispatchEvent(e)
}
history = {}
historyIndex = nil
historyLimit = 128
_G._print = fn(...) {
toprint = pack(...)
line = document:createElement("pre")
line.style["white-space"] = "pre-wrap"
output:appendChild(line)
for i = 1, toprint.n {
if i ~= 1 {
line:appendChild(document:createTextNode("\t"))
}
line:appendChild(document:createTextNode(tostring(toprint[i])))
}
output.scrollTop = output.scrollHeight
}
local mooc_config = {
shebang = false,
fi_scope = { "*" = true }
}
fn moocLoadString(text) {
ret, emsg = Core.toAST(mooc_config, text)
if not ret {
return nil, emsg
}
ret, emsg = Core.toLua(mooc_config, ret)
if not ret {
return nil, emsg
}
ret, emsg = load(ret, "stdin")
if not ret {
return nil, emsg
}
return ret
}
fn doREPL() {
do {
line = document:createElement("span")
line:appendChild(document:createTextNode(prompt.textContent))
--
item = document:createElement("pre")
item:setAttribute('class', 'line')
item.className = "lua"
item.style.padding = "0"
item.style.display = "inline"
item.style["white-space"] = "pre-wrap"
item.textContent = input.value
--
line:appendChild(item)
output:appendChild(line)
output:appendChild(document:createElement("br"))
output.scrollTop = output.scrollHeight
}
if input.value.length == 0 {
return
}
line = input.value
if history[#history] ~= line {
tinsert(history, line)
if #history > historyLimit {
tremove(history, 1)
}
}
fc, err = moocLoadString("return " .. line)
if not fc {
fc, err = moocLoadString(line)
}
if fc {
_ENV.print = _G._print
results = pack(xpcall(fc, traceback))
if results[1] {
if results.n > 1 {
_G._print(unpack(results, 2, results.n))
}
} else {
_G._print(results[2])
}
} else {
_G._print(err)
}
input.value = ""
triggerEvent(output, "change")
}
fn input:onkeydown(e) {
if not e {
e = Window.event
}
key = e.key or e.which
if key == "Enter" and not e.shiftKey {
historyIndex = nil
doREPL()
return false
} elseif key == "ArrowUp" or key == "Up" {
if historyIndex {
if historyIndex > 1 {
historyIndex = historyIndex - 1
}
} else { -- start with more recent history item
hist_len = #history
if hist_len > 0 {
historyIndex = hist_len
}
}
input.value = history[historyIndex]
return false
} elseif key == "ArrowDown" or key == "Down" {
newvalue = ""
if historyIndex {
if historyIndex < #history {
historyIndex = historyIndex + 1
newvalue = history[historyIndex]
} else { -- no longer in history
historyIndex = nil
}
}
input.value = newvalue
return false
} elseif key == "l"
and e.ctrlKey
and not e.shiftKey
and not e.altKey
and not e.metaKey
and not e.isComposing {
-- Ctrl+L clears screen like you would expect in a terminal
output.innerHTML = ""
_G._print(_G._COPYRIGHT)
_G._print(Core.version())
_G._print("export * -- default global variable")
return false
} else {
}
}
_G._print(_G._COPYRIGHT)
_G._print(Core.version())
_G._print("export * -- default global variable")
moocLoadString("export *")