pythloite/ExprCompiler: attempt compile-time evaluation first
authorSebastien Bourdeauducq <sebastien@milkymist.org>
Sat, 27 Jul 2013 13:38:29 +0000 (15:38 +0200)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Sat, 27 Jul 2013 13:38:29 +0000 (15:38 +0200)
migen/pytholite/expr.py

index 186c8854f4f13461a558c42c6e701d0a76c82b7a..b59013c19a2bf336453facbc81d72e8e76a01e17 100644 (file)
@@ -11,6 +11,14 @@ class ExprCompiler:
                self.symdict = symdict
        
        def visit_expr(self, node):
+               # Attempt compile-time evaluation first
+               try:
+                       result = eval_ast(node, self.symdict)
+               except:
+                       result = None
+               if isinstance(result, int):
+                       return result
+
                if isinstance(node, ast.Call):
                        return self.visit_expr_call(node)
                elif isinstance(node, ast.BinOp):