Пример #1
0
def parse_f(l: List[Union[int, str]]) -> Node:
    str_to_unop = {
        "+": operations.identity,
        "-": operations.negate,
    }
    if l[0] in str_to_unop:
        op = l.pop(0)
        return UnaryOp(str_to_unop[op], parse_f(l))
    return parse_p(l)
Пример #2
0
def stack_to_tree(s: List[Union[int, str]]) -> Node:
    top = s.pop()
    if type(top) is int:
        return Constant(top)
    top = cast(str, top)
    if top == "@":
        rhs = stack_to_tree(s)
        return UnaryOp(operations.negate, rhs)
    rhs = stack_to_tree(s)
    lhs = stack_to_tree(s)
    return BinOp(operations.STR_TO_BIN[top], lhs, rhs)
Пример #3
0
def queue_to_tree(q: List[Union[int, str]]) -> Node:
    top = q.pop(0)
    if type(top) is int:
        return Constant(top)
    top = cast(str, top)
    if top == "@":
        rhs = queue_to_tree(q)
        return UnaryOp(operations.negate, rhs)
    lhs = queue_to_tree(q)
    rhs = queue_to_tree(q)
    return BinOp(operations.STR_TO_BIN[top], lhs, rhs)
Пример #4
0
 def visit_unaryop(self, b: UnaryOp) -> None:
     rhs_val = self.visit_and_get_value(b.rhs)
     self.value = b.op(rhs_val)
Пример #5
0
def test_evaluate_unaryop_negate(vis):
    op = UnaryOp(operations.negate, Constant(-42))
    assert vis.visit_and_get_value(op) == 42
Пример #6
0
def test_evaluate_unaryop_identity(vis):
    op = UnaryOp(operations.identity, Constant(42))
    assert vis.visit_and_get_value(op) == 42
Пример #7
0
def test_parse_doubly_negated_constant():
    assert parse_postfix("42@@") == UnaryOp(
        operations.negate, UnaryOp(operations.negate, Constant(42)))
Пример #8
0
def test_parse_doubly_negated_constant():
    assert parse_prefix("@@42") == UnaryOp(
        operations.negate, UnaryOp(operations.negate, Constant(42)))