diff --git a/bin/compiler.js b/bin/compiler.js index 5fc68f2..0114e69 100644 --- a/bin/compiler.js +++ b/bin/compiler.js @@ -27,7 +27,7 @@ var special63 = function (k) { return is63(getenv(k, "special")); }; var special_form63 = function (form) { - return ! atom63(form) && special63(hd(form)); + return hd63(form, special63); }; var statement63 = function (k) { return special63(k) && getenv(k, "stmt"); @@ -197,7 +197,7 @@ var can_unquote63 = function (depth) { return quoting63(depth) && depth === 1; }; var quasisplice63 = function (x, depth) { - return can_unquote63(depth) && ! atom63(x) && hd(x) === "unquote-splicing"; + return can_unquote63(depth) && hd63(x, "unquote-splicing"); }; var expand_local = function (__x38) { var ____id1 = __x38; @@ -576,7 +576,7 @@ var infix63 = function (x) { return is63(getop(x)); }; infix_operator63 = function (x) { - return obj63(x) && infix63(hd(x)); + return hd63(x, infix63); }; var compile_args = function (args) { var __s1 = "("; @@ -683,7 +683,7 @@ var compile_special = function (form, stmt63) { return apply(__special, __args2) + __tr; }; var parenthesize_call63 = function (x) { - return ! atom63(x) && hd(x) === "%function" || precedence(x) > 0; + return hd63(x, "%function") || precedence(x) > 0; }; var compile_call = function (form) { var __f = hd(form); @@ -811,7 +811,7 @@ compile = function (form) { __e45 = compile_atom(__form); } else { var __e46 = undefined; - if (infix63(hd(__form))) { + if (infix_operator63(__form)) { __e46 = compile_infix(__form); } else { __e46 = compile_call(__form); @@ -853,7 +853,7 @@ var literal63 = function (form) { return atom63(form) || hd(form) === "%array" || hd(form) === "%object"; }; var standalone63 = function (form) { - return ! atom63(form) && ! infix63(hd(form)) && ! literal63(form) && !( "get" === hd(form)) || id_literal63(form); + return ! atom63(form) && ! infix_operator63(form) && ! literal63(form) && ! hd63(form, "get") || id_literal63(form); }; var lower_do = function (args, hoist, stmt63, tail63) { var ____x95 = almost(args); @@ -992,7 +992,7 @@ var lower_pairwise = function (form) { } }; var lower_infix63 = function (form) { - return infix63(hd(form)) && _35(form) > 3; + return infix_operator63(form) && _35(form) > 3; }; var lower_infix = function (form, hoist) { var __form3 = lower_pairwise(form); @@ -1090,7 +1090,9 @@ _eval = function (form) { return _37result; }; immediate_call63 = function (x) { - return obj63(x) && obj63(hd(x)) && hd(hd(x)) === "%function"; + return hd63(x, function (x) { + return hd63(x, "%function"); + }); }; setenv("do", {_stash: true, special: function () { var __forms1 = unstash(Array.prototype.slice.call(arguments, 0)); @@ -1103,10 +1105,8 @@ setenv("do", {_stash: true, special: function () { __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") { - break; - } + if (hd63(__x137, "return") || hd63(__x137, "break")) { + break; } ____i19 = ____i19 + 1; } diff --git a/bin/compiler.lua b/bin/compiler.lua index c1ba3f0..7d59200 100644 --- a/bin/compiler.lua +++ b/bin/compiler.lua @@ -27,7 +27,7 @@ local function special63(k) return is63(getenv(k, "special")) end local function special_form63(form) - return not atom63(form) and special63(hd(form)) + return hd63(form, special63) end local function statement63(k) return special63(k) and getenv(k, "stmt") @@ -176,7 +176,7 @@ local function can_unquote63(depth) return quoting63(depth) and depth == 1 end local function quasisplice63(x, depth) - return can_unquote63(depth) and not atom63(x) and hd(x) == "unquote-splicing" + return can_unquote63(depth) and hd63(x, "unquote-splicing") end local function expand_local(__x38) local ____id1 = __x38 @@ -522,7 +522,7 @@ local function infix63(x) return is63(getop(x)) end function infix_operator63(x) - return obj63(x) and infix63(hd(x)) + return hd63(x, infix63) end local function compile_args(args) local __s1 = "(" @@ -629,7 +629,7 @@ local function compile_special(form, stmt63) return apply(__special, __args2) .. __tr end local function parenthesize_call63(x) - return not atom63(x) and hd(x) == "%function" or precedence(x) > 0 + return hd63(x, "%function") or precedence(x) > 0 end local function compile_call(form) local __f = hd(form) @@ -757,7 +757,7 @@ function compile(form, ...) __e37 = compile_atom(__form) else local __e38 = nil - if infix63(hd(__form)) then + if infix_operator63(__form) then __e38 = compile_infix(__form) else __e38 = compile_call(__form) @@ -799,7 +799,7 @@ local function literal63(form) return atom63(form) or hd(form) == "%array" or hd(form) == "%object" end local function standalone63(form) - return not atom63(form) and not infix63(hd(form)) and not literal63(form) and not( "get" == hd(form)) or id_literal63(form) + return not atom63(form) and not infix_operator63(form) and not literal63(form) and not hd63(form, "get") or id_literal63(form) end local function lower_do(args, hoist, stmt63, tail63) local ____x98 = almost(args) @@ -938,7 +938,7 @@ local function lower_pairwise(form) end end local function lower_infix63(form) - return infix63(hd(form)) and _35(form) > 3 + return infix_operator63(form) and _35(form) > 3 end local function lower_infix(form, hoist) local __form3 = lower_pairwise(form) @@ -1043,7 +1043,9 @@ function _eval(form) return _37result end function immediate_call63(x) - return obj63(x) and obj63(hd(x)) and hd(hd(x)) == "%function" + return hd63(x, function (x) + return hd63(x, "%function") + end) end setenv("do", {_stash = true, special = function (...) local __forms1 = unstash({...}) @@ -1056,10 +1058,8 @@ setenv("do", {_stash = true, special = function (...) __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 - break - end + if hd63(__x141, "return") or hd63(__x141, "break") then + break end ____i19 = ____i19 + 1 end diff --git a/bin/lumen.js b/bin/lumen.js index f760831..dc44614 100644 --- a/bin/lumen.js +++ b/bin/lumen.js @@ -62,7 +62,20 @@ atom63 = function (x) { return nil63(x) || string63(x) || number63(x) || boolean63(x); }; hd63 = function (l, x) { - return obj63(l) && hd(l) === x; + var __id2 = obj63(l); + var __e1 = undefined; + if (__id2) { + var __e2 = undefined; + if (function63(x)) { + __e2 = x(hd(l)); + } else { + __e2 = hd(l) === x; + } + __e1 = __e2; + } else { + __e1 = __id2; + } + return __e1; }; nan = 0 / 0; inf = 1 / 0; @@ -79,21 +92,21 @@ clip = function (s, from, upto) { cut = function (x, from, upto) { var __l = []; var __j = 0; - var __e = undefined; + var __e3 = undefined; if (nil63(from) || from < 0) { - __e = 0; + __e3 = 0; } else { - __e = from; + __e3 = from; } - var __i = __e; + var __i = __e3; var __n = _35(x); - var __e1 = undefined; + var __e4 = undefined; if (nil63(upto) || upto > __n) { - __e1 = __n; + __e4 = __n; } else { - __e1 = upto; + __e4 = upto; } - var __upto = __e1; + var __upto = __e4; while (__i < __upto) { __l[__j] = x[__i]; __i = __i + 1; @@ -103,13 +116,13 @@ cut = function (x, from, upto) { var __k = undefined; for (__k in ____o) { var __v = ____o[__k]; - var __e2 = undefined; + var __e5 = undefined; if (numeric63(__k)) { - __e2 = parseInt(__k); + __e5 = parseInt(__k); } else { - __e2 = __k; + __e5 = __k; } - var __k1 = __e2; + var __k1 = __e5; if (! number63(__k1)) { __l[__k1] = __v; } @@ -122,13 +135,13 @@ keys = function (x) { var __k2 = undefined; for (__k2 in ____o1) { var __v1 = ____o1[__k2]; - var __e3 = undefined; + var __e6 = undefined; if (numeric63(__k2)) { - __e3 = parseInt(__k2); + __e6 = parseInt(__k2); } else { - __e3 = __k2; + __e6 = __k2; } - var __k3 = __e3; + var __k3 = __e6; if (! number63(__k3)) { __t[__k3] = __v1; } @@ -202,13 +215,13 @@ join = function () { var __k4 = undefined; for (__k4 in ____o2) { var __v2 = ____o2[__k4]; - var __e4 = undefined; + var __e7 = undefined; if (numeric63(__k4)) { - __e4 = parseInt(__k4); + __e7 = parseInt(__k4); } else { - __e4 = __k4; + __e7 = __k4; } - var __k5 = __e4; + var __k5 = __e7; if (number63(__k5)) { __k5 = __k5 + __n3; } @@ -224,13 +237,13 @@ find = function (f, t) { var ____i6 = undefined; for (____i6 in ____o3) { var __x2 = ____o3[____i6]; - var __e5 = undefined; + var __e8 = undefined; if (numeric63(____i6)) { - __e5 = parseInt(____i6); + __e8 = parseInt(____i6); } else { - __e5 = ____i6; + __e8 = ____i6; } - var ____i61 = __e5; + var ____i61 = __e8; var __y = f(__x2); if (__y) { return __y; @@ -265,9 +278,9 @@ pair = function (l) { return __l12; }; sort = function (l, f) { - var __e6 = undefined; + var __e9 = undefined; if (f) { - __e6 = function (a, b) { + __e9 = function (a, b) { if (f(a, b)) { return -1; } else { @@ -275,7 +288,7 @@ sort = function (l, f) { } }; } - return l.sort(__e6); + return l.sort(__e9); }; map = function (f, x) { var __t1 = []; @@ -293,13 +306,13 @@ map = function (f, x) { var __k6 = undefined; for (__k6 in ____o4) { var __v4 = ____o4[__k6]; - var __e7 = undefined; + var __e10 = undefined; if (numeric63(__k6)) { - __e7 = parseInt(__k6); + __e10 = parseInt(__k6); } else { - __e7 = __k6; + __e10 = __k6; } - var __k7 = __e7; + var __k7 = __e10; if (! number63(__k7)) { var __y3 = f(__v4); if (is63(__y3)) { @@ -321,13 +334,13 @@ keys63 = function (t) { var __k8 = undefined; for (__k8 in ____o5) { var __v5 = ____o5[__k8]; - var __e8 = undefined; + var __e11 = undefined; if (numeric63(__k8)) { - __e8 = parseInt(__k8); + __e11 = parseInt(__k8); } else { - __e8 = __k8; + __e11 = __k8; } - var __k9 = __e8; + var __k9 = __e11; if (! number63(__k9)) { return true; } @@ -339,13 +352,13 @@ empty63 = function (t) { var ____i12 = undefined; for (____i12 in ____o6) { var __x7 = ____o6[____i12]; - var __e9 = undefined; + var __e12 = undefined; if (numeric63(____i12)) { - __e9 = parseInt(____i12); + __e12 = parseInt(____i12); } else { - __e9 = ____i12; + __e12 = ____i12; } - var ____i121 = __e9; + var ____i121 = __e12; return false; } return true; @@ -357,13 +370,13 @@ stash = function (args) { var __k10 = undefined; for (__k10 in ____o7) { var __v6 = ____o7[__k10]; - var __e10 = undefined; + var __e13 = undefined; if (numeric63(__k10)) { - __e10 = parseInt(__k10); + __e13 = parseInt(__k10); } else { - __e10 = __k10; + __e13 = __k10; } - var __k11 = __e10; + var __k11 = __e13; if (! number63(__k11)) { __p[__k11] = __v6; } @@ -384,13 +397,13 @@ unstash = function (args) { var __k12 = undefined; for (__k12 in ____o8) { var __v7 = ____o8[__k12]; - var __e11 = undefined; + var __e14 = undefined; if (numeric63(__k12)) { - __e11 = parseInt(__k12); + __e14 = parseInt(__k12); } else { - __e11 = __k12; + __e14 = __k12; } - var __k13 = __e11; + var __k13 = __e14; if (!( __k13 === "_stash")) { __args1[__k13] = __v7; } @@ -407,13 +420,13 @@ destash33 = function (l, args1) { var __k14 = undefined; for (__k14 in ____o9) { var __v8 = ____o9[__k14]; - var __e12 = undefined; + var __e15 = undefined; if (numeric63(__k14)) { - __e12 = parseInt(__k14); + __e15 = parseInt(__k14); } else { - __e12 = __k14; + __e15 = __k14; } - var __k15 = __e12; + var __k15 = __e15; if (!( __k15 === "_stash")) { args1[__k15] = __v8; } @@ -553,31 +566,31 @@ escape = function (s) { var __i20 = 0; while (__i20 < _35(s)) { var __c = char(s, __i20); - var __e13 = undefined; + var __e16 = undefined; if (__c === "\n") { - __e13 = "\\n"; + __e16 = "\\n"; } else { - var __e14 = undefined; + var __e17 = undefined; if (__c === "\r") { - __e14 = "\\r"; + __e17 = "\\r"; } else { - var __e15 = undefined; + var __e18 = undefined; if (__c === "\"") { - __e15 = "\\\""; + __e18 = "\\\""; } else { - var __e16 = undefined; + var __e19 = undefined; if (__c === "\\") { - __e16 = "\\\\"; + __e19 = "\\\\"; } else { - __e16 = __c; + __e19 = __c; } - __e15 = __e16; + __e18 = __e19; } - __e14 = __e15; + __e17 = __e18; } - __e13 = __e14; + __e16 = __e17; } - var __c1 = __e13; + var __c1 = __e16; __s1 = __s1 + __c1; __i20 = __i20 + 1; } @@ -628,13 +641,13 @@ str = function (x, stack) { var __k16 = undefined; for (__k16 in ____o10) { var __v9 = ____o10[__k16]; - var __e17 = undefined; + var __e20 = undefined; if (numeric63(__k16)) { - __e17 = parseInt(__k16); + __e20 = parseInt(__k16); } else { - __e17 = __k16; + __e20 = __k16; } - var __k17 = __e17; + var __k17 = __e20; if (number63(__k17)) { __xs11[__k17] = str(__v9, __l4); } else { @@ -647,13 +660,13 @@ str = function (x, stack) { var ____i22 = undefined; for (____i22 in ____o11) { var __v10 = ____o11[____i22]; - var __e18 = undefined; + var __e21 = undefined; if (numeric63(____i22)) { - __e18 = parseInt(____i22); + __e21 = parseInt(____i22); } else { - __e18 = ____i22; + __e21 = ____i22; } - var ____i221 = __e18; + var ____i221 = __e21; __s = __s + __sp + __v10; __sp = " "; } @@ -686,25 +699,25 @@ setenv = function (k) { var ____id1 = ____r76; var __keys = cut(____id1, 0); if (string63(__k18)) { - var __e19 = undefined; + var __e22 = undefined; if (__keys.toplevel) { - __e19 = hd(environment); + __e22 = hd(environment); } else { - __e19 = last(environment); + __e22 = last(environment); } - var __frame = __e19; + var __frame = __e22; var __entry = __frame[__k18] || {}; var ____o12 = __keys; var __k19 = undefined; for (__k19 in ____o12) { var __v11 = ____o12[__k19]; - var __e20 = undefined; + var __e23 = undefined; if (numeric63(__k19)) { - __e20 = parseInt(__k19); + __e23 = parseInt(__k19); } else { - __e20 = __k19; + __e23 = __k19; } - var __k20 = __e20; + var __k20 = __e23; __entry[__k20] = __v11; } __frame[__k18] = __entry; diff --git a/bin/lumen.lua b/bin/lumen.lua index 6478c38..4a1908e 100644 --- a/bin/lumen.lua +++ b/bin/lumen.lua @@ -59,7 +59,20 @@ function atom63(x) return nil63(x) or string63(x) or number63(x) or boolean63(x) end function hd63(l, x) - return obj63(l) and hd(l) == x + local __id2 = obj63(l) + local __e1 = nil + if __id2 then + local __e2 = nil + if function63(x) then + __e2 = x(hd(l)) + else + __e2 = hd(l) == x + end + __e1 = __e2 + else + __e1 = __id2 + end + return __e1 end nan = 0 / 0 inf = 1 / 0 @@ -76,21 +89,21 @@ end function cut(x, from, upto) local __l = {} local __j = 0 - local __e = nil + local __e3 = nil if nil63(from) or from < 0 then - __e = 0 + __e3 = 0 else - __e = from + __e3 = from end - local __i = __e + local __i = __e3 local __n = _35(x) - local __e1 = nil + local __e4 = nil if nil63(upto) or upto > __n then - __e1 = __n + __e4 = __n else - __e1 = upto + __e4 = upto end - local __upto = __e1 + local __upto = __e4 while __i < __upto do __l[__j + 1] = x[__i + 1] __i = __i + 1 @@ -131,11 +144,11 @@ function char(s, n) return clip(s, n, n + 1) end function code(s, n) - local __e2 = nil + local __e5 = nil if n then - __e2 = n + 1 + __e5 = n + 1 end - return string.byte(s, __e2) + return string.byte(s, __e5) end function string_literal63(x) return string63(x) and char(x, 0) == "\"" @@ -344,11 +357,11 @@ function destash33(l, args1) end end function search(s, pattern, start) - local __e3 = nil + local __e6 = nil if start then - __e3 = start + 1 + __e6 = start + 1 end - local __start = __e3 + local __start = __e6 local __i16 = string.find(s, pattern, __start, true) return __i16 and __i16 - 1 end @@ -471,31 +484,31 @@ function escape(s) local __i20 = 0 while __i20 < _35(s) do local __c = char(s, __i20) - local __e4 = nil + local __e7 = nil if __c == "\n" then - __e4 = "\\n" + __e7 = "\\n" else - local __e5 = nil + local __e8 = nil if __c == "\r" then - __e5 = "\\r" + __e8 = "\\r" else - local __e6 = nil + local __e9 = nil if __c == "\"" then - __e6 = "\\\"" + __e9 = "\\\"" else - local __e7 = nil + local __e10 = nil if __c == "\\" then - __e7 = "\\\\" + __e10 = "\\\\" else - __e7 = __c + __e10 = __c end - __e6 = __e7 + __e9 = __e10 end - __e5 = __e6 + __e8 = __e9 end - __e4 = __e5 + __e7 = __e8 end - local __c1 = __e4 + local __c1 = __e7 __s1 = __s1 .. __c1 __i20 = __i20 + 1 end @@ -594,13 +607,13 @@ function setenv(k, ...) local ____id1 = ____r73 local __keys = cut(____id1, 0) if string63(__k9) then - local __e8 = nil + local __e11 = nil if __keys.toplevel then - __e8 = hd(environment) + __e11 = hd(environment) else - __e8 = last(environment) + __e11 = last(environment) end - local __frame = __e8 + local __frame = __e11 local __entry = __frame[__k9] or {} local ____o12 = __keys local __k10 = nil diff --git a/compiler.l b/compiler.l index de2bbbd..59b5120 100644 --- a/compiler.l +++ b/compiler.l @@ -17,7 +17,7 @@ (is? (getenv k 'special))) (define special-form? (form) - (and (not (atom? form)) (special? (hd form)))) + (hd? form special?)) (define statement? (k) (and (special? k) (getenv k 'stmt))) @@ -115,8 +115,7 @@ (define quasisplice? (x depth) (and (can-unquote? depth) - (not (atom? x)) - (= (hd x) 'unquote-splicing))) + (hd? x 'unquote-splicing))) (define expand-local ((x name value)) (setenv name :variable) @@ -306,7 +305,7 @@ (is? (getop x))) (define-global infix-operator? (x) - (and (obj? x) (infix? (hd x)))) + (hd? x infix?)) (define compile-args (args) (let (s "(" c "") @@ -348,8 +347,7 @@ (cat (apply special args) tr))) (define parenthesize-call? (x) - (or (and (not (atom? x)) - (= (hd x) '%function)) + (or (hd? x '%function) (> (precedence x) 0))) (define compile-call (form) @@ -407,7 +405,7 @@ (let (tr (terminator stmt) ind (if stmt (indentation) "") form (if (atom? form) (compile-atom form) - (infix? (hd form)) (compile-infix form) + (infix-operator? form) (compile-infix form) (compile-call form))) (cat ind form tr)))) @@ -431,9 +429,9 @@ (define standalone? (form) (or (and (not (atom? form)) - (not (infix? (hd form))) + (not (infix-operator? form)) (not (literal? form)) - (not (= 'get (hd form)))) + (not (hd? form 'get))) (id-literal? form))) (define lower-do (args hoist stmt? tail?) @@ -530,7 +528,7 @@ form)) (define lower-infix? (form) - (and (infix? (hd form)) (> (# form) 3))) + (and (infix-operator? form) (> (# form) 3))) (define lower-infix (form hoist) (let (form (lower-pairwise form) @@ -589,7 +587,7 @@ %result))) (define-global immediate-call? (x) - (and (obj? x) (obj? (hd x)) (= (hd (hd x)) '%function))) + (hd? x (fn (x) (hd? x '%function)))) (define-special do forms :stmt :tr (with s "" @@ -599,10 +597,9 @@ (= "\n" (char s (edge s)))) (set s (cat (clip s 0 (edge s)) ";\n"))) (cat! s (compile x :stmt)) - (unless (atom? x) - (if (or (= (hd x) 'return) - (= (hd x) 'break)) - (break)))))) + (when (or (hd? x 'return) + (hd? x 'break)) + (break))))) (define-special %if (cond cons alt) :stmt :tr (let (cond (compile cond) diff --git a/runtime.l b/runtime.l index 50951c8..322f2cf 100644 --- a/runtime.l +++ b/runtime.l @@ -41,7 +41,10 @@ (or (nil? x) (string? x) (number? x) (boolean? x))) (define-global hd? (l x) - (and (obj? l) (= (hd l) x))) + (and (obj? l) + (if (function? x) + (x (hd l)) + (= (hd l) x)))) (define-global nan (/ 0 0)) (define-global inf (/ 1 0))