|
@@ -1,411 +0,0 @@
|
1
|
|
-print('--- Empty functions')
|
2
|
|
-
|
3
|
|
-func f() end
|
4
|
|
-
|
5
|
|
-print(f())
|
6
|
|
-
|
7
|
|
-print('--- While loop')
|
8
|
|
-a = 1
|
9
|
|
-while a < 10 do
|
10
|
|
- print(a)
|
11
|
|
- a += 1
|
12
|
|
-end
|
13
|
|
-
|
14
|
|
-print("--- Range")
|
15
|
|
-
|
16
|
|
-func mul9(b)
|
17
|
|
- for i in range(#b) do
|
18
|
|
- b[i] *= 9
|
19
|
|
- end
|
20
|
|
-end
|
21
|
|
-
|
22
|
|
-l = [1 2 3 4 5]
|
23
|
|
-print(range(#l))
|
24
|
|
-print("--- Iter list")
|
25
|
|
-for i in l do print(i) end
|
26
|
|
-print("--- Index list")
|
27
|
|
-for i in range(#l) do print(i, l[i]) end
|
28
|
|
-print('--- mul9')
|
29
|
|
-mul9(l)
|
30
|
|
-for i in l do print(i) end
|
31
|
|
-print('--- Iter mul9')
|
32
|
|
-for i in range(#l) do print(i, l[i]) end
|
33
|
|
-
|
34
|
|
-print("--- Mapgen")
|
35
|
|
-
|
36
|
|
-PI = 3.14159265358979
|
37
|
|
-
|
38
|
|
-d = {
|
39
|
|
- integer = 1
|
40
|
|
- string = "hello"
|
41
|
|
- submap = {
|
42
|
|
- stamina = 100
|
43
|
|
- health = 42.0
|
44
|
|
- }
|
45
|
|
- sublist = [1 1 2 3 5 8],
|
46
|
|
- ["this time with spaces"] = PI*2,
|
47
|
|
- [1 + 5] = 1 + 5,
|
48
|
|
- [1 + 9] = 1 + 9
|
49
|
|
-}
|
50
|
|
-
|
51
|
|
-print(d)
|
52
|
|
-print('--- Map iter')
|
53
|
|
-for i in d do print(i, d[i]) end
|
54
|
|
-
|
55
|
|
-print('--- try')
|
56
|
|
-
|
57
|
|
-func bad(x)
|
58
|
|
- print(x)
|
59
|
|
- return x.c()
|
60
|
|
-end
|
61
|
|
-
|
62
|
|
-test1 = {c = func() return 15 end}
|
63
|
|
-test2 = {}
|
64
|
|
-
|
65
|
|
-print(try(bad, test1))
|
66
|
|
-print(try(bad, test2))
|
67
|
|
-
|
68
|
|
-print(bad(test1))
|
69
|
|
---print(bad(test2))
|
70
|
|
-
|
71
|
|
-print('--- Induced errors')
|
72
|
|
-
|
73
|
|
-func raise(x)
|
74
|
|
- error(x)
|
75
|
|
-end
|
76
|
|
-
|
77
|
|
-print(try(raise, "lp0 on fire"))
|
78
|
|
-
|
79
|
|
-print('--- Indexing')
|
80
|
|
-
|
81
|
|
-print(d["integer"])
|
82
|
|
-print(d.integer)
|
83
|
|
-d.integer += 5
|
84
|
|
-print(d.integer)
|
85
|
|
-
|
86
|
|
-print('--- Function binding')
|
87
|
|
-
|
88
|
|
-func outer(a)
|
89
|
|
- func inner(b)
|
90
|
|
- return a+b
|
91
|
|
- end
|
92
|
|
- inner.closure.a = a
|
93
|
|
- return inner
|
94
|
|
-end
|
95
|
|
-
|
96
|
|
-i = outer(5)
|
97
|
|
-print(i(3), i(4), i(5))
|
98
|
|
-j = outer(8)
|
99
|
|
-print(j(3), j(4), j(5))
|
100
|
|
-
|
101
|
|
-print('--- Iterators')
|
102
|
|
-
|
103
|
|
-func myiter()
|
104
|
|
- if i>10 then return StopIteration end
|
105
|
|
- i+=1
|
106
|
|
- return i-1
|
107
|
|
-end
|
108
|
|
-myiter.closure.i = 1
|
109
|
|
-
|
110
|
|
-for i in myiter do print(i) end
|
111
|
|
-
|
112
|
|
-print('--- Method calls')
|
113
|
|
-
|
114
|
|
-d = {a = func(a, b) print(a, b) end}
|
115
|
|
-
|
116
|
|
-d.a(1, 2)
|
117
|
|
-d:a(3)
|
118
|
|
-
|
119
|
|
-print('--- Special methods')
|
120
|
|
-
|
121
|
|
-d = {__index = func(obj, key) print('Index', obj, key) return key end,
|
122
|
|
- __setindex = func(obj, key, val) print('SetIndex', obj, key, val) end,
|
123
|
|
- __call = func(obj, arg1, arg2) print('Call', obj, arg1, arg2) return arg1 end}
|
124
|
|
-
|
125
|
|
-print(d[3], d[5])
|
126
|
|
-
|
127
|
|
-d.a = 7
|
128
|
|
-
|
129
|
|
-print(d("q", "r"))
|
130
|
|
-
|
131
|
|
-e = {a=1, b=2}
|
132
|
|
-d = {__index = e, __setindex = e}
|
133
|
|
-
|
134
|
|
-print(d, d.a, d.b)
|
135
|
|
-
|
136
|
|
-d.c = 5
|
137
|
|
-d.b = 7
|
138
|
|
-
|
139
|
|
-print(d, e)
|
140
|
|
-
|
141
|
|
-print('--- Data sharing')
|
142
|
|
-
|
143
|
|
-d = {}
|
144
|
|
-e = [1 2 3 4 5]
|
145
|
|
-d.a = e
|
146
|
|
-d.b = e
|
147
|
|
-
|
148
|
|
-print(d)
|
149
|
|
-
|
150
|
|
-e[2]=7
|
151
|
|
-e[3]="c"
|
152
|
|
-
|
153
|
|
-print(d)
|
154
|
|
-
|
155
|
|
-d.a:insert(#(d.a), "q")
|
156
|
|
-d.b:remove(1)
|
157
|
|
-d.a[3]="f"
|
158
|
|
-
|
159
|
|
-print(d)
|
160
|
|
-
|
161
|
|
-print('--- Arithmetic structure operations')
|
162
|
|
-
|
163
|
|
-print('ab'+'cd')
|
164
|
|
-print('l'+'ol'*32)
|
165
|
|
-print([1 2 3]+[4 5])
|
166
|
|
-print([1 2 3]*5)
|
167
|
|
-print({a=1 b=2}+{c=3})
|
168
|
|
-
|
169
|
|
-print('--- Map/filter')
|
170
|
|
-
|
171
|
|
-l = [1 2 3 4 5]
|
172
|
|
-print(l)
|
173
|
|
-
|
174
|
|
-l:map(func (i) return i*3 end)
|
175
|
|
-print(l)
|
176
|
|
-
|
177
|
|
-l:filter(func (i) return i & 1 end)
|
178
|
|
-print(l)
|
179
|
|
-
|
180
|
|
-print('--- Map/filter chain')
|
181
|
|
-
|
182
|
|
-print([1 2 3 4 5]:map(func (i) return i * 3 end):filter(func (i) return i & 1 end))
|
183
|
|
-
|
184
|
|
-print('--- Exec/eval')
|
185
|
|
-
|
186
|
|
-exec('print("Hello from exec!")')
|
187
|
|
-print(eval('5 + 3'))
|
188
|
|
-execfile('subtest.sol')
|
189
|
|
-
|
190
|
|
-print('--- Modulus')
|
191
|
|
-
|
192
|
|
-print(5%3)
|
193
|
|
-print(13%5)
|
194
|
|
-print(15%15)
|
195
|
|
-
|
196
|
|
-print('--- Special function manipulation')
|
197
|
|
-
|
198
|
|
-func foo(x)
|
199
|
|
- return x
|
200
|
|
-end
|
201
|
|
-
|
202
|
|
-print(foo)
|
203
|
|
-foo.name = "bar"
|
204
|
|
-print(foo)
|
205
|
|
-
|
206
|
|
-func something()
|
207
|
|
- return i
|
208
|
|
-end
|
209
|
|
-
|
210
|
|
-something.closure = {i=[1, 2, 3]}
|
211
|
|
-print(something())
|
212
|
|
-
|
213
|
|
-cl = something.closure
|
214
|
|
-cl.i:insert(0, "b")
|
215
|
|
-print(something())
|
216
|
|
-
|
217
|
|
-print('--- Function body swapping')
|
218
|
|
-
|
219
|
|
-func a()
|
220
|
|
- return 0
|
221
|
|
-end
|
222
|
|
-
|
223
|
|
-func b()
|
224
|
|
- return 2
|
225
|
|
-end
|
226
|
|
-
|
227
|
|
-print(a, a())
|
228
|
|
-print(b, b())
|
229
|
|
-
|
230
|
|
-print(a.stmt)
|
231
|
|
-print(b.stmt)
|
232
|
|
-
|
233
|
|
-temp = a.stmt
|
234
|
|
-a.stmt = b.stmt
|
235
|
|
-b.stmt = temp
|
236
|
|
-
|
237
|
|
-print(a, a())
|
238
|
|
-print(b, b())
|
239
|
|
-
|
240
|
|
-newbody = parse('return 4')
|
241
|
|
-print(newbody)
|
242
|
|
-
|
243
|
|
-a.stmt = newbody
|
244
|
|
-b.stmt = newbody
|
245
|
|
-
|
246
|
|
-print(a, a())
|
247
|
|
-print(b, b())
|
248
|
|
-
|
249
|
|
-print('--- More complicated ASTs')
|
250
|
|
-
|
251
|
|
-print(outer, outer.stmt, outer.stmt.stmtlist)
|
252
|
|
-
|
253
|
|
-print('--- Exec- and eval-by-parse')
|
254
|
|
-
|
255
|
|
-parse('print("Hello from parse()!")')()
|
256
|
|
-print(parse('5 + 3').stmtlist[0].expr())
|
257
|
|
-
|
258
|
|
-print('--- Mutating ASTs')
|
259
|
|
-
|
260
|
|
-func f()
|
261
|
|
- return 5 + 7
|
262
|
|
-end
|
263
|
|
-
|
264
|
|
-print(f, f.stmt, f())
|
265
|
|
-
|
266
|
|
-f.stmt.stmtlist[0].ret.right.ival = 11
|
267
|
|
-
|
268
|
|
-print(f, f.stmt, f())
|
269
|
|
-
|
270
|
|
-func g()
|
271
|
|
- a=1
|
272
|
|
- b=2
|
273
|
|
- print("a=", a, ", b=", b)
|
274
|
|
-end
|
275
|
|
-
|
276
|
|
-print(g, g.stmt, g())
|
277
|
|
-
|
278
|
|
-g.stmt.stmtlist[1].expr.value = parse('a').stmtlist[0].expr
|
279
|
|
-
|
280
|
|
-print(g, g.stmt, g())
|
281
|
|
-
|
282
|
|
-print('--- AST Environments')
|
283
|
|
-
|
284
|
|
-code = parse('print("a is", a, "and b is", b"); a = 4; b = 5')
|
285
|
|
-print(code)
|
286
|
|
-code()
|
287
|
|
-
|
288
|
|
-d = {a=1, b=2}
|
289
|
|
-print(d)
|
290
|
|
-code(d)
|
291
|
|
-print(d)
|
292
|
|
-e = {a="hello", b=["world"]}
|
293
|
|
-print(e)
|
294
|
|
-code(e)
|
295
|
|
-print(e)
|
296
|
|
-
|
297
|
|
-e = {a=1, b=2}
|
298
|
|
-d = {__index = e}
|
299
|
|
-print(d)
|
300
|
|
-print(e)
|
301
|
|
-code(d)
|
302
|
|
-print(d)
|
303
|
|
-print(e)
|
304
|
|
-
|
305
|
|
-print('--- Basic buffers')
|
306
|
|
-
|
307
|
|
-print('(buffer.fromstring = ', buffer.fromstring, ')')
|
308
|
|
-b = buffer.fromstring("Hello, world!")
|
309
|
|
-print(b)
|
310
|
|
-print('(b.get = ', b.get, ')')
|
311
|
|
-print(b:get(buffer.type.cstr))
|
312
|
|
-b:set(buffer.type.char, "Q")
|
313
|
|
-b:set(buffer.type.char, "L", 2)
|
314
|
|
-print(b:get(buffer.type.cstr))
|
315
|
|
-print(b:get(buffer.type.cstr, 5))
|
316
|
|
-print(b:get(buffer.type.uint32))
|
317
|
|
---b:set(buffer.type.double, 1.243416560929)
|
318
|
|
-b:set(buffer.type.uint32, 1886545252)
|
319
|
|
-print(b:get(buffer.type.uint32))
|
320
|
|
-print(b:get(buffer.type.cstr))
|
321
|
|
-q = buffer.fromaddress(b:address(), b:size())
|
322
|
|
-print(q:get(buffer.type.cstr))
|
323
|
|
-q:set(buffer.type.cstr, "Goodbye!")
|
324
|
|
-print(q:get(buffer.type.cstr), b:get(buffer.type.cstr))
|
325
|
|
-
|
326
|
|
-s = "A string!"
|
327
|
|
-b = buffer.fromobject(s)
|
328
|
|
-prepr(s)
|
329
|
|
-print('...is a', buffer.objtype[b:get(buffer.type.int, 0)])
|
330
|
|
-print('(buffer.sizeof.ptr = ', buffer.sizeof.ptr, ')')
|
331
|
|
-print('(buffer.sizeof.int = ', buffer.sizeof.int, ')')
|
332
|
|
-print('(buffer.sizeof.int*2 = ', buffer.sizeof.int*2, ')')
|
333
|
|
-print('(buffer.sizeof.int*2 + buffer.sizeof.ptr = ', buffer.sizeof.int*2 + (buffer.sizeof.ptr), ')')
|
334
|
|
-bs = b:get(buffer.type.ptr, buffer.sizeof.int*2 + (buffer.sizeof.ptr))
|
335
|
|
-print('...string buffer:', bs)
|
336
|
|
-print('...with value:', bs:get(buffer.type.cstr))
|
337
|
|
-
|
338
|
|
-print('--- IO redirection')
|
339
|
|
-
|
340
|
|
-oldstdout = io.stdout
|
341
|
|
-io.stdout = io.open('stdout', io.MODE_WRITE|io.MODE_TRUNCATE)
|
342
|
|
-
|
343
|
|
-print('A line!')
|
344
|
|
-print('An object:', {a=1, b=2, c="turkey"})
|
345
|
|
-print('Something mysterious :o')
|
346
|
|
-io.stdout:write('Writing directly to a file :D')
|
347
|
|
-io.stdout:flush()
|
348
|
|
-
|
349
|
|
-io.stdout = oldstdout
|
350
|
|
-
|
351
|
|
-print('...restored stdout.')
|
352
|
|
-
|
353
|
|
-f = io.open('stdout', io.MODE_READ)
|
354
|
|
-s = f:read(io.ALL)
|
355
|
|
-print('Buffered output was:')
|
356
|
|
-prepr(s)
|
357
|
|
-f = None
|
358
|
|
-
|
359
|
|
-print('...second time.')
|
360
|
|
-
|
361
|
|
-io.stdout = io.open('stdout', io.MODE_WRITE|io.MODE_TRUNCATE)
|
362
|
|
-
|
363
|
|
-print('Hey there!')
|
364
|
|
-print('l'+'ol'*32)
|
365
|
|
-io.stdout:flush()
|
366
|
|
-
|
367
|
|
-io.stdout = oldstdout
|
368
|
|
-
|
369
|
|
-print('...restored.')
|
370
|
|
-print('Output was:')
|
371
|
|
-prepr(io.open('stdout', io.MODE_READ):read(io.ALL))
|
372
|
|
-
|
373
|
|
-print('--- Substrings')
|
374
|
|
-
|
375
|
|
-s = 'This is a test!'
|
376
|
|
-prepr(s)
|
377
|
|
-prepr(s:sub(1, -1))
|
378
|
|
-prepr(s:sub(3, -3))
|
379
|
|
-prepr(s:sub(3, 5))
|
380
|
|
-prepr(s:sub(3, 11))
|
381
|
|
-prepr(s:sub(-1000, -1000))
|
382
|
|
-
|
383
|
|
-print('--- Splitting')
|
384
|
|
-s = 'This is a test!'
|
385
|
|
-prepr(s)
|
386
|
|
-prepr(s:split(' '))
|
387
|
|
-prepr(s:split('i'))
|
388
|
|
-prepr(s:split('0'))
|
389
|
|
-prepr(s:split('aeiou'))
|
390
|
|
-
|
391
|
|
-l = s:split(' ')
|
392
|
|
-for i in l do
|
393
|
|
- prepr(i, type(i))
|
394
|
|
-end
|
395
|
|
-
|
396
|
|
-print('--- Continue/break')
|
397
|
|
-
|
398
|
|
-l = range(10)
|
399
|
|
-for i in l do
|
400
|
|
- print(i)
|
401
|
|
- if i >= 5 then break end
|
402
|
|
-end
|
403
|
|
-
|
404
|
|
-print('---')
|
405
|
|
-
|
406
|
|
-for i in l do
|
407
|
|
- if i%2 == 0 then continue end
|
408
|
|
- print(i)
|
409
|
|
-end
|
410
|
|
-
|
411
|
|
-print('--- All done!')
|