Skip to content

Commit

Permalink
miniMAL: add classOf and fix printing.
Browse files Browse the repository at this point in the history
  • Loading branch information
kanaka committed Feb 26, 2015
1 parent bf8237d commit 3fb3743
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
36 changes: 36 additions & 0 deletions js/interop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Node vs browser behavior
var interop = {};
if (typeof module === 'undefined') {
var exports = interop,
GLOBAL = window;
}

function resolve_js(str) {
if (str.match(/\./)) {
var re = /^(.*)\.\([^\.]*)$/,
match = re.exec(str);
return [eval(match[0]), eval(str)];
} else {
return [GLOBAL, eval(str)];
}
}

function js_to_mal(obj) {
var cache = [];
var str = JSON.stringify(obj, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
});
cache = null; // Enable garbage collection
return JSON.parse(str);
}

exports.resolve_js = interop.resolve_js = resolve_js;
exports.js_to_mal = interop.js_to_mal = js_to_mal;
3 changes: 3 additions & 0 deletions miniMAL/miniMAL-core.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
["`", "or_FIXME"],
["concat", ["`", ["or"]], ["rest", "xs"]]]]]]]]],

["def", "classOf", ["fn", ["a"],
[".", [".-", [".-", "Object", ["`", "prototype"]], ["`", "toString"]],
["`", "call"], "a"]]],


["def", "repl", ["fn",["prompt", "rep"],
Expand Down
8 changes: 5 additions & 3 deletions miniMAL/printer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
["keys", "exp"]],
["`", "join"], ["`", " "]],
["`", "}"]],
["if", ["=", ["`", "string"], ["type", "exp"]],
["if", ["=", ["`", "[object String]"], ["classOf", "exp"]],
["if", ["=", ["`", "\u029e"], ["get", "exp", 0]],
["str", ["`", ":"], ["slice", "exp", 1]],
["if", "print_readably",
Expand All @@ -36,7 +36,7 @@
["`", "replace"], ["RegExp", ["`", "\n"], ["`", "g"]], ["`", "\\n"]],
["`", "\""]],
"exp"]],
["if", ["=", ["`", "number"], ["type", "exp"]],
["if", ["=", ["`", "[object Number]"], ["classOf", "exp"]],
"exp",
["if", ["=", null, "exp"],
["`", "nil"],
Expand All @@ -52,9 +52,11 @@
["`", " "],
["pr-str", ["get", "exp", ["`", "ast"]]],
["`", ")"]],
["if", ["=", ["`", "[object Function]"], ["classOf", "exp"]],
["str", ["`", "#<native function>"]],
["if", ["atom?", "exp"],
["str", ["`", "(atom "], ["get", "exp", ["`", "val"]], ["`", ")"]],
["str", ["`", "#<unknown: "], "exp", ["`", ">"]]]]]]]]]]]]]]],
["str", ["`", "#<unknown: "], "exp", ["`", ">"]]]]]]]]]]]]]]]],

["def", "pr-list", ["fn", ["lst", "print_readably", "sep"],
[".", ["map", ["fn", ["s"], ["pr-str", "s", "print_readably"]], "lst"],
Expand Down
2 changes: 1 addition & 1 deletion miniMAL/types.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
["str", ["`", "\u029e"], "name"]]],

["def", "keyword?", ["fn", ["kw"],
["and", ["=", ["`", "string"], ["type", "kw"]],
["and", ["=", ["`", "[object String]"], ["classOf", "kw"]],
["=", ["`", "\u029e"], ["get", "kw", 0]]]]],


Expand Down

0 comments on commit 3fb3743

Please sign in to comment.