Skip to content

Commit

Permalink
Pass 3
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon committed Oct 10, 2024
1 parent 52190af commit e2ef542
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 56 deletions.
31 changes: 14 additions & 17 deletions impls/scala/step2_eval.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import types.{MalList, _list_Q, MalVector, MalHashMap}
import types.{MalList, _list_Q, MalVector, MalHashMap, MalFunction}

object step2_eval {
// read
Expand All @@ -23,23 +23,20 @@ object step2_eval {
}

// apply list

ast.asInstanceOf[MalList].value match {
case Nil => {
return ast
}
case first :: rest => {
// function call
EVAL(first, env) match {
case fn: (List[Any] => Any) => {
val el = rest.map(EVAL(_, env))
return fn(el)
}
case f => {
throw new Exception("attempt to call non-function: " + f)
}
}
if (ast.asInstanceOf[MalList].value.length == 0)
return ast
ast.asInstanceOf[MalList].map(EVAL(_, env)).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")
}
}

Expand Down
25 changes: 14 additions & 11 deletions impls/scala/step3_env.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import types.{MalList, _list_Q, MalVector, MalHashMap}
import types.{MalList, _list_Q, MalVector, MalHashMap, MalFunction}
import env.Env

object step3_env {
Expand Down Expand Up @@ -29,7 +29,6 @@ object step3_env {
}

// apply list

ast.asInstanceOf[MalList].value match {
case Nil => {
return ast
Expand All @@ -44,17 +43,21 @@ object step3_env {
}
return EVAL(a2, let_env)
}
case first :: rest => {
case _ => {
// function call
EVAL(first, env) match {
case fn: (List[Any] => Any) => {
val el = rest.map(EVAL(_, env))
return fn(el)
}
case f => {
throw new Exception("attempt to call non-function: " + f)
}
ast.asInstanceOf[MalList].map(EVAL(_, env)).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")
}
}
}
}
Expand Down
23 changes: 13 additions & 10 deletions impls/scala/step4_if_fn_do.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ object step4_if_fn_do {
}

// apply list

ast.asInstanceOf[MalList].value match {
case Nil => {
return ast
Expand Down Expand Up @@ -63,17 +62,21 @@ object step4_if_fn_do {
EVAL(a2, new Env(env, types._toIter(a1), args.iterator))
})
}
case first :: rest => {
case _ => {
// function call
EVAL(first, env) match {
case fn: Func => {
val el = rest.map(EVAL(_, env))
return fn(el)
}
case f => {
throw new Exception("attempt to call non-function: " + f)
}
ast.asInstanceOf[MalList].map(EVAL(_, env)).value match {
case f :: el => {
var fn: Func = null
try {
fn = f.asInstanceOf[Func]
} catch {
case _: Throwable =>
throw new Exception("attempt to call non-function")
}
return fn(el)
}
case _ => throw new Exception("invalid apply")
}
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions impls/scala/step5_tco.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ object step5_tco {
}

// apply list

ast.asInstanceOf[MalList].value match {
case Nil => {
return ast
Expand Down Expand Up @@ -68,22 +67,25 @@ object step5_tco {
}
)
}
case first :: rest => {
case _ => {
// function call
EVAL(first, env) match {
ast.asInstanceOf[MalList].map(EVAL(_, env)).value match {
case f :: el => {
f match {
case fn: MalFunction => {
val el = rest.map(EVAL(_, env))
env = fn.gen_env(el)
ast = fn.ast // continue loop (TCO)
}
case fn: Func => {
val el = rest.map(EVAL(_, env))
return fn(el)
}
case f => {
case _ => {
throw new Exception("attempt to call non-function: " + f)
}
}
}
case _ => throw new Exception("invalid apply")
}
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions impls/scala/step6_file.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ object step6_file {
}

// apply list

ast.asInstanceOf[MalList].value match {
case Nil => {
return ast
Expand Down Expand Up @@ -68,22 +67,25 @@ object step6_file {
}
)
}
case first :: rest => {
case _ => {
// function call
EVAL(first, env) match {
ast.asInstanceOf[MalList].map(EVAL(_, env)).value match {
case f :: el => {
f match {
case fn: MalFunction => {
val el = rest.map(EVAL(_, env))
env = fn.gen_env(el)
ast = fn.ast // continue loop (TCO)
}
case fn: Func => {
val el = rest.map(EVAL(_, env))
return fn(el)
}
case f => {
case _ => {
throw new Exception("attempt to call non-function: " + f)
}
}
}
case _ => throw new Exception("invalid apply")
}
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions impls/scala/step7_quote.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ object step7_quote {
}

// apply list

ast.asInstanceOf[MalList].value match {
case Nil => {
return ast
Expand Down Expand Up @@ -111,22 +110,25 @@ object step7_quote {
}
)
}
case first :: rest => {
case _ => {
// function call
EVAL(first, env) match {
ast.asInstanceOf[MalList].map(EVAL(_, env)).value match {
case f :: el => {
f match {
case fn: MalFunction => {
val el = rest.map(EVAL(_, env))
env = fn.gen_env(el)
ast = fn.ast // continue loop (TCO)
}
case fn: Func => {
val el = rest.map(EVAL(_, env))
return fn(el)
}
case f => {
case _ => {
throw new Exception("attempt to call non-function: " + f)
}
}
}
case _ => throw new Exception("invalid apply")
}
}
}
}
Expand Down

0 comments on commit e2ef542

Please sign in to comment.