From 8cfab77665957e8b26935e2b1690f65585273a09 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Mon, 16 Feb 2015 01:50:04 -0600 Subject: miniMAL: self-hosting! Add atoms. Fix malfunc. - refactor malfunc to use objects like others. --- miniMAL/core.json | 24 ++++++++++++++++++++---- miniMAL/printer.json | 4 +++- miniMAL/stepA_interop.json | 2 +- miniMAL/types.json | 44 +++++++++++++++++++++++++++++--------------- 4 files changed, 53 insertions(+), 21 deletions(-) diff --git a/miniMAL/core.json b/miniMAL/core.json index 9799b16..164a846 100644 --- a/miniMAL/core.json +++ b/miniMAL/core.json @@ -69,6 +69,16 @@ null], null]]], +["def", "reset!", ["fn", ["atm", "val"], + ["set", "atm", ["`", "val"], "val"]]], + +["def", "swap!", ["fn", ["atm", "f", "&", "args"], + ["let", ["fn", ["if", ["malfunc?", "f"], ["get", "f", ["`", "fn"]], "f"], + "fargs", ["cons", ["get", "atm", ["`", "val"]], "args"], + "val", ["apply", "fn", "fargs"]], + ["do", + ["set", "atm", ["`", "val"], "val"], + "val"]]]], ["def", "core-ns", ["hash-map", @@ -128,11 +138,17 @@ ["`", "first"], "_first", ["`", "rest"], ["fn", ["a"], ["rest", "a"]], ["`", "empty?"], "empty?", - ["`", "count"], "_count", - ["`", "apply"], "_apply", - ["`", "map"], "_map", + ["`", "count"], "_count", + ["`", "apply"], "_apply", + ["`", "map"], "_map", + ["`", "conj"], null, ["`", "with-meta"], "with_meta", - ["`", "meta"], "meta"]], + ["`", "meta"], "meta", + ["`", "atom"], "atom", + ["`", "atom?"], "atom?", + ["`", "deref"], ["fn", ["a"], ["get", "a", ["`", "val"]]], + ["`", "reset!"], "reset!", + ["`", "swap!"], "swap!"]], null] diff --git a/miniMAL/printer.json b/miniMAL/printer.json index 273d80a..2a6a51d 100644 --- a/miniMAL/printer.json +++ b/miniMAL/printer.json @@ -52,7 +52,9 @@ ["`", " "], ["pr-str", ["get", "exp", ["`", "ast"]]], ["`", ")"]], - ["str", ["`", "#"]]]]]]]]]]]]]], + ["if", ["atom?", "exp"], + ["str", ["`", "(atom "], ["get", "exp", ["`", "val"]], ["`", ")"]], + ["str", ["`", "#"]]]]]]]]]]]]]]], ["def", "pr-list", ["fn", ["lst", "print_readably", "sep"], [".", ["map", ["fn", ["s"], ["pr-str", "s", "print_readably"]], "lst"], diff --git a/miniMAL/stepA_interop.json b/miniMAL/stepA_interop.json index 44dc33b..2181d30 100644 --- a/miniMAL/stepA_interop.json +++ b/miniMAL/stepA_interop.json @@ -161,7 +161,7 @@ ["rep", ["`", "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"]], ["if", ["not", ["empty?", "*ARGV*"]], - ["rep", ["str", ["`", "(load-file \""], ["get", "*ARGV*", 0], ["`", "\")"]]], + ["println", ["rep", ["str", ["`", "(load-file \""], ["get", "*ARGV*", 0], ["`", "\")"]]]], ["do", ["rep", ["`", "(println (str \"Mal [\" *host-language* \"]\"))"]], ["repl", ["`", "user> "], "rep"]]], diff --git a/miniMAL/types.json b/miniMAL/types.json index 26c5ffe..f67683e 100644 --- a/miniMAL/types.json +++ b/miniMAL/types.json @@ -36,7 +36,16 @@ null]], ["keys", "obj"]], "new-obj"]], - ["throw", "clone of unsupported type"]]]]]], + ["if", ["malfunc?", "obj"], + ["let", ["new-obj", ["malfunc", ["get", "obj", ["`", "fn"]], + ["get", "obj", ["`", "ast"]], + ["get", "obj", ["`", "env"]], + ["get", "obj", ["`", "params"]]]], + ["do", + ["set", "new-obj", ["`", "macro?"], ["get", "obj", ["`", "macro?"]]], + ["set", "new-obj", ["`", "__meta__"], ["get", "obj", ["`", "__meta__"]]], + "new-obj"]], + ["throw", "clone of unsupported type"]]]]]]], ["def", "clone", ["fn", ["obj"], ["let", ["new-obj", ["_clone", "obj"]], @@ -111,21 +120,26 @@ ["def", "map?", ["fn", ["a"], ["isa", "a", "HashMap"]]], - +["def", "MalFunc", ["fn", [], null]], ["def", "malfunc", ["fn", ["fn", "ast", "env", "params"], - ["hash-map", ["`", "type"], ["`", "MalFunc"], - ["`", "fn"], "fn", - ["`", "ast"], "ast", - ["`", "env"], "env", - ["`", "params"], "params", - ["`", "macro?"], false]]], - -["def", "malfunc?", ["fn", ["obj"], - ["if", ["=", "obj", null], - false, - ["if", ["=", ["`", "MalFunc"], ["get", "obj", ["`", "type"]]], - true, - false]]]], + ["assocs!", ["new", "MalFunc"], + ["list", ["`", "fn"], "fn", + ["`", "ast"], "ast", + ["`", "env"], "env", + ["`", "params"], "params", + ["`", "macro?"], false]]]], + +["def", "malfunc?", ["fn", ["a"], + ["isa", "a", "MalFunc"]]], + +["def", "Atom", ["fn", [], null]], +["def", "atom", ["fn", ["a"], + ["let", ["atm", ["new", "Atom"]], + ["do", + ["set", "atm", ["`", "val"], "a"], + "atm"]]]], +["def", "atom?", ["fn", ["a"], + ["isa", "a", "Atom"]]], null ] -- cgit v1.2.3