Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define INSTANCEOF; get rid of TYPE on JS #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion bin/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,18 @@ setenv("new", {_stash: true, special: function (x) {
return "new " + compile(x);
}});
setenv("typeof", {_stash: true, special: function (x) {
return "typeof(" + compile(x) + ")";
if (target === "lua") {
return compile(["type", x]);
} else {
return "typeof(" + compile(x) + ")";
}
}});
setenv("instanceof", {_stash: true, special: function (x, t) {
if (target === "lua") {
return compile(["=", ["getmetatable", x], t]);
} else {
return "((" + compile(x) + ") instanceof " + compile(t) + ")";
}
}});
setenv("throw", {_stash: true, special: function (x) {
var __e56 = undefined;
Expand Down
13 changes: 12 additions & 1 deletion bin/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,18 @@ setenv("new", {_stash = true, special = function (x)
return "new " .. compile(x)
end})
setenv("typeof", {_stash = true, special = function (x)
return "typeof(" .. compile(x) .. ")"
if target == "lua" then
return compile({"type", x})
else
return "typeof(" .. compile(x) .. ")"
end
end})
setenv("instanceof", {_stash = true, special = function (x, t)
if target == "lua" then
return compile({"=", {"getmetatable", x}, t})
else
return "((" .. compile(x) .. ") instanceof " .. compile(t) .. ")"
end
end})
setenv("throw", {_stash = true, special = function (x)
local __e48 = nil
Expand Down
33 changes: 15 additions & 18 deletions bin/lumen.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,20 @@ two63 = function (x) {
hd = function (l) {
return l[0];
};
type = function (x) {
return typeof(x);
};
string63 = function (x) {
return type(x) === "string";
return typeof(x) === "string";
};
number63 = function (x) {
return type(x) === "number";
return typeof(x) === "number";
};
boolean63 = function (x) {
return type(x) === "boolean";
return typeof(x) === "boolean";
};
function63 = function (x) {
return type(x) === "function";
return typeof(x) === "function";
};
obj63 = function (x) {
return is63(x) && type(x) === "object";
return is63(x) && typeof(x) === "object";
};
atom63 = function (x) {
return nil63(x) || string63(x) || number63(x) || boolean63(x);
Expand Down Expand Up @@ -191,13 +188,13 @@ reduce = function (f, x) {
};
join = function () {
var __ls = unstash(Array.prototype.slice.call(arguments, 0));
var __r38 = [];
var __r37 = [];
var ____x1 = __ls;
var ____i4 = 0;
while (____i4 < _35(____x1)) {
var __l11 = ____x1[____i4];
if (__l11) {
var __n3 = _35(__r38);
var __n3 = _35(__r37);
var ____o2 = __l11;
var __k4 = undefined;
for (__k4 in ____o2) {
Expand All @@ -212,12 +209,12 @@ join = function () {
if (number63(__k5)) {
__k5 = __k5 + __n3;
}
__r38[__k5] = __v2;
__r37[__k5] = __v2;
}
}
____i4 = ____i4 + 1;
}
return __r38;
return __r37;
};
find = function (f, t) {
var ____o3 = t;
Expand Down Expand Up @@ -674,16 +671,16 @@ apply = function (f, args) {
return f.apply(f, __args);
};
call = function (f) {
var ____r75 = unstash(Array.prototype.slice.call(arguments, 1));
var __f = destash33(f, ____r75);
var ____id = ____r75;
var ____r74 = unstash(Array.prototype.slice.call(arguments, 1));
var __f = destash33(f, ____r74);
var ____id = ____r74;
var __args11 = cut(____id, 0);
return apply(__f, __args11);
};
setenv = function (k) {
var ____r76 = unstash(Array.prototype.slice.call(arguments, 1));
var __k18 = destash33(k, ____r76);
var ____id1 = ____r76;
var ____r75 = unstash(Array.prototype.slice.call(arguments, 1));
var __k18 = destash33(k, ____r75);
var ____id1 = ____r75;
var __keys = cut(____id1, 0);
if (string63(__k18)) {
var __e19 = undefined;
Expand Down
9 changes: 8 additions & 1 deletion compiler.l
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,14 @@
(cat "new " (compile x)))

(define-special typeof (x)
(cat "typeof(" (compile x) ")"))
(if (= target 'lua)
(compile `(type ,x))
(cat "typeof(" (compile x) ")")))

(define-special instanceof (x t)
(if (= target 'lua)
(compile `(= (getmetatable ,x) ,t))
(cat "((" (compile x) ") instanceof " (compile t) ")")))

(define-special throw (x) :stmt
(let e (if (= target 'js)
Expand Down
14 changes: 6 additions & 8 deletions runtime.l
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@

(define-global hd (l) (at l 0))

(target js: (define-global type (x) (typeof x)))

(define-global string? (x) (= (type x) 'string))
(define-global number? (x) (= (type x) 'number))
(define-global boolean? (x) (= (type x) 'boolean))
(define-global function? (x) (= (type x) 'function))
(define-global string? (x) (= (typeof x) 'string))
(define-global number? (x) (= (typeof x) 'number))
(define-global boolean? (x) (= (typeof x) 'boolean))
(define-global function? (x) (= (typeof x) 'function))

(define-global obj? (x)
(and (is? x)
(= (type x) (target lua: 'table js: 'object))))
(= (typeof x) (target lua: 'table js: 'object))))

(define-global atom? (x)
(or (nil? x) (string? x) (number? x) (boolean? x)))
Expand Down Expand Up @@ -302,7 +300,7 @@
(atom? x) (tostring x)
(function? x) "function"
(and stack (in? x stack)) "circular"
(target js: false lua: (not (= (type x) 'table)))
(target js: false lua: (not (= (typeof x) 'table)))
(escape (tostring x))
(let (s "(" sp ""
xs () ks ()
Expand Down
2 changes: 2 additions & 0 deletions test.l
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,8 @@ c"
(test= 1 ((fn () (return (if true (return 1) 2))))))

(define-test guard
(let ((ok e) (guard (error "foo")))
(test= true (target js: (instanceof e Error) lua: (obj? e))))
(let-macro ((guard1 (x)
(let-unique (ok v)
`(let ((,ok ,v) (guard ,x))
Expand Down