-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
executable file
·388 lines (329 loc) · 7.95 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<!doctype html>
<html>
<head>
<style>
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.node {
font-size: 80%;
display: none;
margin-left: 1em;
}
</style>
</head>
<body>
<select id="code_samples"></select>
<button onclick="execute()">Execute</button>
<small id="time"></small>
<div id="input" style="width:100%;height:360px" rows="10">
</div>
<script title="time-travelling search" type="script/x-lua">amb, assert = **require('ambLib')
-- search for Pythagorean triple
a = amb(1.to(10))
b = amb(1.to(10))
c = amb(1.to(10))
assert(a * a == b * b + c * c and c > b)
echo("{{a}}^2 = {{b}}^2 + {{c}}^2")
</script>
<script title="ambiguous calculation" type="script/x-lua">amb, assert = **require('ambLib')
-- solve eight queen puzzle
N = 8
solve = fn(s)
i, a = s.length, amb(0.to(N))
assert(s.every(fn(b, j) a >= 0 and a ~= b and math.abs(a-b) ~= i-j end))
n = s.concat(a)
n.length == N and n or solve(n)
end
sols = [ ]
s = solve([])
if s[0] >= 0 then
sols.push(s)
assert()
end
echo("{{sols.length}} solutions found")
sols.forEach(s => echo(s))
</script>
<script title="yield and generator" type="script/x-lua">
mkGenerator = **require('genLib')
iter = mkGenerator(fn(yield)
yield(1)
console.log('[i] yielding in if')
if yield('if cond') then
yield('if then')
else
yield('if else')
end
console.log('[i] yielding in function')
i = 0
f = fn()
i += 1
yield('func x' + i)
end
f()
f()
f()
console.log('[i] yielding in while-loop')
i = 0
while i < 3 do
i += 1
yield('while x' + i)
end
console.log('[i] yielding in for-loop')
for i = 1, 4 do
yield('for x' + i)
end
end)
$('<button>Click Me</button>').click(fn(e)
echo(iter())
end).appendTo('#result')
</script>
<script title="async control flow" type="script/x-lua">
doAsync = **require('genLib')
doAsync(fn(yield, next)
echo('starting...')
start = now()
yield(timeout(next, 1000))
echo(now() - start)
for t = [
timeout(next, 300)
timeout(next, 500)
timeout(next, 1000)
] do
yield()
end
echo(now() - start)
json = yield($.get('package.json').done(next).fail(next))
echo(json.description)
xhr = yield($.get('/404').done(next).fail(next))
echo(xhr.statusText)
end)
</script>
<script title="enhanced array/dict" type="script/x-lua">
echo('-- number methods')
2.times(echo)
2.to(6).each(echo)
echo(3.clamp(1, 2))
echo('-- add number method')
0['@proto'].plus = x => self + x
0['@proto'].minus = x => self - x
echo('3 + 2 - 1 =', 3.plus(2).minus(1))
echo('-- array methods')
a = [1, 2, 3]
echo('a.first() =', a.first())
echo('a.last() =', a.last())
echo('-- array iterator')
for v, i = a do
echo(i, '=>', v)
end
echo('-- object methods')
b = { name = 'b', size = 1 }
echo('b.keys() =', b.keys())
echo('b.values() =', b.values())
echo('-- object iterator')
for v, k = b do
echo(k, '=>', v)
end
</script>
<script title="object meta method" type="script/x-lua">
a = self.extend(
name = 'a'
say = x => echo('my name is ' + self.name))
a.say()
lookup = a['@']
b = a.extend(
name = 'b'
'@get' = fn(name)
-- do not use self[name] because it will cause a stackoverflow!
lookup.call(self, name) or fn()
echo("[!] this guy ({{ self.name }}) " +
"cannot {{ name }} ")
end
end)
b.say()
b.cry()
c = self.extend(
'@get@name' = x => 'c'
'@set@name' = x => echo('[!] name is readonly'))
echo('name of c is ', c.name)
c.name = 'd'
echo('name of c is still ', c.name)
</script>
<script title="dsl (html)" type="script/x-lua">
htmlBuilder = fn(env)
css = fn()
loop(arga, (v, k) => "{{ k }}:{{ v }}").join(';')
end
env['@'] = hook(env['@'], '@get' = fn(name)
local[name] or fn()
attrs = loop(arga, (v, k) => "{{ k }}=\"{{ v }}\"").join(' ')
tag = attrs and name + ' ' + attrs or name
if args.length then
"<{{ tag }}>{{ args.join('') }}</{{ name }}>"
else
"<{{ tag }} />"
end
end
end)
end
text = do htmlBuilder(local)
div(class="main"
h1(id="title"
'Hello World!')
p(style=css(color="red", 'font-size'="200%")
'from ', big('#3512'))
input(onclick="alert('ya')", type="submit"))
end
console.log(text)
$('#result').append(text)
</script>
<script title="lib - ambiguous calculation" id="ambLib" type="script/x-lua">
failStack = [ ]
amb = fn(vals)
callcc(cc => failStack.push(cc))
if vals.length then vals.pop() else failStack.pop() and nil end
end
assert = fn(condition)
condition or failStack[failStack.length - 1]()
end
exports = { amb, assert }
</script>
<script title="lib - generator with call/cc" id="genLib" type="script/x-lua">
mkYield = fn(ret)
next = fn(cc)
fn(value)
callcc(fn(rcc)
ret := rcc
cc(value)
end)
end
end
fn(value)
callcc(fn(cc)
ret({ next = next(cc), value })
end)
end
end
mkGenerator = fn(iter)
next = fn()
callcc(fn(cc)
yield = mkYield(cc)
iter(yield)
end)
end
fn(value)
ret = next and next(value)
next := ret and ret.next
ret and ret.value
end
end
doAsync = fn(exec, next)
next = mkGenerator(fn(yield)
exec(yield, next)
-- stop it or the code after doAsync will be executed again
yield()
end)
next()
end
-- used for other scripts
exports = { mkGenerator, doAsync }
</script>
<div id="tree"></div>
<pre id="console"></pre>
<pre id="result"> loading... </pre>
<script src="//cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
<script src="//cdn.bootcss.com/fetch/0.11.0/fetch.min.js"></script>
<script src="//cdn.bootcss.com/babel-polyfill/6.6.1/polyfill.min.js"></script>
<script src="//cdn.bootcss.com/ace/1.2.3/ace.js"></script>
<script src="//cdn.bootcss.com/ace/1.2.3/mode-lua.js"></script>
<script src="build/index.js"></script>
<script>
function format(tree, depth) {
depth = depth || 0
if (typeof(tree) !== typeof({ })) {
return tree
}
else if (depth < 50) {
var children = [ ]
for (var key in tree)
if (tree.hasOwnProperty(key))
children.push('<div class="item">' +
key + ': ' + format(tree[key], depth + 1) + '</div>')
return '<a href onclick="$(this).next().toggle();return false">></a>' +
'<div class="node">' +
children.join('') + '</div>'
}
}
function execute() {
$('#result').empty()
$('#console').empty()
$('#tree').empty()
try {
var input = editor.getValue(),
tree = yalls.parse(input),
code = yalls.compile(tree),
sandbox = yalls.environment(env)
$('#tree').html(format(tree))
var tickStart = Date.now()
yalls.evaluate(code, sandbox)
$('#time').text((Date.now() - tickStart) + 'ms')
}
catch (e) {
console.log(e)
$('#result').text('EvaluateError: ' + e)
}
}
var env = yalls.environment(null, yalls.buildins)
env('echo', function() {
var content = Array.prototype.slice.call(arguments)
.map(function(a) { return '' + a }).join(' ')
$('<pre></pre>').text(content).appendTo('#console')
})
env('require', function(file) {
var input = $('#' + file).text(),
tree = yalls.parse(input),
code = yalls.compile(tree),
sandbox = yalls.environment(null, yalls.buildins)
sandbox('exports', { })
yalls.evaluate(code, sandbox)
return sandbox('exports')
})
env('console', console)
env('math', Math)
env('now', Date.now.bind(Date))
env('timeout', setTimeout.bind(window))
env('$', $)
setTimeout(execute, 0)
</script>
<script>
var editor = ace.edit('input'),
session = editor.getSession()
session.setMode('ace/mode/lua')
session.setUseWorker(false)
editor.$blockScrolling = Infinity
</script>
<script>
var select = $('#code_samples')
$('script[type="script/x-lua"]').each(function(i, e) {
if (!e.title) return
if (!e.id) e.id = 'script' + i
select.append('<option value="' + e.id + '"">' + e.title + '</option>')
})
select.bind('change', function(e) {
location.hash = $(this).val()
})
$(window).on('hashchange', function(e) {
select.val(location.hash.substr(1))
editor.setValue($(location.hash).text(), -1)
execute()
})
if (!location.hash.substr(1))
location.hash = select.val()
else
$(window).trigger('hashchange')
</script>
</body>
</html>