Пример #1
0
def simplify(expr):
    n = _try_eval(expr)
    if n is not None:
        return type_check.Constant(n)

    if isinstance(expr, type_check.BinaryOperator):
        rhs_value = _try_eval(expr.rhs)
        if rhs_value is not None:
            if (expr.exp == '+' or expr.exp == '-') and rhs_value == 0:
                return simplify(expr.lhs)

            if expr.exp == '+' and rhs_value < 0:
                expr_rhs = type_check.Constant(-rhs_value)
                return simplify(_make_binop_expr(expr.lhs, expr_rhs, '-'))

            if expr.exp == '-' and rhs_value < 0:
                expr_rhs = type_check.Constant(-rhs_value)
                return simplify(_make_binop_expr(expr.lhs, expr_rhs, '+'))

            if (expr.exp == '*' or expr.exp == '/'
                    or expr.exp == '//') and rhs_value == 1:
                return simplify(expr.lhs)

        lhs_value = _try_eval(expr.lhs)
        if lhs_value is not None:
            if expr.exp == '+' and lhs_value == 0:
                return simplify(expr.rhs)

        if isinstance(expr.lhs, type_check.BinaryOperator) and \
                expr.lhs.priority == expr.priority:
            if expr.lhs.exp == '+' or expr.lhs.exp == '*':
                expr_exp = expr.exp
            elif expr.lhs.exp == '-':
                if expr.exp == '+':
                    expr_exp = '-'
                else:
                    expr_exp = '+'
            else:
                assert False

            _, expr_func = binops[expr_exp]
            expr_rhs = type_check.BinaryOperator(expr.priority, expr.lhs.rhs,
                                                 expr.rhs, expr_exp, expr_func)
            expr_rhs = simplify(expr_rhs)
            if isinstance(expr_rhs, type_check.Constant):
                return simplify(
                    type_check.BinaryOperator(expr.lhs.priority, expr.lhs.lhs,
                                              expr_rhs, expr.lhs.exp,
                                              expr.lhs.func))

        expr.lhs = simplify(expr.lhs)
        expr.rhs = simplify(expr.rhs)

    # if isinstance(expr, type_check.UnaryOperator):
    #     expr.term = simplify(expr.term)
    return expr
Пример #2
0
 def __init__(self, value_or_name, expr=None):
     assert type(value_or_name) in [int, float, str, type(None)]
     if isinstance(value_or_name, str):
         # name
         self.value = None
         self.expr = type_check.Variable(None, value_or_name)
     else:
         # value
         self.value = value_or_name
         self.expr = simplify(
             expr) if expr is not None else type_check.Constant(
                 value_or_name)
Пример #3
0
 def __init__(self, value_or_name, expr=None):
     assert type(value_or_name) in [int, float, str, type(None)]
     if isinstance(value_or_name, str):
         # name (given via type hints)
         self.value = value_or_name
         self.expr = None
     else:
         # value
         self.value = value_or_name
         if expr is None:
             self.expr = type_check.Constant(value_or_name)
         else:
             self.expr = simplify(expr)
Пример #4
0
def _make_binop(lhs, rhs, symbol):
    priority, func = binops[symbol]

    if not isinstance(rhs, ShapeElem):
        if lhs.value is None:
            return ShapeElem(None)
        expr = type_check.BinaryOperator(priority, lhs.expr,
                                         type_check.Constant(rhs), symbol,
                                         func)
        return ShapeElem(func(lhs.value, rhs), expr=expr)

    if not isinstance(lhs, ShapeElem):
        if rhs.value is None:
            return ShapeElem(None)
        expr = type_check.BinaryOperator(priority, type_check.Constant(lhs),
                                         rhs.expr, symbol, func)
        return ShapeElem(func(lhs, rhs.value), expr=expr)

    if lhs.value is None or rhs.value is None:
        return ShapeElem(None)
    expr = type_check.BinaryOperator(priority, lhs.expr, rhs.expr, symbol,
                                     func)
    return ShapeElem(func(lhs.value, rhs.value), expr=expr)
Пример #5
0
 def setUp(self):
     x = Object()
     self.value = T.GetAttr(T.Variable(x, 'x'), 'value')
     self.value2 = T.GetAttr(T.Variable(x, 'x'), T.Constant('value'))
     self.value3 = T.GetAttr(T.Variable(x, 'x'), 3)
Пример #6
0
 def test_nest_list_str(self):
     self.assertEqual('[[0]]', T._repr([[T.Constant(0)]]))
Пример #7
0
 def test_eval_nest_list(self):
     self.assertTrue((T.Constant([[0]]) == [[T.Constant(0)]]).eval())
Пример #8
0
 def test_tuple_str(self):
     self.assertEqual('()', T._repr(()))
     self.assertEqual('(0,)', T._repr((T.Constant(0),)))
     self.assertEqual('(0, 0)', T._repr((T.Constant(0), T.Constant(0))))
Пример #9
0
 def test_eval_tuple_items(self):
     self.assertTrue((T.Constant((0,)) == (T.Constant(0),)).eval())
Пример #10
0
 def test_eval_list_items(self):
     self.assertTrue((T.Constant([0]) == [T.Constant(0)]).eval())
Пример #11
0
 def setUp(self):
     self.t = T.Constant(0)
Пример #12
0
 def setUp(self):
     self.x = T.Constant(10)