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): return self.visit_expr_binop(node) elif isinstance(node, ast.Compare): return self.visit_expr_compare(node) elif isinstance(node, ast.Name): return self.visit_expr_name(node) elif isinstance(node, ast.Num): return self.visit_expr_num(node) elif isinstance(node, ast.Attribute): return self.visit_expr_attribute(node) elif isinstance(node, ast.Subscript): return self.visit_expr_subscript(node) else: raise NotImplementedError
def visit_expr_call(self, node): if isinstance(node.func, ast.Name): callee = self.symdict[node.func.id] else: raise NotImplementedError if callee == transel.bitslice: if len(node.args) != 2 and len(node.args) != 3: raise TypeError("bitslice() takes 2 or 3 arguments") val = self.visit_expr(node.args[0]) low = eval_ast(node.args[1], self.symdict) if len(node.args) == 3: up = eval_ast(node.args[2], self.symdict) else: up = low + 1 return _Slice(val, low, up) else: raise NotImplementedError