Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: m-labs/artiq
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 70cc0d176630
Choose a base ref
...
head repository: m-labs/artiq
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: be94a8b07c59
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Oct 29, 2014

  1. Copy the full SHA
    c82c631 View commit details
  2. Copy the full SHA
    be94a8b View commit details
Showing with 41 additions and 1 deletion.
  1. +38 −0 artiq/transforms/fold_constants.py
  2. +3 −1 artiq/transforms/tools.py
38 changes: 38 additions & 0 deletions artiq/transforms/fold_constants.py
Original file line number Diff line number Diff line change
@@ -28,6 +28,14 @@
ast.BitAnd: operator.and_
}

_ast_cmpops = {
ast.Eq: operator.eq,
ast.NotEq: operator.ne,
ast.Lt: operator.lt,
ast.LtE: operator.le,
ast.Gt: operator.gt,
ast.GtE: operator.ge
}

_ast_boolops = {
ast.Or: lambda x, y: x or y,
@@ -68,6 +76,36 @@ def visit_BinOp(self, node):
return node
return ast.copy_location(result, node)

def visit_Compare(self, node):
self.generic_visit(node)
try:
operands = [eval_constant(node.left)]
except NotConstant:
operands = [node.left]
ops = []
for op, right_ast in zip(node.ops, node.comparators):
try:
right = eval_constant(right_ast)
except NotConstant:
right = right_ast
if (not isinstance(operands[-1], ast.AST)
and not isinstance(right, ast.AST)):
left = operands.pop()
operands.append(_ast_cmpops[type(op)](left, right))
else:
ops.append(op)
operands.append(right_ast)
operands = [operand if isinstance(operand, ast.AST)
else ast.copy_location(value_to_ast(operand), node)
for operand in operands]
if len(operands) == 1:
return operands[0]
else:
node.left = operands[0]
node.right = operands[1:]
node.ops = ops
return node

def visit_BoolOp(self, node):
self.generic_visit(node)
new_values = []
4 changes: 3 additions & 1 deletion artiq/transforms/tools.py
Original file line number Diff line number Diff line change
@@ -57,7 +57,9 @@ def eval_constant(node):
return node.value
elif isinstance(node, ast.Call):
funcname = node.func.id
if funcname == "Fraction":
if funcname == "int64":
return core_language.int64(eval_constant(node.args[0]))
elif funcname == "Fraction":
numerator = eval_constant(node.args[0])
denominator = eval_constant(node.args[1])
return Fraction(numerator, denominator)