Skip to content

Commit

Permalink
Get rid of global %message-handler
Browse files Browse the repository at this point in the history
Now that we're passing a function to xpcall, we can inline the message
processing and avoid relying on a runtime function.
  • Loading branch information
Scott Bell committed Dec 5, 2015
1 parent e30be7c commit 6f59319
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 106 deletions.
64 changes: 32 additions & 32 deletions bin/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ indentation = function () {
}
return(s);
};
var reserved = {"else": true, "<": true, "true": true, "/": true, "end": true, "typeof": true, "function": true, "switch": true, "=": true, "or": true, "try": true, "catch": true, "until": true, "local": true, "repeat": true, "-": true, "false": true, "continue": true, "==": true, "and": true, "if": true, "for": true, ">=": true, "<=": true, "with": true, "return": true, "finally": true, "nil": true, "new": true, "do": true, "case": true, "break": true, "elseif": true, "+": true, "not": true, "void": true, "var": true, "%": true, "in": true, "delete": true, "throw": true, "debugger": true, "instanceof": true, "this": true, "while": true, "then": true, "default": true, "*": true, ">": true};
var reserved = {"==": true, "do": true, "instanceof": true, "for": true, "var": true, "*": true, "else": true, "switch": true, "this": true, "=": true, "debugger": true, "return": true, "continue": true, "true": true, "/": true, "not": true, "try": true, "until": true, "finally": true, "then": true, "repeat": true, "nil": true, "local": true, "<=": true, "new": true, "if": true, "while": true, "void": true, "case": true, "%": true, "-": true, "and": true, "+": true, ">": true, "or": true, "<": true, "false": true, "end": true, "typeof": true, "elseif": true, "default": true, "in": true, "catch": true, "function": true, ">=": true, "throw": true, "with": true, "delete": true, "break": true};
reserved63 = function (x) {
return(reserved[x]);
};
Expand Down Expand Up @@ -462,8 +462,8 @@ _x58.js = "!";
__x57["not"] = _x58;
var __x59 = [];
__x59["/"] = true;
__x59["*"] = true;
__x59["%"] = true;
__x59["*"] = true;
var __x60 = [];
__x60["+"] = true;
__x60["-"] = true;
Expand All @@ -473,10 +473,10 @@ _x62.lua = "..";
_x62.js = "+";
__x61.cat = _x62;
var __x63 = [];
__x63["<="] = true;
__x63[">="] = true;
__x63["<"] = true;
__x63[">"] = true;
__x63["<"] = true;
__x63[">="] = true;
__x63["<="] = true;
var __x64 = [];
var _x65 = [];
_x65.lua = "==";
Expand Down Expand Up @@ -661,9 +661,9 @@ var compile_special = function (form, stmt63) {
var x = _id5[0];
var args = cut(_id5, 1);
var _id6 = getenv(x);
var self_tr63 = _id6.tr;
var stmt = _id6.stmt;
var special = _id6.special;
var self_tr63 = _id6.tr;
var tr = terminator(stmt63 && ! self_tr63);
return(apply(special, args) + tr);
};
Expand Down Expand Up @@ -1024,7 +1024,7 @@ eval = function (form) {
run(code);
return(_37result);
};
setenv("do", {_stash: true, tr: true, special: function () {
setenv("do", {_stash: true, stmt: true, special: function () {
var forms = unstash(Array.prototype.slice.call(arguments, 0));
var s = "";
var _x107 = forms;
Expand All @@ -1036,8 +1036,8 @@ setenv("do", {_stash: true, tr: true, special: function () {
_i12 = _i12 + 1;
}
return(s);
}, stmt: true});
setenv("%if", {_stash: true, tr: true, special: function (cond, cons, alt) {
}, tr: true});
setenv("%if", {_stash: true, stmt: true, special: function (cond, cons, alt) {
var _cond1 = compile(cond);
indent_level = indent_level + 1;
var _x110 = compile(cons, {_stash: true, stmt: true});
Expand Down Expand Up @@ -1070,8 +1070,8 @@ setenv("%if", {_stash: true, tr: true, special: function (cond, cons, alt) {
} else {
return(s + "\n");
}
}, stmt: true});
setenv("while", {_stash: true, tr: true, special: function (cond, form) {
}, tr: true});
setenv("while", {_stash: true, stmt: true, special: function (cond, form) {
var _cond3 = compile(cond);
indent_level = indent_level + 1;
var _x113 = compile(form, {_stash: true, stmt: true});
Expand All @@ -1083,8 +1083,8 @@ setenv("while", {_stash: true, tr: true, special: function (cond, form) {
} else {
return(ind + "while " + _cond3 + " do\n" + body + ind + "end\n");
}
}, stmt: true});
setenv("%for", {_stash: true, tr: true, special: function (t, k, form) {
}, tr: true});
setenv("%for", {_stash: true, stmt: true, special: function (t, k, form) {
var _t1 = compile(t);
var ind = indentation();
indent_level = indent_level + 1;
Expand All @@ -1096,8 +1096,8 @@ setenv("%for", {_stash: true, tr: true, special: function (t, k, form) {
} else {
return(ind + "for (" + k + " in " + _t1 + ") {\n" + body + ind + "}\n");
}
}, stmt: true});
setenv("%try", {_stash: true, tr: true, special: function (form) {
}, tr: true});
setenv("%try", {_stash: true, stmt: true, special: function (form) {
var e = unique("e");
var ind = indentation();
indent_level = indent_level + 1;
Expand All @@ -1110,33 +1110,33 @@ setenv("%try", {_stash: true, tr: true, special: function (form) {
indent_level = indent_level - 1;
var h = _x127;
return(ind + "try {\n" + body + ind + "}\n" + ind + "catch (" + e + ") {\n" + h + ind + "}\n");
}, stmt: true});
setenv("%delete", {_stash: true, special: function (place) {
}, tr: true});
setenv("%delete", {_stash: true, stmt: true, special: function (place) {
return(indentation() + "delete " + compile(place));
}, stmt: true});
setenv("break", {_stash: true, special: function () {
}});
setenv("break", {_stash: true, stmt: true, special: function () {
return(indentation() + "break");
}, stmt: true});
}});
setenv("%function", {_stash: true, special: function (args, body) {
return(compile_function(args, body));
}});
setenv("%global-function", {_stash: true, tr: true, special: function (name, args, body) {
setenv("%global-function", {_stash: true, stmt: true, special: function (name, args, body) {
if (target === "lua") {
var x = compile_function(args, body, {_stash: true, name: name});
return(indentation() + x);
} else {
return(compile(["set", name, ["%function", args, body]], {_stash: true, stmt: true}));
}
}, stmt: true});
setenv("%local-function", {_stash: true, tr: true, special: function (name, args, body) {
}, tr: true});
setenv("%local-function", {_stash: true, stmt: true, special: function (name, args, body) {
if (target === "lua") {
var x = compile_function(args, body, {_stash: true, name: name, prefix: "local"});
return(indentation() + x);
} else {
return(compile(["%local", name, ["%function", args, body]], {_stash: true, stmt: true}));
}
}, stmt: true});
setenv("return", {_stash: true, special: function (x) {
}, tr: true});
setenv("return", {_stash: true, stmt: true, special: function (x) {
var _e35;
if (nil63(x)) {
_e35 = "return";
Expand All @@ -1145,14 +1145,14 @@ setenv("return", {_stash: true, special: function (x) {
}
var _x137 = _e35;
return(indentation() + _x137);
}, stmt: true});
}});
setenv("new", {_stash: true, special: function (x) {
return("new " + compile(x));
}});
setenv("typeof", {_stash: true, special: function (x) {
return("typeof(" + compile(x) + ")");
}});
setenv("error", {_stash: true, special: function (x) {
setenv("error", {_stash: true, stmt: true, special: function (x) {
var _e36;
if (target === "js") {
_e36 = "throw " + compile(["new", ["Error", x]]);
Expand All @@ -1161,8 +1161,8 @@ setenv("error", {_stash: true, special: function (x) {
}
var e = _e36;
return(indentation() + e);
}, stmt: true});
setenv("%local", {_stash: true, special: function (name, value) {
}});
setenv("%local", {_stash: true, stmt: true, special: function (name, value) {
var _id26 = compile(name);
var value1 = compile(value);
var _e37;
Expand All @@ -1181,8 +1181,8 @@ setenv("%local", {_stash: true, special: function (name, value) {
var keyword = _e38;
var ind = indentation();
return(ind + keyword + _id26 + rh);
}, stmt: true});
setenv("set", {_stash: true, special: function (lh, rh) {
}});
setenv("set", {_stash: true, stmt: true, special: function (lh, rh) {
var _lh1 = compile(lh);
var _e39;
if (nil63(rh)) {
Expand All @@ -1192,7 +1192,7 @@ setenv("set", {_stash: true, special: function (lh, rh) {
}
var _rh1 = compile(_e39);
return(indentation() + _lh1 + " = " + _rh1);
}, stmt: true});
}});
setenv("get", {_stash: true, special: function (t, k) {
var _t3 = compile(t);
var k1 = compile(k);
Expand Down
50 changes: 25 additions & 25 deletions bin/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ function indentation()
end
return(s)
end
local reserved = {["else"] = true, ["<"] = true, ["true"] = true, ["/"] = true, ["end"] = true, ["typeof"] = true, ["function"] = true, ["switch"] = true, ["="] = true, ["or"] = true, ["try"] = true, ["catch"] = true, ["until"] = true, ["local"] = true, ["repeat"] = true, ["-"] = true, ["false"] = true, ["continue"] = true, ["=="] = true, ["and"] = true, ["if"] = true, ["for"] = true, [">="] = true, ["<="] = true, ["with"] = true, ["return"] = true, ["finally"] = true, ["nil"] = true, ["new"] = true, ["do"] = true, ["case"] = true, ["break"] = true, ["elseif"] = true, ["+"] = true, ["not"] = true, ["void"] = true, ["var"] = true, ["%"] = true, ["in"] = true, ["delete"] = true, ["throw"] = true, ["debugger"] = true, ["instanceof"] = true, ["this"] = true, ["while"] = true, ["then"] = true, ["default"] = true, ["*"] = true, [">"] = true}
local reserved = {["return"] = true, ["default"] = true, ["continue"] = true, ["var"] = true, ["+"] = true, ["void"] = true, ["/"] = true, ["function"] = true, ["while"] = true, ["elseif"] = true, ["<="] = true, ["="] = true, ["new"] = true, ["instanceof"] = true, ["with"] = true, ["for"] = true, ["break"] = true, ["this"] = true, ["typeof"] = true, ["-"] = true, ["if"] = true, ["false"] = true, ["try"] = true, ["%"] = true, ["catch"] = true, ["end"] = true, ["case"] = true, ["*"] = true, ["repeat"] = true, ["debugger"] = true, ["and"] = true, ["finally"] = true, ["true"] = true, ["local"] = true, ["delete"] = true, ["not"] = true, ["throw"] = true, ["=="] = true, ["do"] = true, ["or"] = true, ["then"] = true, ["nil"] = true, ["<"] = true, ["switch"] = true, [">"] = true, [">="] = true, ["in"] = true, ["until"] = true, ["else"] = true}
function reserved63(x)
return(reserved[x])
end
Expand Down Expand Up @@ -408,40 +408,40 @@ function mapo(f, t)
end
local __x57 = {}
local _x58 = {}
_x58.lua = "not"
_x58.js = "!"
_x58.lua = "not"
__x57["not"] = _x58
local __x59 = {}
__x59["/"] = true
__x59["*"] = true
__x59["/"] = true
__x59["%"] = true
local __x60 = {}
__x60["+"] = true
__x60["-"] = true
__x60["+"] = true
local __x61 = {}
local _x62 = {}
_x62.lua = ".."
_x62.js = "+"
_x62.lua = ".."
__x61.cat = _x62
local __x63 = {}
__x63["<="] = true
__x63[">="] = true
__x63["<="] = true
__x63["<"] = true
__x63[">"] = true
local __x64 = {}
local _x65 = {}
_x65.lua = "=="
_x65.js = "==="
_x65.lua = "=="
__x64["="] = _x65
local __x66 = {}
local _x67 = {}
_x67.lua = "and"
_x67.js = "&&"
_x67.lua = "and"
__x66["and"] = _x67
local __x68 = {}
local _x69 = {}
_x69.lua = "or"
_x69.js = "||"
_x69.lua = "or"
__x68["or"] = _x69
local infix = {__x57, __x59, __x60, __x61, __x63, __x64, __x66, __x68}
local function unary63(form)
Expand Down Expand Up @@ -607,9 +607,9 @@ local function compile_special(form, stmt63)
local x = _id5[1]
local args = cut(_id5, 1)
local _id6 = getenv(x)
local self_tr63 = _id6.tr
local stmt = _id6.stmt
local special = _id6.special
local self_tr63 = _id6.tr
local tr = terminator(stmt63 and not self_tr63)
return(apply(special, args) .. tr)
end
Expand Down Expand Up @@ -977,7 +977,7 @@ function eval(form)
run(code)
return(_37result)
end
setenv("do", {_stash = true, tr = true, special = function (...)
setenv("do", {_stash = true, special = function (...)
local forms = unstash({...})
local s = ""
local _x111 = forms
Expand All @@ -989,8 +989,8 @@ setenv("do", {_stash = true, tr = true, special = function (...)
_i12 = _i12 + 1
end
return(s)
end, stmt = true})
setenv("%if", {_stash = true, tr = true, special = function (cond, cons, alt)
end, stmt = true, tr = true})
setenv("%if", {_stash = true, special = function (cond, cons, alt)
local _cond1 = compile(cond)
indent_level = indent_level + 1
local _x114 = compile(cons, {_stash = true, stmt = true})
Expand Down Expand Up @@ -1023,8 +1023,8 @@ setenv("%if", {_stash = true, tr = true, special = function (cond, cons, alt)
else
return(s .. "\n")
end
end, stmt = true})
setenv("while", {_stash = true, tr = true, special = function (cond, form)
end, stmt = true, tr = true})
setenv("while", {_stash = true, special = function (cond, form)
local _cond3 = compile(cond)
indent_level = indent_level + 1
local _x117 = compile(form, {_stash = true, stmt = true})
Expand All @@ -1036,8 +1036,8 @@ setenv("while", {_stash = true, tr = true, special = function (cond, form)
else
return(ind .. "while " .. _cond3 .. " do\n" .. body .. ind .. "end\n")
end
end, stmt = true})
setenv("%for", {_stash = true, tr = true, special = function (t, k, form)
end, stmt = true, tr = true})
setenv("%for", {_stash = true, special = function (t, k, form)
local _t1 = compile(t)
local ind = indentation()
indent_level = indent_level + 1
Expand All @@ -1049,8 +1049,8 @@ setenv("%for", {_stash = true, tr = true, special = function (t, k, form)
else
return(ind .. "for (" .. k .. " in " .. _t1 .. ") {\n" .. body .. ind .. "}\n")
end
end, stmt = true})
setenv("%try", {_stash = true, tr = true, special = function (form)
end, stmt = true, tr = true})
setenv("%try", {_stash = true, special = function (form)
local e = unique("e")
local ind = indentation()
indent_level = indent_level + 1
Expand All @@ -1063,7 +1063,7 @@ setenv("%try", {_stash = true, tr = true, special = function (form)
indent_level = indent_level - 1
local h = _x131
return(ind .. "try {\n" .. body .. ind .. "}\n" .. ind .. "catch (" .. e .. ") {\n" .. h .. ind .. "}\n")
end, stmt = true})
end, stmt = true, tr = true})
setenv("%delete", {_stash = true, special = function (place)
return(indentation() .. "delete " .. compile(place))
end, stmt = true})
Expand All @@ -1073,22 +1073,22 @@ end, stmt = true})
setenv("%function", {_stash = true, special = function (args, body)
return(compile_function(args, body))
end})
setenv("%global-function", {_stash = true, tr = true, special = function (name, args, body)
setenv("%global-function", {_stash = true, special = function (name, args, body)
if target == "lua" then
local x = compile_function(args, body, {_stash = true, name = name})
return(indentation() .. x)
else
return(compile({"set", name, {"%function", args, body}}, {_stash = true, stmt = true}))
end
end, stmt = true})
setenv("%local-function", {_stash = true, tr = true, special = function (name, args, body)
end, stmt = true, tr = true})
setenv("%local-function", {_stash = true, special = function (name, args, body)
if target == "lua" then
local x = compile_function(args, body, {_stash = true, name = name, prefix = "local"})
return(indentation() .. x)
else
return(compile({"%local", name, {"%function", args, body}}, {_stash = true, stmt = true}))
end
end, stmt = true})
end, stmt = true, tr = true})
setenv("return", {_stash = true, special = function (x)
local _e27
if nil63(x) then
Expand Down Expand Up @@ -1215,4 +1215,4 @@ setenv("%object", {_stash = true, special = function (...)
end
return(s .. "}")
end})
return({run = run, expand = expand, compile = compile, eval = eval})
return({run = run, eval = eval, expand = expand, compile = compile})
5 changes: 2 additions & 3 deletions bin/lumen.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ setenv("define-global", {_stash: true, macro: function (name, x) {
var _r35 = unstash(Array.prototype.slice.call(arguments, 2));
var _id29 = _r35;
var body = cut(_id29, 0);
setenv(name, {_stash: true, toplevel: true, variable: true});
setenv(name, {_stash: true, variable: true, toplevel: true});
if (some63(body)) {
return(join(["%global-function", name], bind42(x, body)));
} else {
Expand Down Expand Up @@ -922,11 +922,10 @@ setenv("guard", {_stash: true, macro: function (expr) {
if (target === "js") {
return([["fn", join(), ["%try", ["list", true, expr]]]]);
} else {
var e = unique("e");
var x = unique("x");
var msg = unique("msg");
var trace = unique("trace");
return(["let", [x, "nil", msg, "nil", trace, "nil", e, ["xpcall", ["fn", join(), ["set", x, expr]], ["fn", ["m"], ["set", trace, [["get", "debug", ["quote", "traceback"]]]], ["set", msg, ["%message-handler", "m", trace]]]]], ["list", e, ["if", e, x, msg], ["if", e, "nil", trace]]]);
return(["let", [x, "nil", msg, "nil", trace, "nil"], ["if", ["xpcall", ["fn", join(), ["set", x, expr]], ["fn", ["m"], ["set", msg, ["clip", "m", ["+", ["search", "m", "\": \""], 2]]], ["set", trace, [["get", "debug", ["quote", "traceback"]]]]]], ["list", true, x], ["list", false, msg, trace]]]);
}
}});
setenv("each", {_stash: true, macro: function (x, t) {
Expand Down
Loading

0 comments on commit 6f59319

Please sign in to comment.