aboutsummaryrefslogtreecommitdiff
path: root/coffee/step2_eval.coffee
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-11-08 16:56:36 -0600
committerJoel Martin <github@martintribe.org>2015-01-09 16:16:45 -0600
commit891c3f3b478292ad0bfca44b0dc098a2aecc9a5d (patch)
treed30c7923ffee7699cfca94af84f3be297448fff2 /coffee/step2_eval.coffee
parent9b3362e86a57ed7f14c5fd018c37713185e0c154 (diff)
downloadmal-891c3f3b478292ad0bfca44b0dc098a2aecc9a5d.tar.gz
mal-891c3f3b478292ad0bfca44b0dc098a2aecc9a5d.zip
CoffeeScript: add all steps. Self-hosting.
Diffstat (limited to 'coffee/step2_eval.coffee')
-rw-r--r--coffee/step2_eval.coffee52
1 files changed, 52 insertions, 0 deletions
diff --git a/coffee/step2_eval.coffee b/coffee/step2_eval.coffee
new file mode 100644
index 0000000..1b3438e
--- /dev/null
+++ b/coffee/step2_eval.coffee
@@ -0,0 +1,52 @@
+readline = require "./node_readline.coffee"
+types = require "./types.coffee"
+reader = require "./reader.coffee"
+printer = require "./printer.coffee"
+
+# read
+READ = (str) -> reader.read_str str
+
+# eval
+eval_ast = (ast, env) ->
+ if types._symbol_Q(ast) then env[ast.name]
+ else if types._list_Q(ast) then ast.map((a) -> EVAL(a, env))
+ else if types._vector_Q(ast)
+ types._vector(ast.map((a) -> EVAL(a, env))...)
+ else if types._hash_map_Q(ast)
+ new_hm = {}
+ new_hm[k] = EVAL(ast[k],env) for k,v of ast
+ new_hm
+ else ast
+
+EVAL = (ast, env) ->
+ #console.log "EVAL:", printer._pr_str ast
+ if !types._list_Q ast then return eval_ast ast, env
+
+ # apply list
+ [f, args...] = eval_ast ast, env
+ f(args...)
+
+
+# print
+PRINT = (exp) -> printer._pr_str exp, true
+
+# repl
+repl_env = {}
+rep = (str) -> PRINT(EVAL(READ(str), repl_env))
+
+repl_env["+"] = (a,b) -> a+b
+repl_env["-"] = (a,b) -> a-b
+repl_env["*"] = (a,b) -> a*b
+repl_env["/"] = (a,b) -> a/b
+
+# repl loop
+while (line = readline.readline("user> ")) != null
+ continue if line == ""
+ try
+ console.log rep line
+ catch exc
+ continue if exc instanceof reader.BlankException
+ if exc.stack then console.log exc.stack
+ else console.log exc
+
+# vim: ts=2:sw=2