forked from JohnEarnest/ok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
370 lines (350 loc) · 8.7 KB
/
index.html
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<!-- an attempt to create a simple browser REPL for oK. -->
<html>
<head><title>oK repl</title></head>
<style>
body {
background-color: gray;
font-smooth: never;
-webkit-font-smoothing : none;
font-family: "Monaco", monospace;
font-size: 9pt;
height: 100%;
width: 100%;
overflow: hidden;
}
a {
color: black;
}
#editor {
background-color: orange;
position: absolute;
top: 5%;
left: 50%;
width: 40%;
height: 90%;
}
textarea {
background-color: lightyellow;
font-smooth: never;
-webkit-font-smoothing : none;
font-family: "Monaco", monospace;
font-size: 9pt;
width: 100%;
height: 100%;
border: none;
border-left: 1px solid gray;
-moz-tab-size:4;
-o-tab-size:4;
tab-size:4;
}
#repl {
background-color: orange;
position: absolute;
display: table;
top:5%;
left:10%;
width:80%;
height:90%;
}
#scroll-w {
display: table-row;
height: 100%;
}
#scroll-h {
position: relative;
padding: 5px;
overflow-y: scroll;
background-color: lightgray;
height: 100%;
word-wrap: break-word;
word-break: break-all;
hyphens: none;
-moz-box-sizing: border-box;
}
#scroll {
position: absolute;
bottom: 0px;
max-height: 100%;
}
#prompt {
display: table-row;
background-color: black;
font-smooth: never;
-webkit-font-smoothing : none;
font-family: "Monaco", monospace;
font-size: 9pt;
color: white;
padding: 5px 0px 5px 20px;
margin: 0;
border: none;
bottom: 0px;
width: 100%;
height: 25px;
max-height: 25px;
overflow: hidden;
}
.err {
color: red;
background-color: pink;
display: none;
}
textarea:focus {
outline: none;
}
::-webkit-scrollbar {
display: none;
}
</style>
<body>
<div id="repl">
<div id="scroll-w"><div id="scroll-h"><div id="scroll"></div></div></div>
<div><textarea id="prompt" autocomplete="off"></textarea></div>
</div>
<div id="editor" style="display: none;">
<textarea id="textedit"></textarea>
</div>
</body>
<script src="oK.js"></script>
<script src="convert.js"></script>
<script>
document.getElementById("textedit").onkeydown = function(event) {
if (event.keyCode == 13 && event.shiftKey) {
saveBuffer();
if (this.selectionStart == this.selectionEnd) {
prompt.value = this.value;
}
else {
prompt.value = this.value.substring(this.selectionStart, this.selectionEnd);
}
processLine();
prompt.value = "";
printRepl("  ");
return false;
}
if (event.keyCode == 9) {
var text = this.value;
var start = this.selectionStart;
var end = this.selectionEnd;
this.value = text.substring(0, start) + '\t' + text.substring(end);
this.selectionStart = this.selectionEnd = start + 1;
saveBuffer();
return false;
}
saveBuffer();
};
function saveBuffer() {
localStorage.setItem("oKeditbuffer", document.getElementById("textedit").value);
}
function loadBuffer() {
var editbuffer = localStorage.getItem("oKeditbuffer");
if (!editbuffer) { editbuffer = "/ contents of this editor is automatically saved in local storage."; }
document.getElementById("textedit").value = editbuffer;
}
function printForm(x) {
return stok(format(x));
}
var env = baseEnv();
var entries = [];
var entryIndex = 0;
var partial = "";
var eid = 0;
setIO("0:", 2, print);
setIO("0:", 4, print);
setIO("0:", 1, readText);
setIO("1:", 1, readJSON);
setIO("5:", 0, printForm);
setIO("5:", 1, printForm);
var repl = document.getElementById("repl");
var scroll = document.getElementById("scroll");
var panel = document.getElementById("scroll-h");
var prompt = document.getElementById("prompt");
prompt.focus();
prompt.onkeydown = processInput;
scroll.innerHTML =
"Welcome to <a href='https://github.com/JohnEarnest/ok' target='_blank'>oK</a> v"+version+
"<br>(inspired by <a href='http://kparc.com/k.txt' target='_blank'>K5</a>)"+
"<br> ";
function escapeHTML(text) {
text += "";
text = text.replace(/&/g, "&");
text = text.replace(/</g, "<");
text = text.replace(/>/g, ">");
text = text.replace(/ /g, " ");
text = text.replace(/\r/g, "<font color='gray'>→</font>");
text = text.replace(/\n/g, "<br>");
return text;
}
function toggle(id) {
e = document.getElementById(id);
e.style.display = e.style.display == "none" ? "inline" : "none";
}
function processInput(e) {
if (e.keyCode == 38) {
if (entries.length == 0) { return false; }
if (entryIndex > 0) {
entryIndex--;
prompt.value = entries[entryIndex];
}
return false;
}
if (e.keyCode == 40) {
if (entries.length == 0 || entryIndex >= entries.length-1) {
entryIndex = entries.length;
prompt.value = "";
return false;
}
entryIndex++;
prompt.value = entries[entryIndex];
return false;
}
if (e.keyCode != 13) { return; }
printReplBare("<font color='gray'>"+escapeHTML(prompt.value)+"</font>");
if (prompt.value != "") { processLine(); }
if (prompt.value != "") { entries.push(prompt.value); }
entryIndex = entries.length;
prompt.value = "";
printRepl(partial == "" ? "  " : "> ");
return false;
}
function printEnv(filter, label) {
var found = {};
var len = 0;
for(var key in env.d) {
if (!filter(env.d[key])) { continue; }
len = Math.max(key.length, len);
found[key] = env.d[key];
}
for(var name in found) {
var n = name; for(var x=(len+1 - n.length); x>0; x--) { n += " "; }
printRepl(n + ": " + format(found[name]));
}
if (len == 0) {
printRepl("no "+label+" defined.");
}
printRepl("");
}
function processLine() {
if (prompt.value == "\\\\") { partial = ""; return; }
if (prompt.value == "\\e") {
var editor = document.getElementById("editor");
if (editor.style.display == "inline") {
editor.style.display = "none";
repl.style.width = "80%";
saveBuffer();
}
else {
editor.style.display = "inline";
repl.style.width = "40%";
loadBuffer();
document.getElementById("textedit").focus();
}
return;
}
if (prompt.value == "\\r") {
saveBuffer();
prompt.value = document.getElementById("textedit").value;
}
if (prompt.value == "\\c") {
scroll.innerHTML = "";
panel.scrollTop = panel.scrollHeight;
return;
}
if (prompt.value == "\\f") {
printEnv(function(v) { return v.t >= 5 && v.t != 13; }, "functions");
return;
}
if (prompt.value == "\\v") {
printEnv(function(v) { return v.t < 5; }, "variables");
return;
}
if (prompt.value == "kOS") { printRepl("one system/all devices"); return; }
if (prompt.value.lastIndexOf("\\u") == 0) {
var code = prompt.value.slice(2);
var enc = encodeURIComponent(code).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
var url = document.location + "?run=" + enc;
printRepl("oK code url:");
printRepl("<a href=\""+url+"\" target='_blank'>"+url+"</a>");
return;
}
var showtime = false;
if (prompt.value.lastIndexOf("\\t") == 0) {
prompt.value = prompt.value.slice(2);
showtime = true;
}
var parsed = null;
try { parsed = parse(" " + partial + "; " + prompt.value); }
catch(error) {
if (done() && (
error.message == "parse error. '}' expected." ||
error.message == "parse error. ')' expected." ||
error.message == "parse error. ']' expected.")) {
partial += ((partial!="")?";":"") + prompt.value;
return;
}
else {
printRepl("<font color='red'>"+error.message+"</font>");
partial = "";
return;
}
}
partial = "";
try {
var starttime = new Date().getTime();
printRepl(escapeHTML(format(run(parsed, env))));
if (showtime) {
var endtime = new Date().getTime();
printRepl("completed in "+(endtime-starttime)+"ms.");
}
}
catch(error) {
printRepl(
"<span style='color: red; cursor:pointer;' onclick='toggle(\"e"+(eid)+"\")'>"+error.message+
"<div class='err' id='e"+(eid++)+"'><br>"+escapeHTML(error.stack)+"</div></span>"
);
}
}
function printReplBare(x) {
scroll.innerHTML += x;
panel.scrollTop = panel.scrollHeight;
}
function printRepl(x) {
printReplBare("<br>");
printReplBare(x);
}
function print(x, y) {
// todo: use x to select a file descriptor
try {
var t = tojs(y);
if (typeof t == "string") { printRepl(escapeHTML(t)); return y; }
if (Array.isArray(t) && t.every(function(v) { return typeof v == "string"; })) {
t.map(function(v) { printRepl(escapeHTML(v)); }); return y;
}
}
catch(e) {}
throw new Error("0: can only display strings or lists of strings.");
}
function readAjax(x) {
var url = tojs(x);
if (typeof url != 'string') { throw new Error("string expected."); }
var request = new XMLHttpRequest();
request.open('GET', url, false);
request.send(null);
return [request.status, request.responseText];
}
function readText(x) { return tok(readAjax(x)); }
function readJSON(x) {
var t = readAjax(x);
t[1] = JSON.parse(t[1]);
return tok(t);
}
var urlstring = location.search.match(/run=([a-zA-Z0-9-_.!~*'()%]+)/);
if (urlstring) {
var code = decodeURIComponent(urlstring[1]).trim();
printRepl("from url ");
prompt.value = code;
processInput({keyCode:13});
}
</script>
</html>