Exemplo n.º 1
0
def veval_ast_bin_op(astc: 'AstContext', local_field: 'values.Field',
                     graph: 'Graph'):
    """
    eval binary operation.
    Ex. a + b, b // c, etc
    """
    assert (isinstance(astc.nast, gast.gast.BinOp))
    lineprop = utils.LineProperty(astc.lineno)

    left = veval_ast(astc.c(astc.nast.left), local_field, graph)
    right = veval_ast(astc.c(astc.nast.right), local_field, graph)

    left_value = try_get_value(left, 'compare', lineprop)
    right_value = try_get_value(right, 'compare', lineprop)

    binop = nodes.BinOpType.Unknown
    if isinstance(astc.nast.op, gast.Add):
        binop = nodes.BinOpType.Add
    if isinstance(astc.nast.op, gast.Sub):
        binop = nodes.BinOpType.Sub
    if isinstance(astc.nast.op, gast.Mult):
        binop = nodes.BinOpType.Mul

    node_bin_op = nodes.NodeBinOp(left_value, right_value, binop, astc.lineno)

    ret_value = veval_bin.veval(binop, left_value, right_value)

    node_bin_op.set_outputs([ret_value])
    graph.add_node(node_bin_op)

    return values.ValueRef(ret_value)
Exemplo n.º 2
0
def veval_ast_bin_op(astc: 'AstContext', local_field: 'values.Field',
                     graph: 'Graph'):
    """
    eval binary operation.
    Ex. a + b, b // c, etc
    """
    assert (isinstance(astc.nast, gast.gast.BinOp))

    left = veval_ast(astc.c(astc.nast.left), local_field, graph)
    right = veval_ast(astc.c(astc.nast.right), local_field, graph)

    binop = nodes.BinOpType.Unknown
    if isinstance(astc.nast.op, gast.Add):
        binop = nodes.BinOpType.Add
    if isinstance(astc.nast.op, gast.Sub):
        binop = nodes.BinOpType.Sub

    node_bin_op = nodes.NodeBinOp(left.get_value(), right.get_value(), binop,
                                  astc.lineno)

    ret_value = veval_bin.veval(binop, left.get_value(), right.get_value())

    node_bin_op.set_outputs([ret_value])
    graph.add_node(node_bin_op)

    return ret_value