From 8221f48ef1ab29ca56a751d7ace91b91f7df4820 Mon Sep 17 00:00:00 2001 From: Shawn Presser Date: Thu, 20 Sep 2018 14:15:20 -0500 Subject: [PATCH] Fix argument evaluation order --- bin/compiler.js | 210 ++++++++++++++++++++++++++++------------------- bin/compiler.lua | 210 ++++++++++++++++++++++++++++------------------- bin/lumen.js | 13 ++- bin/lumen.lua | 16 ++-- bin/reader.js | 3 +- bin/reader.lua | 3 +- compiler.l | 22 ++++- test.l | 5 +- 8 files changed, 297 insertions(+), 185 deletions(-) diff --git a/bin/compiler.js b/bin/compiler.js index 5fc68f2..6342a06 100644 --- a/bin/compiler.js +++ b/bin/compiler.js @@ -226,7 +226,8 @@ var expand_function = function (__x41) { var ____i51 = __e27; setenv(____x43, {_stash: true, variable: true}); } - var ____x44 = join(["%function", __args], macroexpand(__body)); + var __arg2 = join(["%function", __args], macroexpand(__body)); + var ____x44 = __arg2; drop(environment); return ____x44; }; @@ -250,7 +251,8 @@ var expand_definition = function (__x46) { var ____i61 = __e28; setenv(____x48, {_stash: true, variable: true}); } - var ____x49 = join([__x47, __name1, __args11], macroexpand(__body1)); + var __arg3 = join([__x47, __name1, __args11], macroexpand(__body1)); + var ____x49 = __arg3; drop(environment); return ____x49; }; @@ -897,7 +899,8 @@ var lower_if = function (args, hoist, stmt63, tail63) { if (is63(___else)) { __e51 = [lower_body([___else], tail63)]; } - return add(hoist, join(["%if", lower(__cond, hoist), lower_body([___then], tail63)], __e51)); + var __arg5 = join(["%if", lower(__cond, hoist), lower_body([___then], tail63)], __e51); + return add(hoist, __arg5); } else { var __e3 = unique("e"); add(hoist, ["%local", __e3, "nil"]); @@ -905,7 +908,8 @@ var lower_if = function (args, hoist, stmt63, tail63) { if (is63(___else)) { __e50 = [lower(["%set", __e3, ___else])]; } - add(hoist, join(["%if", lower(__cond, hoist), lower(["%set", __e3, ___then])], __e50)); + var __arg4 = join(["%if", lower(__cond, hoist), lower(["%set", __e3, ___then])], __e50); + add(hoist, __arg4); return __e3; } }; @@ -965,12 +969,47 @@ var lower_definition = function (kind, args, hoist) { var __body8 = cut(____id23, 2); return add(hoist, [kind, __name4, __args6, lower_body(__body8, true)]); }; -var lower_call = function (form, hoist) { - var __form2 = map(function (x) { - return lower(x, hoist); - }, form); - if (some63(__form2)) { - return __form2; +var lower_args = function (args, hoist) { + var __simple = true; + var __args13 = map(function (x) { + var __h = []; + var __x124 = lower(x, __h); + if (some63(__h)) { + __simple = false; + } + return [__h, __x124]; + }, args); + return map(function (__x126) { + var ____id24 = __x126; + var __h1 = ____id24[0]; + var __x127 = ____id24[1]; + var ____x128 = __h1; + var ____i18 = 0; + while (____i18 < _35(____x128)) { + var __v8 = ____x128[____i18]; + add(hoist, __v8); + ____i18 = ____i18 + 1; + } + if (__simple || literal63(__x127)) { + return __x127; + } else { + var __arg = unique("arg"); + add(hoist, ["%local", __arg, __x127]); + return __arg; + } + }, __args13); +}; +var lower_call = function (__x130, hoist) { + var ____id25 = __x130; + var __f11 = ____id25[0]; + var __args7 = cut(____id25, 1); + var __f2 = lower(__f11, hoist); + if (nil63(__f2)) { + if (some63(__args7)) { + return lower_call(__args7, hoist); + } + } else { + return join([__f2], lower_args(__args7, hoist)); } }; var pairwise63 = function (form) { @@ -979,13 +1018,13 @@ var pairwise63 = function (form) { var lower_pairwise = function (form) { if (pairwise63(form)) { var __e4 = []; - var ____id24 = form; - var __x125 = ____id24[0]; - var __args7 = cut(____id24, 1); + var ____id26 = form; + var __x133 = ____id26[0]; + var __args8 = cut(____id26, 1); reduce(function (a, b) { - add(__e4, [__x125, a, b]); + add(__e4, [__x133, a, b]); return a; - }, __args7); + }, __args8); return join(["and"], reverse(__e4)); } else { return form; @@ -995,13 +1034,13 @@ var lower_infix63 = function (form) { return infix63(hd(form)) && _35(form) > 3; }; var lower_infix = function (form, hoist) { - var __form3 = lower_pairwise(form); - var ____id25 = __form3; - var __x128 = ____id25[0]; - var __args8 = cut(____id25, 1); + var __form2 = lower_pairwise(form); + var ____id27 = __form2; + var __x136 = ____id27[0]; + var __args9 = cut(____id27, 1); return lower(reduce(function (a, b) { - return [__x128, b, a]; - }, reverse(__args8)), hoist); + return [__x136, b, a]; + }, reverse(__args9)), hoist); }; var lower_special = function (form, hoist) { var __e5 = lower_call(form, hoist); @@ -1022,40 +1061,40 @@ lower = function (form, hoist, stmt63, tail63) { if (lower_infix63(form)) { return lower_infix(form, hoist); } else { - var ____id26 = form; - var __x131 = ____id26[0]; - var __args9 = cut(____id26, 1); - if (__x131 === "do") { - return lower_do(__args9, hoist, stmt63, tail63); + var ____id28 = form; + var __x139 = ____id28[0]; + var __args10 = cut(____id28, 1); + if (__x139 === "do") { + return lower_do(__args10, hoist, stmt63, tail63); } else { - if (__x131 === "%call") { - return lower(__args9, hoist, stmt63, tail63); + if (__x139 === "%call") { + return lower(__args10, hoist, stmt63, tail63); } else { - if (__x131 === "%set") { - return lower_set(__args9, hoist, stmt63, tail63); + if (__x139 === "%set") { + return lower_set(__args10, hoist, stmt63, tail63); } else { - if (__x131 === "%if") { - return lower_if(__args9, hoist, stmt63, tail63); + if (__x139 === "%if") { + return lower_if(__args10, hoist, stmt63, tail63); } else { - if (__x131 === "%try") { - return lower_try(__args9, hoist, tail63); + if (__x139 === "%try") { + return lower_try(__args10, hoist, tail63); } else { - if (__x131 === "while") { - return lower_while(__args9, hoist); + if (__x139 === "while") { + return lower_while(__args10, hoist); } else { - if (__x131 === "%for") { - return lower_for(__args9, hoist); + if (__x139 === "%for") { + return lower_for(__args10, hoist); } else { - if (__x131 === "%function") { - return lower_function(__args9); + if (__x139 === "%function") { + return lower_function(__args10); } else { - if (__x131 === "%local-function" || __x131 === "%global-function") { - return lower_definition(__x131, __args9, hoist); + if (__x139 === "%local-function" || __x139 === "%global-function") { + return lower_definition(__x139, __args10, hoist); } else { - if (in63(__x131, ["and", "or"])) { - return lower_short(__x131, __args9, hoist); + if (in63(__x139, ["and", "or"])) { + return lower_short(__x139, __args10, hoist); } else { - if (statement63(__x131)) { + if (statement63(__x139)) { return lower_special(form, hoist); } else { return lower_call(form, hoist); @@ -1095,35 +1134,35 @@ immediate_call63 = function (x) { setenv("do", {_stash: true, special: function () { var __forms1 = unstash(Array.prototype.slice.call(arguments, 0)); var __s3 = ""; - var ____x136 = __forms1; - var ____i19 = 0; - while (____i19 < _35(____x136)) { - var __x137 = ____x136[____i19]; - if (target === "lua" && immediate_call63(__x137) && "\n" === char(__s3, edge(__s3))) { + var ____x144 = __forms1; + var ____i20 = 0; + while (____i20 < _35(____x144)) { + var __x145 = ____x144[____i20]; + if (target === "lua" && immediate_call63(__x145) && "\n" === char(__s3, edge(__s3))) { __s3 = clip(__s3, 0, edge(__s3)) + ";\n"; } - __s3 = __s3 + compile(__x137, {_stash: true, stmt: true}); - if (! atom63(__x137)) { - if (hd(__x137) === "return" || hd(__x137) === "break") { + __s3 = __s3 + compile(__x145, {_stash: true, stmt: true}); + if (! atom63(__x145)) { + if (hd(__x145) === "return" || hd(__x145) === "break") { break; } } - ____i19 = ____i19 + 1; + ____i20 = ____i20 + 1; } return __s3; }, stmt: true, tr: true}); setenv("%if", {_stash: true, special: function (cond, cons, alt) { var __cond2 = compile(cond); indent_level = indent_level + 1; - var ____x140 = compile(cons, {_stash: true, stmt: true}); + var ____x148 = compile(cons, {_stash: true, stmt: true}); indent_level = indent_level - 1; - var __cons1 = ____x140; + var __cons1 = ____x148; var __e54 = undefined; if (alt) { indent_level = indent_level + 1; - var ____x141 = compile(alt, {_stash: true, stmt: true}); + var ____x149 = compile(alt, {_stash: true, stmt: true}); indent_level = indent_level - 1; - __e54 = ____x141; + __e54 = ____x149; } var __alt1 = __e54; var __ind3 = indentation(); @@ -1149,9 +1188,9 @@ setenv("%if", {_stash: true, special: function (cond, cons, alt) { setenv("while", {_stash: true, special: function (cond, form) { var __cond4 = compile(cond); indent_level = indent_level + 1; - var ____x143 = compile(form, {_stash: true, stmt: true}); + var ____x151 = compile(form, {_stash: true, stmt: true}); indent_level = indent_level - 1; - var __body10 = ____x143; + var __body10 = ____x151; var __ind5 = indentation(); if (target === "js") { return __ind5 + "while (" + __cond4 + ") {\n" + __body10 + __ind5 + "}\n"; @@ -1163,9 +1202,9 @@ setenv("%for", {_stash: true, special: function (t, k, form) { var __t2 = compile(t); var __ind7 = indentation(); indent_level = indent_level + 1; - var ____x145 = compile(form, {_stash: true, stmt: true}); + var ____x153 = compile(form, {_stash: true, stmt: true}); indent_level = indent_level - 1; - var __body12 = ____x145; + var __body12 = ____x153; if (target === "lua") { return __ind7 + "for " + k + " in next, " + __t2 + " do\n" + __body12 + __ind7 + "end\n"; } else { @@ -1176,15 +1215,15 @@ setenv("%try", {_stash: true, special: function (form) { var __e8 = unique("e"); var __ind9 = indentation(); indent_level = indent_level + 1; - var ____x150 = compile(form, {_stash: true, stmt: true}); + var ____x158 = compile(form, {_stash: true, stmt: true}); indent_level = indent_level - 1; - var __body14 = ____x150; + var __body14 = ____x158; var __hf1 = ["return", ["%array", false, __e8]]; indent_level = indent_level + 1; - var ____x153 = compile(__hf1, {_stash: true, stmt: true}); + var ____x161 = compile(__hf1, {_stash: true, stmt: true}); indent_level = indent_level - 1; - var __h1 = ____x153; - return __ind9 + "try {\n" + __body14 + __ind9 + "}\n" + __ind9 + "catch (" + __e8 + ") {\n" + __h1 + __ind9 + "}\n"; + var __h3 = ____x161; + return __ind9 + "try {\n" + __body14 + __ind9 + "}\n" + __ind9 + "catch (" + __e8 + ") {\n" + __h3 + __ind9 + "}\n"; }, stmt: true, tr: true}); setenv("%delete", {_stash: true, special: function (place) { return indentation() + "delete " + compile(place); @@ -1197,16 +1236,16 @@ setenv("%function", {_stash: true, special: function (args, body) { }}); setenv("%global-function", {_stash: true, special: function (name, args, body) { if (target === "lua") { - var __x157 = compile_function(args, body, {_stash: true, name: name}); - return indentation() + __x157; + var __x165 = compile_function(args, body, {_stash: true, name: name}); + return indentation() + __x165; } else { return compile(["%set", name, ["%function", args, body]], {_stash: true, stmt: true}); } }, stmt: true, tr: true}); setenv("%local-function", {_stash: true, special: function (name, args, body) { if (target === "lua") { - var __x163 = compile_function(args, body, {_stash: true, name: name, prefix: "local"}); - return indentation() + __x163; + var __x171 = compile_function(args, body, {_stash: true, name: name, prefix: "local"}); + return indentation() + __x171; } else { return compile(["%local", name, ["%function", args, body]], {_stash: true, stmt: true}); } @@ -1218,8 +1257,8 @@ setenv("return", {_stash: true, special: function (x) { } else { __e55 = "return " + compile(x); } - var __x167 = __e55; - return indentation() + __x167; + var __x175 = __e55; + return indentation() + __x175; }, stmt: true}); setenv("new", {_stash: true, special: function (x) { return "new " + compile(x); @@ -1238,7 +1277,7 @@ setenv("throw", {_stash: true, special: function (x) { return indentation() + __e12; }, stmt: true}); setenv("%local", {_stash: true, special: function (name, value) { - var __id28 = compile(name); + var __id30 = compile(name); var __value11 = compile(value); var __e57 = undefined; if (is63(value)) { @@ -1255,7 +1294,7 @@ setenv("%local", {_stash: true, special: function (name, value) { } var __keyword1 = __e58; var __ind11 = indentation(); - return __ind11 + __keyword1 + __id28 + __rh2; + return __ind11 + __keyword1 + __id30 + __rh2; }, stmt: true}); setenv("%set", {_stash: true, special: function (lh, rh) { var __lh2 = compile(lh); @@ -1265,7 +1304,8 @@ setenv("%set", {_stash: true, special: function (lh, rh) { } else { __e59 = rh; } - var __rh4 = compile(__e59); + var __arg6 = compile(__e59); + var __rh4 = __arg6; return indentation() + __lh2 + " = " + __rh4; }, stmt: true}); setenv("get", {_stash: true, special: function (t, k) { @@ -1301,7 +1341,7 @@ setenv("%array", {_stash: true, special: function () { var ____o10 = __forms3; var __k16 = undefined; for (__k16 in ____o10) { - var __v9 = ____o10[__k16]; + var __v10 = ____o10[__k16]; var __e62 = undefined; if (numeric63(__k16)) { __e62 = parseInt(__k16); @@ -1310,7 +1350,7 @@ setenv("%array", {_stash: true, special: function () { } var __k17 = __e62; if (number63(__k17)) { - __s7 = __s7 + __c7 + compile(__v9); + __s7 = __s7 + __c7 + compile(__v10); __c7 = ", "; } } @@ -1330,7 +1370,7 @@ setenv("%object", {_stash: true, special: function () { var ____o12 = pair(__forms5); var __k21 = undefined; for (__k21 in ____o12) { - var __v12 = ____o12[__k21]; + var __v13 = ____o12[__k21]; var __e64 = undefined; if (numeric63(__k21)) { __e64 = parseInt(__k21); @@ -1339,21 +1379,21 @@ setenv("%object", {_stash: true, special: function () { } var __k22 = __e64; if (number63(__k22)) { - var ____id30 = __v12; - var __k23 = ____id30[0]; - var __v13 = ____id30[1]; + var ____id32 = __v13; + var __k23 = ____id32[0]; + var __v14 = ____id32[1]; if (! string63(__k23)) { error("Illegal key: " + str(__k23)); } - __s9 = __s9 + __c9 + key(__k23) + __sep1 + compile(__v13); + __s9 = __s9 + __c9 + key(__k23) + __sep1 + compile(__v14); __c9 = ", "; } } return __s9 + "}"; }}); setenv("%literal", {_stash: true, special: function () { - var __args111 = unstash(Array.prototype.slice.call(arguments, 0)); - return apply(cat, map(compile, __args111)); + var __args121 = unstash(Array.prototype.slice.call(arguments, 0)); + return apply(cat, map(compile, __args121)); }}); exports.run = run; exports["eval"] = _eval; diff --git a/bin/compiler.lua b/bin/compiler.lua index c1ba3f0..600a2a1 100644 --- a/bin/compiler.lua +++ b/bin/compiler.lua @@ -198,7 +198,8 @@ local function expand_function(__x41) local ____x43 = ____o3[____i5] setenv(____x43, {_stash = true, variable = true}) end - local ____x44 = join({"%function", __args}, macroexpand(__body)) + local __arg2 = join({"%function", __args}, macroexpand(__body)) + local ____x44 = __arg2 drop(environment) return ____x44 end @@ -215,7 +216,8 @@ local function expand_definition(__x46) local ____x48 = ____o4[____i6] setenv(____x48, {_stash = true, variable = true}) end - local ____x49 = join({__x47, __name1, __args11}, macroexpand(__body1)) + local __arg3 = join({__x47, __name1, __args11}, macroexpand(__body1)) + local ____x49 = __arg3 drop(environment) return ____x49 end @@ -843,7 +845,8 @@ local function lower_if(args, hoist, stmt63, tail63) if is63(___else) then __e43 = {lower_body({___else}, tail63)} end - return add(hoist, join({"%if", lower(__cond, hoist), lower_body({___then}, tail63)}, __e43)) + local __arg5 = join({"%if", lower(__cond, hoist), lower_body({___then}, tail63)}, __e43) + return add(hoist, __arg5) else local __e3 = unique("e") add(hoist, {"%local", __e3, "nil"}) @@ -851,7 +854,8 @@ local function lower_if(args, hoist, stmt63, tail63) if is63(___else) then __e42 = {lower({"%set", __e3, ___else})} end - add(hoist, join({"%if", lower(__cond, hoist), lower({"%set", __e3, ___then})}, __e42)) + local __arg4 = join({"%if", lower(__cond, hoist), lower({"%set", __e3, ___then})}, __e42) + add(hoist, __arg4) return __e3 end end @@ -911,12 +915,47 @@ local function lower_definition(kind, args, hoist) local __body8 = cut(____id23, 2) return add(hoist, {kind, __name4, __args6, lower_body(__body8, true)}) end -local function lower_call(form, hoist) - local __form2 = map(function (x) - return lower(x, hoist) - end, form) - if some63(__form2) then - return __form2 +local function lower_args(args, hoist) + local __simple = true + local __args13 = map(function (x) + local __h = {} + local __x127 = lower(x, __h) + if some63(__h) then + __simple = false + end + return {__h, __x127} + end, args) + return map(function (__x129) + local ____id24 = __x129 + local __h1 = ____id24[1] + local __x130 = ____id24[2] + local ____x131 = __h1 + local ____i18 = 0 + while ____i18 < _35(____x131) do + local __v8 = ____x131[____i18 + 1] + add(hoist, __v8) + ____i18 = ____i18 + 1 + end + if __simple or literal63(__x130) then + return __x130 + else + local __arg = unique("arg") + add(hoist, {"%local", __arg, __x130}) + return __arg + end + end, __args13) +end +local function lower_call(__x133, hoist) + local ____id25 = __x133 + local __f11 = ____id25[1] + local __args7 = cut(____id25, 1) + local __f2 = lower(__f11, hoist) + if nil63(__f2) then + if some63(__args7) then + return lower_call(__args7, hoist) + end + else + return join({__f2}, lower_args(__args7, hoist)) end end local function pairwise63(form) @@ -925,13 +964,13 @@ end local function lower_pairwise(form) if pairwise63(form) then local __e4 = {} - local ____id24 = form - local __x128 = ____id24[1] - local __args7 = cut(____id24, 1) + local ____id26 = form + local __x136 = ____id26[1] + local __args8 = cut(____id26, 1) reduce(function (a, b) - add(__e4, {__x128, a, b}) + add(__e4, {__x136, a, b}) return a - end, __args7) + end, __args8) return join({"and"}, reverse(__e4)) else return form @@ -941,13 +980,13 @@ local function lower_infix63(form) return infix63(hd(form)) and _35(form) > 3 end local function lower_infix(form, hoist) - local __form3 = lower_pairwise(form) - local ____id25 = __form3 - local __x131 = ____id25[1] - local __args8 = cut(____id25, 1) + local __form2 = lower_pairwise(form) + local ____id27 = __form2 + local __x139 = ____id27[1] + local __args9 = cut(____id27, 1) return lower(reduce(function (a, b) - return {__x131, b, a} - end, reverse(__args8)), hoist) + return {__x139, b, a} + end, reverse(__args9)), hoist) end local function lower_special(form, hoist) local __e5 = lower_call(form, hoist) @@ -968,40 +1007,40 @@ function lower(form, hoist, stmt63, tail63) if lower_infix63(form) then return lower_infix(form, hoist) else - local ____id26 = form - local __x134 = ____id26[1] - local __args9 = cut(____id26, 1) - if __x134 == "do" then - return lower_do(__args9, hoist, stmt63, tail63) + local ____id28 = form + local __x142 = ____id28[1] + local __args10 = cut(____id28, 1) + if __x142 == "do" then + return lower_do(__args10, hoist, stmt63, tail63) else - if __x134 == "%call" then - return lower(__args9, hoist, stmt63, tail63) + if __x142 == "%call" then + return lower(__args10, hoist, stmt63, tail63) else - if __x134 == "%set" then - return lower_set(__args9, hoist, stmt63, tail63) + if __x142 == "%set" then + return lower_set(__args10, hoist, stmt63, tail63) else - if __x134 == "%if" then - return lower_if(__args9, hoist, stmt63, tail63) + if __x142 == "%if" then + return lower_if(__args10, hoist, stmt63, tail63) else - if __x134 == "%try" then - return lower_try(__args9, hoist, tail63) + if __x142 == "%try" then + return lower_try(__args10, hoist, tail63) else - if __x134 == "while" then - return lower_while(__args9, hoist) + if __x142 == "while" then + return lower_while(__args10, hoist) else - if __x134 == "%for" then - return lower_for(__args9, hoist) + if __x142 == "%for" then + return lower_for(__args10, hoist) else - if __x134 == "%function" then - return lower_function(__args9) + if __x142 == "%function" then + return lower_function(__args10) else - if __x134 == "%local-function" or __x134 == "%global-function" then - return lower_definition(__x134, __args9, hoist) + if __x142 == "%local-function" or __x142 == "%global-function" then + return lower_definition(__x142, __args10, hoist) else - if in63(__x134, {"and", "or"}) then - return lower_short(__x134, __args9, hoist) + if in63(__x142, {"and", "or"}) then + return lower_short(__x142, __args10, hoist) else - if statement63(__x134) then + if statement63(__x142) then return lower_special(form, hoist) else return lower_call(form, hoist) @@ -1048,35 +1087,35 @@ end setenv("do", {_stash = true, special = function (...) local __forms1 = unstash({...}) local __s3 = "" - local ____x140 = __forms1 - local ____i19 = 0 - while ____i19 < _35(____x140) do - local __x141 = ____x140[____i19 + 1] - if target == "lua" and immediate_call63(__x141) and "\n" == char(__s3, edge(__s3)) then + local ____x148 = __forms1 + local ____i20 = 0 + while ____i20 < _35(____x148) do + local __x149 = ____x148[____i20 + 1] + if target == "lua" and immediate_call63(__x149) and "\n" == char(__s3, edge(__s3)) then __s3 = clip(__s3, 0, edge(__s3)) .. ";\n" end - __s3 = __s3 .. compile(__x141, {_stash = true, stmt = true}) - if not atom63(__x141) then - if hd(__x141) == "return" or hd(__x141) == "break" then + __s3 = __s3 .. compile(__x149, {_stash = true, stmt = true}) + if not atom63(__x149) then + if hd(__x149) == "return" or hd(__x149) == "break" then break end end - ____i19 = ____i19 + 1 + ____i20 = ____i20 + 1 end return __s3 end, stmt = true, tr = true}) setenv("%if", {_stash = true, special = function (cond, cons, alt) local __cond2 = compile(cond) indent_level = indent_level + 1 - local ____x144 = compile(cons, {_stash = true, stmt = true}) + local ____x152 = compile(cons, {_stash = true, stmt = true}) indent_level = indent_level - 1 - local __cons1 = ____x144 + local __cons1 = ____x152 local __e46 = nil if alt then indent_level = indent_level + 1 - local ____x145 = compile(alt, {_stash = true, stmt = true}) + local ____x153 = compile(alt, {_stash = true, stmt = true}) indent_level = indent_level - 1 - __e46 = ____x145 + __e46 = ____x153 end local __alt1 = __e46 local __ind3 = indentation() @@ -1102,9 +1141,9 @@ end, stmt = true, tr = true}) setenv("while", {_stash = true, special = function (cond, form) local __cond4 = compile(cond) indent_level = indent_level + 1 - local ____x147 = compile(form, {_stash = true, stmt = true}) + local ____x155 = compile(form, {_stash = true, stmt = true}) indent_level = indent_level - 1 - local __body10 = ____x147 + local __body10 = ____x155 local __ind5 = indentation() if target == "js" then return __ind5 .. "while (" .. __cond4 .. ") {\n" .. __body10 .. __ind5 .. "}\n" @@ -1116,9 +1155,9 @@ setenv("%for", {_stash = true, special = function (t, k, form) local __t2 = compile(t) local __ind7 = indentation() indent_level = indent_level + 1 - local ____x149 = compile(form, {_stash = true, stmt = true}) + local ____x157 = compile(form, {_stash = true, stmt = true}) indent_level = indent_level - 1 - local __body12 = ____x149 + local __body12 = ____x157 if target == "lua" then return __ind7 .. "for " .. k .. " in next, " .. __t2 .. " do\n" .. __body12 .. __ind7 .. "end\n" else @@ -1129,15 +1168,15 @@ setenv("%try", {_stash = true, special = function (form) local __e8 = unique("e") local __ind9 = indentation() indent_level = indent_level + 1 - local ____x154 = compile(form, {_stash = true, stmt = true}) + local ____x162 = compile(form, {_stash = true, stmt = true}) indent_level = indent_level - 1 - local __body14 = ____x154 + local __body14 = ____x162 local __hf1 = {"return", {"%array", false, __e8}} indent_level = indent_level + 1 - local ____x157 = compile(__hf1, {_stash = true, stmt = true}) + local ____x165 = compile(__hf1, {_stash = true, stmt = true}) indent_level = indent_level - 1 - local __h1 = ____x157 - return __ind9 .. "try {\n" .. __body14 .. __ind9 .. "}\n" .. __ind9 .. "catch (" .. __e8 .. ") {\n" .. __h1 .. __ind9 .. "}\n" + local __h3 = ____x165 + return __ind9 .. "try {\n" .. __body14 .. __ind9 .. "}\n" .. __ind9 .. "catch (" .. __e8 .. ") {\n" .. __h3 .. __ind9 .. "}\n" end, stmt = true, tr = true}) setenv("%delete", {_stash = true, special = function (place) return indentation() .. "delete " .. compile(place) @@ -1150,16 +1189,16 @@ setenv("%function", {_stash = true, special = function (args, body) end}) setenv("%global-function", {_stash = true, special = function (name, args, body) if target == "lua" then - local __x161 = compile_function(args, body, {_stash = true, name = name}) - return indentation() .. __x161 + local __x169 = compile_function(args, body, {_stash = true, name = name}) + return indentation() .. __x169 else return compile({"%set", name, {"%function", args, body}}, {_stash = true, stmt = true}) end end, stmt = true, tr = true}) setenv("%local-function", {_stash = true, special = function (name, args, body) if target == "lua" then - local __x167 = compile_function(args, body, {_stash = true, name = name, prefix = "local"}) - return indentation() .. __x167 + local __x175 = compile_function(args, body, {_stash = true, name = name, prefix = "local"}) + return indentation() .. __x175 else return compile({"%local", name, {"%function", args, body}}, {_stash = true, stmt = true}) end @@ -1171,8 +1210,8 @@ setenv("return", {_stash = true, special = function (x) else __e47 = "return " .. compile(x) end - local __x171 = __e47 - return indentation() .. __x171 + local __x179 = __e47 + return indentation() .. __x179 end, stmt = true}) setenv("new", {_stash = true, special = function (x) return "new " .. compile(x) @@ -1191,7 +1230,7 @@ setenv("throw", {_stash = true, special = function (x) return indentation() .. __e12 end, stmt = true}) setenv("%local", {_stash = true, special = function (name, value) - local __id28 = compile(name) + local __id30 = compile(name) local __value11 = compile(value) local __e49 = nil if is63(value) then @@ -1208,7 +1247,7 @@ setenv("%local", {_stash = true, special = function (name, value) end local __keyword1 = __e50 local __ind11 = indentation() - return __ind11 .. __keyword1 .. __id28 .. __rh2 + return __ind11 .. __keyword1 .. __id30 .. __rh2 end, stmt = true}) setenv("%set", {_stash = true, special = function (lh, rh) local __lh2 = compile(lh) @@ -1218,7 +1257,8 @@ setenv("%set", {_stash = true, special = function (lh, rh) else __e51 = rh end - local __rh4 = compile(__e51) + local __arg6 = compile(__e51) + local __rh4 = __arg6 return indentation() .. __lh2 .. " = " .. __rh4 end, stmt = true}) setenv("get", {_stash = true, special = function (t, k) @@ -1254,9 +1294,9 @@ setenv("%array", {_stash = true, special = function (...) local ____o10 = __forms3 local __k10 = nil for __k10 in next, ____o10 do - local __v9 = ____o10[__k10] + local __v10 = ____o10[__k10] if number63(__k10) then - __s7 = __s7 .. __c7 .. compile(__v9) + __s7 = __s7 .. __c7 .. compile(__v10) __c7 = ", " end end @@ -1276,22 +1316,22 @@ setenv("%object", {_stash = true, special = function (...) local ____o12 = pair(__forms5) local __k14 = nil for __k14 in next, ____o12 do - local __v12 = ____o12[__k14] + local __v13 = ____o12[__k14] if number63(__k14) then - local ____id30 = __v12 - local __k15 = ____id30[1] - local __v13 = ____id30[2] + local ____id32 = __v13 + local __k15 = ____id32[1] + local __v14 = ____id32[2] if not string63(__k15) then error("Illegal key: " .. str(__k15)) end - __s9 = __s9 .. __c9 .. key(__k15) .. __sep1 .. compile(__v13) + __s9 = __s9 .. __c9 .. key(__k15) .. __sep1 .. compile(__v14) __c9 = ", " end end return __s9 .. "}" end}) setenv("%literal", {_stash = true, special = function (...) - local __args111 = unstash({...}) - return apply(cat, map(compile, __args111)) + local __args121 = unstash({...}) + return apply(cat, map(compile, __args121)) end}) return {run = run, ["eval"] = _eval, expand = expand, compile = compile} diff --git a/bin/lumen.js b/bin/lumen.js index f760831..53b82cb 100644 --- a/bin/lumen.js +++ b/bin/lumen.js @@ -919,7 +919,9 @@ setenv("define-special", {_stash: true, macro: function (name, args) { var __body17 = cut(____id25, 0); var ____x109 = ["setenv", ["quote", __name3]]; ____x109.special = join(["fn", __args5], __body17); - var __form3 = join(____x109, keys(__body17)); + var __arg5 = keys(__body17); + var __arg6 = join(____x109, __arg5); + var __form3 = __arg6; _eval(__form3); return __form3; }}); @@ -991,7 +993,8 @@ setenv("let-macro", {_stash: true, macro: function (definitions) { map(function (m) { return macroexpand(join(["define-macro"], m)); }, __definitions1); - var ____x167 = join(["do"], macroexpand(__body29)); + var __arg7 = join(["do"], macroexpand(__body29)); + var ____x167 = __arg7; drop(environment); return ____x167; }}); @@ -1007,7 +1010,8 @@ setenv("let-symbol", {_stash: true, macro: function (expansions) { var __exp1 = ____id43[1]; return macroexpand(["define-symbol", __name9, __exp1]); }, pair(__expansions1)); - var ____x174 = join(["do"], macroexpand(__body31)); + var __arg8 = join(["do"], macroexpand(__body31)); + var ____x174 = __arg8; drop(environment); return ____x174; }}); @@ -1079,7 +1083,8 @@ setenv("each", {_stash: true, macro: function (x, t) { } else { __e11 = [join(["let", __k5, ["if", ["numeric?", __k5], ["parseInt", __k5], __k5]], __body37)]; } - return ["let", [__o3, __t1, __k5, "nil"], ["%for", __o3, __k5, join(["let", [__v7, ["get", __o3, __k5]]], __e11)]]; + var __arg9 = join(["let", [__v7, ["get", __o3, __k5]]], __e11); + return ["let", [__o3, __t1, __k5, "nil"], ["%for", __o3, __k5, __arg9]]; }}); setenv("for", {_stash: true, macro: function (i, to) { var ____r63 = unstash(Array.prototype.slice.call(arguments, 2)); diff --git a/bin/lumen.lua b/bin/lumen.lua index 6478c38..cd4a3f1 100644 --- a/bin/lumen.lua +++ b/bin/lumen.lua @@ -807,7 +807,9 @@ setenv("define-special", {_stash = true, macro = function (name, args, ...) local __body17 = cut(____id25, 0) local ____x121 = {"setenv", {"quote", __name3}} ____x121.special = join({"fn", __args5}, __body17) - local __form3 = join(____x121, keys(__body17)) + local __arg5 = keys(__body17) + local __arg6 = join(____x121, __arg5) + local __form3 = __arg6 _eval(__form3) return __form3 end}) @@ -879,7 +881,8 @@ setenv("let-macro", {_stash = true, macro = function (definitions, ...) map(function (m) return macroexpand(join({"define-macro"}, m)) end, __definitions1) - local ____x185 = join({"do"}, macroexpand(__body29)) + local __arg7 = join({"do"}, macroexpand(__body29)) + local ____x185 = __arg7 drop(environment) return ____x185 end}) @@ -895,7 +898,8 @@ setenv("let-symbol", {_stash = true, macro = function (expansions, ...) local __exp1 = ____id43[2] return macroexpand({"define-symbol", __name9, __exp1}) end, pair(__expansions1)) - local ____x193 = join({"do"}, macroexpand(__body31)) + local __arg8 = join({"do"}, macroexpand(__body31)) + local ____x193 = __arg8 drop(environment) return ____x193 end}) @@ -967,7 +971,8 @@ setenv("each", {_stash = true, macro = function (x, t, ...) else __e10 = {join({"let", __k4, {"if", {"numeric?", __k4}, {"parseInt", __k4}, __k4}}, __body37)} end - return {"let", {__o3, __t1, __k4, "nil"}, {"%for", __o3, __k4, join({"let", {__v7, {"get", __o3, __k4}}}, __e10)}} + local __arg9 = join({"let", {__v7, {"get", __o3, __k4}}}, __e10) + return {"let", {__o3, __t1, __k4, "nil"}, {"%for", __o3, __k4, __arg9}} end}) setenv("for", {_stash = true, macro = function (i, to, ...) local ____r63 = unstash({...}) @@ -1080,6 +1085,7 @@ local function eval_print(form) if obj63(m) then return m else + local __arg1 = debug.traceback() local __e = nil if string63(m) then __e = clip(m, (search(m, ": ") or -2) + 2) @@ -1092,7 +1098,7 @@ local function eval_print(form) end __e = __e1 end - return {stack = debug.traceback(), message = __e} + return {stack = __arg1, message = __e} end end)} local __ok = ____id[1] diff --git a/bin/reader.js b/bin/reader.js index 5b8e744..adb3b57 100644 --- a/bin/reader.js +++ b/bin/reader.js @@ -100,7 +100,8 @@ var hex_prefix63 = function (str) { var __e1 = undefined; if (__id2) { __i = __i + 1; - var __n = code(str, __i); + var __arg1 = code(str, __i); + var __n = __arg1; __e1 = __n === 120 || __n === 88; } else { __e1 = __id2; diff --git a/bin/reader.lua b/bin/reader.lua index c0cf2c9..2071fba 100644 --- a/bin/reader.lua +++ b/bin/reader.lua @@ -100,7 +100,8 @@ local function hex_prefix63(str) local __e1 = nil if __id2 then __i = __i + 1 - local __n = code(str, __i) + local __arg1 = code(str, __i) + local __n = __arg1 __e1 = __n == 120 or __n == 88 else __e1 = __id2 diff --git a/compiler.l b/compiler.l index de2bbbd..8a7fa6a 100644 --- a/compiler.l +++ b/compiler.l @@ -513,9 +513,25 @@ (let ((name args rest: body) args) (add hoist `(,kind ,name ,args ,(lower-body body true))))) -(define lower-call (form hoist) - (let form (map (fn (x) (lower x hoist)) form) - (if (some? form) form))) +(define lower-args (args hoist) + (let (simple true + args1 (map (fn (x) + (let (h () x (lower x h)) + (if (some? h) (set simple false)) + (list h x))) + args)) + (map (fn ((h x)) + (step v h + (add hoist v)) + (if (or simple (literal? x)) x + (with arg (unique 'arg) + (add hoist `(%local ,arg ,x))))) + args1))) + +(define lower-call ((f rest: args) hoist) + (let f (lower f hoist) + (if (nil? f) (if (some? args) (lower-call args hoist)) + `(,f ,@(lower-args args hoist))))) (define pairwise? (form) (in? (hd form) '(< <= = >= >))) diff --git a/test.l b/test.l index 80b089a..6b25faf 100755 --- a/test.l +++ b/test.l @@ -338,7 +338,10 @@ c" (test= 42 ((at l 0))) (test= 42 ((get t 'f))) (test= nil ((fn () (return)))) - (test= 10 ((fn (x) (- x 2)) 12)))) + (test= 10 ((fn (x) (- x 2)) 12)) + (let (r () + l (list (add r 1) (if true (add r 2)))) + (test= '(1 2) r)))) (define-test id (let (a 10