-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
240 lines (211 loc) · 5.13 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
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<title>whitespace</title>
<style>
:root {
--text-primary: #000;
--background-primary: #fff;
}
@media (prefers-color-scheme: dark) {
:root {
--text-primary: #fff;
--background-primary: #1b1b1b;
}
}
body, textarea {
background-color: var(--background-primary);
color: var(--text-primary);
}
</style>
<script type="module">
import init, { parseString } from "./pkg/whitespace.js";
let isLoaded = false;
let isReady = false;
init().then(() => {
isLoaded = true;
// Run on load.
if (isReady) {
const sourceEl = document.getElementById('source');
onChange(sourceEl);
}
});
function escapeString(str) {
return JSON.stringify(str);
}
const IDENTIFIER_REGEXP = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
const JS_RESERVED = new Set([
'break',
'case',
'catch',
'class',
'const',
'continue',
'debugger',
'default',
'delete',
'do',
'else',
'export',
'extends',
'false',
'finally',
'for',
'function',
'if',
'import',
'in',
'instanceof',
'new',
'null',
'return',
'super',
'switch',
'this',
'throw',
'true',
'try',
'typeof',
'var',
'void',
'while',
'with',
]);
function escapeMapKey(str) {
if (IDENTIFIER_REGEXP.test(str) && !JS_RESERVED.has(str)) {
// It's a simple string; just use a bare identifier.
return str;
} else {
return escapeString(str);
}
}
// Pretty-print an arbitrary JS value to a string.
function pretty(obj, prefix = '') {
const indent = ' ';
let parts = [];
const typ = typeof obj;
if (obj === null) {
parts.push('null');
} else if (obj === undefined) {
parts.push('undefined');
} else if (typ === 'number') {
parts.push(obj.toString());
} else if (typ === 'string') {
parts.push(escapeString(obj));
} else if (Array.isArray(obj)) {
parts.push('\n' + prefix + '[');
for (const item of obj) {
parts.push(indent + pretty(item, prefix + indent) + ',');
}
parts.push(']');
} else if (obj instanceof Map) {
parts.push('\n' + prefix + '{');
for (const [key, item] of obj) {
const value = pretty(item, prefix + indent);
parts.push(`${indent}${escapeMapKey(key)}: ${value},`);
}
parts.push('}');
} else {
// Arbitrary object.
parts.push('\n' + prefix + '{');
const keys = Object.keys(obj);
for (const key of keys) {
const item = obj[key];
const value = pretty(item, prefix + indent);
parts.push(`${indent}${escapeMapKey(key)}: ${value},`);
}
parts.push('}');
}
return parts.join('\n' + prefix);
}
// Display text in the DOM.
function displayText(text) {
const resultEl = document.getElementById('result');
resultEl.innerText = text;
}
// Display a JS value in the DOM.
function display(obj) {
displayText(pretty(obj));
}
// Source changed.
function onChange(el) {
let result;
try {
result = parseString(el.value);
} catch (e) {
// Display parse errors.
console.log(e);
displayText('' + e.message);
return;
}
console.log(result);
display(result);
}
document.addEventListener('DOMContentLoaded', function (event) {
// Document ready.
isReady = true;
const sourceEl = document.getElementById('source');
sourceEl.addEventListener('input', function (event) {
// Wait for async load.
if (!isLoaded) return;
event.preventDefault();
onChange(this);
})
});
</script>
</head>
<body>
<textarea id="source" rows="20" cols="70">// Add numbers.
fun add(x, y)
var sum = x + y
return sum
// Parentheses to call a function are optional when there's at least
// one argument.
add 1, 2
add(2, 3)
// For arrays, a comma at the end of each line is required.
on_off_list = [
1,
0,
1,
]
// Literal maps. Trailing commas are optional.
var params =
color: "#ff00ff"
opacity: 0.8
"key with special characters": 25
// Single line map.
var m = { fruit: "apple", bread: "rye" }
// "print" is super special, for now. Parens not allowed.
print on_off_list[1]
var n = 9
while (true)
if (n / 4 == 2)
break
n = n - 1
for (var i = 0; i < 10; i = i + 1)
// No remainder or modulus operator.
if (i == 2 or i == 4 or i == 6 or i == 8)
continue
print i
// Classes are a thing.
class Counter
// This is the constructor.
init(start)
this.n = start
increment()
this.n = this.n + 1
// Instantiate it.
var c = Counter(0)
c.increment()
print c.n
// Subclass and use the superclass.
class DoubleCounter < Counter
increment()
super.increment()
super.increment()
</textarea>
<pre><code id="result"></code></pre>
</body>
</html>