-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathpith-ception.html
296 lines (266 loc) · 8.1 KB
/
pith-ception.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
<!doctype html>
<html>
<head>
<title>Pith</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="initial-scale=1.0, minimum-scale=0.25">
<meta name="author" content="Gabriel Sroka">
<meta name="copyright" content="Copyright 2007-2022 Gabriel Sroka">
<style>
body {
font-family: sans-serif;
}
a {
color: black;
text-decoration: none;
}
@media (prefers-color-scheme: dark) {
*, a {
background-color: #121212;
color: #ddd;
}
}
@media (orientation: landscape) and (max-width: 865px) {
* {
font-size: 0.8em;
}
}
</style>
</head>
<body>
<a href=https://github.com/gabrielsroka/gabrielsroka.github.io/blob/master/webpages/pith.html target=_blank>Pith</a>
<a href=https://github.com/gabrielsroka/gabrielsroka.github.io/blob/master/pith.js target=_blank>.js</a>
<button id=runProgram accesskey="R" title="Run (Ctrl + Enter)">Run</button>
<button id=newProgram accesskey="N" title="New">New</button>
<button id=saveProgram accesskey="S" title="Save">Save</button>
<select id=prognames></select>
<span id=bar></span>
<br><br>
<textarea id="editor" style="width: 99%;" cols="80" rows="16" spellcheck="false" autocapitalize="none"></textarea>
<textarea id="debug" style="width: 99%;" cols="80" rows="12" spellcheck="false" autocapitalize="none"></textarea>
<div id=results></div>
<script>
var programs;
var progname;
var tab = 2;
if (localStorage.pithPrograms) {
load();
} else {
programs = {};
editor.value = `max = 6
ids = await getApi('/topstories')
ids.slice(0, max).forEach(getItem)
async def getItem(id)
item = await getApi('/item/' + id)
? item.title, '|', item.score, 'pts'
u = '//news.ycombinator.com/item?id='
> item.title.link(u + id) + '<br>'
async def getApi(path)
u = '//hacker-news.firebaseio.com/v0'
return getJson(u + path + '.json')`;
progname = 'Hello HN';
addOption(progname, editor.value);
save();
}
if (!navigator.userAgent.match("Android")) {
runProgram.innerHTML = '<u>R</u>un';
newProgram.innerHTML = '<u>N</u>ew';
saveProgram.innerHTML = '<u>S</u>ave';
}
runProgram.onclick = function () {
save();
debug.clear();
console.clear();
run(editor.value);
};
newProgram.onclick = function () {
const n = prompt("Program name");
if (!n) return;
progname = n;
addOption(progname, '');
prognames.selectedIndex = prognames.options.length - 1;
editor.value = "";
debug.clear();
};
saveProgram.onclick = save;
prognames.onchange = function () {
editor.value = this.value;
progname = this.options[this.selectedIndex].text;
debug.clear();
};
editor.focus();
editor.onkeydown = function (event) {
const ENTER = 13;
if (event.ctrlKey && event.keyCode == ENTER) {
runProgram.onclick();
} else {
saveProgram.innerHTML = 'Save *';
}
};
debug.log = debug.print = function (s) {
debug.value += (s ? s.toString() : s) + '\n';
debug.scrollTo(0, debug.scrollHeight);
};
debug.clear = function () {
debug.value = '';
results.innerHTML = '<table id=table><thead></thead><tbody></tbody></table>';
};
debug.show = function () {
debug.visible = true;
debug.style.display = '';
};
debug.hide = function () {
debug.visible = false;
debug.style.display = 'none';
};
debug.toggle = function () {
debug.visible ? debug.hide() : debug.show();
};
debug.visible = true;
function print(...args) {
debug.print(args.join(' '));
}
function button(html, onclick) {
return `<button onclick=${onclick}>${html}</button>`;
}
results.html = bar.html = function (s) {
if (s == undefined) {
return this.innerHTML;
} else {
this.innerHTML = s;
}
};
function save() {
if (editor.value) {
programs[progname] = editor.value;
prognames.options[prognames.selectedIndex].value = editor.value;
} else {
delete programs[progname];
prognames.options.remove(prognames.selectedIndex);
}
localStorage.pithPrograms = JSON.stringify(programs);
saveProgram.innerHTML = 'Save';
}
function load() {
programs = JSON.parse(localStorage.pithPrograms);
for (const [progname, value] of Object.entries(programs)) {
if (value) addOption(progname, value);
}
editor.value = Object.values(programs)[0];
progname = Object.keys(programs)[0];
}
function addOption(name, value) {
const option = document.createElement('option');
option.text = name;
option.value = value;
prognames.add(option);
}
</script>
<script>
var tab;
// Mini-Pith for bootstrapping.
function boot(lines) {
lines = lines.split('\n');
var ind = 0;
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
const tLine = line.trim();
if (tLine.startsWith('for ')) {
line = line.replace('for', 'for (var').replace(' in ', ' of ') + ')';
} else if (tLine.startsWith('while ')) {
line = line.replace('while ', 'while (') + ')';
} else if (tLine.startsWith('def ') || tLine.startsWith('async def ')) {
line = line.replace('def', 'function');
} else if (tLine.startsWith('if ') || tLine.startsWith('else if ')) {
line = line.replace('if ', 'if (') + ')';
}
var newInd = line.match(/^( *)/)[1].length / tab;
for (var t = newInd; t < ind; t++) {
line = ' '.repeat(tab * t) + '}\n' + line;
}
if (newInd > ind) lines[i - 1] += ' {';
ind = newInd;
lines[i] = line;
}
lines = lines.join('\n');
eval(lines);
}
// Bootstrap the Pith run function and std lib using JS Mini-Pith boot function.
tab = 4;
boot(`
run =
def (lines)
const nl = '\\n'
lines = lines.split(nl)
var ind = 0
for i in range(lines.length)
var line = lines[i]
const tLine = line.trim()
if tLine.startsWith('for ')
line = line.replace('for', 'for (var').replace(' in ', ' of ') + ')'
else if tLine.startsWith('while ')
line = line.replace('while ', 'while (') + ')'
else if tLine.startsWith('def ') || tLine.startsWith('async def ')
if line.endsWith(' main')
line = 'main()' + nl + line
line = line.replace('def', 'function')
if !line.endsWith(')')
line += '()'
else if tLine.startsWith('if ') || tLine.startsWith('else if ') || tLine.startsWith('elif ')
line = line.replace('elif', 'else if').replace('if ', 'if (') + ')'
else if tLine.startsWith('#')
line = line.replace('#', '//')
else if tLine.startsWith('print ') || tLine.startsWith('?')
line = line.replace(/(print|\\?) ?/, 'print(') + ')'
else if tLine.startsWith('>')
line = line.replace('>', 'results.innerHTML +=') + " + '<br>'"
else if tLine.startsWith('|')
line = line.replace('|', 'table.tHead.innerHTML += "<tr><th>" +')
else if tLine.startsWith('+')
line = line.replace('+', 'table.tBodies[0].innerHTML += "<tr><td>" +')
else if tLine.startsWith('tab = ')
eval(line)
line = '// ' + line
var newInd = line.match(/^( *)/)[1].length / tab
for t in range(newInd, ind)
line = ' '.repeat(tab * t) + '}' + nl + line
if newInd > ind
lines[i - 1] += ' {'
ind = newInd
lines[i] = line
lines = '(async function () {' + nl + lines.join(nl) + (nl + '}').repeat(ind) + nl + '})()'
console.log(lines)
eval(lines)
range =
def * (start, stop, step = 1)
if stop == undefined
stop = start
start = 0
while start < stop
yield start
start += step
ac =
def (it, cond, fn)
return [...gen(it, cond, fn)]
gen =
def * (it, cond, fn)
for n in it
if cond == undefined || cond(n)
yield fn ? fn(n) : n
sum =
def (it)
var total = 0
for n in it
total += n
return total
getJson =
async def (url, init)
const r = await fetch(url, init)
return r.json()
`);
tab = 2;
const get = fetch;
document.querySelectorAll('script[type="text/pith"]').forEach(s => run(s.innerText));
</script>
</body>
</html>