make, swift3: fix parsing empty literal sequences.
[jackhill/mal.git] / rpython / stepA_mal.py
index 195a4c4..5fd5592 100644 (file)
@@ -1,4 +1,13 @@
-import sys, traceback
+import sys
+IS_RPYTHON = sys.argv[0].endswith('rpython')
+
+if IS_RPYTHON:
+    #from rpython.rlib.debug import fatalerror
+    from rpython.rtyper.lltypesystem import lltype
+    from rpython.rtyper.lltypesystem.lloperation import llop
+else:
+    import traceback
+
 import mal_readline
 import mal_types as types
 from mal_types import (MalSym, MalInt, MalStr,
@@ -77,10 +86,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):
@@ -110,6 +121,8 @@ def EVAL(ast, env):
         elif u"macroexpand" == a0sym:
             return macroexpand(ast[1], env)
         elif u"try*" == a0sym:
+            if len(ast) < 3:
+                return EVAL(ast[1], env);
             a1, a2 = ast[1], ast[2]
             a20 = a2[0]
             if isinstance(a20, MalSym):
@@ -179,11 +192,13 @@ def entry_point(argv):
     repl_env.set(_symbol(u'*ARGV*'), MalList(mal_args))
 
     # core.mal: defined using the language itself
-    REP("(def! *host-language* \"python\")", repl_env)
+    REP("(def! *host-language* \"rpython\")", repl_env)
     REP("(def! not (fn* (a) (if a false true)))", repl_env)
     REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))", repl_env)
     REP("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))", repl_env)
-    REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))", repl_env)
+    REP("(def! inc (fn* [x] (+ x 1)))", repl_env)
+    REP("(def! gensym (let* [counter (atom 0)] (fn* [] (symbol (str \"G__\" (swap! counter inc))))))", repl_env)
+    REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) `(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs)))))))))", repl_env)
 
     if len(argv) >= 2:
         REP('(load-file "' + argv[1] + '")', repl_env)
@@ -201,9 +216,12 @@ def entry_point(argv):
             continue
         except types.MalException as e:
             print(u"Error: %s" % printer._pr_str(e.object, False))
-#        except Exception as e:
-#            print("Error: %s" % e)
-            #print("".join(traceback.format_exception(*sys.exc_info())))
+        except Exception as e:
+            print("Error: %s" % e)
+            if IS_RPYTHON:
+                llop.debug_print_traceback(lltype.Void)
+            else:
+                print("".join(traceback.format_exception(*sys.exc_info())))
     return 0
 
 # _____ Define and setup target ___