From: Sebastien Bourdeauducq Date: Fri, 9 Nov 2012 17:41:32 +0000 (+0100) Subject: pytholite/compiler: support comparisons in expressions X-Git-Tag: 24jan2021_ls180~2099^2~810 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=92ff5095daee369aa8815c011839aecf62cc6009;p=litex.git pytholite/compiler: support comparisons in expressions --- diff --git a/migen/pytholite/compiler.py b/migen/pytholite/compiler.py index b41d0436..acac9738 100644 --- a/migen/pytholite/compiler.py +++ b/migen/pytholite/compiler.py @@ -118,6 +118,8 @@ class _Compiler: raise NotImplementedError 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): @@ -160,6 +162,32 @@ class _Compiler: else: raise NotImplementedError + def visit_expr_compare(self, node): + test = visit_expr(node.test) + r = None + for op, rcomparator in zip(node.ops, node.comparators): + comparator = visit_expr(rcomparator) + if isinstance(op, ast.Eq): + comparison = test == comparator + elif isinstance(op, ast.NotEq): + comparison = test != comparator + elif isinstance(op, ast.Lt): + comparison = test < comparator + elif isinstance(op, ast.LtE): + comparison = test <= comparator + elif isinstance(op, ast.Gt): + comparison = test > comparator + elif isinstance(op, ast.GtE): + comparison = test >= comparator + else: + raise NotImplementedError + if r is None: + r = comparison + else: + r = r & comparison + test = comparator + return r + def visit_expr_name(self, node): r = self.symdict[node.id] if isinstance(r, _Register): @@ -186,14 +214,14 @@ def make_pytholite(func): symdict = func.__globals__.copy() registers = [] + print("ast:") + print(ast.dump(tree)) + states = _Compiler(symdict, registers).visit_top(tree) print("compilation result:") print(states) - print("ast:") - print(ast.dump(tree)) - regf = Fragment() for register in registers: register.finalize()