Merge pull request #217 from dubek/lua-interop
[jackhill/mal.git] / nim / step4_if_fn_do.nim
index b235733..e017974 100644 (file)
@@ -7,7 +7,7 @@ proc eval(ast: MalType, env: var Env): MalType
 proc eval_ast(ast: MalType, env: var Env): MalType =
   case ast.kind
   of Symbol:
-    result = env.get(ast.symbol)
+    result = env.get(ast.str)
   of List:
     result = list ast.list.mapIt(MalType, it.eval(env))
   of Vector:
@@ -22,15 +22,16 @@ proc eval_ast(ast: MalType, env: var Env): MalType =
 proc eval(ast: MalType, env: var Env): MalType =
   case ast.kind
   of List:
+    if ast.list.len == 0: return ast
     let a0 = ast.list[0]
     case a0.kind
     of Symbol:
-      case a0.symbol
+      case a0.str
       of "def!":
         let
           a1 = ast.list[1]
           a2 = ast.list[2]
-        result = env.set(a1.symbol, a2.eval(env))
+        result = env.set(a1.str, a2.eval(env))
 
       of "let*":
         let
@@ -42,12 +43,12 @@ proc eval(ast: MalType, env: var Env): MalType =
         case a1.kind
         of List, Vector:
           for i in countup(0, a1.list.high, 2):
-            letEnv.set(a1.list[i].symbol, a1.list[i+1].eval(letEnv))
+            letEnv.set(a1.list[i].str, a1.list[i+1].eval(letEnv))
         else: discard
         result = a2.eval(letEnv)
 
       of "do":
-        let el = (list ast.list[1 .. -1]).eval_ast(env)
+        let el = (list ast.list[1 .. ^1]).eval_ast(env)
         result = el.list[el.list.high]
 
       of "if":
@@ -72,11 +73,11 @@ proc eval(ast: MalType, env: var Env): MalType =
 
       else:
         let el = ast.eval_ast(env)
-        result = el.list[0].fun(el.list[1 .. -1])
+        result = el.list[0].fun(el.list[1 .. ^1])
 
     else:
       let el = ast.eval_ast(env)
-      result = el.list[0].fun(el.list[1 .. -1])
+      result = el.list[0].fun(el.list[1 .. ^1])
 
   else:
     result = ast.eval_ast(env)