1
1
mirror of https://github.com/kanaka/mal.git synced 2024-08-18 02:00:40 +03:00
mal/impls/scala/step2_eval.scala
Joel Martin 8a19f60386 Move implementations into impls/ dir
- Reorder README to have implementation list after "learning tool"
  bullet.

- This also moves tests/ and libs/ into impls. It would be preferrable
  to have these directories at the top level.  However, this causes
  difficulties with the wasm implementations which need pre-open
  directories and have trouble with paths starting with "../../". So
  in lieu of that, symlink those directories to the top-level.

- Move the run_argv_test.sh script into the tests directory for
  general hygiene.
2020-02-10 23:50:16 -06:00

76 lines
2.0 KiB
Scala

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,v) => (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
if (ast.asInstanceOf[MalList].value.length == 0)
return ast
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