Skip to content

Commit

Permalink
scala: merge eval_ast into eval
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon committed Oct 10, 2024
1 parent f16a22a commit c3ef1a7
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 275 deletions.
23 changes: 10 additions & 13 deletions impls/scala/step2_eval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@ object step2_eval {
}

// eval
def eval_ast(ast: Any, env: Map[Symbol,Any]): Any = {
def EVAL(ast: Any, env: Map[Symbol,Any]): Any = {

// println("EVAL: " + printer._pr_str(ast,true))

ast match {
case s : Symbol => env(s)
case v: MalVector => v.map(EVAL(_, env))
case l: MalList => l.map(EVAL(_, env))
case s : Symbol => return env(s)
case v: MalVector => return v.map(EVAL(_, env))
case l: MalList => {}
case m: MalHashMap => {
m.map{case (k,v) => (k, EVAL(v, env))}
return m.map{case (k,v) => (k, EVAL(v, env))}
}
case _ => ast
case _ => return 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 {
ast.asInstanceOf[MalList].map(EVAL(_, env)).value match {
case f :: el => {
var fn: List[Any] => Any = null
try {
Expand Down
28 changes: 15 additions & 13 deletions impls/scala/step3_env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ object step3_env {
}

// eval
def eval_ast(ast: Any, env: Env): Any = {
def EVAL(ast: Any, env: Env): Any = {

if (env.find(Symbol("DEBUG-EVAL")) != null) {
val dbgeval = env.get(Symbol("DEBUG-EVAL"))
if (dbgeval != null && dbgeval != false) {
println("EVAL: " + printer._pr_str(ast,true))
}
}

ast match {
case s : Symbol => env.get(s)
case v: MalVector => v.map(EVAL(_, env))
case l: MalList => l.map(EVAL(_, env))
case s : Symbol => return env.get(s)
case v: MalVector => return v.map(EVAL(_, env))
case l: MalList => {}
case m: MalHashMap => {
m.map{case (k,v) => (k, EVAL(v, env))}
return m.map{case (k,v) => (k, EVAL(v, env))}
}
case _ => ast
case _ => return ast
}
}

def EVAL(ast: Any, env: Env): Any = {
//println("EVAL: " + printer._pr_str(ast,true))
if (!_list_Q(ast))
return eval_ast(ast, env)

// apply list
ast.asInstanceOf[MalList].value match {
Expand All @@ -42,7 +44,7 @@ object step3_env {
}
case _ => {
// function call
eval_ast(ast, env).asInstanceOf[MalList].value match {
ast.asInstanceOf[MalList].map(EVAL(_, env)).value match {
case f :: el => {
var fn: List[Any] => Any = null
try {
Expand Down
32 changes: 17 additions & 15 deletions impls/scala/step4_if_fn_do.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ object step4_if_fn_do {
}

// eval
def eval_ast(ast: Any, env: Env): Any = {
def EVAL(ast: Any, env: Env): Any = {

if (env.find(Symbol("DEBUG-EVAL")) != null) {
val dbgeval = env.get(Symbol("DEBUG-EVAL"))
if (dbgeval != null && dbgeval != false) {
println("EVAL: " + printer._pr_str(ast,true))
}
}

ast match {
case s : Symbol => env.get(s)
case v: MalVector => v.map(EVAL(_, env))
case l: MalList => l.map(EVAL(_, env))
case s : Symbol => return env.get(s)
case v: MalVector => return v.map(EVAL(_, env))
case l: MalList => {}
case m: MalHashMap => {
m.map{case (k,v) => (k, EVAL(v, env))}
return m.map{case (k,v) => (k, EVAL(v, env))}
}
case _ => ast
case _ => return ast
}
}

def EVAL(ast: Any, env: Env): Any = {
//println("EVAL: " + printer._pr_str(ast,true))
if (!_list_Q(ast))
return eval_ast(ast, env)

// apply list
ast.asInstanceOf[MalList].value match {
Expand All @@ -42,8 +44,8 @@ object step4_if_fn_do {
return EVAL(a2, let_env)
}
case Symbol("do") :: rest => {
val el = eval_ast(_list(rest:_*), env)
return el.asInstanceOf[MalList].value.last
val el = rest.map(EVAL(_, env))
return el.last
}
case Symbol("if") :: a1 :: a2 :: rest => {
val cond = EVAL(a1, env)
Expand All @@ -61,7 +63,7 @@ object step4_if_fn_do {
}
case _ => {
// function call
eval_ast(ast, env).asInstanceOf[MalList].value match {
ast.asInstanceOf[MalList].map(EVAL(_, env)).value match {
case f :: el => {
var fn: Func = null
try {
Expand Down
35 changes: 18 additions & 17 deletions impls/scala/step5_tco.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ object step5_tco {
}

// eval
def eval_ast(ast: Any, env: Env): Any = {
ast match {
case s : Symbol => env.get(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(orig_ast: Any, orig_env: Env): Any = {
var ast = orig_ast; var env = orig_env;
while (true) {

//println("EVAL: " + printer._pr_str(ast,true))
if (!_list_Q(ast))
return eval_ast(ast, env)
if (env.find(Symbol("DEBUG-EVAL")) != null) {
val dbgeval = env.get(Symbol("DEBUG-EVAL"))
if (dbgeval != null && dbgeval != false) {
println("EVAL: " + printer._pr_str(ast,true))
}
}

ast match {
case s : Symbol => return env.get(s)
case v: MalVector => return v.map(EVAL(_, env))
case l: MalList => {}
case m: MalHashMap => {
return m.map{case (k,v) => (k, EVAL(v, env))}
}
case _ => return ast
}

// apply list
ast.asInstanceOf[MalList].value match {
Expand All @@ -46,7 +47,7 @@ object step5_tco {
ast = a2 // continue loop (TCO)
}
case Symbol("do") :: rest => {
eval_ast(_list(rest.slice(0,rest.length-1):_*), env)
rest.slice(0,rest.length-1).map(EVAL(_, env))
ast = ast.asInstanceOf[MalList].value.last // continue loop (TCO)
}
case Symbol("if") :: a1 :: a2 :: rest => {
Expand All @@ -67,7 +68,7 @@ object step5_tco {
}
case _ => {
// function call
eval_ast(ast, env).asInstanceOf[MalList].value match {
ast.asInstanceOf[MalList].map(EVAL(_, env)).value match {
case f :: el => {
f match {
case fn: MalFunction => {
Expand Down
35 changes: 18 additions & 17 deletions impls/scala/step6_file.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ object step6_file {
}

// eval
def eval_ast(ast: Any, env: Env): Any = {
ast match {
case s : Symbol => env.get(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(orig_ast: Any, orig_env: Env): Any = {
var ast = orig_ast; var env = orig_env;
while (true) {

//println("EVAL: " + printer._pr_str(ast,true))
if (!_list_Q(ast))
return eval_ast(ast, env)
if (env.find(Symbol("DEBUG-EVAL")) != null) {
val dbgeval = env.get(Symbol("DEBUG-EVAL"))
if (dbgeval != null && dbgeval != false) {
println("EVAL: " + printer._pr_str(ast,true))
}
}

ast match {
case s : Symbol => return env.get(s)
case v: MalVector => return v.map(EVAL(_, env))
case l: MalList => {}
case m: MalHashMap => {
return m.map{case (k,v) => (k, EVAL(v, env))}
}
case _ => return ast
}

// apply list
ast.asInstanceOf[MalList].value match {
Expand All @@ -46,7 +47,7 @@ object step6_file {
ast = a2 // continue loop (TCO)
}
case Symbol("do") :: rest => {
eval_ast(_list(rest.slice(0,rest.length-1):_*), env)
rest.slice(0,rest.length-1).map(EVAL(_, env))
ast = ast.asInstanceOf[MalList].value.last // continue loop (TCO)
}
case Symbol("if") :: a1 :: a2 :: rest => {
Expand All @@ -67,7 +68,7 @@ object step6_file {
}
case _ => {
// function call
eval_ast(ast, env).asInstanceOf[MalList].value match {
ast.asInstanceOf[MalList].map(EVAL(_, env)).value match {
case f :: el => {
f match {
case fn: MalFunction => {
Expand Down
38 changes: 18 additions & 20 deletions impls/scala/step7_quote.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,26 @@ object step7_quote {
}
}

def eval_ast(ast: Any, env: Env): Any = {
ast match {
case s : Symbol => env.get(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(orig_ast: Any, orig_env: Env): Any = {
var ast = orig_ast; var env = orig_env;
while (true) {

//println("EVAL: " + printer._pr_str(ast,true))
if (!_list_Q(ast))
return eval_ast(ast, env)
if (env.find(Symbol("DEBUG-EVAL")) != null) {
val dbgeval = env.get(Symbol("DEBUG-EVAL"))
if (dbgeval != null && dbgeval != false) {
println("EVAL: " + printer._pr_str(ast,true))
}
}

ast match {
case s : Symbol => return env.get(s)
case v: MalVector => return v.map(EVAL(_, env))
case l: MalList => {}
case m: MalHashMap => {
return m.map{case (k,v) => (k, EVAL(v, env))}
}
case _ => return ast
}

// apply list
ast.asInstanceOf[MalList].value match {
Expand All @@ -85,14 +86,11 @@ object step7_quote {
case Symbol("quote") :: a1 :: Nil => {
return a1
}
case Symbol("quasiquoteexpand") :: a1 :: Nil => {
return quasiquote(a1)
}
case Symbol("quasiquote") :: a1 :: Nil => {
ast = quasiquote(a1) // continue loop (TCO)
}
case Symbol("do") :: rest => {
eval_ast(_list(rest.slice(0,rest.length-1):_*), env)
rest.slice(0,rest.length-1).map(EVAL(_, env))
ast = ast.asInstanceOf[MalList].value.last // continue loop (TCO)
}
case Symbol("if") :: a1 :: a2 :: rest => {
Expand All @@ -113,7 +111,7 @@ object step7_quote {
}
case _ => {
// function call
eval_ast(ast, env).asInstanceOf[MalList].value match {
ast.asInstanceOf[MalList].map(EVAL(_, env)).value match {
case f :: el => {
f match {
case fn: MalFunction => {
Expand Down
Loading

0 comments on commit c3ef1a7

Please sign in to comment.