aboutsummaryrefslogtreecommitdiff
path: root/scala/step2_eval.scala
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2015-03-02 21:33:10 -0600
committerJoel Martin <github@martintribe.org>2015-03-02 21:33:10 -0600
commit835fb7d8b06e2b44792a97ac89994658bf6d00af (patch)
tree578f67726ab9e3ce5fcbc50220e9761a66c5ddf1 /scala/step2_eval.scala
parent6b72e6078a7d505ecf9d711eb4a16fc4dfac36b6 (diff)
parent8a98ef9a3f3a6b6d05d02dc305a0c886c907e0f3 (diff)
downloadmal-835fb7d8b06e2b44792a97ac89994658bf6d00af.tar.gz
mal-835fb7d8b06e2b44792a97ac89994658bf6d00af.zip
Merge branch 'master' into gh-pages
Conflicts: .gitignore
Diffstat (limited to 'scala/step2_eval.scala')
-rw-r--r--scala/step2_eval.scala73
1 files changed, 73 insertions, 0 deletions
diff --git a/scala/step2_eval.scala b/scala/step2_eval.scala
new file mode 100644
index 0000000..d31d339
--- /dev/null
+++ b/scala/step2_eval.scala
@@ -0,0 +1,73 @@
+import types.{MalList, _list_Q, MalVector, MalHashMap, MalFunction}
+
+object step2_eval {
+ // read
+ def READ(str: String): Any = {
+ reader.read_str(str)
+ }
+
+ // eval
+ def eval_ast(ast: Any, env: Map[Symbol,Any]): Any = {
+ ast match {
+ case s : Symbol => env(s)
+ case v: MalVector => v.map(EVAL(_, env))
+ case l: MalList => l.map(EVAL(_, env))
+ case m: MalHashMap => {
+ m.map{case (k: String,v: Any) => (k, EVAL(v, env))}
+ }
+ case _ => ast
+ }
+ }
+
+ def EVAL(ast: Any, env: Map[Symbol,Any]): Any = {
+ //println("EVAL: " + printer._pr_str(ast,true))
+ if (!_list_Q(ast))
+ return eval_ast(ast, env)
+
+ // apply list
+ eval_ast(ast, env).asInstanceOf[MalList].value match {
+ case f :: el => {
+ var fn: List[Any] => Any = null
+ try {
+ fn = f.asInstanceOf[List[Any] => Any]
+ } catch {
+ case _: Throwable =>
+ throw new Exception("attempt to call non-function")
+ }
+ return fn(el)
+ }
+ case _ => throw new Exception("invalid apply")
+ }
+ }
+
+ // print
+ def PRINT(exp: Any): String = {
+ printer._pr_str(exp, true)
+ }
+
+ // repl
+ def main(args: Array[String]) = {
+ val repl_env: Map[Symbol,Any] = Map(
+ '+ -> ((a: List[Any]) => a(0).asInstanceOf[Long] + a(1).asInstanceOf[Long]),
+ '- -> ((a: List[Any]) => a(0).asInstanceOf[Long] - a(1).asInstanceOf[Long]),
+ '* -> ((a: List[Any]) => a(0).asInstanceOf[Long] * a(1).asInstanceOf[Long]),
+ '/ -> ((a: List[Any]) => a(0).asInstanceOf[Long] / a(1).asInstanceOf[Long]))
+ val REP = (str: String) => {
+ PRINT(EVAL(READ(str), repl_env))
+ }
+
+ var line:String = null
+ while ({line = readLine("user> "); line != null}) {
+ try {
+ println(REP(line))
+ } catch {
+ case e : Exception => {
+ println("Error: " + e.getMessage)
+ println(" " + e.getStackTrace.mkString("\n "))
+ }
+ }
+ }
+ }
+}
+
+// vim: ts=2:sw=2