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