-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
329 lines (286 loc) · 8.79 KB
/
main.js
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
// NOTE: This seems to produce wasm related errors:
// import TreeSitter from 'web-tree-sitter';
// import TSCognateURL from './public/tree-sitter-cognate.wasm?url';
// import PreludeURL from './prelude.cog?url'
import './base.css';
import { CM, Linter } from './editor/editor.js';
import { Runner, setPreludeReady, initPrelude, initTS, escape } from './cognate.js';
const $selectExample = document.getElementById("select-example");
const $output = document.getElementById("output");
const $externalError = document.getElementById("external-error");
const $externalErrorBox = document.getElementById("external-error-box");
const $noticeDismiss = document.getElementById("notice-dismiss");
const $outputError = document.getElementById("output-error");
const $errorsContainer = document.getElementById("errors-container");
const $debugContainer = document.getElementById("debug-container");
const $outputContainer = document.getElementById("output-container");
const $outputDebug = document.getElementById("output-debug");
const STORAGE_KEY = "cognate-playground";
const Store = {
getInput: () => JSON.parse(localStorage.getItem(STORAGE_KEY) || '{}').custom || 'Print "Hello, world!";',
saveInput: (newInput) => {
localStorage.setItem(STORAGE_KEY, JSON.stringify({ custom: newInput }));
},
};
const App = {
// The select preset
selectionChange: false,
preludeLines: [],
runner: null,
fetchPrelude: () => {
fetch("prelude.cog")
.then((res) => {
if (!res.ok) {
throw new Error(`The server returned status ${res.status}${res.statusText ? ": " + res.statusText : ''}.`);
}
return res.text();
})
.then((text) => {
App.preludeLines = text.split('\n');
initPrelude(text);
})
.catch((e) => {
$externalErrorBox.classList.remove("hidden");
$externalError.innerHTML = `<p>Unable to fetch prelude file! ${e.message}</p><p>You can continue to use the playground normally, but built-in functions defined in the prelude will not be available.</p>`;
}).finally(() => {
setPreludeReady();
App.runner.run(Store.getInput());
App.postRun();
});
},
postRun: () => {
if ($output.innerHTML == '') {
$outputContainer.classList.add("hidden");
} else {
$outputContainer.classList.remove("hidden");
}
if ($outputDebug.innerHTML == '') {
$debugContainer.classList.add("hidden");
} else {
$debugContainer.classList.remove("hidden");
}
},
init: async () => {
await initTS();
App.runner = new Runner({
output: Output,
errors: {
add(e) {
Errors.push(e);
},
clear() {
Errors = [];
},
redraw(heading) {
redrawErrors(heading);
},
hasAny: () => Errors.length > 0,
},
diagnostics: {
add(...args) {
Linter.addDiagnostic(...args);
},
clear() {
Linter.diagnostics = [];
},
},
style: {
marked: (s) => textMarked(s),
light: (s) => textLight(s),
},
editor: CM,
store: Store,
$stack: $outputDebug,
});
CM.setup(
Store.getInput(),
document.getElementById("input"),
(update) => {
if (update.docChanged) {
// Don't update Store if the code input changed due to a <select> change.
let shouldRedraw = false;
let changes = [];
update.changes.iterChanges((fromA, toA, fromB, toB, inserted) => {
if (fromA != fromB || toA != toB || inserted.length != 0) {
shouldRedraw = true;
}
changes.push([fromA, toA, fromB, toB, inserted])
})
if (shouldRedraw) {
changes.forEach((info) => {
App.runner.tree.edit(CM.change2tsEdit(update.startState, update.state, ...info));
});
// TODO: Do it in a worker thread or some other asynchronous way to
// prevent blocking view updates.
App.runner.run(update.state.doc.toString(), !App.selectionChange);
App.postRun();
App.selectionChange = false;
}
}
}
);
$selectExample.value = "custom";
App.fetchPrelude();
},
};
const Output = {
clear: () => { $output.innerHTML = "" },
add: (str) => {
$output.innerHTML += str.style ? `<span style='${str.style}'>${escape(str.value)}</span>` : escape(str.value);
},
newline: () => {
$output.innerHTML += "\n";
}
};
let Errors = [];
function redrawErrors(heading) {
if (Errors.length != 0) {
let html = '';
if (heading) {
html += `<p>${heading}</p>`;
}
html += "<ol>";
html += Errors.map((item) => `<li>${item}</li>`).join("");
html += "</ol>";
$outputError.innerHTML = html;
$errorsContainer.classList.remove("hidden");
} else {
$outputError.innerHTML = '';
$errorsContainer.classList.add("hidden");
}
}
const ExamplePresets = {
custom: ``,
hanoi: `Def Move discs as (
Let N be the Number! of discs;
Let A be the first rod;
Let B be the second rod;
Let C be the third rod;
Unless Zero? N (
Move - 1 N discs from A via C to B;
Prints ("disc " N " : " A " --> " C);
Move - 1 N discs from B via A to C;
)
);
Move 3 discs from \\X via \\Y to \\Z;`,
fizzbuzz: `Def Fizzbuzz (
Let N be the current Number!;
Def Multiple as ( Zero? Modulo Swap N );
Print
If Multiple of 15 then "fizzbuzz"
If Multiple of 3 then "fizz"
If Multiple of 5 then "buzz"
else N
);
For each in Range 1 to 100 ( Fizzbuzz );`,
primes: `Def Factor (Zero? Modulo Swap);
Def Primes (
Let U be the upper bound Number!;
initialize an Empty list;
For Range 2 to U (
Let I be our potential prime;
Let Primes are the found primes;
Let To-check be Take-while (<= Sqrt I) Primes;
When None (Factor of I) To-check
(Append List (I)) to Primes;
)
);
Print Primes up to 100;`,
fib: `Def Fib
Case (< 3) then (1 Drop)
else (Let N ; Fib of - 1 N ; + Fib of - 2 N);
Put "The 20th fibonacci number is: ";
Print Fib 20;`,
numbers: `~~ Numbers are what you expect. Both integers
~~ and floats are supported.
Print 0;
Print 0.2;
Put "1 + 2 = ";
Print + 1 2;
Put "(1 + 2) * 3 = ";
Print * 3 + 1 2;
Put "10 Modulo 3 = ";
Print Modulo 3 10;
Put "Sqrt 4 = ";
Print Sqrt 4;`,
variables: `~~ Variables in cognate are bound using Let.
Let N 0;
Print N;
~~ Variables are immutable.
Let N 1;`,
strings: `~~ String literals are denoted by ""
Let A "apples + banana + peanuts";
Put "First: \\t";
Print First A;
Put "Rest: \\t";
Print Rest A;
Put "Substring: \\t";
~~ Ranges are inclusive on both ends.
Print Substring 0 4 A;
Print "\\nSplitting by the plus sign:";
Put "we get a list: \\t";
Print Split " + " A;
Print "\\nConverting a character to and from its UTF16 code:";
Print Character Ordinal "A";
~~ String escape sequences:
~~ \\b, \\t, \\n, \\r, \\", \\v, \\\\
~~ ...are supported. A backslash followed by a
~~ character other than any of these will be
~~ treated literally.
~~
~~ You can see string escape sequences in action
~~ above.
~~
~~ Note that '\\a' that is supported by CognaC,
~~ is not supported here in the playground.
Print "\\nConverting between cases:";
Print Uppercase "ÀḅḈɗe";
Print Lowercase "ÀḅḈƊE";
`,
blocks: `~~ Blocks are denoted with (). They will not
~~ be evaluated until you call Do.
(Print "hi");
Drop;
Do (Print "hi 2");
They can also be bound to functions, calling them
won't require 'Do'.`,
functions: `~~ Define functions using Def.
~~ Def works similarly to Let, except they must be
~~ bound to a block.
Def Hello ( Print "hello!" );
~~ Call a function by writing its name directly.
~~ There's no need to call Do.
Hello;`,
lists: `~~ Lists in Cognate are constructed using
~~ the List function, which takes a block, evaluates
~~ it, then collects the remaining items in the stack
~~ into a list.
Print List (1; 2; 3; 4; 5);
Print List (1 2 3 4 5);
Print "\\nShow strings literally when in a list:";
Print List ("1 2 \\t 3 \\n 4");
~~ Some useful list functions;
Print Index 4 List (0 1 2 3 4);
Print First of List (0 1 2);
Print Rest of List (0 1 2);
Print Length of List (0 1 2);
~~ Notice how Length is used for both strings and lists.
`,
};
function textLight(text) {
return `<span style='color: var(--text-light)'>${text}</span>`;
}
function textMarked(text) {
return `<span style='color: var(--marked)'>${text}</span>`;
}
//////////////////////////////////////////////////////////////////////
$selectExample.addEventListener("change", function () {
let key = $selectExample.value;
let newContent = key == "custom" ? Store.getInput() : ExamplePresets[key];
App.selectionChange = true;
CM.setText(newContent);
});
$noticeDismiss.addEventListener("click", function () {
$externalErrorBox.classList.add("hidden");
});
App.init().then(() => {}).catch(err => { console.error(err) });