aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2015-02-25 23:01:16 -0600
committerJoel Martin <github@martintribe.org>2015-02-25 23:01:16 -0600
commit3fb3743ff483cd1e4612d87557eecc62817b10b2 (patch)
tree6da27695b27567362b4c01dc80679d7c78380519
parentbf8237d5ee3f1cbb3a3a7eb9d5094720361c46bf (diff)
downloadmal-3fb3743ff483cd1e4612d87557eecc62817b10b2.tar.gz
mal-3fb3743ff483cd1e4612d87557eecc62817b10b2.zip
miniMAL: add classOf and fix printing.
-rw-r--r--js/interop.js36
-rw-r--r--miniMAL/miniMAL-core.json3
-rw-r--r--miniMAL/printer.json8
-rw-r--r--miniMAL/types.json2
4 files changed, 45 insertions, 4 deletions
diff --git a/js/interop.js b/js/interop.js
new file mode 100644
index 0000000..89da4a6
--- /dev/null
+++ b/js/interop.js
@@ -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;
diff --git a/miniMAL/miniMAL-core.json b/miniMAL/miniMAL-core.json
index 94fedec..c22376a 100644
--- a/miniMAL/miniMAL-core.json
+++ b/miniMAL/miniMAL-core.json
@@ -88,6 +88,9 @@
["`", "or_FIXME"],
["concat", ["`", ["or"]], ["rest", "xs"]]]]]]]]],
+["def", "classOf", ["fn", ["a"],
+ [".", [".-", [".-", "Object", ["`", "prototype"]], ["`", "toString"]],
+ ["`", "call"], "a"]]],
["def", "repl", ["fn",["prompt", "rep"],
diff --git a/miniMAL/printer.json b/miniMAL/printer.json
index 2a6a51d..7463c78 100644
--- a/miniMAL/printer.json
+++ b/miniMAL/printer.json
@@ -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",
@@ -36,7 +36,7 @@
["`", "replace"], ["RegExp", ["`", "\n"], ["`", "g"]], ["`", "\\n"]],
["`", "\""]],
"exp"]],
- ["if", ["=", ["`", "number"], ["type", "exp"]],
+ ["if", ["=", ["`", "[object Number]"], ["classOf", "exp"]],
"exp",
["if", ["=", null, "exp"],
["`", "nil"],
@@ -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"],
diff --git a/miniMAL/types.json b/miniMAL/types.json
index f67683e..372dff9 100644
--- a/miniMAL/types.json
+++ b/miniMAL/types.json
@@ -75,7 +75,7 @@
["str", ["`", "\u029e"], "name"]]],
["def", "keyword?", ["fn", ["kw"],
- ["and", ["=", ["`", "string"], ["type", "kw"]],
+ ["and", ["=", ["`", "[object String]"], ["classOf", "kw"]],
["=", ["`", "\u029e"], ["get", "kw", 0]]]]],