Merge pull request #406 from chr15m/lib-alias-hacks
[jackhill/mal.git] / rpython / step8_macros.py
index 2b6faeb..c231b10 100644 (file)
@@ -1,8 +1,9 @@
 import sys, traceback
 import mal_readline
 import mal_types as types
-from mal_types import (MalSym, MalInt, MalStr, nil, true, false,
-                       _symbol, _keywordu, MalList, _list, MalFunc)
+from mal_types import (MalSym, MalInt, MalStr,
+                       nil, true, false, _symbol, _keywordu,
+                       MalList, _list, MalVector, MalHashMap, MalFunc)
 import reader, printer
 from env import Env
 import core
@@ -58,14 +59,16 @@ def eval_ast(ast, env):
         for a in ast.values:
             res.append(EVAL(a, env))
         return MalList(res)
-##    elif types._vector_Q(ast):
-##        return types._vector(*map(lambda a: EVAL(a, env), ast))
-##    elif types._hash_map_Q(ast):
-##        keyvals = []
-##        for k in ast.keys():
-##            keyvals.append(EVAL(k, env))
-##            keyvals.append(EVAL(ast[k], env))
-##        return types._hash_map(*keyvals)
+    elif types._vector_Q(ast):
+        res = []
+        for a in ast.values:
+            res.append(EVAL(a, env))
+        return MalVector(res)
+    elif types._hash_map_Q(ast):
+        new_dct = {}
+        for k in ast.dct.keys():
+            new_dct[k] = EVAL(ast.dct[k], env)
+        return MalHashMap(new_dct)
     else:
         return ast  # primitive value, return unchanged
 
@@ -74,10 +77,12 @@ def EVAL(ast, env):
         #print("EVAL %s" % printer._pr_str(ast))
         if not types._list_Q(ast):
             return eval_ast(ast, env)
+        if len(ast) == 0: return ast
 
         # apply list
         ast = macroexpand(ast, env)
-        if not types._list_Q(ast): return ast
+        if not types._list_Q(ast):
+            return eval_ast(ast, env)
         if len(ast) == 0: return ast
         a0 = ast[0]
         if isinstance(a0, MalSym):
@@ -178,8 +183,10 @@ def entry_point(argv):
             break
         except reader.Blank:
             continue
+        except types.MalException as e:
+            print(u"Error: %s" % printer._pr_str(e.object, False))
         except Exception as e:
-            print("%s" % e)
+            print("Error: %s" % e)
             #print("".join(traceback.format_exception(*sys.exc_info())))
     return 0