aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2015-02-11 21:56:47 -0600
committerJoel Martin <github@martintribe.org>2015-02-11 21:56:47 -0600
commitc1fe72ae2b94d37cbf027aa185c27307d9776c56 (patch)
tree6826d1941743bf7d450b538bbcae86fb6749b8e9
parentff26ebdb816da07b28b29073868994fc7eabf8d1 (diff)
downloadmal-c1fe72ae2b94d37cbf027aa185c27307d9776c56.tar.gz
mal-c1fe72ae2b94d37cbf027aa185c27307d9776c56.zip
miniMAL: step1.
-rw-r--r--Makefile5
-rw-r--r--miniMAL/miniMAL-core.json98
-rw-r--r--miniMAL/printer.json25
-rw-r--r--miniMAL/reader.json80
-rw-r--r--miniMAL/step0_repl.json21
-rw-r--r--miniMAL/step1_read_print.json26
6 files changed, 254 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 8d13bce..47c7829 100644
--- a/Makefile
+++ b/Makefile
@@ -11,7 +11,8 @@ PYTHON = python
#
IMPLS = bash c clojure coffee cs forth go haskell java js lua make mal \
- ocaml matlab perl php ps python r racket ruby rust scala vb
+ ocaml matlab miniMAL perl php ps python r racket ruby rust \
+ scala vb
step0 = step0_repl
step1 = step1_read_print
@@ -64,6 +65,7 @@ make_STEP_TO_PROG = make/$($(1)).mk
mal_STEP_TO_PROG = mal/$($(1)).mal
ocaml_STEP_TO_PROG = ocaml/$($(1))
matlab_STEP_TO_PROG = matlab/$($(1)).m
+miniMAL_STEP_TO_PROG = miniMAL/$($(1)).json
perl_STEP_TO_PROG = perl/$($(1)).pl
php_STEP_TO_PROG = php/$($(1)).php
ps_STEP_TO_PROG = ps/$($(1)).ps
@@ -96,6 +98,7 @@ mal_RUNSTEP = $(call $(MAL_IMPL)_RUNSTEP,$(1),$(call $(MAL_IMPL)_STEP_TO_PRO
ocaml_RUNSTEP = ../$(2) $(3)
matlab_args = $(subst $(SPACE),$(COMMA),$(foreach x,$(strip $(1)),'$(x)'))
matlab_RUNSTEP = matlab -nodisplay -nosplash -nodesktop -nojvm -r "$($(1))($(call matlab_args,$(3)));quit;"
+miniMAL_RUNSTEP = miniMAL ../$(2) $(3)
perl_RUNSTEP = perl ../$(2) --raw $(3)
php_RUNSTEP = php ../$(2) $(3)
ps_RUNSTEP = $(4)gs -q -I./ -dNODISPLAY -- ../$(2) $(3)$(4)
diff --git a/miniMAL/miniMAL-core.json b/miniMAL/miniMAL-core.json
new file mode 100644
index 0000000..a4acfec
--- /dev/null
+++ b/miniMAL/miniMAL-core.json
@@ -0,0 +1,98 @@
+["do",
+
+["def", "map", ["fn", ["a", "b"], [".", "b", ["`", "map"], "a"]]],
+["def", "not", ["fn", ["a"], ["if", "a", false, true]]],
+
+["def", "nil?", ["fn", ["a"], ["=", null, "a"]]],
+["def", "true?", ["fn", ["a"], ["=", true, "a"]]],
+["def", "false?", ["fn", ["a"], ["=", false, "a"]]],
+["def", "string?", ["fn", ["a"],
+ ["if", ["=", "a", null],
+ false,
+ ["=", ["`", "String"],
+ [".-", [".-", "a", ["`", "constructor"]],
+ ["`", "name"]]]]]],
+
+["def", "pr-list*", ["fn", ["a", "pr", "sep"],
+ [".", ["map", ["fn", ["x"],
+ ["if", "pr",
+ [".", "JSON", ["`", "stringify"], "x"],
+ ["if", ["string?", "x"],
+ "x",
+ [".", "JSON", ["`", "stringify"], "x"]]]],
+ "a"],
+ ["`", "join"], "sep"]]],
+["def", "pr-str", ["fn", ["&", "a"],
+ ["pr-list*", "a", true, ["`", " "]]]],
+["def", "str", ["fn", ["&", "a"],
+ ["pr-list*", "a", false, ["`", ""]]]],
+["def", "prn", ["fn", ["&", "a"],
+ [".", "console", ["`", "log"],
+ ["pr-list*", "a", true, ["`", " "]]]]],
+["def", "println", ["fn", ["&", "a"],
+ [".", "console", ["`", "log"],
+ ["pr-list*", "a", false, ["`", " "]]]]],
+
+["def", ">=", ["fn", ["a", "b"],
+ ["if", ["<", "a", "b"], false, true]]],
+["def", ">", ["fn", ["a", "b"],
+ ["if", [">=", "a", "b"], ["if", ["=", "a", "b"], false, true], false]]],
+["def", "<=", ["fn", ["a", "b"],
+ ["if", [">", "a", "b"], false, true]]],
+
+["def", "list", ["fn", ["&", "a"], "a"]],
+["def", "list?", ["fn", ["a"], [".", "Array", ["`", "isArray"], "a"]]],
+["def", "assoc!", ["fn", ["a", "b", "c"], ["do", ["set", "a", "b", "c"], "a"]]],
+["def", "assocs!", ["fn", ["hm", "kvs"],
+ ["if", ["empty?", "kvs"],
+ "hm",
+ ["do",
+ ["assoc!", "hm", ["get", "kvs", 0], ["get", "kvs", 1]],
+ ["assocs!", "hm", ["slice", "kvs", 2]]]]]],
+["def", "hash-map", ["fn", ["&", "a"],
+ ["assocs!", ["new", "Object"], "a"]]],
+["def", "get", ["fn", ["a", "b"], [".-", "a", "b"]]],
+["def", "set", ["fn", ["a", "b", "c"], [".-", "a", "b", "c"]]],
+["def", "contains?", ["fn", ["a", "b"], [".", "a", ["`", "hasOwnProperty"], "b"]]],
+["def", "keys", ["fn", ["a"], [".", "Object", ["`", "keys"], "a"]]],
+["def", "vals", ["fn", ["a"], ["map",["fn",["k"],["get","a","k"]],["keys", "a"]]]],
+
+["def", "cons", ["fn", ["a", "b"],
+ [".", ["`", []],
+ ["`", "concat"], ["list", "a"], "b"]]],
+["def", "concat", ["fn", ["&", "a"],
+ [".", [".-", ["list"], ["`", "concat"]],
+ ["`", "apply"], ["list"], "a"]]],
+["def", "nth", "get"],
+["def", "first", ["fn", ["a"], ["nth", "a", 0]]],
+["def", "rest", ["fn", ["a"], [".", "a", ["`", "slice"], 1]]],
+["def", "empty?", ["fn", ["a"], ["if", ["list?", "a"], ["if", ["=", 0, [".-", "a", ["`", "length"]]], true, false], ["=", "a", null]]]],
+["def", "count", ["fn", ["a"],
+ [".-", "a", ["`", "length"]]]],
+["def", "slice", ["fn", ["a", "start", "&", "endl"],
+ ["let", ["end", ["if", ["count", "endl"],
+ ["get", "endl", 0],
+ [".-", "a", ["`", "length"]]]],
+ [".", "a", ["`", "slice"], "start", "end"]]]],
+
+["def", "apply", ["fn", ["a", "b"], [".", "a", ["`", "apply"], "a", "b"]]],
+
+["def", "typeof", ["fn", ["a"], ["js", ["str", ["`", "typeof "], ["pr-str", "a"]]]]],
+
+["def", "repl", ["fn",["prompt", "rep"],
+ ["let", ["r", ["require", ["`", "repl"]],
+ "evl", ["fn", ["l", "c", "f", "cb"],
+ ["let", ["line", ["slice", "l", 1, ["-", [".-", "l", ["`", "length"]], 2]]],
+ ["do",
+ ["println", ["rep", "line"]],
+ ["cb"]]]],
+ "opts", {"ignoreUndefined": true,
+ "terminal": false},
+ "opts", ["assoc!", "opts", ["`", "prompt"], "prompt"],
+ "opts", ["assoc!", "opts", ["`", "eval"], "evl"]],
+ [".", "r", ["`", "start"], "opts"]]]],
+
+null
+
+]
+
diff --git a/miniMAL/printer.json b/miniMAL/printer.json
new file mode 100644
index 0000000..28d5d4d
--- /dev/null
+++ b/miniMAL/printer.json
@@ -0,0 +1,25 @@
+["do",
+
+["def", "pr-str", ["fn", ["exp", "print_readably"],
+ ["if", ["list?", "exp"],
+ ["str",
+ ["`", "("],
+ [".", ["map", ["fn", ["x"], ["pr-str", "x", "print_readably"]], "exp"],
+ ["`", "join"], ["`", " "]],
+ ["`", ")"]],
+ ["if", ["=", ["`", "string"], ["type", "exp"]],
+ ["str", ["`", "\""], "exp", ["`", "\""]],
+ ["if", ["=", ["`", "number"], ["type", "exp"]],
+ "exp",
+ ["if", ["=", null, "exp"],
+ ["`", "nil"],
+ ["if", ["=", true, "exp"],
+ ["`", "true"],
+ ["if", ["=", false, "exp"],
+ ["`", "false"],
+ ["if", ["=", ["`", "Symbol"], ["get", "exp", ["`", "type"]]],
+ ["get", "exp", ["`", "val"]],
+ ["str", ["`", "#<unknown: "], "exp", ["`", ">"]]]]]]]]]]],
+
+null
+]
diff --git a/miniMAL/reader.json b/miniMAL/reader.json
new file mode 100644
index 0000000..2cdcde4
--- /dev/null
+++ b/miniMAL/reader.json
@@ -0,0 +1,80 @@
+["do",
+
+["def", "rdr-new", ["fn", ["tokens"],
+ ["hash-map", ["`", "tokens"], "tokens",
+ ["`", "position"], 0]]],
+
+["def", "rdr-next", ["fn", ["rdr"],
+ ["let", ["pos", ["get", "rdr", ["`", "position"]],
+ "val", ["get", ["get", "rdr", ["`", "tokens"]], "pos"]],
+ ["do",
+ ["assoc!", "rdr", ["`", "position"], ["+", 1, "pos"]],
+ "val"]]]],
+
+["def", "rdr-peek", ["fn", ["rdr"],
+ ["let", ["pos", ["get", "rdr", ["`", "position"]]],
+ ["get", ["get", "rdr", ["`", "tokens"]], "pos"]]]],
+
+
+["def", "re-matches", ["fn", ["re", "strn", "acc"],
+ ["let", ["match", [".", "re", ["`", "exec"], "strn"],
+ "g1", ["get", "match", 1]],
+ ["if", ["=", "g1", ["`", ""]],
+ "acc",
+ ["re-matches", "re", "strn", ["concat", "acc", "g1"]]]]]],
+
+["def", "tokenize", ["fn", ["strn"],
+ ["let", ["re-str", ["`", "[\\s,]*(~@|[\\[\\]{}()'`~^@]|\"(?:\\\\.|[^\\\\\"])*\"|;.*|[^\\s\\[\\]{}('\"`,;)]*)"],
+ "re", ["RegExp", "re-str", ["`", "g"]]],
+ ["re-matches", "re", "strn", ["`", []]]]]],
+
+["def", "read-atom", ["fn", ["rdr"],
+ ["let", ["token", ["rdr-next", "rdr"]],
+ ["if", [".", "token", ["`", "match"], ["RegExp", ["`", "^-?[0-9]+$"]]],
+ ["parseInt", "token", 10],
+ ["if", ["=", ["`", "\""], ["get", "token", 0]],
+ ["slice", "token", 1, ["-", ["count", "token"], 1]],
+ ["if", ["=", ["`", "nil"], "token"],
+ null,
+ ["if", ["=", ["`", "true"], "token"],
+ true,
+ ["if", ["=", ["`", "false"], "token"],
+ false,
+ ["hash-map", ["`", "type"], ["`", "Symbol"],
+ ["`", "val"], "token"]]]]]]]]],
+
+["def", "read-list-entries", ["fn", ["rdr"],
+ ["let", ["tok", ["rdr-peek", "rdr"]],
+ ["if", "tok",
+ ["if", ["=", ["`", ")"], "tok"],
+ ["`", []],
+ ["cons", ["read-form", "rdr"],
+ ["read-list-entries", "rdr"]]],
+ ["throw", ["`", "expected ')'"]]]]]],
+
+["def", "read-list", ["fn", ["rdr"],
+ ["let", ["token", ["rdr-next", "rdr"]],
+ ["if", ["=", ["`", "("], "token"],
+ ["let", ["lst", ["read-list-entries", "rdr"]],
+ ["do",
+ ["rdr-next", "rdr"],
+ "lst"]],
+ ["throw", ["`", "expected '('"]]]]]],
+
+["def", "read-form", ["fn", ["rdr"],
+ ["let", ["token", ["rdr-peek", "rdr"]],
+ ["if", ["=", ["`", ")"], "token"],
+ ["throw", ["`", "unexpected ')'"]],
+ ["if", ["=", ["`", "("], "token"],
+ ["read-list", "rdr"],
+ ["read-atom", "rdr"]]]]]],
+
+["def", "read-str", ["fn", ["strn"],
+ ["let", ["tokens", ["tokenize", "strn"],
+ "rdr", ["rdr-new", "tokens"]],
+ ["if", ["empty?", "tokens"],
+ null,
+ ["read-form", "rdr"]]]]],
+
+null
+]
diff --git a/miniMAL/step0_repl.json b/miniMAL/step0_repl.json
new file mode 100644
index 0000000..6599930
--- /dev/null
+++ b/miniMAL/step0_repl.json
@@ -0,0 +1,21 @@
+["do",
+
+["load-file", ["`", "miniMAL-core.json"]],
+
+["def", "READ", ["fn", ["strng"],
+ "strng"]],
+
+["def", "EVAL", ["fn", ["ast", "env"],
+ "ast"]],
+
+["def", "PRINT", ["fn", ["exp"],
+ "exp"]],
+
+["def", "rep", ["fn", ["strng"],
+ ["PRINT", ["EVAL", ["READ", "strng"], null]]]],
+
+["repl", ["`", "user> "], "rep"],
+
+null
+
+]
diff --git a/miniMAL/step1_read_print.json b/miniMAL/step1_read_print.json
new file mode 100644
index 0000000..392cab6
--- /dev/null
+++ b/miniMAL/step1_read_print.json
@@ -0,0 +1,26 @@
+["do",
+
+["load-file", ["`", "miniMAL-core.json"]],
+["load-file", ["`", "reader.json"]],
+["load-file", ["`", "printer.json"]],
+
+["def", "READ", ["fn", ["strng"],
+ ["read-str", "strng"]]],
+
+["def", "EVAL", ["fn", ["ast", "env"],
+ "ast"]],
+
+["def", "PRINT", ["fn", ["exp"],
+ ["pr-str", "exp", true]]],
+
+["def", "rep", ["fn", ["strng"],
+ ["try",
+ ["PRINT", ["EVAL", ["READ", "strng"], null]],
+ ["catch", "exc",
+ ["str", ["`", "Error: "], "exc"]]]]],
+
+["repl", ["`", "user> "], "rep"],
+
+null
+
+]