diff options
author | Li Jin <dragon-fly@qq.com> | 2023-10-31 17:06:43 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2023-10-31 17:06:50 +0800 |
commit | 9908433cc0d493c6910d0aed5cdf263397cee87f (patch) | |
tree | 4a62db9fc0d49e9a05ce9cb1e35bb956c9b03b3b /spec/outputs/codes_from_doc.lua | |
parent | ca6f13ea62e1fb566a604a00176beb6dda0dcb1a (diff) | |
download | yuescript-9908433cc0d493c6910d0aed5cdf263397cee87f.tar.gz yuescript-9908433cc0d493c6910d0aed5cdf263397cee87f.tar.bz2 yuescript-9908433cc0d493c6910d0aed5cdf263397cee87f.zip |
add doc codes check.
Diffstat (limited to 'spec/outputs/codes_from_doc.lua')
-rw-r--r-- | spec/outputs/codes_from_doc.lua | 4156 |
1 files changed, 4156 insertions, 0 deletions
diff --git a/spec/outputs/codes_from_doc.lua b/spec/outputs/codes_from_doc.lua new file mode 100644 index 0000000..1bcc587 --- /dev/null +++ b/spec/outputs/codes_from_doc.lua | |||
@@ -0,0 +1,4156 @@ | |||
1 | local _module_0 = { } | ||
2 | local p, to_lua | ||
3 | do | ||
4 | local _obj_0 = require("yue") | ||
5 | p, to_lua = _obj_0.p, _obj_0.to_lua | ||
6 | end | ||
7 | local inventory = { | ||
8 | equipment = { | ||
9 | "sword", | ||
10 | "shield" | ||
11 | }, | ||
12 | items = { | ||
13 | { | ||
14 | name = "potion", | ||
15 | count = 10 | ||
16 | }, | ||
17 | { | ||
18 | name = "bread", | ||
19 | count = 3 | ||
20 | } | ||
21 | } | ||
22 | } | ||
23 | print(reduce(filter(map({ | ||
24 | 1, | ||
25 | 2, | ||
26 | 3 | ||
27 | }, function(x) | ||
28 | return x * 2 | ||
29 | end), function(x) | ||
30 | return x > 4 | ||
31 | end), 0, function(a, b) | ||
32 | return a + b | ||
33 | end)) | ||
34 | local apple = setmetatable({ | ||
35 | size = 15, | ||
36 | }, { | ||
37 | __index = { | ||
38 | color = 0x00ffff | ||
39 | } | ||
40 | }) | ||
41 | if (getmetatable(apple) ~= nil) then | ||
42 | p(apple.color, getmetatable(apple).__index) | ||
43 | end | ||
44 | local _ud83c_udf1b = "ζδΉθζ¬" | ||
45 | _module_0["π"] = _ud83c_udf1b | ||
46 | return _module_0 | ||
47 | local area = 6.2831853071796 * 5 | ||
48 | print('hello world') | ||
49 | assert(item ~= nil) | ||
50 | local value = item | ||
51 | if (f1() and f2() and f3()) then | ||
52 | print("OK") | ||
53 | end | ||
54 | local funcA | ||
55 | funcA = function() end | ||
56 | funcA = function() | ||
57 | return "assign the Yue defined variable" | ||
58 | end | ||
59 | local function funcB() end | ||
60 | funcB = function() | ||
61 | return "assign the Lua defined variable" | ||
62 | end | ||
63 | -- raw Lua codes insertion | ||
64 | if cond then | ||
65 | print("output") | ||
66 | end | ||
67 | print("yuescript") | ||
68 | print(3) | ||
69 | if tb ~= nil then | ||
70 | tb:func() | ||
71 | end | ||
72 | if tb ~= nil then | ||
73 | tb:func() | ||
74 | end | ||
75 | print(1 < 2 and 2 <= 2 and 2 < 3 and 3 == 3 and 3 > 2 and 2 >= 1 and 1 == 1 and 1 < 3 and 3 ~= 5) | ||
76 | local a = 5 | ||
77 | print(1 <= a and a <= 10) | ||
78 | local v | ||
79 | v = function(x) | ||
80 | print(x) | ||
81 | return x | ||
82 | end | ||
83 | print((function() | ||
84 | local _cond_0 = v(2) | ||
85 | if not (v(1) < _cond_0) then | ||
86 | return false | ||
87 | else | ||
88 | return _cond_0 <= v(3) | ||
89 | end | ||
90 | end)()) | ||
91 | print((function() | ||
92 | local _cond_0 = v(2) | ||
93 | if not (v(1) > _cond_0) then | ||
94 | return false | ||
95 | else | ||
96 | return _cond_0 <= v(3) | ||
97 | end | ||
98 | end)()) | ||
99 | local tab = { } | ||
100 | tab[#tab + 1] = "Value" | ||
101 | local parts = { | ||
102 | "shoulders", | ||
103 | "knees" | ||
104 | } | ||
105 | local lyrics | ||
106 | do | ||
107 | local _tab_0 = { | ||
108 | "head" | ||
109 | } | ||
110 | local _idx_0 = 1 | ||
111 | for _key_0, _value_0 in pairs(parts) do | ||
112 | if _idx_0 == _key_0 then | ||
113 | _tab_0[#_tab_0 + 1] = _value_0 | ||
114 | _idx_0 = _idx_0 + 1 | ||
115 | else | ||
116 | _tab_0[_key_0] = _value_0 | ||
117 | end | ||
118 | end | ||
119 | _tab_0[#_tab_0 + 1] = "and" | ||
120 | _tab_0[#_tab_0 + 1] = "toes" | ||
121 | lyrics = _tab_0 | ||
122 | end | ||
123 | local copy | ||
124 | do | ||
125 | local _tab_0 = { } | ||
126 | local _idx_0 = 1 | ||
127 | for _key_0, _value_0 in pairs(other) do | ||
128 | if _idx_0 == _key_0 then | ||
129 | _tab_0[#_tab_0 + 1] = _value_0 | ||
130 | _idx_0 = _idx_0 + 1 | ||
131 | else | ||
132 | _tab_0[_key_0] = _value_0 | ||
133 | end | ||
134 | end | ||
135 | copy = _tab_0 | ||
136 | end | ||
137 | local a = { | ||
138 | 1, | ||
139 | 2, | ||
140 | 3, | ||
141 | x = 1 | ||
142 | } | ||
143 | local b = { | ||
144 | 4, | ||
145 | 5, | ||
146 | y = 1 | ||
147 | } | ||
148 | local merge | ||
149 | do | ||
150 | local _tab_0 = { } | ||
151 | local _idx_0 = 1 | ||
152 | for _key_0, _value_0 in pairs(a) do | ||
153 | if _idx_0 == _key_0 then | ||
154 | _tab_0[#_tab_0 + 1] = _value_0 | ||
155 | _idx_0 = _idx_0 + 1 | ||
156 | else | ||
157 | _tab_0[_key_0] = _value_0 | ||
158 | end | ||
159 | end | ||
160 | local _idx_1 = 1 | ||
161 | for _key_0, _value_0 in pairs(b) do | ||
162 | if _idx_1 == _key_0 then | ||
163 | _tab_0[#_tab_0 + 1] = _value_0 | ||
164 | _idx_1 = _idx_1 + 1 | ||
165 | else | ||
166 | _tab_0[_key_0] = _value_0 | ||
167 | end | ||
168 | end | ||
169 | merge = _tab_0 | ||
170 | end | ||
171 | local mt = { } | ||
172 | local add | ||
173 | add = function(self, right) | ||
174 | return setmetatable({ | ||
175 | value = self.value + right.value | ||
176 | }, mt) | ||
177 | end | ||
178 | mt.__add = add | ||
179 | local a = setmetatable({ | ||
180 | value = 1 | ||
181 | }, mt) | ||
182 | local b = setmetatable({ | ||
183 | value = 2 | ||
184 | }, { | ||
185 | __add = add | ||
186 | }) | ||
187 | local c = setmetatable({ | ||
188 | value = 3 | ||
189 | }, { | ||
190 | __add = mt.__add | ||
191 | }) | ||
192 | local d = a + b + c | ||
193 | print(d.value) | ||
194 | local _ <close> = setmetatable({ }, { | ||
195 | __close = function() | ||
196 | return print("out of scope") | ||
197 | end | ||
198 | }) | ||
199 | local tb = setmetatable({ }, { | ||
200 | ["value"] = 123 | ||
201 | }) | ||
202 | getmetatable(tb).__index = getmetatable(tb) | ||
203 | print(tb.value) | ||
204 | setmetatable(tb, { | ||
205 | __index = { | ||
206 | item = "hello" | ||
207 | } | ||
208 | }) | ||
209 | print(tb.item) | ||
210 | local item, new, close, getter | ||
211 | do | ||
212 | local _obj_0 = tb | ||
213 | item, new = _obj_0[1], _obj_0.new | ||
214 | do | ||
215 | local _obj_1 = getmetatable(_obj_0) | ||
216 | close, getter = _obj_1.__close, _obj_1.__index | ||
217 | end | ||
218 | end | ||
219 | print(item, new, close, getter) | ||
220 | do | ||
221 | local _obj_0 = func | ||
222 | if _obj_0 ~= nil then | ||
223 | _obj_0() | ||
224 | end | ||
225 | end | ||
226 | print((function() | ||
227 | local _obj_0 = abc | ||
228 | if _obj_0 ~= nil then | ||
229 | local _obj_1 = _obj_0["hello world"] | ||
230 | if _obj_1 ~= nil then | ||
231 | return _obj_1.xyz | ||
232 | end | ||
233 | return nil | ||
234 | end | ||
235 | return nil | ||
236 | end)()) | ||
237 | local x | ||
238 | do | ||
239 | local _obj_0 = tab | ||
240 | if _obj_0 ~= nil then | ||
241 | x = _obj_0.value | ||
242 | end | ||
243 | end | ||
244 | local len = (function() | ||
245 | local _obj_0 = utf8 | ||
246 | if _obj_0 ~= nil then | ||
247 | return _obj_0.len | ||
248 | end | ||
249 | return nil | ||
250 | end)() or (function() | ||
251 | local _obj_0 = string | ||
252 | if _obj_0 ~= nil then | ||
253 | return _obj_0.len | ||
254 | end | ||
255 | return nil | ||
256 | end)() or function(o) | ||
257 | return #o | ||
258 | end | ||
259 | if print and (x ~= nil) then | ||
260 | print(x) | ||
261 | end | ||
262 | do | ||
263 | local _with_0 = io.open("test.txt", "w") | ||
264 | if _with_0 ~= nil then | ||
265 | _with_0:write("hello") | ||
266 | _with_0:close() | ||
267 | end | ||
268 | end | ||
269 | print("hello") | ||
270 | print(1, 2) | ||
271 | print(1, 2, 3) | ||
272 | print(render(emit(parse(extract(readFile("example.txt"), language, { }), language)))) | ||
273 | local a, b, c, d | ||
274 | if b ~= nil then | ||
275 | a = b | ||
276 | else | ||
277 | if c ~= nil then | ||
278 | a = c | ||
279 | else | ||
280 | a = d | ||
281 | end | ||
282 | end | ||
283 | func((function() | ||
284 | if a ~= nil then | ||
285 | return a | ||
286 | else | ||
287 | return { } | ||
288 | end | ||
289 | end)()) | ||
290 | if a == nil then | ||
291 | a = false | ||
292 | end | ||
293 | local list = { | ||
294 | 1, | ||
295 | 2, | ||
296 | 3 | ||
297 | } | ||
298 | func({ | ||
299 | 1, | ||
300 | 2, | ||
301 | 3 | ||
302 | }) | ||
303 | local tb = { | ||
304 | name = "abc", | ||
305 | values = { | ||
306 | "a", | ||
307 | "b", | ||
308 | "c" | ||
309 | }, | ||
310 | objects = { | ||
311 | { | ||
312 | name = "a", | ||
313 | value = 1, | ||
314 | func = function(self) | ||
315 | return self.value + 1 | ||
316 | end, | ||
317 | tb = { | ||
318 | fieldA = 1 | ||
319 | } | ||
320 | }, | ||
321 | { | ||
322 | name = "b", | ||
323 | value = 2, | ||
324 | func = function(self) | ||
325 | return self.value + 2 | ||
326 | end, | ||
327 | tb = { } | ||
328 | } | ||
329 | } | ||
330 | } | ||
331 | do | ||
332 | local insert, concat = table.insert, table.concat | ||
333 | local C, Ct, Cmt | ||
334 | do | ||
335 | local _obj_0 = require("lpeg") | ||
336 | C, Ct, Cmt = _obj_0.C, _obj_0.Ct, _obj_0.Cmt | ||
337 | end | ||
338 | local x, y, z | ||
339 | do | ||
340 | local _obj_0 = require('mymodule') | ||
341 | x, y, z = _obj_0.x, _obj_0.y, _obj_0.z | ||
342 | end | ||
343 | local a, b, c | ||
344 | do | ||
345 | local _obj_0 = require('module') | ||
346 | a, b, c = _obj_0.a, _obj_0.b, _obj_0.c | ||
347 | end | ||
348 | end | ||
349 | do | ||
350 | local module = require('module') | ||
351 | local module_x = require('module_x') | ||
352 | local d_a_s_h_e_s = require("d-a-s-h-e-s") | ||
353 | local part = require("module.part") | ||
354 | end | ||
355 | do | ||
356 | local PlayerModule = require("player") | ||
357 | local C, Ct, Cmt | ||
358 | do | ||
359 | local _obj_0 = require("lpeg") | ||
360 | C, Ct, Cmt = _obj_0.C, _obj_0.Ct, _obj_0.Cmt | ||
361 | end | ||
362 | local one, two, ch | ||
363 | do | ||
364 | local _obj_0 = require("export") | ||
365 | one, two, ch = _obj_0[1], _obj_0[2], _obj_0.Something.umm[1] | ||
366 | end | ||
367 | end | ||
368 | local _module_0 = { } | ||
369 | local a, b, c = 1, 2, 3 | ||
370 | _module_0["a"], _module_0["b"], _module_0["c"] = a, b, c | ||
371 | local cool = "cat" | ||
372 | _module_0["cool"] = cool | ||
373 | local What | ||
374 | if this then | ||
375 | What = "abc" | ||
376 | else | ||
377 | What = "def" | ||
378 | end | ||
379 | _module_0["What"] = What | ||
380 | local y | ||
381 | y = function() | ||
382 | local hallo = 3434 | ||
383 | end | ||
384 | _module_0["y"] = y | ||
385 | local Something | ||
386 | do | ||
387 | local _class_0 | ||
388 | local _base_0 = { | ||
389 | umm = "cool" | ||
390 | } | ||
391 | if _base_0.__index == nil then | ||
392 | _base_0.__index = _base_0 | ||
393 | end | ||
394 | _class_0 = setmetatable({ | ||
395 | __init = function() end, | ||
396 | __base = _base_0, | ||
397 | __name = "Something" | ||
398 | }, { | ||
399 | __index = _base_0, | ||
400 | __call = function(cls, ...) | ||
401 | local _self_0 = setmetatable({ }, _base_0) | ||
402 | cls.__init(_self_0, ...) | ||
403 | return _self_0 | ||
404 | end | ||
405 | }) | ||
406 | _base_0.__class = _class_0 | ||
407 | Something = _class_0 | ||
408 | end | ||
409 | _module_0["Something"] = Something | ||
410 | return _module_0 | ||
411 | local _module_0 = { } | ||
412 | local loadstring, tolua | ||
413 | do | ||
414 | local _obj_0 = yue | ||
415 | loadstring, tolua = _obj_0.loadstring, _obj_0.to_lua | ||
416 | end | ||
417 | _module_0["loadstring"], _module_0["tolua"] = loadstring, tolua | ||
418 | local fieldA = tb.itemA.fieldA | ||
419 | if fieldA == nil then | ||
420 | fieldA = 'default' | ||
421 | end | ||
422 | _module_0["fieldA"] = fieldA | ||
423 | return _module_0 | ||
424 | local _module_0 = setmetatable({ }, { }) | ||
425 | _module_0.itemA = tb | ||
426 | getmetatable(_module_0).__index = items | ||
427 | _module_0["a-b-c"] = 123 | ||
428 | return _module_0 | ||
429 | local _module_0 = { } | ||
430 | local d, e, f = 3, 2, 1 | ||
431 | _module_0[#_module_0 + 1] = d | ||
432 | _module_0[#_module_0 + 1] = e | ||
433 | _module_0[#_module_0 + 1] = f | ||
434 | if this then | ||
435 | _module_0[#_module_0 + 1] = 123 | ||
436 | else | ||
437 | _module_0[#_module_0 + 1] = 456 | ||
438 | end | ||
439 | do | ||
440 | local _with_0 = tmp | ||
441 | local j = 2000 | ||
442 | _module_0[#_module_0 + 1] = _with_0 | ||
443 | end | ||
444 | return _module_0 | ||
445 | local _module_0 = nil | ||
446 | _module_0 = function() | ||
447 | print("hello") | ||
448 | return 123 | ||
449 | end | ||
450 | return _module_0 | ||
451 | local hello = "world" | ||
452 | local a, b, c = 1, 2, 3 | ||
453 | hello = 123 | ||
454 | local x = 1 | ||
455 | x = x + 1 | ||
456 | x = x - 1 | ||
457 | x = x * 10 | ||
458 | x = x / 10 | ||
459 | x = x % 10 | ||
460 | local s = s .. "world" | ||
461 | local arg = arg or "default value" | ||
462 | local a = 0 | ||
463 | local b = 0 | ||
464 | local c = 0 | ||
465 | local d = 0 | ||
466 | local e = 0 | ||
467 | local x = f() | ||
468 | local y = x | ||
469 | local z = x | ||
470 | do | ||
471 | local a | ||
472 | a = 1 | ||
473 | local x, y, z | ||
474 | print("forward declare all variables as locals") | ||
475 | x = function() | ||
476 | return 1 + y + z | ||
477 | end | ||
478 | y, z = 2, 3 | ||
479 | instance = Item:new() | ||
480 | end | ||
481 | do | ||
482 | local X | ||
483 | X = 1 | ||
484 | local B | ||
485 | print("only forward declare upper case variables") | ||
486 | local a = 1 | ||
487 | B = 2 | ||
488 | end | ||
489 | do | ||
490 | a = 1 | ||
491 | print("declare all variables as globals") | ||
492 | local x | ||
493 | x = function() | ||
494 | return 1 + y + z | ||
495 | end | ||
496 | local y, z = 2, 3 | ||
497 | end | ||
498 | do | ||
499 | X = 1 | ||
500 | print("only declare upper case variables as globals") | ||
501 | local a = 1 | ||
502 | local B = 2 | ||
503 | local Temp | ||
504 | Temp = "a local value" | ||
505 | end | ||
506 | local thing = { | ||
507 | 1, | ||
508 | 2 | ||
509 | } | ||
510 | local a, b = thing[1], thing[2] | ||
511 | print(a, b) | ||
512 | local obj = { | ||
513 | hello = "world", | ||
514 | day = "tuesday", | ||
515 | length = 20 | ||
516 | } | ||
517 | local hello, the_day = obj.hello, obj.day | ||
518 | print(hello, the_day) | ||
519 | local day = obj.day | ||
520 | local obj2 = { | ||
521 | numbers = { | ||
522 | 1, | ||
523 | 2, | ||
524 | 3, | ||
525 | 4 | ||
526 | }, | ||
527 | properties = { | ||
528 | color = "green", | ||
529 | height = 13.5 | ||
530 | } | ||
531 | } | ||
532 | local first, second = obj2.numbers[1], obj2.numbers[2] | ||
533 | print(first, second, color) | ||
534 | local first, second, color | ||
535 | do | ||
536 | local _obj_0 = obj2 | ||
537 | first, second, color = _obj_0.numbers[1], _obj_0.numbers[2], _obj_0.properties.color | ||
538 | end | ||
539 | local concat, insert | ||
540 | do | ||
541 | local _obj_0 = table | ||
542 | concat, insert = _obj_0.concat, _obj_0.insert | ||
543 | end | ||
544 | local mix, max, rand | ||
545 | do | ||
546 | local _obj_0 = math | ||
547 | mix, max, rand = _obj_0.mix, _obj_0.max, _obj_0.random | ||
548 | end | ||
549 | local name, job | ||
550 | do | ||
551 | local _obj_0 = person | ||
552 | name, job = _obj_0.name, _obj_0.job | ||
553 | if name == nil then | ||
554 | name = "nameless" | ||
555 | end | ||
556 | if job == nil then | ||
557 | job = "jobless" | ||
558 | end | ||
559 | end | ||
560 | local two, four | ||
561 | do | ||
562 | local _obj_0 = items | ||
563 | two, four = _obj_0[2], _obj_0[4] | ||
564 | end | ||
565 | local tuples = { | ||
566 | { | ||
567 | "hello", | ||
568 | "world" | ||
569 | }, | ||
570 | { | ||
571 | "egg", | ||
572 | "head" | ||
573 | } | ||
574 | } | ||
575 | for _index_0 = 1, #tuples do | ||
576 | local _des_0 = tuples[_index_0] | ||
577 | local left, right = _des_0[1], _des_0[2] | ||
578 | print(left, right) | ||
579 | end | ||
580 | do | ||
581 | local user = database.find_user("moon") | ||
582 | if user then | ||
583 | print(user.name) | ||
584 | end | ||
585 | end | ||
586 | do | ||
587 | local hello = os.getenv("hello") | ||
588 | if hello then | ||
589 | print("You have hello", hello) | ||
590 | else | ||
591 | do | ||
592 | local world = os.getenv("world") | ||
593 | if world then | ||
594 | print("you have world", world) | ||
595 | else | ||
596 | print("nothing :(") | ||
597 | end | ||
598 | end | ||
599 | end | ||
600 | end | ||
601 | do | ||
602 | local success, result = pcall(function() | ||
603 | return "get result without problems" | ||
604 | end) | ||
605 | if success then | ||
606 | print(result) | ||
607 | end | ||
608 | end | ||
609 | print("OK") | ||
610 | local list = { | ||
611 | 1, | ||
612 | 2, | ||
613 | 3, | ||
614 | 4, | ||
615 | 5 | ||
616 | } | ||
617 | local fn | ||
618 | fn = function(ok) | ||
619 | return ok, table.unpack(list) | ||
620 | end | ||
621 | (function(_arg_0, ...) | ||
622 | local ok = _arg_0 | ||
623 | local count = select('#', ...) | ||
624 | local first = select(1, ...) | ||
625 | return print(ok, count, first) | ||
626 | end)(fn(true)) | ||
627 | Rx.Observable.fromRange(1, 8):filter(function(x) | ||
628 | return x % 2 == 0 | ||
629 | end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) | ||
630 | return value .. '!' | ||
631 | end):subscribe(print) | ||
632 | local str = strA .. strB .. strC | ||
633 | func(3000, "192.168.1.1") | ||
634 | xpcall(func, function(err) | ||
635 | return print(yue.traceback(err)) | ||
636 | end, 1, 2, 3) | ||
637 | local success, result = xpcall(func, function(err) | ||
638 | return yue.traceback(err) | ||
639 | end, 1, 2, 3) | ||
640 | xpcall(func, function(err) | ||
641 | return print(yue.traceback(err)) | ||
642 | end, 1, 2, 3) | ||
643 | success, result = pcall(func, 1, 2, 3) | ||
644 | pcall(function() | ||
645 | print("trying") | ||
646 | return func(1, 2, 3) | ||
647 | end) | ||
648 | success, result = xpcall(func, function(err) | ||
649 | return print(yue.traceback(err)) | ||
650 | end, 1, 2, 3) | ||
651 | if success then | ||
652 | print(result) | ||
653 | end | ||
654 | local a <const> = 123 | ||
655 | local _ <close> = setmetatable({ }, { | ||
656 | __close = function() | ||
657 | return print("Out of scope.") | ||
658 | end | ||
659 | }) | ||
660 | local a, b, c, d | ||
661 | do | ||
662 | local _obj_0 = tb | ||
663 | a, b, c, d = _obj_0.a, _obj_0.b, _obj_0[1], _obj_0[2] | ||
664 | end | ||
665 | local some_string = "Here is a string\n that has a line break in it." | ||
666 | print("I am " .. tostring(math.random() * 100) .. "% sure.") | ||
667 | local integer = 1000000 | ||
668 | local hex = 0xEFBBBF | ||
669 | local my_function | ||
670 | my_function = function() end | ||
671 | my_function() | ||
672 | local func_a | ||
673 | func_a = function() | ||
674 | return print("hello world") | ||
675 | end | ||
676 | local func_b | ||
677 | func_b = function() | ||
678 | local value = 100 | ||
679 | return print("The value:", value) | ||
680 | end | ||
681 | func_a() | ||
682 | func_b() | ||
683 | local sum | ||
684 | sum = function(x, y) | ||
685 | return print("sum", x + y) | ||
686 | end | ||
687 | sum(10, 20) | ||
688 | print(sum(10, 20)) | ||
689 | a(b(c("a", "b", "c"))) | ||
690 | print("x:", sum(10, 20), "y:", sum(30, 40)) | ||
691 | local sum | ||
692 | sum = function(x, y) | ||
693 | return x + y | ||
694 | end | ||
695 | print("The sum is ", sum(10, 20)) | ||
696 | local sum | ||
697 | sum = function(x, y) | ||
698 | return x + y | ||
699 | end | ||
700 | local mystery | ||
701 | mystery = function(x, y) | ||
702 | return x + y, x - y | ||
703 | end | ||
704 | local a, b = mystery(10, 20) | ||
705 | local func | ||
706 | func = function(self, num) | ||
707 | return self.value + num | ||
708 | end | ||
709 | local my_function | ||
710 | my_function = function(name, height) | ||
711 | if name == nil then | ||
712 | name = "something" | ||
713 | end | ||
714 | if height == nil then | ||
715 | height = 100 | ||
716 | end | ||
717 | print("Hello I am", name) | ||
718 | return print("My height is", height) | ||
719 | end | ||
720 | local some_args | ||
721 | some_args = function(x, y) | ||
722 | if x == nil then | ||
723 | x = 100 | ||
724 | end | ||
725 | if y == nil then | ||
726 | y = x + 1000 | ||
727 | end | ||
728 | return print(x + y) | ||
729 | end | ||
730 | local a = x - 10 | ||
731 | local b = x - 10 | ||
732 | local c = x(-y) | ||
733 | local d = x - z | ||
734 | local x = func("hello") + 100 | ||
735 | local y = func("hello" + 100) | ||
736 | my_func(5, 4, 3, 8, 9, 10) | ||
737 | cool_func(1, 2, 3, 4, 5, 6, 7, 8) | ||
738 | my_func(5, 6, 7, 6, another_func(6, 7, 8, 9, 1, 2), 5, 4) | ||
739 | local x = { | ||
740 | 1, | ||
741 | 2, | ||
742 | 3, | ||
743 | 4, | ||
744 | a_func(4, 5, 5, 6), | ||
745 | 8, | ||
746 | 9, | ||
747 | 10 | ||
748 | } | ||
749 | local y = { | ||
750 | my_func(1, 2, 3, 4, 5), | ||
751 | 5, | ||
752 | 6, | ||
753 | 7 | ||
754 | } | ||
755 | if func(1, 2, 3, "hello", "world") then | ||
756 | print("hello") | ||
757 | print("I am inside if") | ||
758 | end | ||
759 | if func(1, 2, 3, "hello", "world") then | ||
760 | print("hello") | ||
761 | print("I am inside if") | ||
762 | end | ||
763 | f(function() | ||
764 | return print("hello") | ||
765 | end) | ||
766 | f(function(self) | ||
767 | return print(self.value) | ||
768 | end) | ||
769 | map(function(x) | ||
770 | return x * 2 | ||
771 | end, { | ||
772 | 1, | ||
773 | 2, | ||
774 | 3 | ||
775 | }) | ||
776 | local result, msg | ||
777 | do | ||
778 | result, msg = readAsync("filename.txt", function(data) | ||
779 | print(data) | ||
780 | return processAsync(data, function(info) | ||
781 | return check(info) | ||
782 | end) | ||
783 | end) | ||
784 | end | ||
785 | print(result, msg) | ||
786 | local some_values = { | ||
787 | 1, | ||
788 | 2, | ||
789 | 3, | ||
790 | 4 | ||
791 | } | ||
792 | local some_values = { | ||
793 | name = "Bill", | ||
794 | age = 200, | ||
795 | ["favorite food"] = "rice" | ||
796 | } | ||
797 | local profile = { | ||
798 | height = "4 feet", | ||
799 | shoe_size = 13, | ||
800 | favorite_foods = { | ||
801 | "ice cream", | ||
802 | "donuts" | ||
803 | } | ||
804 | } | ||
805 | local values = { | ||
806 | 1, | ||
807 | 2, | ||
808 | 3, | ||
809 | 4, | ||
810 | 5, | ||
811 | 6, | ||
812 | 7, | ||
813 | 8, | ||
814 | name = "superman", | ||
815 | occupation = "crime fighting" | ||
816 | } | ||
817 | my_function({ | ||
818 | dance = "Tango", | ||
819 | partner = "none" | ||
820 | }) | ||
821 | local y = { | ||
822 | type = "dog", | ||
823 | legs = 4, | ||
824 | tails = 1 | ||
825 | } | ||
826 | local tbl = { | ||
827 | ["do"] = "something", | ||
828 | ["end"] = "hunger" | ||
829 | } | ||
830 | local hair = "golden" | ||
831 | local height = 200 | ||
832 | local person = { | ||
833 | hair = hair, | ||
834 | height = height, | ||
835 | shoe_size = 40 | ||
836 | } | ||
837 | print_table({ | ||
838 | hair = hair, | ||
839 | height = height | ||
840 | }) | ||
841 | local t = { | ||
842 | [1 + 2] = "hello", | ||
843 | ["hello world"] = true | ||
844 | } | ||
845 | local some_values = { | ||
846 | 1, | ||
847 | 2, | ||
848 | 3, | ||
849 | 4 | ||
850 | } | ||
851 | local list_with_one_element = { | ||
852 | 1 | ||
853 | } | ||
854 | local items = { | ||
855 | 1, | ||
856 | 2, | ||
857 | 3, | ||
858 | 4 | ||
859 | } | ||
860 | local doubled | ||
861 | do | ||
862 | local _accum_0 = { } | ||
863 | local _len_0 = 1 | ||
864 | for i, item in ipairs(items) do | ||
865 | _accum_0[_len_0] = item * 2 | ||
866 | _len_0 = _len_0 + 1 | ||
867 | end | ||
868 | doubled = _accum_0 | ||
869 | end | ||
870 | local iter = ipairs(items) | ||
871 | local slice | ||
872 | do | ||
873 | local _accum_0 = { } | ||
874 | local _len_0 = 1 | ||
875 | for i, item in iter do | ||
876 | if i > 1 and i < 3 then | ||
877 | _accum_0[_len_0] = item | ||
878 | _len_0 = _len_0 + 1 | ||
879 | end | ||
880 | end | ||
881 | slice = _accum_0 | ||
882 | end | ||
883 | local doubled | ||
884 | do | ||
885 | local _accum_0 = { } | ||
886 | local _len_0 = 1 | ||
887 | local _list_0 = items | ||
888 | for _index_0 = 1, #_list_0 do | ||
889 | local item = _list_0[_index_0] | ||
890 | _accum_0[_len_0] = item * 2 | ||
891 | _len_0 = _len_0 + 1 | ||
892 | end | ||
893 | doubled = _accum_0 | ||
894 | end | ||
895 | local x_coords = { | ||
896 | 4, | ||
897 | 5, | ||
898 | 6, | ||
899 | 7 | ||
900 | } | ||
901 | local y_coords = { | ||
902 | 9, | ||
903 | 2, | ||
904 | 3 | ||
905 | } | ||
906 | local points | ||
907 | do | ||
908 | local _accum_0 = { } | ||
909 | local _len_0 = 1 | ||
910 | for _index_0 = 1, #x_coords do | ||
911 | local x = x_coords[_index_0] | ||
912 | for _index_1 = 1, #y_coords do | ||
913 | local y = y_coords[_index_1] | ||
914 | _accum_0[_len_0] = { | ||
915 | x, | ||
916 | y | ||
917 | } | ||
918 | _len_0 = _len_0 + 1 | ||
919 | end | ||
920 | end | ||
921 | points = _accum_0 | ||
922 | end | ||
923 | local evens | ||
924 | do | ||
925 | local _accum_0 = { } | ||
926 | local _len_0 = 1 | ||
927 | for i = 1, 100 do | ||
928 | if i % 2 == 0 then | ||
929 | _accum_0[_len_0] = i | ||
930 | _len_0 = _len_0 + 1 | ||
931 | end | ||
932 | end | ||
933 | evens = _accum_0 | ||
934 | end | ||
935 | local thing = { | ||
936 | color = "red", | ||
937 | name = "fast", | ||
938 | width = 123 | ||
939 | } | ||
940 | local thing_copy | ||
941 | do | ||
942 | local _tbl_0 = { } | ||
943 | for k, v in pairs(thing) do | ||
944 | _tbl_0[k] = v | ||
945 | end | ||
946 | thing_copy = _tbl_0 | ||
947 | end | ||
948 | local no_color | ||
949 | do | ||
950 | local _tbl_0 = { } | ||
951 | for k, v in pairs(thing) do | ||
952 | if k ~= "color" then | ||
953 | _tbl_0[k] = v | ||
954 | end | ||
955 | end | ||
956 | no_color = _tbl_0 | ||
957 | end | ||
958 | local numbers = { | ||
959 | 1, | ||
960 | 2, | ||
961 | 3, | ||
962 | 4 | ||
963 | } | ||
964 | local sqrts | ||
965 | do | ||
966 | local _tbl_0 = { } | ||
967 | for _index_0 = 1, #numbers do | ||
968 | local i = numbers[_index_0] | ||
969 | _tbl_0[i] = math.sqrt(i) | ||
970 | end | ||
971 | sqrts = _tbl_0 | ||
972 | end | ||
973 | local tuples = { | ||
974 | { | ||
975 | "hello", | ||
976 | "world" | ||
977 | }, | ||
978 | { | ||
979 | "foo", | ||
980 | "bar" | ||
981 | } | ||
982 | } | ||
983 | local tbl | ||
984 | do | ||
985 | local _tbl_0 = { } | ||
986 | for _index_0 = 1, #tuples do | ||
987 | local tuple = tuples[_index_0] | ||
988 | local _key_0, _val_0 = unpack(tuple) | ||
989 | _tbl_0[_key_0] = _val_0 | ||
990 | end | ||
991 | tbl = _tbl_0 | ||
992 | end | ||
993 | local slice | ||
994 | do | ||
995 | local _accum_0 = { } | ||
996 | local _len_0 = 1 | ||
997 | local _list_0 = items | ||
998 | local _max_0 = 5 | ||
999 | for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do | ||
1000 | local item = _list_0[_index_0] | ||
1001 | _accum_0[_len_0] = item | ||
1002 | _len_0 = _len_0 + 1 | ||
1003 | end | ||
1004 | slice = _accum_0 | ||
1005 | end | ||
1006 | local slice | ||
1007 | do | ||
1008 | local _accum_0 = { } | ||
1009 | local _len_0 = 1 | ||
1010 | local _list_0 = items | ||
1011 | for _index_0 = 2, #_list_0 do | ||
1012 | local item = _list_0[_index_0] | ||
1013 | _accum_0[_len_0] = item | ||
1014 | _len_0 = _len_0 + 1 | ||
1015 | end | ||
1016 | slice = _accum_0 | ||
1017 | end | ||
1018 | local slice | ||
1019 | do | ||
1020 | local _accum_0 = { } | ||
1021 | local _len_0 = 1 | ||
1022 | local _list_0 = items | ||
1023 | for _index_0 = 1, #_list_0, 2 do | ||
1024 | local item = _list_0[_index_0] | ||
1025 | _accum_0[_len_0] = item | ||
1026 | _len_0 = _len_0 + 1 | ||
1027 | end | ||
1028 | slice = _accum_0 | ||
1029 | end | ||
1030 | for i = 10, 20 do | ||
1031 | print(i) | ||
1032 | end | ||
1033 | for k = 1, 15, 2 do | ||
1034 | print(k) | ||
1035 | end | ||
1036 | for key, value in pairs(object) do | ||
1037 | print(key, value) | ||
1038 | end | ||
1039 | do | ||
1040 | local _list_0 = items | ||
1041 | local _max_0 = 4 | ||
1042 | for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do | ||
1043 | local item = _list_0[_index_0] | ||
1044 | print(item) | ||
1045 | end | ||
1046 | end | ||
1047 | local _list_0 = items | ||
1048 | for _index_0 = 1, #_list_0 do | ||
1049 | local item = _list_0[_index_0] | ||
1050 | print(item) | ||
1051 | end | ||
1052 | for j = 1, 10, 3 do | ||
1053 | print(j) | ||
1054 | end | ||
1055 | local doubled_evens | ||
1056 | do | ||
1057 | local _accum_0 = { } | ||
1058 | local _len_0 = 1 | ||
1059 | for i = 1, 20 do | ||
1060 | if i % 2 == 0 then | ||
1061 | _accum_0[_len_0] = i * 2 | ||
1062 | else | ||
1063 | _accum_0[_len_0] = i | ||
1064 | end | ||
1065 | _len_0 = _len_0 + 1 | ||
1066 | end | ||
1067 | doubled_evens = _accum_0 | ||
1068 | end | ||
1069 | local func_a | ||
1070 | func_a = function() | ||
1071 | for i = 1, 10 do | ||
1072 | print(i) | ||
1073 | end | ||
1074 | end | ||
1075 | local func_b | ||
1076 | func_b = function() | ||
1077 | local _accum_0 = { } | ||
1078 | local _len_0 = 1 | ||
1079 | for i = 1, 10 do | ||
1080 | _accum_0[_len_0] = i | ||
1081 | _len_0 = _len_0 + 1 | ||
1082 | end | ||
1083 | return _accum_0 | ||
1084 | end | ||
1085 | print(func_a()) | ||
1086 | print(func_b()) | ||
1087 | local i = 10 | ||
1088 | repeat | ||
1089 | print(i) | ||
1090 | i = i - 1 | ||
1091 | until i == 0 | ||
1092 | local i = 10 | ||
1093 | while i > 0 do | ||
1094 | print(i) | ||
1095 | i = i - 1 | ||
1096 | end | ||
1097 | while running == true do | ||
1098 | my_function() | ||
1099 | end | ||
1100 | local i = 10 | ||
1101 | while not (i == 0) do | ||
1102 | print(i) | ||
1103 | i = i - 1 | ||
1104 | end | ||
1105 | while not (running == false) do | ||
1106 | my_function() | ||
1107 | end | ||
1108 | local i = 0 | ||
1109 | while i < 10 do | ||
1110 | i = i + 1 | ||
1111 | if i % 2 == 0 then | ||
1112 | goto _continue_0 | ||
1113 | end | ||
1114 | print(i) | ||
1115 | ::_continue_0:: | ||
1116 | end | ||
1117 | local my_numbers = { | ||
1118 | 1, | ||
1119 | 2, | ||
1120 | 3, | ||
1121 | 4, | ||
1122 | 5, | ||
1123 | 6 | ||
1124 | } | ||
1125 | local odds | ||
1126 | do | ||
1127 | local _accum_0 = { } | ||
1128 | local _len_0 = 1 | ||
1129 | for _index_0 = 1, #my_numbers do | ||
1130 | local x = my_numbers[_index_0] | ||
1131 | if x % 2 == 1 then | ||
1132 | goto _continue_0 | ||
1133 | end | ||
1134 | _accum_0[_len_0] = x | ||
1135 | _len_0 = _len_0 + 1 | ||
1136 | ::_continue_0:: | ||
1137 | end | ||
1138 | odds = _accum_0 | ||
1139 | end | ||
1140 | local have_coins = false | ||
1141 | if have_coins then | ||
1142 | print("Got coins") | ||
1143 | else | ||
1144 | print("No coins") | ||
1145 | end | ||
1146 | local have_coins = false | ||
1147 | if have_coins then | ||
1148 | print("Got coins") | ||
1149 | else | ||
1150 | print("No coins") | ||
1151 | end | ||
1152 | local have_coins = false | ||
1153 | print((function() | ||
1154 | if have_coins then | ||
1155 | return "Got coins" | ||
1156 | else | ||
1157 | return "No coins" | ||
1158 | end | ||
1159 | end)()) | ||
1160 | local is_tall | ||
1161 | is_tall = function(name) | ||
1162 | if name == "Rob" then | ||
1163 | return true | ||
1164 | else | ||
1165 | return false | ||
1166 | end | ||
1167 | end | ||
1168 | local message | ||
1169 | if is_tall("Rob") then | ||
1170 | message = "I am very tall" | ||
1171 | else | ||
1172 | message = "I am not so tall" | ||
1173 | end | ||
1174 | print(message) | ||
1175 | if not (os.date("%A") == "Monday") then | ||
1176 | print("it is not Monday!") | ||
1177 | end | ||
1178 | if not (math.random() > 0.1) then | ||
1179 | print("You're lucky!") | ||
1180 | end | ||
1181 | local a = 5 | ||
1182 | if (1 == a or 3 == a or 5 == a or 7 == a) then | ||
1183 | print("checking equality with discrete values") | ||
1184 | end | ||
1185 | if (function() | ||
1186 | local _check_0 = list | ||
1187 | for _index_0 = 1, #_check_0 do | ||
1188 | if _check_0[_index_0] == a then | ||
1189 | return true | ||
1190 | end | ||
1191 | end | ||
1192 | return false | ||
1193 | end)() then | ||
1194 | print("checking if `a` is in a list") | ||
1195 | end | ||
1196 | if not (math.random() > 0.1) then | ||
1197 | print("You're lucky!") | ||
1198 | end | ||
1199 | if name == "Rob" then | ||
1200 | print("hello world") | ||
1201 | end | ||
1202 | local _list_0 = items | ||
1203 | for _index_0 = 1, #_list_0 do | ||
1204 | local item = _list_0[_index_0] | ||
1205 | print("item: ", item) | ||
1206 | end | ||
1207 | while game:isRunning() do | ||
1208 | game:update() | ||
1209 | end | ||
1210 | while not reader:eof() do | ||
1211 | reader:parse_line() | ||
1212 | end | ||
1213 | local name = "Dan" | ||
1214 | if "Robert" == name then | ||
1215 | print("You are Robert") | ||
1216 | elseif "Dan" == name or "Daniel" == name then | ||
1217 | print("Your name, it's Dan") | ||
1218 | else | ||
1219 | print("I don't know about your name") | ||
1220 | end | ||
1221 | local b = 1 | ||
1222 | local next_number | ||
1223 | if 1 == b then | ||
1224 | next_number = 2 | ||
1225 | elseif 2 == b then | ||
1226 | next_number = 3 | ||
1227 | else | ||
1228 | next_number = error("can't count that high!") | ||
1229 | end | ||
1230 | local msg | ||
1231 | do | ||
1232 | local _exp_0 = math.random(1, 5) | ||
1233 | if 1 == _exp_0 then | ||
1234 | msg = "you are lucky" | ||
1235 | elseif 2 == _exp_0 then | ||
1236 | msg = "you are almost lucky" | ||
1237 | else | ||
1238 | msg = "not so lucky" | ||
1239 | end | ||
1240 | end | ||
1241 | do | ||
1242 | local _exp_0 = math.random(1, 5) | ||
1243 | if 1 == _exp_0 then | ||
1244 | print("you are lucky") | ||
1245 | else | ||
1246 | print("not so lucky") | ||
1247 | end | ||
1248 | end | ||
1249 | do | ||
1250 | local _exp_0 = math.random(1, 5) | ||
1251 | if 1 == _exp_0 then | ||
1252 | print("you are lucky") | ||
1253 | else | ||
1254 | print("not so lucky") | ||
1255 | end | ||
1256 | end | ||
1257 | local items = { | ||
1258 | { | ||
1259 | x = 100, | ||
1260 | y = 200 | ||
1261 | }, | ||
1262 | { | ||
1263 | width = 300, | ||
1264 | height = 400 | ||
1265 | } | ||
1266 | } | ||
1267 | for _index_0 = 1, #items do | ||
1268 | local item = items[_index_0] | ||
1269 | do | ||
1270 | local _type_0 = type(item) | ||
1271 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
1272 | local _match_0 = false | ||
1273 | if _tab_0 then | ||
1274 | local x = item.x | ||
1275 | local y = item.y | ||
1276 | if x ~= nil and y ~= nil then | ||
1277 | _match_0 = true | ||
1278 | print("Vec2 " .. tostring(x) .. ", " .. tostring(y)) | ||
1279 | end | ||
1280 | end | ||
1281 | if not _match_0 then | ||
1282 | if _tab_0 then | ||
1283 | local width = item.width | ||
1284 | local height = item.height | ||
1285 | if width ~= nil and height ~= nil then | ||
1286 | print("size " .. tostring(width) .. ", " .. tostring(height)) | ||
1287 | end | ||
1288 | end | ||
1289 | end | ||
1290 | end | ||
1291 | end | ||
1292 | local item = { } | ||
1293 | local x, y = item.pos.x, item.pos.y | ||
1294 | if x == nil then | ||
1295 | x = 50 | ||
1296 | end | ||
1297 | if y == nil then | ||
1298 | y = 200 | ||
1299 | end | ||
1300 | do | ||
1301 | local _type_0 = type(item) | ||
1302 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
1303 | if _tab_0 then | ||
1304 | do | ||
1305 | local _obj_0 = item.pos | ||
1306 | local _type_1 = type(_obj_0) | ||
1307 | if "table" == _type_1 or "userdata" == _type_1 then | ||
1308 | x = _obj_0.x | ||
1309 | end | ||
1310 | end | ||
1311 | do | ||
1312 | local _obj_0 = item.pos | ||
1313 | local _type_1 = type(_obj_0) | ||
1314 | if "table" == _type_1 or "userdata" == _type_1 then | ||
1315 | y = _obj_0.y | ||
1316 | end | ||
1317 | end | ||
1318 | if x == nil then | ||
1319 | x = 50 | ||
1320 | end | ||
1321 | if y == nil then | ||
1322 | y = 200 | ||
1323 | end | ||
1324 | print("Vec2 " .. tostring(x) .. ", " .. tostring(y)) | ||
1325 | end | ||
1326 | end | ||
1327 | local Inventory | ||
1328 | do | ||
1329 | local _class_0 | ||
1330 | local _base_0 = { | ||
1331 | add_item = function(self, name) | ||
1332 | if self.items[name] then | ||
1333 | local _obj_0 = self.items | ||
1334 | _obj_0[name] = _obj_0[name] + 1 | ||
1335 | else | ||
1336 | self.items[name] = 1 | ||
1337 | end | ||
1338 | end | ||
1339 | } | ||
1340 | if _base_0.__index == nil then | ||
1341 | _base_0.__index = _base_0 | ||
1342 | end | ||
1343 | _class_0 = setmetatable({ | ||
1344 | __init = function(self) | ||
1345 | self.items = { } | ||
1346 | end, | ||
1347 | __base = _base_0, | ||
1348 | __name = "Inventory" | ||
1349 | }, { | ||
1350 | __index = _base_0, | ||
1351 | __call = function(cls, ...) | ||
1352 | local _self_0 = setmetatable({ }, _base_0) | ||
1353 | cls.__init(_self_0, ...) | ||
1354 | return _self_0 | ||
1355 | end | ||
1356 | }) | ||
1357 | _base_0.__class = _class_0 | ||
1358 | Inventory = _class_0 | ||
1359 | end | ||
1360 | local inv = Inventory() | ||
1361 | inv:add_item("t-shirt") | ||
1362 | inv:add_item("pants") | ||
1363 | local Person | ||
1364 | do | ||
1365 | local _class_0 | ||
1366 | local _base_0 = { | ||
1367 | clothes = { }, | ||
1368 | give_item = function(self, name) | ||
1369 | return table.insert(self.clothes, name) | ||
1370 | end | ||
1371 | } | ||
1372 | if _base_0.__index == nil then | ||
1373 | _base_0.__index = _base_0 | ||
1374 | end | ||
1375 | _class_0 = setmetatable({ | ||
1376 | __init = function() end, | ||
1377 | __base = _base_0, | ||
1378 | __name = "Person" | ||
1379 | }, { | ||
1380 | __index = _base_0, | ||
1381 | __call = function(cls, ...) | ||
1382 | local _self_0 = setmetatable({ }, _base_0) | ||
1383 | cls.__init(_self_0, ...) | ||
1384 | return _self_0 | ||
1385 | end | ||
1386 | }) | ||
1387 | _base_0.__class = _class_0 | ||
1388 | Person = _class_0 | ||
1389 | end | ||
1390 | local a = Person() | ||
1391 | local b = Person() | ||
1392 | a:give_item("pants") | ||
1393 | b:give_item("shirt") | ||
1394 | local _list_0 = a.clothes | ||
1395 | for _index_0 = 1, #_list_0 do | ||
1396 | local item = _list_0[_index_0] | ||
1397 | print(item) | ||
1398 | end | ||
1399 | local Person | ||
1400 | do | ||
1401 | local _class_0 | ||
1402 | local _base_0 = { } | ||
1403 | if _base_0.__index == nil then | ||
1404 | _base_0.__index = _base_0 | ||
1405 | end | ||
1406 | _class_0 = setmetatable({ | ||
1407 | __init = function(self) | ||
1408 | self.clothes = { } | ||
1409 | end, | ||
1410 | __base = _base_0, | ||
1411 | __name = "Person" | ||
1412 | }, { | ||
1413 | __index = _base_0, | ||
1414 | __call = function(cls, ...) | ||
1415 | local _self_0 = setmetatable({ }, _base_0) | ||
1416 | cls.__init(_self_0, ...) | ||
1417 | return _self_0 | ||
1418 | end | ||
1419 | }) | ||
1420 | _base_0.__class = _class_0 | ||
1421 | Person = _class_0 | ||
1422 | end | ||
1423 | local BackPack | ||
1424 | do | ||
1425 | local _class_0 | ||
1426 | local _parent_0 = Inventory | ||
1427 | local _base_0 = { | ||
1428 | size = 10, | ||
1429 | add_item = function(self, name) | ||
1430 | if #self.items > size then | ||
1431 | error("backpack is full") | ||
1432 | end | ||
1433 | return _class_0.__parent.__base.add_item(self, name) | ||
1434 | end | ||
1435 | } | ||
1436 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1437 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1438 | _base_0[_key_0] = _val_0 | ||
1439 | end | ||
1440 | end | ||
1441 | if _base_0.__index == nil then | ||
1442 | _base_0.__index = _base_0 | ||
1443 | end | ||
1444 | setmetatable(_base_0, _parent_0.__base) | ||
1445 | _class_0 = setmetatable({ | ||
1446 | __init = function(self, ...) | ||
1447 | return _class_0.__parent.__init(self, ...) | ||
1448 | end, | ||
1449 | __base = _base_0, | ||
1450 | __name = "BackPack", | ||
1451 | __parent = _parent_0 | ||
1452 | }, { | ||
1453 | __index = function(cls, name) | ||
1454 | local val = rawget(_base_0, name) | ||
1455 | if val == nil then | ||
1456 | local parent = rawget(cls, "__parent") | ||
1457 | if parent then | ||
1458 | return parent[name] | ||
1459 | end | ||
1460 | else | ||
1461 | return val | ||
1462 | end | ||
1463 | end, | ||
1464 | __call = function(cls, ...) | ||
1465 | local _self_0 = setmetatable({ }, _base_0) | ||
1466 | cls.__init(_self_0, ...) | ||
1467 | return _self_0 | ||
1468 | end | ||
1469 | }) | ||
1470 | _base_0.__class = _class_0 | ||
1471 | if _parent_0.__inherited then | ||
1472 | _parent_0.__inherited(_parent_0, _class_0) | ||
1473 | end | ||
1474 | BackPack = _class_0 | ||
1475 | end | ||
1476 | local Shelf | ||
1477 | do | ||
1478 | local _class_0 | ||
1479 | local _base_0 = { } | ||
1480 | if _base_0.__index == nil then | ||
1481 | _base_0.__index = _base_0 | ||
1482 | end | ||
1483 | _class_0 = setmetatable({ | ||
1484 | __init = function() end, | ||
1485 | __base = _base_0, | ||
1486 | __name = "Shelf" | ||
1487 | }, { | ||
1488 | __index = _base_0, | ||
1489 | __call = function(cls, ...) | ||
1490 | local _self_0 = setmetatable({ }, _base_0) | ||
1491 | cls.__init(_self_0, ...) | ||
1492 | return _self_0 | ||
1493 | end | ||
1494 | }) | ||
1495 | _base_0.__class = _class_0 | ||
1496 | local self = _class_0; | ||
1497 | self.__inherited = function(self, child) | ||
1498 | return print(self.__name, "was inherited by", child.__name) | ||
1499 | end | ||
1500 | Shelf = _class_0 | ||
1501 | end | ||
1502 | local Cupboard | ||
1503 | do | ||
1504 | local _class_0 | ||
1505 | local _parent_0 = Shelf | ||
1506 | local _base_0 = { } | ||
1507 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1508 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1509 | _base_0[_key_0] = _val_0 | ||
1510 | end | ||
1511 | end | ||
1512 | if _base_0.__index == nil then | ||
1513 | _base_0.__index = _base_0 | ||
1514 | end | ||
1515 | setmetatable(_base_0, _parent_0.__base) | ||
1516 | _class_0 = setmetatable({ | ||
1517 | __init = function(self, ...) | ||
1518 | return _class_0.__parent.__init(self, ...) | ||
1519 | end, | ||
1520 | __base = _base_0, | ||
1521 | __name = "Cupboard", | ||
1522 | __parent = _parent_0 | ||
1523 | }, { | ||
1524 | __index = function(cls, name) | ||
1525 | local val = rawget(_base_0, name) | ||
1526 | if val == nil then | ||
1527 | local parent = rawget(cls, "__parent") | ||
1528 | if parent then | ||
1529 | return parent[name] | ||
1530 | end | ||
1531 | else | ||
1532 | return val | ||
1533 | end | ||
1534 | end, | ||
1535 | __call = function(cls, ...) | ||
1536 | local _self_0 = setmetatable({ }, _base_0) | ||
1537 | cls.__init(_self_0, ...) | ||
1538 | return _self_0 | ||
1539 | end | ||
1540 | }) | ||
1541 | _base_0.__class = _class_0 | ||
1542 | if _parent_0.__inherited then | ||
1543 | _parent_0.__inherited(_parent_0, _class_0) | ||
1544 | end | ||
1545 | Cupboard = _class_0 | ||
1546 | end | ||
1547 | local MyClass | ||
1548 | do | ||
1549 | local _class_0 | ||
1550 | local _parent_0 = ParentClass | ||
1551 | local _base_0 = { | ||
1552 | a_method = function(self) | ||
1553 | _class_0.__parent.__base.a_method(self, "hello", "world") | ||
1554 | _class_0.__parent.a_method(self, "hello", "world") | ||
1555 | _class_0.__parent.a_method(self, "hello", "world") | ||
1556 | return assert(_class_0.__parent == ParentClass) | ||
1557 | end | ||
1558 | } | ||
1559 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1560 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1561 | _base_0[_key_0] = _val_0 | ||
1562 | end | ||
1563 | end | ||
1564 | if _base_0.__index == nil then | ||
1565 | _base_0.__index = _base_0 | ||
1566 | end | ||
1567 | setmetatable(_base_0, _parent_0.__base) | ||
1568 | _class_0 = setmetatable({ | ||
1569 | __init = function(self, ...) | ||
1570 | return _class_0.__parent.__init(self, ...) | ||
1571 | end, | ||
1572 | __base = _base_0, | ||
1573 | __name = "MyClass", | ||
1574 | __parent = _parent_0 | ||
1575 | }, { | ||
1576 | __index = function(cls, name) | ||
1577 | local val = rawget(_base_0, name) | ||
1578 | if val == nil then | ||
1579 | local parent = rawget(cls, "__parent") | ||
1580 | if parent then | ||
1581 | return parent[name] | ||
1582 | end | ||
1583 | else | ||
1584 | return val | ||
1585 | end | ||
1586 | end, | ||
1587 | __call = function(cls, ...) | ||
1588 | local _self_0 = setmetatable({ }, _base_0) | ||
1589 | cls.__init(_self_0, ...) | ||
1590 | return _self_0 | ||
1591 | end | ||
1592 | }) | ||
1593 | _base_0.__class = _class_0 | ||
1594 | if _parent_0.__inherited then | ||
1595 | _parent_0.__inherited(_parent_0, _class_0) | ||
1596 | end | ||
1597 | MyClass = _class_0 | ||
1598 | end | ||
1599 | local b = BackPack() | ||
1600 | assert(b.__class == BackPack) | ||
1601 | print(BackPack.size) | ||
1602 | print(BackPack.__name) | ||
1603 | local Things | ||
1604 | do | ||
1605 | local _class_0 | ||
1606 | local _base_0 = { } | ||
1607 | if _base_0.__index == nil then | ||
1608 | _base_0.__index = _base_0 | ||
1609 | end | ||
1610 | _class_0 = setmetatable({ | ||
1611 | __init = function() end, | ||
1612 | __base = _base_0, | ||
1613 | __name = "Things" | ||
1614 | }, { | ||
1615 | __index = _base_0, | ||
1616 | __call = function(cls, ...) | ||
1617 | local _self_0 = setmetatable({ }, _base_0) | ||
1618 | cls.__init(_self_0, ...) | ||
1619 | return _self_0 | ||
1620 | end | ||
1621 | }) | ||
1622 | _base_0.__class = _class_0 | ||
1623 | local self = _class_0; | ||
1624 | self.some_func = function(self) | ||
1625 | return print("Hello from", self.__name) | ||
1626 | end | ||
1627 | Things = _class_0 | ||
1628 | end | ||
1629 | Things:some_func() | ||
1630 | assert(Things().some_func == nil) | ||
1631 | local Counter | ||
1632 | do | ||
1633 | local _class_0 | ||
1634 | local _base_0 = { } | ||
1635 | if _base_0.__index == nil then | ||
1636 | _base_0.__index = _base_0 | ||
1637 | end | ||
1638 | _class_0 = setmetatable({ | ||
1639 | __init = function(self) | ||
1640 | self.__class.count = self.__class.count + 1 | ||
1641 | end, | ||
1642 | __base = _base_0, | ||
1643 | __name = "Counter" | ||
1644 | }, { | ||
1645 | __index = _base_0, | ||
1646 | __call = function(cls, ...) | ||
1647 | local _self_0 = setmetatable({ }, _base_0) | ||
1648 | cls.__init(_self_0, ...) | ||
1649 | return _self_0 | ||
1650 | end | ||
1651 | }) | ||
1652 | _base_0.__class = _class_0 | ||
1653 | local self = _class_0; | ||
1654 | self.count = 0 | ||
1655 | Counter = _class_0 | ||
1656 | end | ||
1657 | Counter() | ||
1658 | Counter() | ||
1659 | print(Counter.count) | ||
1660 | self.__class:hello(1, 2, 3, 4) | ||
1661 | local Things | ||
1662 | do | ||
1663 | local _class_0 | ||
1664 | local _base_0 = { } | ||
1665 | if _base_0.__index == nil then | ||
1666 | _base_0.__index = _base_0 | ||
1667 | end | ||
1668 | _class_0 = setmetatable({ | ||
1669 | __init = function() end, | ||
1670 | __base = _base_0, | ||
1671 | __name = "Things" | ||
1672 | }, { | ||
1673 | __index = _base_0, | ||
1674 | __call = function(cls, ...) | ||
1675 | local _self_0 = setmetatable({ }, _base_0) | ||
1676 | cls.__init(_self_0, ...) | ||
1677 | return _self_0 | ||
1678 | end | ||
1679 | }) | ||
1680 | _base_0.__class = _class_0 | ||
1681 | local self = _class_0; | ||
1682 | self.class_var = "hello world" | ||
1683 | Things = _class_0 | ||
1684 | end | ||
1685 | local MoreThings | ||
1686 | do | ||
1687 | local _class_0 | ||
1688 | local secret, log | ||
1689 | local _base_0 = { | ||
1690 | some_method = function(self) | ||
1691 | return log("hello world: " .. secret) | ||
1692 | end | ||
1693 | } | ||
1694 | if _base_0.__index == nil then | ||
1695 | _base_0.__index = _base_0 | ||
1696 | end | ||
1697 | _class_0 = setmetatable({ | ||
1698 | __init = function() end, | ||
1699 | __base = _base_0, | ||
1700 | __name = "MoreThings" | ||
1701 | }, { | ||
1702 | __index = _base_0, | ||
1703 | __call = function(cls, ...) | ||
1704 | local _self_0 = setmetatable({ }, _base_0) | ||
1705 | cls.__init(_self_0, ...) | ||
1706 | return _self_0 | ||
1707 | end | ||
1708 | }) | ||
1709 | _base_0.__class = _class_0 | ||
1710 | local self = _class_0; | ||
1711 | secret = 123 | ||
1712 | log = function(msg) | ||
1713 | return print("LOG:", msg) | ||
1714 | end | ||
1715 | MoreThings = _class_0 | ||
1716 | end | ||
1717 | assert(self == self) | ||
1718 | assert(self.__class == self.__class) | ||
1719 | local some_instance_method | ||
1720 | some_instance_method = function(self, ...) | ||
1721 | return self.__class(...) | ||
1722 | end | ||
1723 | local Something | ||
1724 | do | ||
1725 | local _class_0 | ||
1726 | local _base_0 = { } | ||
1727 | if _base_0.__index == nil then | ||
1728 | _base_0.__index = _base_0 | ||
1729 | end | ||
1730 | _class_0 = setmetatable({ | ||
1731 | __init = function(self, foo, bar, biz, baz) | ||
1732 | self.foo = foo | ||
1733 | self.bar = bar | ||
1734 | self.__class.biz = biz | ||
1735 | self.__class.baz = baz | ||
1736 | end, | ||
1737 | __base = _base_0, | ||
1738 | __name = "Something" | ||
1739 | }, { | ||
1740 | __index = _base_0, | ||
1741 | __call = function(cls, ...) | ||
1742 | local _self_0 = setmetatable({ }, _base_0) | ||
1743 | cls.__init(_self_0, ...) | ||
1744 | return _self_0 | ||
1745 | end | ||
1746 | }) | ||
1747 | _base_0.__class = _class_0 | ||
1748 | Something = _class_0 | ||
1749 | end | ||
1750 | do | ||
1751 | local _class_0 | ||
1752 | local _base_0 = { } | ||
1753 | if _base_0.__index == nil then | ||
1754 | _base_0.__index = _base_0 | ||
1755 | end | ||
1756 | _class_0 = setmetatable({ | ||
1757 | __init = function(self, foo, bar, biz, baz) | ||
1758 | self.foo = foo | ||
1759 | self.bar = bar | ||
1760 | self.__class.biz = biz | ||
1761 | self.__class.baz = baz | ||
1762 | end, | ||
1763 | __base = _base_0, | ||
1764 | __name = "Something" | ||
1765 | }, { | ||
1766 | __index = _base_0, | ||
1767 | __call = function(cls, ...) | ||
1768 | local _self_0 = setmetatable({ }, _base_0) | ||
1769 | cls.__init(_self_0, ...) | ||
1770 | return _self_0 | ||
1771 | end | ||
1772 | }) | ||
1773 | _base_0.__class = _class_0 | ||
1774 | Something = _class_0 | ||
1775 | end | ||
1776 | local new | ||
1777 | new = function(self, fieldA, fieldB) | ||
1778 | self.fieldA = fieldA | ||
1779 | self.fieldB = fieldB | ||
1780 | return self | ||
1781 | end | ||
1782 | local obj = new({ }, 123, "abc") | ||
1783 | print(obj) | ||
1784 | local x | ||
1785 | local Bucket | ||
1786 | do | ||
1787 | local _class_0 | ||
1788 | local _base_0 = { | ||
1789 | drops = 0, | ||
1790 | add_drop = function(self) | ||
1791 | self.drops = self.drops + 1 | ||
1792 | end | ||
1793 | } | ||
1794 | if _base_0.__index == nil then | ||
1795 | _base_0.__index = _base_0 | ||
1796 | end | ||
1797 | _class_0 = setmetatable({ | ||
1798 | __init = function() end, | ||
1799 | __base = _base_0, | ||
1800 | __name = "Bucket" | ||
1801 | }, { | ||
1802 | __index = _base_0, | ||
1803 | __call = function(cls, ...) | ||
1804 | local _self_0 = setmetatable({ }, _base_0) | ||
1805 | cls.__init(_self_0, ...) | ||
1806 | return _self_0 | ||
1807 | end | ||
1808 | }) | ||
1809 | _base_0.__class = _class_0 | ||
1810 | Bucket = _class_0 | ||
1811 | x = _class_0 | ||
1812 | end | ||
1813 | local BigBucket | ||
1814 | do | ||
1815 | local _class_0 | ||
1816 | local _parent_0 = Bucket | ||
1817 | local _base_0 = { | ||
1818 | add_drop = function(self) | ||
1819 | self.drops = self.drops + 10 | ||
1820 | end | ||
1821 | } | ||
1822 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1823 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1824 | _base_0[_key_0] = _val_0 | ||
1825 | end | ||
1826 | end | ||
1827 | if _base_0.__index == nil then | ||
1828 | _base_0.__index = _base_0 | ||
1829 | end | ||
1830 | setmetatable(_base_0, _parent_0.__base) | ||
1831 | _class_0 = setmetatable({ | ||
1832 | __init = function(self, ...) | ||
1833 | return _class_0.__parent.__init(self, ...) | ||
1834 | end, | ||
1835 | __base = _base_0, | ||
1836 | __name = "BigBucket", | ||
1837 | __parent = _parent_0 | ||
1838 | }, { | ||
1839 | __index = function(cls, name) | ||
1840 | local val = rawget(_base_0, name) | ||
1841 | if val == nil then | ||
1842 | local parent = rawget(cls, "__parent") | ||
1843 | if parent then | ||
1844 | return parent[name] | ||
1845 | end | ||
1846 | else | ||
1847 | return val | ||
1848 | end | ||
1849 | end, | ||
1850 | __call = function(cls, ...) | ||
1851 | local _self_0 = setmetatable({ }, _base_0) | ||
1852 | cls.__init(_self_0, ...) | ||
1853 | return _self_0 | ||
1854 | end | ||
1855 | }) | ||
1856 | _base_0.__class = _class_0 | ||
1857 | if _parent_0.__inherited then | ||
1858 | _parent_0.__inherited(_parent_0, _class_0) | ||
1859 | end | ||
1860 | BigBucket = _class_0 | ||
1861 | end | ||
1862 | assert(Bucket.__name == "BigBucket") | ||
1863 | local x | ||
1864 | do | ||
1865 | local _class_0 | ||
1866 | local _base_0 = { } | ||
1867 | if _base_0.__index == nil then | ||
1868 | _base_0.__index = _base_0 | ||
1869 | end | ||
1870 | _class_0 = setmetatable({ | ||
1871 | __init = function() end, | ||
1872 | __base = _base_0, | ||
1873 | __name = "x" | ||
1874 | }, { | ||
1875 | __index = _base_0, | ||
1876 | __call = function(cls, ...) | ||
1877 | local _self_0 = setmetatable({ }, _base_0) | ||
1878 | cls.__init(_self_0, ...) | ||
1879 | return _self_0 | ||
1880 | end | ||
1881 | }) | ||
1882 | _base_0.__class = _class_0 | ||
1883 | x = _class_0 | ||
1884 | end | ||
1885 | local MyIndex = { | ||
1886 | __index = { | ||
1887 | var = 1 | ||
1888 | } | ||
1889 | } | ||
1890 | local X | ||
1891 | do | ||
1892 | local _class_0 | ||
1893 | local _base_0 = { | ||
1894 | func = function(self) | ||
1895 | return print(123) | ||
1896 | end | ||
1897 | } | ||
1898 | local _list_0 = { | ||
1899 | MyIndex | ||
1900 | } | ||
1901 | for _index_0 = 1, #_list_0 do | ||
1902 | local _item_0 = _list_0[_index_0] | ||
1903 | local _cls_0, _mixin_0 = (_item_0.__base ~= nil), _item_0.__base or _item_0 | ||
1904 | for _key_0, _val_0 in pairs(_mixin_0) do | ||
1905 | if _base_0[_key_0] == nil and (not _cls_0 or not _key_0:match("^__")) then | ||
1906 | _base_0[_key_0] = _val_0 | ||
1907 | end | ||
1908 | end | ||
1909 | end | ||
1910 | if _base_0.__index == nil then | ||
1911 | _base_0.__index = _base_0 | ||
1912 | end | ||
1913 | _class_0 = setmetatable({ | ||
1914 | __init = function() end, | ||
1915 | __base = _base_0, | ||
1916 | __name = "X" | ||
1917 | }, { | ||
1918 | __index = _base_0, | ||
1919 | __call = function(cls, ...) | ||
1920 | local _self_0 = setmetatable({ }, _base_0) | ||
1921 | cls.__init(_self_0, ...) | ||
1922 | return _self_0 | ||
1923 | end | ||
1924 | }) | ||
1925 | _base_0.__class = _class_0 | ||
1926 | X = _class_0 | ||
1927 | end | ||
1928 | local x = X() | ||
1929 | print(x.var) | ||
1930 | local Y | ||
1931 | do | ||
1932 | local _class_0 | ||
1933 | local _base_0 = { } | ||
1934 | local _list_0 = { | ||
1935 | X | ||
1936 | } | ||
1937 | for _index_0 = 1, #_list_0 do | ||
1938 | local _item_0 = _list_0[_index_0] | ||
1939 | local _cls_0, _mixin_0 = (_item_0.__base ~= nil), _item_0.__base or _item_0 | ||
1940 | for _key_0, _val_0 in pairs(_mixin_0) do | ||
1941 | if _base_0[_key_0] == nil and (not _cls_0 or not _key_0:match("^__")) then | ||
1942 | _base_0[_key_0] = _val_0 | ||
1943 | end | ||
1944 | end | ||
1945 | end | ||
1946 | if _base_0.__index == nil then | ||
1947 | _base_0.__index = _base_0 | ||
1948 | end | ||
1949 | _class_0 = setmetatable({ | ||
1950 | __init = function() end, | ||
1951 | __base = _base_0, | ||
1952 | __name = "Y" | ||
1953 | }, { | ||
1954 | __index = _base_0, | ||
1955 | __call = function(cls, ...) | ||
1956 | local _self_0 = setmetatable({ }, _base_0) | ||
1957 | cls.__init(_self_0, ...) | ||
1958 | return _self_0 | ||
1959 | end | ||
1960 | }) | ||
1961 | _base_0.__class = _class_0 | ||
1962 | Y = _class_0 | ||
1963 | end | ||
1964 | local y = Y() | ||
1965 | y:func() | ||
1966 | assert(y.__class.__parent ~= X) | ||
1967 | do | ||
1968 | local _with_0 = Person() | ||
1969 | _with_0.name = "Oswald" | ||
1970 | _with_0:add_relative(my_dad) | ||
1971 | _with_0:save() | ||
1972 | print(_with_0.name) | ||
1973 | end | ||
1974 | local file | ||
1975 | do | ||
1976 | local _with_0 = File("favorite_foods.txt") | ||
1977 | _with_0:set_encoding("utf8") | ||
1978 | file = _with_0 | ||
1979 | end | ||
1980 | local create_person | ||
1981 | create_person = function(name, relatives) | ||
1982 | local _with_0 = Person() | ||
1983 | _with_0.name = name | ||
1984 | for _index_0 = 1, #relatives do | ||
1985 | local relative = relatives[_index_0] | ||
1986 | _with_0:add_relative(relative) | ||
1987 | end | ||
1988 | return _with_0 | ||
1989 | end | ||
1990 | local me = create_person("Leaf", { | ||
1991 | dad, | ||
1992 | mother, | ||
1993 | sister | ||
1994 | }) | ||
1995 | do | ||
1996 | local str = "Hello" | ||
1997 | print("original:", str) | ||
1998 | print("upper:", str:upper()) | ||
1999 | end | ||
2000 | do | ||
2001 | local _with_0 = tb | ||
2002 | _with_0[1] = 1 | ||
2003 | print(_with_0[2]) | ||
2004 | do | ||
2005 | local _with_1 = _with_0[abc] | ||
2006 | _with_1[3] = _with_1[2]:func() | ||
2007 | _with_1["key-name"] = value | ||
2008 | end | ||
2009 | _with_0[#_with_0 + 1] = "abc" | ||
2010 | end | ||
2011 | do | ||
2012 | local var = "hello" | ||
2013 | print(var) | ||
2014 | end | ||
2015 | print(var) | ||
2016 | local counter | ||
2017 | do | ||
2018 | local i = 0 | ||
2019 | counter = function() | ||
2020 | i = i + 1 | ||
2021 | return i | ||
2022 | end | ||
2023 | end | ||
2024 | print(counter()) | ||
2025 | print(counter()) | ||
2026 | local tbl = { | ||
2027 | key = (function() | ||
2028 | print("assigning key!") | ||
2029 | return 1234 | ||
2030 | end)() | ||
2031 | } | ||
2032 | local my_object = { | ||
2033 | value = 1000, | ||
2034 | write = function(self) | ||
2035 | return print("the value:", self.value) | ||
2036 | end | ||
2037 | } | ||
2038 | local run_callback | ||
2039 | run_callback = function(func) | ||
2040 | print("running callback...") | ||
2041 | return func() | ||
2042 | end | ||
2043 | run_callback(my_object.write) | ||
2044 | run_callback((function() | ||
2045 | local _base_0 = my_object | ||
2046 | local _fn_0 = _base_0.write | ||
2047 | return _fn_0 and function(...) | ||
2048 | return _fn_0(_base_0, ...) | ||
2049 | end | ||
2050 | end)()) | ||
2051 | local i = 100 | ||
2052 | local my_func | ||
2053 | my_func = function() | ||
2054 | i = 10 | ||
2055 | while i > 0 do | ||
2056 | print(i) | ||
2057 | i = i - 1 | ||
2058 | end | ||
2059 | end | ||
2060 | my_func() | ||
2061 | print(i) | ||
2062 | local i = 100 | ||
2063 | local my_func | ||
2064 | my_func = function() | ||
2065 | local i = "hello" | ||
2066 | end | ||
2067 | my_func() | ||
2068 | print(i) | ||
2069 | local tmp = 1213 | ||
2070 | local i, k = 100, 50 | ||
2071 | local my_func | ||
2072 | my_func = function(add) | ||
2073 | local tmp = tmp + add | ||
2074 | i = i + tmp | ||
2075 | k = k + tmp | ||
2076 | end | ||
2077 | my_func(22) | ||
2078 | print(i, k) | ||
2079 | local _module_0 = { } | ||
2080 | local p, to_lua | ||
2081 | do | ||
2082 | local _obj_0 = require("yue") | ||
2083 | p, to_lua = _obj_0.p, _obj_0.to_lua | ||
2084 | end | ||
2085 | local inventory = { | ||
2086 | equipment = { | ||
2087 | "sword", | ||
2088 | "shield" | ||
2089 | }, | ||
2090 | items = { | ||
2091 | { | ||
2092 | name = "potion", | ||
2093 | count = 10 | ||
2094 | }, | ||
2095 | { | ||
2096 | name = "bread", | ||
2097 | count = 3 | ||
2098 | } | ||
2099 | } | ||
2100 | } | ||
2101 | print(reduce(filter(map({ | ||
2102 | 1, | ||
2103 | 2, | ||
2104 | 3 | ||
2105 | }, function(x) | ||
2106 | return x * 2 | ||
2107 | end), function(x) | ||
2108 | return x > 4 | ||
2109 | end), 0, function(a, b) | ||
2110 | return a + b | ||
2111 | end)) | ||
2112 | local apple = setmetatable({ | ||
2113 | size = 15, | ||
2114 | }, { | ||
2115 | __index = { | ||
2116 | color = 0x00ffff | ||
2117 | } | ||
2118 | }) | ||
2119 | if (getmetatable(apple) ~= nil) then | ||
2120 | p(apple.color, getmetatable(apple).__index) | ||
2121 | end | ||
2122 | local _ud83c_udf1b = "ζδΉθζ¬" | ||
2123 | _module_0["π"] = _ud83c_udf1b | ||
2124 | return _module_0 | ||
2125 | local area = 6.2831853071796 * 5 | ||
2126 | print('hello world') | ||
2127 | assert(item ~= nil) | ||
2128 | local value = item | ||
2129 | if (f1() and f2() and f3()) then | ||
2130 | print("OK") | ||
2131 | end | ||
2132 | local funcA | ||
2133 | funcA = function() end | ||
2134 | funcA = function() | ||
2135 | return "assign the Yue defined variable" | ||
2136 | end | ||
2137 | local function funcB() end | ||
2138 | funcB = function() | ||
2139 | return "assign the Lua defined variable" | ||
2140 | end | ||
2141 | -- raw Lua codes insertion | ||
2142 | if cond then | ||
2143 | print("output") | ||
2144 | end | ||
2145 | print("yuescript") | ||
2146 | print(3) | ||
2147 | if tb ~= nil then | ||
2148 | tb:func() | ||
2149 | end | ||
2150 | if tb ~= nil then | ||
2151 | tb:func() | ||
2152 | end | ||
2153 | print(1 < 2 and 2 <= 2 and 2 < 3 and 3 == 3 and 3 > 2 and 2 >= 1 and 1 == 1 and 1 < 3 and 3 ~= 5) | ||
2154 | local a = 5 | ||
2155 | print(1 <= a and a <= 10) | ||
2156 | local v | ||
2157 | v = function(x) | ||
2158 | print(x) | ||
2159 | return x | ||
2160 | end | ||
2161 | print((function() | ||
2162 | local _cond_0 = v(2) | ||
2163 | if not (v(1) < _cond_0) then | ||
2164 | return false | ||
2165 | else | ||
2166 | return _cond_0 <= v(3) | ||
2167 | end | ||
2168 | end)()) | ||
2169 | print((function() | ||
2170 | local _cond_0 = v(2) | ||
2171 | if not (v(1) > _cond_0) then | ||
2172 | return false | ||
2173 | else | ||
2174 | return _cond_0 <= v(3) | ||
2175 | end | ||
2176 | end)()) | ||
2177 | local tab = { } | ||
2178 | tab[#tab + 1] = "Value" | ||
2179 | local parts = { | ||
2180 | "shoulders", | ||
2181 | "knees" | ||
2182 | } | ||
2183 | local lyrics | ||
2184 | do | ||
2185 | local _tab_0 = { | ||
2186 | "head" | ||
2187 | } | ||
2188 | local _idx_0 = 1 | ||
2189 | for _key_0, _value_0 in pairs(parts) do | ||
2190 | if _idx_0 == _key_0 then | ||
2191 | _tab_0[#_tab_0 + 1] = _value_0 | ||
2192 | _idx_0 = _idx_0 + 1 | ||
2193 | else | ||
2194 | _tab_0[_key_0] = _value_0 | ||
2195 | end | ||
2196 | end | ||
2197 | _tab_0[#_tab_0 + 1] = "and" | ||
2198 | _tab_0[#_tab_0 + 1] = "toes" | ||
2199 | lyrics = _tab_0 | ||
2200 | end | ||
2201 | local copy | ||
2202 | do | ||
2203 | local _tab_0 = { } | ||
2204 | local _idx_0 = 1 | ||
2205 | for _key_0, _value_0 in pairs(other) do | ||
2206 | if _idx_0 == _key_0 then | ||
2207 | _tab_0[#_tab_0 + 1] = _value_0 | ||
2208 | _idx_0 = _idx_0 + 1 | ||
2209 | else | ||
2210 | _tab_0[_key_0] = _value_0 | ||
2211 | end | ||
2212 | end | ||
2213 | copy = _tab_0 | ||
2214 | end | ||
2215 | local a = { | ||
2216 | 1, | ||
2217 | 2, | ||
2218 | 3, | ||
2219 | x = 1 | ||
2220 | } | ||
2221 | local b = { | ||
2222 | 4, | ||
2223 | 5, | ||
2224 | y = 1 | ||
2225 | } | ||
2226 | local merge | ||
2227 | do | ||
2228 | local _tab_0 = { } | ||
2229 | local _idx_0 = 1 | ||
2230 | for _key_0, _value_0 in pairs(a) do | ||
2231 | if _idx_0 == _key_0 then | ||
2232 | _tab_0[#_tab_0 + 1] = _value_0 | ||
2233 | _idx_0 = _idx_0 + 1 | ||
2234 | else | ||
2235 | _tab_0[_key_0] = _value_0 | ||
2236 | end | ||
2237 | end | ||
2238 | local _idx_1 = 1 | ||
2239 | for _key_0, _value_0 in pairs(b) do | ||
2240 | if _idx_1 == _key_0 then | ||
2241 | _tab_0[#_tab_0 + 1] = _value_0 | ||
2242 | _idx_1 = _idx_1 + 1 | ||
2243 | else | ||
2244 | _tab_0[_key_0] = _value_0 | ||
2245 | end | ||
2246 | end | ||
2247 | merge = _tab_0 | ||
2248 | end | ||
2249 | local mt = { } | ||
2250 | local add | ||
2251 | add = function(self, right) | ||
2252 | return setmetatable({ | ||
2253 | value = self.value + right.value | ||
2254 | }, mt) | ||
2255 | end | ||
2256 | mt.__add = add | ||
2257 | local a = setmetatable({ | ||
2258 | value = 1 | ||
2259 | }, mt) | ||
2260 | local b = setmetatable({ | ||
2261 | value = 2 | ||
2262 | }, { | ||
2263 | __add = add | ||
2264 | }) | ||
2265 | local c = setmetatable({ | ||
2266 | value = 3 | ||
2267 | }, { | ||
2268 | __add = mt.__add | ||
2269 | }) | ||
2270 | local d = a + b + c | ||
2271 | print(d.value) | ||
2272 | local _ <close> = setmetatable({ }, { | ||
2273 | __close = function() | ||
2274 | return print("out of scope") | ||
2275 | end | ||
2276 | }) | ||
2277 | local tb = setmetatable({ }, { | ||
2278 | ["value"] = 123 | ||
2279 | }) | ||
2280 | getmetatable(tb).__index = getmetatable(tb) | ||
2281 | print(tb.value) | ||
2282 | setmetatable(tb, { | ||
2283 | __index = { | ||
2284 | item = "hello" | ||
2285 | } | ||
2286 | }) | ||
2287 | print(tb.item) | ||
2288 | local item, new, close, getter | ||
2289 | do | ||
2290 | local _obj_0 = tb | ||
2291 | item, new = _obj_0[1], _obj_0.new | ||
2292 | do | ||
2293 | local _obj_1 = getmetatable(_obj_0) | ||
2294 | close, getter = _obj_1.__close, _obj_1.__index | ||
2295 | end | ||
2296 | end | ||
2297 | print(item, new, close, getter) | ||
2298 | do | ||
2299 | local _obj_0 = func | ||
2300 | if _obj_0 ~= nil then | ||
2301 | _obj_0() | ||
2302 | end | ||
2303 | end | ||
2304 | print((function() | ||
2305 | local _obj_0 = abc | ||
2306 | if _obj_0 ~= nil then | ||
2307 | local _obj_1 = _obj_0["hello world"] | ||
2308 | if _obj_1 ~= nil then | ||
2309 | return _obj_1.xyz | ||
2310 | end | ||
2311 | return nil | ||
2312 | end | ||
2313 | return nil | ||
2314 | end)()) | ||
2315 | local x | ||
2316 | do | ||
2317 | local _obj_0 = tab | ||
2318 | if _obj_0 ~= nil then | ||
2319 | x = _obj_0.value | ||
2320 | end | ||
2321 | end | ||
2322 | local len = (function() | ||
2323 | local _obj_0 = utf8 | ||
2324 | if _obj_0 ~= nil then | ||
2325 | return _obj_0.len | ||
2326 | end | ||
2327 | return nil | ||
2328 | end)() or (function() | ||
2329 | local _obj_0 = string | ||
2330 | if _obj_0 ~= nil then | ||
2331 | return _obj_0.len | ||
2332 | end | ||
2333 | return nil | ||
2334 | end)() or function(o) | ||
2335 | return #o | ||
2336 | end | ||
2337 | if print and (x ~= nil) then | ||
2338 | print(x) | ||
2339 | end | ||
2340 | do | ||
2341 | local _with_0 = io.open("test.txt", "w") | ||
2342 | if _with_0 ~= nil then | ||
2343 | _with_0:write("hello") | ||
2344 | _with_0:close() | ||
2345 | end | ||
2346 | end | ||
2347 | print("hello") | ||
2348 | print(1, 2) | ||
2349 | print(1, 2, 3) | ||
2350 | print(render(emit(parse(extract(readFile("example.txt"), language, { }), language)))) | ||
2351 | local a, b, c, d | ||
2352 | if b ~= nil then | ||
2353 | a = b | ||
2354 | else | ||
2355 | if c ~= nil then | ||
2356 | a = c | ||
2357 | else | ||
2358 | a = d | ||
2359 | end | ||
2360 | end | ||
2361 | func((function() | ||
2362 | if a ~= nil then | ||
2363 | return a | ||
2364 | else | ||
2365 | return { } | ||
2366 | end | ||
2367 | end)()) | ||
2368 | if a == nil then | ||
2369 | a = false | ||
2370 | end | ||
2371 | local list = { | ||
2372 | 1, | ||
2373 | 2, | ||
2374 | 3 | ||
2375 | } | ||
2376 | func({ | ||
2377 | 1, | ||
2378 | 2, | ||
2379 | 3 | ||
2380 | }) | ||
2381 | local tb = { | ||
2382 | name = "abc", | ||
2383 | values = { | ||
2384 | "a", | ||
2385 | "b", | ||
2386 | "c" | ||
2387 | }, | ||
2388 | objects = { | ||
2389 | { | ||
2390 | name = "a", | ||
2391 | value = 1, | ||
2392 | func = function(self) | ||
2393 | return self.value + 1 | ||
2394 | end, | ||
2395 | tb = { | ||
2396 | fieldA = 1 | ||
2397 | } | ||
2398 | }, | ||
2399 | { | ||
2400 | name = "b", | ||
2401 | value = 2, | ||
2402 | func = function(self) | ||
2403 | return self.value + 2 | ||
2404 | end, | ||
2405 | tb = { } | ||
2406 | } | ||
2407 | } | ||
2408 | } | ||
2409 | do | ||
2410 | local insert, concat = table.insert, table.concat | ||
2411 | local C, Ct, Cmt | ||
2412 | do | ||
2413 | local _obj_0 = require("lpeg") | ||
2414 | C, Ct, Cmt = _obj_0.C, _obj_0.Ct, _obj_0.Cmt | ||
2415 | end | ||
2416 | local x, y, z | ||
2417 | do | ||
2418 | local _obj_0 = require('mymodule') | ||
2419 | x, y, z = _obj_0.x, _obj_0.y, _obj_0.z | ||
2420 | end | ||
2421 | local a, b, c | ||
2422 | do | ||
2423 | local _obj_0 = require('module') | ||
2424 | a, b, c = _obj_0.a, _obj_0.b, _obj_0.c | ||
2425 | end | ||
2426 | end | ||
2427 | do | ||
2428 | local module = require('module') | ||
2429 | local module_x = require('module_x') | ||
2430 | local d_a_s_h_e_s = require("d-a-s-h-e-s") | ||
2431 | local part = require("module.part") | ||
2432 | end | ||
2433 | do | ||
2434 | local PlayerModule = require("player") | ||
2435 | local C, Ct, Cmt | ||
2436 | do | ||
2437 | local _obj_0 = require("lpeg") | ||
2438 | C, Ct, Cmt = _obj_0.C, _obj_0.Ct, _obj_0.Cmt | ||
2439 | end | ||
2440 | local one, two, ch | ||
2441 | do | ||
2442 | local _obj_0 = require("export") | ||
2443 | one, two, ch = _obj_0[1], _obj_0[2], _obj_0.Something.umm[1] | ||
2444 | end | ||
2445 | end | ||
2446 | local _module_0 = { } | ||
2447 | local a, b, c = 1, 2, 3 | ||
2448 | _module_0["a"], _module_0["b"], _module_0["c"] = a, b, c | ||
2449 | local cool = "cat" | ||
2450 | _module_0["cool"] = cool | ||
2451 | local What | ||
2452 | if this then | ||
2453 | What = "abc" | ||
2454 | else | ||
2455 | What = "def" | ||
2456 | end | ||
2457 | _module_0["What"] = What | ||
2458 | local y | ||
2459 | y = function() | ||
2460 | local hallo = 3434 | ||
2461 | end | ||
2462 | _module_0["y"] = y | ||
2463 | local Something | ||
2464 | do | ||
2465 | local _class_0 | ||
2466 | local _base_0 = { | ||
2467 | umm = "cool" | ||
2468 | } | ||
2469 | if _base_0.__index == nil then | ||
2470 | _base_0.__index = _base_0 | ||
2471 | end | ||
2472 | _class_0 = setmetatable({ | ||
2473 | __init = function() end, | ||
2474 | __base = _base_0, | ||
2475 | __name = "Something" | ||
2476 | }, { | ||
2477 | __index = _base_0, | ||
2478 | __call = function(cls, ...) | ||
2479 | local _self_0 = setmetatable({ }, _base_0) | ||
2480 | cls.__init(_self_0, ...) | ||
2481 | return _self_0 | ||
2482 | end | ||
2483 | }) | ||
2484 | _base_0.__class = _class_0 | ||
2485 | Something = _class_0 | ||
2486 | end | ||
2487 | _module_0["Something"] = Something | ||
2488 | return _module_0 | ||
2489 | local _module_0 = { } | ||
2490 | local loadstring, tolua | ||
2491 | do | ||
2492 | local _obj_0 = yue | ||
2493 | loadstring, tolua = _obj_0.loadstring, _obj_0.to_lua | ||
2494 | end | ||
2495 | _module_0["loadstring"], _module_0["tolua"] = loadstring, tolua | ||
2496 | local fieldA = tb.itemA.fieldA | ||
2497 | if fieldA == nil then | ||
2498 | fieldA = 'default' | ||
2499 | end | ||
2500 | _module_0["fieldA"] = fieldA | ||
2501 | return _module_0 | ||
2502 | local _module_0 = setmetatable({ }, { }) | ||
2503 | _module_0.itemA = tb | ||
2504 | getmetatable(_module_0).__index = items | ||
2505 | _module_0["a-b-c"] = 123 | ||
2506 | return _module_0 | ||
2507 | local _module_0 = { } | ||
2508 | local d, e, f = 3, 2, 1 | ||
2509 | _module_0[#_module_0 + 1] = d | ||
2510 | _module_0[#_module_0 + 1] = e | ||
2511 | _module_0[#_module_0 + 1] = f | ||
2512 | if this then | ||
2513 | _module_0[#_module_0 + 1] = 123 | ||
2514 | else | ||
2515 | _module_0[#_module_0 + 1] = 456 | ||
2516 | end | ||
2517 | do | ||
2518 | local _with_0 = tmp | ||
2519 | local j = 2000 | ||
2520 | _module_0[#_module_0 + 1] = _with_0 | ||
2521 | end | ||
2522 | return _module_0 | ||
2523 | local _module_0 = nil | ||
2524 | _module_0 = function() | ||
2525 | print("hello") | ||
2526 | return 123 | ||
2527 | end | ||
2528 | return _module_0 | ||
2529 | local hello = "world" | ||
2530 | local a, b, c = 1, 2, 3 | ||
2531 | hello = 123 | ||
2532 | local x = 1 | ||
2533 | x = x + 1 | ||
2534 | x = x - 1 | ||
2535 | x = x * 10 | ||
2536 | x = x / 10 | ||
2537 | x = x % 10 | ||
2538 | local s = s .. "world" | ||
2539 | local arg = arg or "default value" | ||
2540 | local a = 0 | ||
2541 | local b = 0 | ||
2542 | local c = 0 | ||
2543 | local d = 0 | ||
2544 | local e = 0 | ||
2545 | local x = f() | ||
2546 | local y = x | ||
2547 | local z = x | ||
2548 | do | ||
2549 | local a | ||
2550 | a = 1 | ||
2551 | local x, y, z | ||
2552 | print("forward declare all variables as locals") | ||
2553 | x = function() | ||
2554 | return 1 + y + z | ||
2555 | end | ||
2556 | y, z = 2, 3 | ||
2557 | instance = Item:new() | ||
2558 | end | ||
2559 | do | ||
2560 | local X | ||
2561 | X = 1 | ||
2562 | local B | ||
2563 | print("only forward declare upper case variables") | ||
2564 | local a = 1 | ||
2565 | B = 2 | ||
2566 | end | ||
2567 | do | ||
2568 | a = 1 | ||
2569 | print("declare all variables as globals") | ||
2570 | local x | ||
2571 | x = function() | ||
2572 | return 1 + y + z | ||
2573 | end | ||
2574 | local y, z = 2, 3 | ||
2575 | end | ||
2576 | do | ||
2577 | X = 1 | ||
2578 | print("only declare upper case variables as globals") | ||
2579 | local a = 1 | ||
2580 | local B = 2 | ||
2581 | local Temp | ||
2582 | Temp = "a local value" | ||
2583 | end | ||
2584 | local thing = { | ||
2585 | 1, | ||
2586 | 2 | ||
2587 | } | ||
2588 | local a, b = thing[1], thing[2] | ||
2589 | print(a, b) | ||
2590 | local obj = { | ||
2591 | hello = "world", | ||
2592 | day = "tuesday", | ||
2593 | length = 20 | ||
2594 | } | ||
2595 | local hello, the_day = obj.hello, obj.day | ||
2596 | print(hello, the_day) | ||
2597 | local day = obj.day | ||
2598 | local obj2 = { | ||
2599 | numbers = { | ||
2600 | 1, | ||
2601 | 2, | ||
2602 | 3, | ||
2603 | 4 | ||
2604 | }, | ||
2605 | properties = { | ||
2606 | color = "green", | ||
2607 | height = 13.5 | ||
2608 | } | ||
2609 | } | ||
2610 | local first, second = obj2.numbers[1], obj2.numbers[2] | ||
2611 | print(first, second, color) | ||
2612 | local first, second, color | ||
2613 | do | ||
2614 | local _obj_0 = obj2 | ||
2615 | first, second, color = _obj_0.numbers[1], _obj_0.numbers[2], _obj_0.properties.color | ||
2616 | end | ||
2617 | local concat, insert | ||
2618 | do | ||
2619 | local _obj_0 = table | ||
2620 | concat, insert = _obj_0.concat, _obj_0.insert | ||
2621 | end | ||
2622 | local mix, max, rand | ||
2623 | do | ||
2624 | local _obj_0 = math | ||
2625 | mix, max, rand = _obj_0.mix, _obj_0.max, _obj_0.random | ||
2626 | end | ||
2627 | local name, job | ||
2628 | do | ||
2629 | local _obj_0 = person | ||
2630 | name, job = _obj_0.name, _obj_0.job | ||
2631 | if name == nil then | ||
2632 | name = "nameless" | ||
2633 | end | ||
2634 | if job == nil then | ||
2635 | job = "jobless" | ||
2636 | end | ||
2637 | end | ||
2638 | local two, four | ||
2639 | do | ||
2640 | local _obj_0 = items | ||
2641 | two, four = _obj_0[2], _obj_0[4] | ||
2642 | end | ||
2643 | local tuples = { | ||
2644 | { | ||
2645 | "hello", | ||
2646 | "world" | ||
2647 | }, | ||
2648 | { | ||
2649 | "egg", | ||
2650 | "head" | ||
2651 | } | ||
2652 | } | ||
2653 | for _index_0 = 1, #tuples do | ||
2654 | local _des_0 = tuples[_index_0] | ||
2655 | local left, right = _des_0[1], _des_0[2] | ||
2656 | print(left, right) | ||
2657 | end | ||
2658 | do | ||
2659 | local user = database.find_user("moon") | ||
2660 | if user then | ||
2661 | print(user.name) | ||
2662 | end | ||
2663 | end | ||
2664 | do | ||
2665 | local hello = os.getenv("hello") | ||
2666 | if hello then | ||
2667 | print("You have hello", hello) | ||
2668 | else | ||
2669 | do | ||
2670 | local world = os.getenv("world") | ||
2671 | if world then | ||
2672 | print("you have world", world) | ||
2673 | else | ||
2674 | print("nothing :(") | ||
2675 | end | ||
2676 | end | ||
2677 | end | ||
2678 | end | ||
2679 | do | ||
2680 | local success, result = pcall(function() | ||
2681 | return "get result without problems" | ||
2682 | end) | ||
2683 | if success then | ||
2684 | print(result) | ||
2685 | end | ||
2686 | end | ||
2687 | print("OK") | ||
2688 | local list = { | ||
2689 | 1, | ||
2690 | 2, | ||
2691 | 3, | ||
2692 | 4, | ||
2693 | 5 | ||
2694 | } | ||
2695 | local fn | ||
2696 | fn = function(ok) | ||
2697 | return ok, table.unpack(list) | ||
2698 | end | ||
2699 | (function(_arg_0, ...) | ||
2700 | local ok = _arg_0 | ||
2701 | local count = select('#', ...) | ||
2702 | local first = select(1, ...) | ||
2703 | return print(ok, count, first) | ||
2704 | end)(fn(true)) | ||
2705 | Rx.Observable.fromRange(1, 8):filter(function(x) | ||
2706 | return x % 2 == 0 | ||
2707 | end):concat(Rx.Observable.of('who do we appreciate')):map(function(value) | ||
2708 | return value .. '!' | ||
2709 | end):subscribe(print) | ||
2710 | local str = strA .. strB .. strC | ||
2711 | func(3000, "192.168.1.1") | ||
2712 | xpcall(func, function(err) | ||
2713 | return print(yue.traceback(err)) | ||
2714 | end, 1, 2, 3) | ||
2715 | local success, result = xpcall(func, function(err) | ||
2716 | return yue.traceback(err) | ||
2717 | end, 1, 2, 3) | ||
2718 | xpcall(func, function(err) | ||
2719 | return print(yue.traceback(err)) | ||
2720 | end, 1, 2, 3) | ||
2721 | success, result = pcall(func, 1, 2, 3) | ||
2722 | pcall(function() | ||
2723 | print("trying") | ||
2724 | return func(1, 2, 3) | ||
2725 | end) | ||
2726 | success, result = xpcall(func, function(err) | ||
2727 | return print(yue.traceback(err)) | ||
2728 | end, 1, 2, 3) | ||
2729 | if success then | ||
2730 | print(result) | ||
2731 | end | ||
2732 | local a <const> = 123 | ||
2733 | local _ <close> = setmetatable({ }, { | ||
2734 | __close = function() | ||
2735 | return print("Out of scope.") | ||
2736 | end | ||
2737 | }) | ||
2738 | local a, b, c, d | ||
2739 | do | ||
2740 | local _obj_0 = tb | ||
2741 | a, b, c, d = _obj_0.a, _obj_0.b, _obj_0[1], _obj_0[2] | ||
2742 | end | ||
2743 | local some_string = "Here is a string\n that has a line break in it." | ||
2744 | print("I am " .. tostring(math.random() * 100) .. "% sure.") | ||
2745 | local integer = 1000000 | ||
2746 | local hex = 0xEFBBBF | ||
2747 | local my_function | ||
2748 | my_function = function() end | ||
2749 | my_function() | ||
2750 | local func_a | ||
2751 | func_a = function() | ||
2752 | return print("hello world") | ||
2753 | end | ||
2754 | local func_b | ||
2755 | func_b = function() | ||
2756 | local value = 100 | ||
2757 | return print("The value:", value) | ||
2758 | end | ||
2759 | func_a() | ||
2760 | func_b() | ||
2761 | local sum | ||
2762 | sum = function(x, y) | ||
2763 | return print("sum", x + y) | ||
2764 | end | ||
2765 | sum(10, 20) | ||
2766 | print(sum(10, 20)) | ||
2767 | a(b(c("a", "b", "c"))) | ||
2768 | print("x:", sum(10, 20), "y:", sum(30, 40)) | ||
2769 | local sum | ||
2770 | sum = function(x, y) | ||
2771 | return x + y | ||
2772 | end | ||
2773 | print("The sum is ", sum(10, 20)) | ||
2774 | local sum | ||
2775 | sum = function(x, y) | ||
2776 | return x + y | ||
2777 | end | ||
2778 | local mystery | ||
2779 | mystery = function(x, y) | ||
2780 | return x + y, x - y | ||
2781 | end | ||
2782 | local a, b = mystery(10, 20) | ||
2783 | local func | ||
2784 | func = function(self, num) | ||
2785 | return self.value + num | ||
2786 | end | ||
2787 | local my_function | ||
2788 | my_function = function(name, height) | ||
2789 | if name == nil then | ||
2790 | name = "something" | ||
2791 | end | ||
2792 | if height == nil then | ||
2793 | height = 100 | ||
2794 | end | ||
2795 | print("Hello I am", name) | ||
2796 | return print("My height is", height) | ||
2797 | end | ||
2798 | local some_args | ||
2799 | some_args = function(x, y) | ||
2800 | if x == nil then | ||
2801 | x = 100 | ||
2802 | end | ||
2803 | if y == nil then | ||
2804 | y = x + 1000 | ||
2805 | end | ||
2806 | return print(x + y) | ||
2807 | end | ||
2808 | local a = x - 10 | ||
2809 | local b = x - 10 | ||
2810 | local c = x(-y) | ||
2811 | local d = x - z | ||
2812 | local x = func("hello") + 100 | ||
2813 | local y = func("hello" + 100) | ||
2814 | my_func(5, 4, 3, 8, 9, 10) | ||
2815 | cool_func(1, 2, 3, 4, 5, 6, 7, 8) | ||
2816 | my_func(5, 6, 7, 6, another_func(6, 7, 8, 9, 1, 2), 5, 4) | ||
2817 | local x = { | ||
2818 | 1, | ||
2819 | 2, | ||
2820 | 3, | ||
2821 | 4, | ||
2822 | a_func(4, 5, 5, 6), | ||
2823 | 8, | ||
2824 | 9, | ||
2825 | 10 | ||
2826 | } | ||
2827 | local y = { | ||
2828 | my_func(1, 2, 3, 4, 5), | ||
2829 | 5, | ||
2830 | 6, | ||
2831 | 7 | ||
2832 | } | ||
2833 | if func(1, 2, 3, "hello", "world") then | ||
2834 | print("hello") | ||
2835 | print("I am inside if") | ||
2836 | end | ||
2837 | if func(1, 2, 3, "hello", "world") then | ||
2838 | print("hello") | ||
2839 | print("I am inside if") | ||
2840 | end | ||
2841 | f(function() | ||
2842 | return print("hello") | ||
2843 | end) | ||
2844 | f(function(self) | ||
2845 | return print(self.value) | ||
2846 | end) | ||
2847 | map(function(x) | ||
2848 | return x * 2 | ||
2849 | end, { | ||
2850 | 1, | ||
2851 | 2, | ||
2852 | 3 | ||
2853 | }) | ||
2854 | local result, msg | ||
2855 | do | ||
2856 | result, msg = readAsync("filename.txt", function(data) | ||
2857 | print(data) | ||
2858 | return processAsync(data, function(info) | ||
2859 | return check(info) | ||
2860 | end) | ||
2861 | end) | ||
2862 | end | ||
2863 | print(result, msg) | ||
2864 | local some_values = { | ||
2865 | 1, | ||
2866 | 2, | ||
2867 | 3, | ||
2868 | 4 | ||
2869 | } | ||
2870 | local some_values = { | ||
2871 | name = "Bill", | ||
2872 | age = 200, | ||
2873 | ["favorite food"] = "rice" | ||
2874 | } | ||
2875 | local profile = { | ||
2876 | height = "4 feet", | ||
2877 | shoe_size = 13, | ||
2878 | favorite_foods = { | ||
2879 | "ice cream", | ||
2880 | "donuts" | ||
2881 | } | ||
2882 | } | ||
2883 | local values = { | ||
2884 | 1, | ||
2885 | 2, | ||
2886 | 3, | ||
2887 | 4, | ||
2888 | 5, | ||
2889 | 6, | ||
2890 | 7, | ||
2891 | 8, | ||
2892 | name = "superman", | ||
2893 | occupation = "crime fighting" | ||
2894 | } | ||
2895 | my_function({ | ||
2896 | dance = "Tango", | ||
2897 | partner = "none" | ||
2898 | }) | ||
2899 | local y = { | ||
2900 | type = "dog", | ||
2901 | legs = 4, | ||
2902 | tails = 1 | ||
2903 | } | ||
2904 | local tbl = { | ||
2905 | ["do"] = "something", | ||
2906 | ["end"] = "hunger" | ||
2907 | } | ||
2908 | local hair = "golden" | ||
2909 | local height = 200 | ||
2910 | local person = { | ||
2911 | hair = hair, | ||
2912 | height = height, | ||
2913 | shoe_size = 40 | ||
2914 | } | ||
2915 | print_table({ | ||
2916 | hair = hair, | ||
2917 | height = height | ||
2918 | }) | ||
2919 | local t = { | ||
2920 | [1 + 2] = "hello", | ||
2921 | ["hello world"] = true | ||
2922 | } | ||
2923 | local some_values = { | ||
2924 | 1, | ||
2925 | 2, | ||
2926 | 3, | ||
2927 | 4 | ||
2928 | } | ||
2929 | local list_with_one_element = { | ||
2930 | 1 | ||
2931 | } | ||
2932 | local items = { | ||
2933 | 1, | ||
2934 | 2, | ||
2935 | 3, | ||
2936 | 4 | ||
2937 | } | ||
2938 | local doubled | ||
2939 | do | ||
2940 | local _accum_0 = { } | ||
2941 | local _len_0 = 1 | ||
2942 | for i, item in ipairs(items) do | ||
2943 | _accum_0[_len_0] = item * 2 | ||
2944 | _len_0 = _len_0 + 1 | ||
2945 | end | ||
2946 | doubled = _accum_0 | ||
2947 | end | ||
2948 | local iter = ipairs(items) | ||
2949 | local slice | ||
2950 | do | ||
2951 | local _accum_0 = { } | ||
2952 | local _len_0 = 1 | ||
2953 | for i, item in iter do | ||
2954 | if i > 1 and i < 3 then | ||
2955 | _accum_0[_len_0] = item | ||
2956 | _len_0 = _len_0 + 1 | ||
2957 | end | ||
2958 | end | ||
2959 | slice = _accum_0 | ||
2960 | end | ||
2961 | local doubled | ||
2962 | do | ||
2963 | local _accum_0 = { } | ||
2964 | local _len_0 = 1 | ||
2965 | local _list_0 = items | ||
2966 | for _index_0 = 1, #_list_0 do | ||
2967 | local item = _list_0[_index_0] | ||
2968 | _accum_0[_len_0] = item * 2 | ||
2969 | _len_0 = _len_0 + 1 | ||
2970 | end | ||
2971 | doubled = _accum_0 | ||
2972 | end | ||
2973 | local x_coords = { | ||
2974 | 4, | ||
2975 | 5, | ||
2976 | 6, | ||
2977 | 7 | ||
2978 | } | ||
2979 | local y_coords = { | ||
2980 | 9, | ||
2981 | 2, | ||
2982 | 3 | ||
2983 | } | ||
2984 | local points | ||
2985 | do | ||
2986 | local _accum_0 = { } | ||
2987 | local _len_0 = 1 | ||
2988 | for _index_0 = 1, #x_coords do | ||
2989 | local x = x_coords[_index_0] | ||
2990 | for _index_1 = 1, #y_coords do | ||
2991 | local y = y_coords[_index_1] | ||
2992 | _accum_0[_len_0] = { | ||
2993 | x, | ||
2994 | y | ||
2995 | } | ||
2996 | _len_0 = _len_0 + 1 | ||
2997 | end | ||
2998 | end | ||
2999 | points = _accum_0 | ||
3000 | end | ||
3001 | local evens | ||
3002 | do | ||
3003 | local _accum_0 = { } | ||
3004 | local _len_0 = 1 | ||
3005 | for i = 1, 100 do | ||
3006 | if i % 2 == 0 then | ||
3007 | _accum_0[_len_0] = i | ||
3008 | _len_0 = _len_0 + 1 | ||
3009 | end | ||
3010 | end | ||
3011 | evens = _accum_0 | ||
3012 | end | ||
3013 | local thing = { | ||
3014 | color = "red", | ||
3015 | name = "fast", | ||
3016 | width = 123 | ||
3017 | } | ||
3018 | local thing_copy | ||
3019 | do | ||
3020 | local _tbl_0 = { } | ||
3021 | for k, v in pairs(thing) do | ||
3022 | _tbl_0[k] = v | ||
3023 | end | ||
3024 | thing_copy = _tbl_0 | ||
3025 | end | ||
3026 | local no_color | ||
3027 | do | ||
3028 | local _tbl_0 = { } | ||
3029 | for k, v in pairs(thing) do | ||
3030 | if k ~= "color" then | ||
3031 | _tbl_0[k] = v | ||
3032 | end | ||
3033 | end | ||
3034 | no_color = _tbl_0 | ||
3035 | end | ||
3036 | local numbers = { | ||
3037 | 1, | ||
3038 | 2, | ||
3039 | 3, | ||
3040 | 4 | ||
3041 | } | ||
3042 | local sqrts | ||
3043 | do | ||
3044 | local _tbl_0 = { } | ||
3045 | for _index_0 = 1, #numbers do | ||
3046 | local i = numbers[_index_0] | ||
3047 | _tbl_0[i] = math.sqrt(i) | ||
3048 | end | ||
3049 | sqrts = _tbl_0 | ||
3050 | end | ||
3051 | local tuples = { | ||
3052 | { | ||
3053 | "hello", | ||
3054 | "world" | ||
3055 | }, | ||
3056 | { | ||
3057 | "foo", | ||
3058 | "bar" | ||
3059 | } | ||
3060 | } | ||
3061 | local tbl | ||
3062 | do | ||
3063 | local _tbl_0 = { } | ||
3064 | for _index_0 = 1, #tuples do | ||
3065 | local tuple = tuples[_index_0] | ||
3066 | local _key_0, _val_0 = unpack(tuple) | ||
3067 | _tbl_0[_key_0] = _val_0 | ||
3068 | end | ||
3069 | tbl = _tbl_0 | ||
3070 | end | ||
3071 | local slice | ||
3072 | do | ||
3073 | local _accum_0 = { } | ||
3074 | local _len_0 = 1 | ||
3075 | local _list_0 = items | ||
3076 | local _max_0 = 5 | ||
3077 | for _index_0 = 1, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do | ||
3078 | local item = _list_0[_index_0] | ||
3079 | _accum_0[_len_0] = item | ||
3080 | _len_0 = _len_0 + 1 | ||
3081 | end | ||
3082 | slice = _accum_0 | ||
3083 | end | ||
3084 | local slice | ||
3085 | do | ||
3086 | local _accum_0 = { } | ||
3087 | local _len_0 = 1 | ||
3088 | local _list_0 = items | ||
3089 | for _index_0 = 2, #_list_0 do | ||
3090 | local item = _list_0[_index_0] | ||
3091 | _accum_0[_len_0] = item | ||
3092 | _len_0 = _len_0 + 1 | ||
3093 | end | ||
3094 | slice = _accum_0 | ||
3095 | end | ||
3096 | local slice | ||
3097 | do | ||
3098 | local _accum_0 = { } | ||
3099 | local _len_0 = 1 | ||
3100 | local _list_0 = items | ||
3101 | for _index_0 = 1, #_list_0, 2 do | ||
3102 | local item = _list_0[_index_0] | ||
3103 | _accum_0[_len_0] = item | ||
3104 | _len_0 = _len_0 + 1 | ||
3105 | end | ||
3106 | slice = _accum_0 | ||
3107 | end | ||
3108 | for i = 10, 20 do | ||
3109 | print(i) | ||
3110 | end | ||
3111 | for k = 1, 15, 2 do | ||
3112 | print(k) | ||
3113 | end | ||
3114 | for key, value in pairs(object) do | ||
3115 | print(key, value) | ||
3116 | end | ||
3117 | do | ||
3118 | local _list_0 = items | ||
3119 | local _max_0 = 4 | ||
3120 | for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do | ||
3121 | local item = _list_0[_index_0] | ||
3122 | print(item) | ||
3123 | end | ||
3124 | end | ||
3125 | local _list_0 = items | ||
3126 | for _index_0 = 1, #_list_0 do | ||
3127 | local item = _list_0[_index_0] | ||
3128 | print(item) | ||
3129 | end | ||
3130 | for j = 1, 10, 3 do | ||
3131 | print(j) | ||
3132 | end | ||
3133 | local doubled_evens | ||
3134 | do | ||
3135 | local _accum_0 = { } | ||
3136 | local _len_0 = 1 | ||
3137 | for i = 1, 20 do | ||
3138 | if i % 2 == 0 then | ||
3139 | _accum_0[_len_0] = i * 2 | ||
3140 | else | ||
3141 | _accum_0[_len_0] = i | ||
3142 | end | ||
3143 | _len_0 = _len_0 + 1 | ||
3144 | end | ||
3145 | doubled_evens = _accum_0 | ||
3146 | end | ||
3147 | local func_a | ||
3148 | func_a = function() | ||
3149 | for i = 1, 10 do | ||
3150 | print(i) | ||
3151 | end | ||
3152 | end | ||
3153 | local func_b | ||
3154 | func_b = function() | ||
3155 | local _accum_0 = { } | ||
3156 | local _len_0 = 1 | ||
3157 | for i = 1, 10 do | ||
3158 | _accum_0[_len_0] = i | ||
3159 | _len_0 = _len_0 + 1 | ||
3160 | end | ||
3161 | return _accum_0 | ||
3162 | end | ||
3163 | print(func_a()) | ||
3164 | print(func_b()) | ||
3165 | local i = 10 | ||
3166 | repeat | ||
3167 | print(i) | ||
3168 | i = i - 1 | ||
3169 | until i == 0 | ||
3170 | local i = 10 | ||
3171 | while i > 0 do | ||
3172 | print(i) | ||
3173 | i = i - 1 | ||
3174 | end | ||
3175 | while running == true do | ||
3176 | my_function() | ||
3177 | end | ||
3178 | local i = 10 | ||
3179 | while not (i == 0) do | ||
3180 | print(i) | ||
3181 | i = i - 1 | ||
3182 | end | ||
3183 | while not (running == false) do | ||
3184 | my_function() | ||
3185 | end | ||
3186 | local i = 0 | ||
3187 | while i < 10 do | ||
3188 | i = i + 1 | ||
3189 | if i % 2 == 0 then | ||
3190 | goto _continue_0 | ||
3191 | end | ||
3192 | print(i) | ||
3193 | ::_continue_0:: | ||
3194 | end | ||
3195 | local my_numbers = { | ||
3196 | 1, | ||
3197 | 2, | ||
3198 | 3, | ||
3199 | 4, | ||
3200 | 5, | ||
3201 | 6 | ||
3202 | } | ||
3203 | local odds | ||
3204 | do | ||
3205 | local _accum_0 = { } | ||
3206 | local _len_0 = 1 | ||
3207 | for _index_0 = 1, #my_numbers do | ||
3208 | local x = my_numbers[_index_0] | ||
3209 | if x % 2 == 1 then | ||
3210 | goto _continue_0 | ||
3211 | end | ||
3212 | _accum_0[_len_0] = x | ||
3213 | _len_0 = _len_0 + 1 | ||
3214 | ::_continue_0:: | ||
3215 | end | ||
3216 | odds = _accum_0 | ||
3217 | end | ||
3218 | local have_coins = false | ||
3219 | if have_coins then | ||
3220 | print("Got coins") | ||
3221 | else | ||
3222 | print("No coins") | ||
3223 | end | ||
3224 | local have_coins = false | ||
3225 | if have_coins then | ||
3226 | print("Got coins") | ||
3227 | else | ||
3228 | print("No coins") | ||
3229 | end | ||
3230 | local have_coins = false | ||
3231 | print((function() | ||
3232 | if have_coins then | ||
3233 | return "Got coins" | ||
3234 | else | ||
3235 | return "No coins" | ||
3236 | end | ||
3237 | end)()) | ||
3238 | local is_tall | ||
3239 | is_tall = function(name) | ||
3240 | if name == "Rob" then | ||
3241 | return true | ||
3242 | else | ||
3243 | return false | ||
3244 | end | ||
3245 | end | ||
3246 | local message | ||
3247 | if is_tall("Rob") then | ||
3248 | message = "I am very tall" | ||
3249 | else | ||
3250 | message = "I am not so tall" | ||
3251 | end | ||
3252 | print(message) | ||
3253 | if not (os.date("%A") == "Monday") then | ||
3254 | print("it is not Monday!") | ||
3255 | end | ||
3256 | if not (math.random() > 0.1) then | ||
3257 | print("You're lucky!") | ||
3258 | end | ||
3259 | local a = 5 | ||
3260 | if (1 == a or 3 == a or 5 == a or 7 == a) then | ||
3261 | print("checking equality with discrete values") | ||
3262 | end | ||
3263 | if (function() | ||
3264 | local _check_0 = list | ||
3265 | for _index_0 = 1, #_check_0 do | ||
3266 | if _check_0[_index_0] == a then | ||
3267 | return true | ||
3268 | end | ||
3269 | end | ||
3270 | return false | ||
3271 | end)() then | ||
3272 | print("checking if `a` is in a list") | ||
3273 | end | ||
3274 | if not (math.random() > 0.1) then | ||
3275 | print("You're lucky!") | ||
3276 | end | ||
3277 | if name == "Rob" then | ||
3278 | print("hello world") | ||
3279 | end | ||
3280 | local _list_0 = items | ||
3281 | for _index_0 = 1, #_list_0 do | ||
3282 | local item = _list_0[_index_0] | ||
3283 | print("item: ", item) | ||
3284 | end | ||
3285 | while game:isRunning() do | ||
3286 | game:update() | ||
3287 | end | ||
3288 | while not reader:eof() do | ||
3289 | reader:parse_line() | ||
3290 | end | ||
3291 | local name = "Dan" | ||
3292 | if "Robert" == name then | ||
3293 | print("You are Robert") | ||
3294 | elseif "Dan" == name or "Daniel" == name then | ||
3295 | print("Your name, it's Dan") | ||
3296 | else | ||
3297 | print("I don't know about your name") | ||
3298 | end | ||
3299 | local b = 1 | ||
3300 | local next_number | ||
3301 | if 1 == b then | ||
3302 | next_number = 2 | ||
3303 | elseif 2 == b then | ||
3304 | next_number = 3 | ||
3305 | else | ||
3306 | next_number = error("can't count that high!") | ||
3307 | end | ||
3308 | local msg | ||
3309 | do | ||
3310 | local _exp_0 = math.random(1, 5) | ||
3311 | if 1 == _exp_0 then | ||
3312 | msg = "you are lucky" | ||
3313 | elseif 2 == _exp_0 then | ||
3314 | msg = "you are almost lucky" | ||
3315 | else | ||
3316 | msg = "not so lucky" | ||
3317 | end | ||
3318 | end | ||
3319 | do | ||
3320 | local _exp_0 = math.random(1, 5) | ||
3321 | if 1 == _exp_0 then | ||
3322 | print("you are lucky") | ||
3323 | else | ||
3324 | print("not so lucky") | ||
3325 | end | ||
3326 | end | ||
3327 | do | ||
3328 | local _exp_0 = math.random(1, 5) | ||
3329 | if 1 == _exp_0 then | ||
3330 | print("you are lucky") | ||
3331 | else | ||
3332 | print("not so lucky") | ||
3333 | end | ||
3334 | end | ||
3335 | local items = { | ||
3336 | { | ||
3337 | x = 100, | ||
3338 | y = 200 | ||
3339 | }, | ||
3340 | { | ||
3341 | width = 300, | ||
3342 | height = 400 | ||
3343 | } | ||
3344 | } | ||
3345 | for _index_0 = 1, #items do | ||
3346 | local item = items[_index_0] | ||
3347 | do | ||
3348 | local _type_0 = type(item) | ||
3349 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
3350 | local _match_0 = false | ||
3351 | if _tab_0 then | ||
3352 | local x = item.x | ||
3353 | local y = item.y | ||
3354 | if x ~= nil and y ~= nil then | ||
3355 | _match_0 = true | ||
3356 | print("Vec2 " .. tostring(x) .. ", " .. tostring(y)) | ||
3357 | end | ||
3358 | end | ||
3359 | if not _match_0 then | ||
3360 | if _tab_0 then | ||
3361 | local width = item.width | ||
3362 | local height = item.height | ||
3363 | if width ~= nil and height ~= nil then | ||
3364 | print("size " .. tostring(width) .. ", " .. tostring(height)) | ||
3365 | end | ||
3366 | end | ||
3367 | end | ||
3368 | end | ||
3369 | end | ||
3370 | local item = { } | ||
3371 | local x, y = item.pos.x, item.pos.y | ||
3372 | if x == nil then | ||
3373 | x = 50 | ||
3374 | end | ||
3375 | if y == nil then | ||
3376 | y = 200 | ||
3377 | end | ||
3378 | do | ||
3379 | local _type_0 = type(item) | ||
3380 | local _tab_0 = "table" == _type_0 or "userdata" == _type_0 | ||
3381 | if _tab_0 then | ||
3382 | do | ||
3383 | local _obj_0 = item.pos | ||
3384 | local _type_1 = type(_obj_0) | ||
3385 | if "table" == _type_1 or "userdata" == _type_1 then | ||
3386 | x = _obj_0.x | ||
3387 | end | ||
3388 | end | ||
3389 | do | ||
3390 | local _obj_0 = item.pos | ||
3391 | local _type_1 = type(_obj_0) | ||
3392 | if "table" == _type_1 or "userdata" == _type_1 then | ||
3393 | y = _obj_0.y | ||
3394 | end | ||
3395 | end | ||
3396 | if x == nil then | ||
3397 | x = 50 | ||
3398 | end | ||
3399 | if y == nil then | ||
3400 | y = 200 | ||
3401 | end | ||
3402 | print("Vec2 " .. tostring(x) .. ", " .. tostring(y)) | ||
3403 | end | ||
3404 | end | ||
3405 | local Inventory | ||
3406 | do | ||
3407 | local _class_0 | ||
3408 | local _base_0 = { | ||
3409 | add_item = function(self, name) | ||
3410 | if self.items[name] then | ||
3411 | local _obj_0 = self.items | ||
3412 | _obj_0[name] = _obj_0[name] + 1 | ||
3413 | else | ||
3414 | self.items[name] = 1 | ||
3415 | end | ||
3416 | end | ||
3417 | } | ||
3418 | if _base_0.__index == nil then | ||
3419 | _base_0.__index = _base_0 | ||
3420 | end | ||
3421 | _class_0 = setmetatable({ | ||
3422 | __init = function(self) | ||
3423 | self.items = { } | ||
3424 | end, | ||
3425 | __base = _base_0, | ||
3426 | __name = "Inventory" | ||
3427 | }, { | ||
3428 | __index = _base_0, | ||
3429 | __call = function(cls, ...) | ||
3430 | local _self_0 = setmetatable({ }, _base_0) | ||
3431 | cls.__init(_self_0, ...) | ||
3432 | return _self_0 | ||
3433 | end | ||
3434 | }) | ||
3435 | _base_0.__class = _class_0 | ||
3436 | Inventory = _class_0 | ||
3437 | end | ||
3438 | local inv = Inventory() | ||
3439 | inv:add_item("t-shirt") | ||
3440 | inv:add_item("pants") | ||
3441 | local Person | ||
3442 | do | ||
3443 | local _class_0 | ||
3444 | local _base_0 = { | ||
3445 | clothes = { }, | ||
3446 | give_item = function(self, name) | ||
3447 | return table.insert(self.clothes, name) | ||
3448 | end | ||
3449 | } | ||
3450 | if _base_0.__index == nil then | ||
3451 | _base_0.__index = _base_0 | ||
3452 | end | ||
3453 | _class_0 = setmetatable({ | ||
3454 | __init = function() end, | ||
3455 | __base = _base_0, | ||
3456 | __name = "Person" | ||
3457 | }, { | ||
3458 | __index = _base_0, | ||
3459 | __call = function(cls, ...) | ||
3460 | local _self_0 = setmetatable({ }, _base_0) | ||
3461 | cls.__init(_self_0, ...) | ||
3462 | return _self_0 | ||
3463 | end | ||
3464 | }) | ||
3465 | _base_0.__class = _class_0 | ||
3466 | Person = _class_0 | ||
3467 | end | ||
3468 | local a = Person() | ||
3469 | local b = Person() | ||
3470 | a:give_item("pants") | ||
3471 | b:give_item("shirt") | ||
3472 | local _list_0 = a.clothes | ||
3473 | for _index_0 = 1, #_list_0 do | ||
3474 | local item = _list_0[_index_0] | ||
3475 | print(item) | ||
3476 | end | ||
3477 | local Person | ||
3478 | do | ||
3479 | local _class_0 | ||
3480 | local _base_0 = { } | ||
3481 | if _base_0.__index == nil then | ||
3482 | _base_0.__index = _base_0 | ||
3483 | end | ||
3484 | _class_0 = setmetatable({ | ||
3485 | __init = function(self) | ||
3486 | self.clothes = { } | ||
3487 | end, | ||
3488 | __base = _base_0, | ||
3489 | __name = "Person" | ||
3490 | }, { | ||
3491 | __index = _base_0, | ||
3492 | __call = function(cls, ...) | ||
3493 | local _self_0 = setmetatable({ }, _base_0) | ||
3494 | cls.__init(_self_0, ...) | ||
3495 | return _self_0 | ||
3496 | end | ||
3497 | }) | ||
3498 | _base_0.__class = _class_0 | ||
3499 | Person = _class_0 | ||
3500 | end | ||
3501 | local BackPack | ||
3502 | do | ||
3503 | local _class_0 | ||
3504 | local _parent_0 = Inventory | ||
3505 | local _base_0 = { | ||
3506 | size = 10, | ||
3507 | add_item = function(self, name) | ||
3508 | if #self.items > size then | ||
3509 | error("backpack is full") | ||
3510 | end | ||
3511 | return _class_0.__parent.__base.add_item(self, name) | ||
3512 | end | ||
3513 | } | ||
3514 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
3515 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
3516 | _base_0[_key_0] = _val_0 | ||
3517 | end | ||
3518 | end | ||
3519 | if _base_0.__index == nil then | ||
3520 | _base_0.__index = _base_0 | ||
3521 | end | ||
3522 | setmetatable(_base_0, _parent_0.__base) | ||
3523 | _class_0 = setmetatable({ | ||
3524 | __init = function(self, ...) | ||
3525 | return _class_0.__parent.__init(self, ...) | ||
3526 | end, | ||
3527 | __base = _base_0, | ||
3528 | __name = "BackPack", | ||
3529 | __parent = _parent_0 | ||
3530 | }, { | ||
3531 | __index = function(cls, name) | ||
3532 | local val = rawget(_base_0, name) | ||
3533 | if val == nil then | ||
3534 | local parent = rawget(cls, "__parent") | ||
3535 | if parent then | ||
3536 | return parent[name] | ||
3537 | end | ||
3538 | else | ||
3539 | return val | ||
3540 | end | ||
3541 | end, | ||
3542 | __call = function(cls, ...) | ||
3543 | local _self_0 = setmetatable({ }, _base_0) | ||
3544 | cls.__init(_self_0, ...) | ||
3545 | return _self_0 | ||
3546 | end | ||
3547 | }) | ||
3548 | _base_0.__class = _class_0 | ||
3549 | if _parent_0.__inherited then | ||
3550 | _parent_0.__inherited(_parent_0, _class_0) | ||
3551 | end | ||
3552 | BackPack = _class_0 | ||
3553 | end | ||
3554 | local Shelf | ||
3555 | do | ||
3556 | local _class_0 | ||
3557 | local _base_0 = { } | ||
3558 | if _base_0.__index == nil then | ||
3559 | _base_0.__index = _base_0 | ||
3560 | end | ||
3561 | _class_0 = setmetatable({ | ||
3562 | __init = function() end, | ||
3563 | __base = _base_0, | ||
3564 | __name = "Shelf" | ||
3565 | }, { | ||
3566 | __index = _base_0, | ||
3567 | __call = function(cls, ...) | ||
3568 | local _self_0 = setmetatable({ }, _base_0) | ||
3569 | cls.__init(_self_0, ...) | ||
3570 | return _self_0 | ||
3571 | end | ||
3572 | }) | ||
3573 | _base_0.__class = _class_0 | ||
3574 | local self = _class_0; | ||
3575 | self.__inherited = function(self, child) | ||
3576 | return print(self.__name, "was inherited by", child.__name) | ||
3577 | end | ||
3578 | Shelf = _class_0 | ||
3579 | end | ||
3580 | local Cupboard | ||
3581 | do | ||
3582 | local _class_0 | ||
3583 | local _parent_0 = Shelf | ||
3584 | local _base_0 = { } | ||
3585 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
3586 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
3587 | _base_0[_key_0] = _val_0 | ||
3588 | end | ||
3589 | end | ||
3590 | if _base_0.__index == nil then | ||
3591 | _base_0.__index = _base_0 | ||
3592 | end | ||
3593 | setmetatable(_base_0, _parent_0.__base) | ||
3594 | _class_0 = setmetatable({ | ||
3595 | __init = function(self, ...) | ||
3596 | return _class_0.__parent.__init(self, ...) | ||
3597 | end, | ||
3598 | __base = _base_0, | ||
3599 | __name = "Cupboard", | ||
3600 | __parent = _parent_0 | ||
3601 | }, { | ||
3602 | __index = function(cls, name) | ||
3603 | local val = rawget(_base_0, name) | ||
3604 | if val == nil then | ||
3605 | local parent = rawget(cls, "__parent") | ||
3606 | if parent then | ||
3607 | return parent[name] | ||
3608 | end | ||
3609 | else | ||
3610 | return val | ||
3611 | end | ||
3612 | end, | ||
3613 | __call = function(cls, ...) | ||
3614 | local _self_0 = setmetatable({ }, _base_0) | ||
3615 | cls.__init(_self_0, ...) | ||
3616 | return _self_0 | ||
3617 | end | ||
3618 | }) | ||
3619 | _base_0.__class = _class_0 | ||
3620 | if _parent_0.__inherited then | ||
3621 | _parent_0.__inherited(_parent_0, _class_0) | ||
3622 | end | ||
3623 | Cupboard = _class_0 | ||
3624 | end | ||
3625 | local MyClass | ||
3626 | do | ||
3627 | local _class_0 | ||
3628 | local _parent_0 = ParentClass | ||
3629 | local _base_0 = { | ||
3630 | a_method = function(self) | ||
3631 | _class_0.__parent.__base.a_method(self, "hello", "world") | ||
3632 | _class_0.__parent.a_method(self, "hello", "world") | ||
3633 | _class_0.__parent.a_method(self, "hello", "world") | ||
3634 | return assert(_class_0.__parent == ParentClass) | ||
3635 | end | ||
3636 | } | ||
3637 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
3638 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
3639 | _base_0[_key_0] = _val_0 | ||
3640 | end | ||
3641 | end | ||
3642 | if _base_0.__index == nil then | ||
3643 | _base_0.__index = _base_0 | ||
3644 | end | ||
3645 | setmetatable(_base_0, _parent_0.__base) | ||
3646 | _class_0 = setmetatable({ | ||
3647 | __init = function(self, ...) | ||
3648 | return _class_0.__parent.__init(self, ...) | ||
3649 | end, | ||
3650 | __base = _base_0, | ||
3651 | __name = "MyClass", | ||
3652 | __parent = _parent_0 | ||
3653 | }, { | ||
3654 | __index = function(cls, name) | ||
3655 | local val = rawget(_base_0, name) | ||
3656 | if val == nil then | ||
3657 | local parent = rawget(cls, "__parent") | ||
3658 | if parent then | ||
3659 | return parent[name] | ||
3660 | end | ||
3661 | else | ||
3662 | return val | ||
3663 | end | ||
3664 | end, | ||
3665 | __call = function(cls, ...) | ||
3666 | local _self_0 = setmetatable({ }, _base_0) | ||
3667 | cls.__init(_self_0, ...) | ||
3668 | return _self_0 | ||
3669 | end | ||
3670 | }) | ||
3671 | _base_0.__class = _class_0 | ||
3672 | if _parent_0.__inherited then | ||
3673 | _parent_0.__inherited(_parent_0, _class_0) | ||
3674 | end | ||
3675 | MyClass = _class_0 | ||
3676 | end | ||
3677 | local b = BackPack() | ||
3678 | assert(b.__class == BackPack) | ||
3679 | print(BackPack.size) | ||
3680 | print(BackPack.__name) | ||
3681 | local Things | ||
3682 | do | ||
3683 | local _class_0 | ||
3684 | local _base_0 = { } | ||
3685 | if _base_0.__index == nil then | ||
3686 | _base_0.__index = _base_0 | ||
3687 | end | ||
3688 | _class_0 = setmetatable({ | ||
3689 | __init = function() end, | ||
3690 | __base = _base_0, | ||
3691 | __name = "Things" | ||
3692 | }, { | ||
3693 | __index = _base_0, | ||
3694 | __call = function(cls, ...) | ||
3695 | local _self_0 = setmetatable({ }, _base_0) | ||
3696 | cls.__init(_self_0, ...) | ||
3697 | return _self_0 | ||
3698 | end | ||
3699 | }) | ||
3700 | _base_0.__class = _class_0 | ||
3701 | local self = _class_0; | ||
3702 | self.some_func = function(self) | ||
3703 | return print("Hello from", self.__name) | ||
3704 | end | ||
3705 | Things = _class_0 | ||
3706 | end | ||
3707 | Things:some_func() | ||
3708 | assert(Things().some_func == nil) | ||
3709 | local Counter | ||
3710 | do | ||
3711 | local _class_0 | ||
3712 | local _base_0 = { } | ||
3713 | if _base_0.__index == nil then | ||
3714 | _base_0.__index = _base_0 | ||
3715 | end | ||
3716 | _class_0 = setmetatable({ | ||
3717 | __init = function(self) | ||
3718 | self.__class.count = self.__class.count + 1 | ||
3719 | end, | ||
3720 | __base = _base_0, | ||
3721 | __name = "Counter" | ||
3722 | }, { | ||
3723 | __index = _base_0, | ||
3724 | __call = function(cls, ...) | ||
3725 | local _self_0 = setmetatable({ }, _base_0) | ||
3726 | cls.__init(_self_0, ...) | ||
3727 | return _self_0 | ||
3728 | end | ||
3729 | }) | ||
3730 | _base_0.__class = _class_0 | ||
3731 | local self = _class_0; | ||
3732 | self.count = 0 | ||
3733 | Counter = _class_0 | ||
3734 | end | ||
3735 | Counter() | ||
3736 | Counter() | ||
3737 | print(Counter.count) | ||
3738 | self.__class:hello(1, 2, 3, 4) | ||
3739 | local Things | ||
3740 | do | ||
3741 | local _class_0 | ||
3742 | local _base_0 = { } | ||
3743 | if _base_0.__index == nil then | ||
3744 | _base_0.__index = _base_0 | ||
3745 | end | ||
3746 | _class_0 = setmetatable({ | ||
3747 | __init = function() end, | ||
3748 | __base = _base_0, | ||
3749 | __name = "Things" | ||
3750 | }, { | ||
3751 | __index = _base_0, | ||
3752 | __call = function(cls, ...) | ||
3753 | local _self_0 = setmetatable({ }, _base_0) | ||
3754 | cls.__init(_self_0, ...) | ||
3755 | return _self_0 | ||
3756 | end | ||
3757 | }) | ||
3758 | _base_0.__class = _class_0 | ||
3759 | local self = _class_0; | ||
3760 | self.class_var = "hello world" | ||
3761 | Things = _class_0 | ||
3762 | end | ||
3763 | local MoreThings | ||
3764 | do | ||
3765 | local _class_0 | ||
3766 | local secret, log | ||
3767 | local _base_0 = { | ||
3768 | some_method = function(self) | ||
3769 | return log("hello world: " .. secret) | ||
3770 | end | ||
3771 | } | ||
3772 | if _base_0.__index == nil then | ||
3773 | _base_0.__index = _base_0 | ||
3774 | end | ||
3775 | _class_0 = setmetatable({ | ||
3776 | __init = function() end, | ||
3777 | __base = _base_0, | ||
3778 | __name = "MoreThings" | ||
3779 | }, { | ||
3780 | __index = _base_0, | ||
3781 | __call = function(cls, ...) | ||
3782 | local _self_0 = setmetatable({ }, _base_0) | ||
3783 | cls.__init(_self_0, ...) | ||
3784 | return _self_0 | ||
3785 | end | ||
3786 | }) | ||
3787 | _base_0.__class = _class_0 | ||
3788 | local self = _class_0; | ||
3789 | secret = 123 | ||
3790 | log = function(msg) | ||
3791 | return print("LOG:", msg) | ||
3792 | end | ||
3793 | MoreThings = _class_0 | ||
3794 | end | ||
3795 | assert(self == self) | ||
3796 | assert(self.__class == self.__class) | ||
3797 | local some_instance_method | ||
3798 | some_instance_method = function(self, ...) | ||
3799 | return self.__class(...) | ||
3800 | end | ||
3801 | local Something | ||
3802 | do | ||
3803 | local _class_0 | ||
3804 | local _base_0 = { } | ||
3805 | if _base_0.__index == nil then | ||
3806 | _base_0.__index = _base_0 | ||
3807 | end | ||
3808 | _class_0 = setmetatable({ | ||
3809 | __init = function(self, foo, bar, biz, baz) | ||
3810 | self.foo = foo | ||
3811 | self.bar = bar | ||
3812 | self.__class.biz = biz | ||
3813 | self.__class.baz = baz | ||
3814 | end, | ||
3815 | __base = _base_0, | ||
3816 | __name = "Something" | ||
3817 | }, { | ||
3818 | __index = _base_0, | ||
3819 | __call = function(cls, ...) | ||
3820 | local _self_0 = setmetatable({ }, _base_0) | ||
3821 | cls.__init(_self_0, ...) | ||
3822 | return _self_0 | ||
3823 | end | ||
3824 | }) | ||
3825 | _base_0.__class = _class_0 | ||
3826 | Something = _class_0 | ||
3827 | end | ||
3828 | do | ||
3829 | local _class_0 | ||
3830 | local _base_0 = { } | ||
3831 | if _base_0.__index == nil then | ||
3832 | _base_0.__index = _base_0 | ||
3833 | end | ||
3834 | _class_0 = setmetatable({ | ||
3835 | __init = function(self, foo, bar, biz, baz) | ||
3836 | self.foo = foo | ||
3837 | self.bar = bar | ||
3838 | self.__class.biz = biz | ||
3839 | self.__class.baz = baz | ||
3840 | end, | ||
3841 | __base = _base_0, | ||
3842 | __name = "Something" | ||
3843 | }, { | ||
3844 | __index = _base_0, | ||
3845 | __call = function(cls, ...) | ||
3846 | local _self_0 = setmetatable({ }, _base_0) | ||
3847 | cls.__init(_self_0, ...) | ||
3848 | return _self_0 | ||
3849 | end | ||
3850 | }) | ||
3851 | _base_0.__class = _class_0 | ||
3852 | Something = _class_0 | ||
3853 | end | ||
3854 | local new | ||
3855 | new = function(self, fieldA, fieldB) | ||
3856 | self.fieldA = fieldA | ||
3857 | self.fieldB = fieldB | ||
3858 | return self | ||
3859 | end | ||
3860 | local obj = new({ }, 123, "abc") | ||
3861 | print(obj) | ||
3862 | local x | ||
3863 | local Bucket | ||
3864 | do | ||
3865 | local _class_0 | ||
3866 | local _base_0 = { | ||
3867 | drops = 0, | ||
3868 | add_drop = function(self) | ||
3869 | self.drops = self.drops + 1 | ||
3870 | end | ||
3871 | } | ||
3872 | if _base_0.__index == nil then | ||
3873 | _base_0.__index = _base_0 | ||
3874 | end | ||
3875 | _class_0 = setmetatable({ | ||
3876 | __init = function() end, | ||
3877 | __base = _base_0, | ||
3878 | __name = "Bucket" | ||
3879 | }, { | ||
3880 | __index = _base_0, | ||
3881 | __call = function(cls, ...) | ||
3882 | local _self_0 = setmetatable({ }, _base_0) | ||
3883 | cls.__init(_self_0, ...) | ||
3884 | return _self_0 | ||
3885 | end | ||
3886 | }) | ||
3887 | _base_0.__class = _class_0 | ||
3888 | Bucket = _class_0 | ||
3889 | x = _class_0 | ||
3890 | end | ||
3891 | local BigBucket | ||
3892 | do | ||
3893 | local _class_0 | ||
3894 | local _parent_0 = Bucket | ||
3895 | local _base_0 = { | ||
3896 | add_drop = function(self) | ||
3897 | self.drops = self.drops + 10 | ||
3898 | end | ||
3899 | } | ||
3900 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
3901 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
3902 | _base_0[_key_0] = _val_0 | ||
3903 | end | ||
3904 | end | ||
3905 | if _base_0.__index == nil then | ||
3906 | _base_0.__index = _base_0 | ||
3907 | end | ||
3908 | setmetatable(_base_0, _parent_0.__base) | ||
3909 | _class_0 = setmetatable({ | ||
3910 | __init = function(self, ...) | ||
3911 | return _class_0.__parent.__init(self, ...) | ||
3912 | end, | ||
3913 | __base = _base_0, | ||
3914 | __name = "BigBucket", | ||
3915 | __parent = _parent_0 | ||
3916 | }, { | ||
3917 | __index = function(cls, name) | ||
3918 | local val = rawget(_base_0, name) | ||
3919 | if val == nil then | ||
3920 | local parent = rawget(cls, "__parent") | ||
3921 | if parent then | ||
3922 | return parent[name] | ||
3923 | end | ||
3924 | else | ||
3925 | return val | ||
3926 | end | ||
3927 | end, | ||
3928 | __call = function(cls, ...) | ||
3929 | local _self_0 = setmetatable({ }, _base_0) | ||
3930 | cls.__init(_self_0, ...) | ||
3931 | return _self_0 | ||
3932 | end | ||
3933 | }) | ||
3934 | _base_0.__class = _class_0 | ||
3935 | if _parent_0.__inherited then | ||
3936 | _parent_0.__inherited(_parent_0, _class_0) | ||
3937 | end | ||
3938 | BigBucket = _class_0 | ||
3939 | end | ||
3940 | assert(Bucket.__name == "BigBucket") | ||
3941 | local x | ||
3942 | do | ||
3943 | local _class_0 | ||
3944 | local _base_0 = { } | ||
3945 | if _base_0.__index == nil then | ||
3946 | _base_0.__index = _base_0 | ||
3947 | end | ||
3948 | _class_0 = setmetatable({ | ||
3949 | __init = function() end, | ||
3950 | __base = _base_0, | ||
3951 | __name = "x" | ||
3952 | }, { | ||
3953 | __index = _base_0, | ||
3954 | __call = function(cls, ...) | ||
3955 | local _self_0 = setmetatable({ }, _base_0) | ||
3956 | cls.__init(_self_0, ...) | ||
3957 | return _self_0 | ||
3958 | end | ||
3959 | }) | ||
3960 | _base_0.__class = _class_0 | ||
3961 | x = _class_0 | ||
3962 | end | ||
3963 | local MyIndex = { | ||
3964 | __index = { | ||
3965 | var = 1 | ||
3966 | } | ||
3967 | } | ||
3968 | local X | ||
3969 | do | ||
3970 | local _class_0 | ||
3971 | local _base_0 = { | ||
3972 | func = function(self) | ||
3973 | return print(123) | ||
3974 | end | ||
3975 | } | ||
3976 | local _list_0 = { | ||
3977 | MyIndex | ||
3978 | } | ||
3979 | for _index_0 = 1, #_list_0 do | ||
3980 | local _item_0 = _list_0[_index_0] | ||
3981 | local _cls_0, _mixin_0 = (_item_0.__base ~= nil), _item_0.__base or _item_0 | ||
3982 | for _key_0, _val_0 in pairs(_mixin_0) do | ||
3983 | if _base_0[_key_0] == nil and (not _cls_0 or not _key_0:match("^__")) then | ||
3984 | _base_0[_key_0] = _val_0 | ||
3985 | end | ||
3986 | end | ||
3987 | end | ||
3988 | if _base_0.__index == nil then | ||
3989 | _base_0.__index = _base_0 | ||
3990 | end | ||
3991 | _class_0 = setmetatable({ | ||
3992 | __init = function() end, | ||
3993 | __base = _base_0, | ||
3994 | __name = "X" | ||
3995 | }, { | ||
3996 | __index = _base_0, | ||
3997 | __call = function(cls, ...) | ||
3998 | local _self_0 = setmetatable({ }, _base_0) | ||
3999 | cls.__init(_self_0, ...) | ||
4000 | return _self_0 | ||
4001 | end | ||
4002 | }) | ||
4003 | _base_0.__class = _class_0 | ||
4004 | X = _class_0 | ||
4005 | end | ||
4006 | local x = X() | ||
4007 | print(x.var) | ||
4008 | local Y | ||
4009 | do | ||
4010 | local _class_0 | ||
4011 | local _base_0 = { } | ||
4012 | local _list_0 = { | ||
4013 | X | ||
4014 | } | ||
4015 | for _index_0 = 1, #_list_0 do | ||
4016 | local _item_0 = _list_0[_index_0] | ||
4017 | local _cls_0, _mixin_0 = (_item_0.__base ~= nil), _item_0.__base or _item_0 | ||
4018 | for _key_0, _val_0 in pairs(_mixin_0) do | ||
4019 | if _base_0[_key_0] == nil and (not _cls_0 or not _key_0:match("^__")) then | ||
4020 | _base_0[_key_0] = _val_0 | ||
4021 | end | ||
4022 | end | ||
4023 | end | ||
4024 | if _base_0.__index == nil then | ||
4025 | _base_0.__index = _base_0 | ||
4026 | end | ||
4027 | _class_0 = setmetatable({ | ||
4028 | __init = function() end, | ||
4029 | __base = _base_0, | ||
4030 | __name = "Y" | ||
4031 | }, { | ||
4032 | __index = _base_0, | ||
4033 | __call = function(cls, ...) | ||
4034 | local _self_0 = setmetatable({ }, _base_0) | ||
4035 | cls.__init(_self_0, ...) | ||
4036 | return _self_0 | ||
4037 | end | ||
4038 | }) | ||
4039 | _base_0.__class = _class_0 | ||
4040 | Y = _class_0 | ||
4041 | end | ||
4042 | local y = Y() | ||
4043 | y:func() | ||
4044 | assert(y.__class.__parent ~= X) | ||
4045 | do | ||
4046 | local _with_0 = Person() | ||
4047 | _with_0.name = "Oswald" | ||
4048 | _with_0:add_relative(my_dad) | ||
4049 | _with_0:save() | ||
4050 | print(_with_0.name) | ||
4051 | end | ||
4052 | local file | ||
4053 | do | ||
4054 | local _with_0 = File("favorite_foods.txt") | ||
4055 | _with_0:set_encoding("utf8") | ||
4056 | file = _with_0 | ||
4057 | end | ||
4058 | local create_person | ||
4059 | create_person = function(name, relatives) | ||
4060 | local _with_0 = Person() | ||
4061 | _with_0.name = name | ||
4062 | for _index_0 = 1, #relatives do | ||
4063 | local relative = relatives[_index_0] | ||
4064 | _with_0:add_relative(relative) | ||
4065 | end | ||
4066 | return _with_0 | ||
4067 | end | ||
4068 | local me = create_person("Leaf", { | ||
4069 | dad, | ||
4070 | mother, | ||
4071 | sister | ||
4072 | }) | ||
4073 | do | ||
4074 | local str = "Hello" | ||
4075 | print("original:", str) | ||
4076 | print("upper:", str:upper()) | ||
4077 | end | ||
4078 | do | ||
4079 | local _with_0 = tb | ||
4080 | _with_0[1] = 1 | ||
4081 | print(_with_0[2]) | ||
4082 | do | ||
4083 | local _with_1 = _with_0[abc] | ||
4084 | _with_1[3] = _with_1[2]:func() | ||
4085 | _with_1["key-name"] = value | ||
4086 | end | ||
4087 | _with_0[#_with_0 + 1] = "abc" | ||
4088 | end | ||
4089 | do | ||
4090 | local var = "hello" | ||
4091 | print(var) | ||
4092 | end | ||
4093 | print(var) | ||
4094 | local counter | ||
4095 | do | ||
4096 | local i = 0 | ||
4097 | counter = function() | ||
4098 | i = i + 1 | ||
4099 | return i | ||
4100 | end | ||
4101 | end | ||
4102 | print(counter()) | ||
4103 | print(counter()) | ||
4104 | local tbl = { | ||
4105 | key = (function() | ||
4106 | print("assigning key!") | ||
4107 | return 1234 | ||
4108 | end)() | ||
4109 | } | ||
4110 | local my_object = { | ||
4111 | value = 1000, | ||
4112 | write = function(self) | ||
4113 | return print("the value:", self.value) | ||
4114 | end | ||
4115 | } | ||
4116 | local run_callback | ||
4117 | run_callback = function(func) | ||
4118 | print("running callback...") | ||
4119 | return func() | ||
4120 | end | ||
4121 | run_callback(my_object.write) | ||
4122 | run_callback((function() | ||
4123 | local _base_0 = my_object | ||
4124 | local _fn_0 = _base_0.write | ||
4125 | return _fn_0 and function(...) | ||
4126 | return _fn_0(_base_0, ...) | ||
4127 | end | ||
4128 | end)()) | ||
4129 | local i = 100 | ||
4130 | local my_func | ||
4131 | my_func = function() | ||
4132 | i = 10 | ||
4133 | while i > 0 do | ||
4134 | print(i) | ||
4135 | i = i - 1 | ||
4136 | end | ||
4137 | end | ||
4138 | my_func() | ||
4139 | print(i) | ||
4140 | local i = 100 | ||
4141 | local my_func | ||
4142 | my_func = function() | ||
4143 | local i = "hello" | ||
4144 | end | ||
4145 | my_func() | ||
4146 | print(i) | ||
4147 | local tmp = 1213 | ||
4148 | local i, k = 100, 50 | ||
4149 | local my_func | ||
4150 | my_func = function(add) | ||
4151 | local tmp = tmp + add | ||
4152 | i = i + tmp | ||
4153 | k = k + tmp | ||
4154 | end | ||
4155 | my_func(22) | ||
4156 | print(i, k) | ||