예제 #1
0
    def __init__(self, line):
        self.inputs = []
        self.outputs = []
        self.subgraphs = []
        self.lineprop = utils.LineProperty()

        if isinstance(line, int):
            self.lineprop.lineno = line
        else:
            self.lineprop = line

        return
예제 #2
0
def veval_ast_assign(astc: 'AstContext', local_field: 'values.Field',
                     graph: 'Graph'):
    assert (isinstance(astc.nast, gast.gast.Assign))
    lineprop = utils.LineProperty(astc.lineno)

    value = veval_ast(astc.c(astc.nast.value), local_field, graph)
    value_obj = try_get_obj(value, 'assign', lineprop)

    if value is None:
        if config.show_warnings:
            print(
                'It is possible that assiging value is invalid in L.{}'.format(
                    astc.lineno))
        return None

    target_option = VEvalOption()
    target_option.eval_as_written_target = True
    targets = veval_ast(astc.c(astc.nast.targets[0]), local_field, graph,
                        target_option)

    def return_value_or_ref(obj: 'value.Object'):
        if isinstance(obj.get_value(), values.NumberValue):
            return values.Object(obj.get_value())

        if isinstance(obj.get_value(), values.StrValue):
            return values.Object(obj.get_value())

        if isinstance(obj.get_value(), values.BoolValue):
            return values.Object(obj.get_value())

        if isinstance(obj.get_value(), values.NoneValue):
            return values.Object(obj.get_value())

        return obj

    isTuple = False
    if targets.has_obj() and isinstance(targets.get_obj().get_value(),
                                        values.TupleValue):
        targets = targets.get_obj().values
        isTuple = True

    if isTuple:
        for i in range(len(targets)):
            node_assign = nodes.NodeAssign(targets[i], value.values[i],
                                           astc.lineno)
            targets[i].revise(value.values[i])
            graph.add_node(node_assign)
    else:
        assigned_obj = return_value_or_ref(value_obj)
        node_assign = nodes.NodeAssign(targets, assigned_obj, astc.lineno)
        targets.revise(assigned_obj)
        graph.add_node(node_assign)
예제 #3
0
def veval_ast_name_constant(astc : 'AstContext', local_field : 'values.Field', graph : 'Graph'):
    '''
    Ex. True
    '''
    assert(isinstance(astc.nast, gast.gast.NameConstant))
    lineprop = utils.LineProperty(astc.lineno)
    if astc.nast.value == True:
        return values.BoolValue(True)
    if astc.nast.value == False:
        return values.BoolValue(False)
    if astc.nast.value is None:
        return values.BoolValue(False)
    return None
예제 #4
0
def veval_ast_str(astc: 'AstContext', local_field: 'values.Field',
                  graph: 'Graph'):
    '''
    Ex. "str"
    '''
    assert (isinstance(astc.nast, gast.gast.Str))
    lineprop = utils.LineProperty(astc.lineno)
    value = values.StrValue(astc.nast.s)
    ret = values.ValueRef(value)

    name = values.create_ref_value_name_with_constant(ret)
    ret.name = name
    ret.get_value().name = name
    return ret
예제 #5
0
def veval_ast_num(astc: 'AstContext', local_field: 'values.Field',
                  graph: 'Graph'):
    '''
    Ex. 1, 2, ...
    '''
    assert (isinstance(astc.nast, gast.gast.Num))
    lineprop = utils.LineProperty(astc.lineno)
    value = values.NumberValue(astc.nast.n)
    ret = values.Object(value)

    name = utils.create_obj_value_name_with_constant(
        ret.get_value().internal_value)
    ret.name = name
    ret.get_value().name = name
    return ret
예제 #6
0
def veval_ast_call(astc: 'AstContext', local_field: 'values.Field',
                   graph: 'Graph') -> 'Attribute':
    assert (isinstance(astc.nast, gast.gast.Call))

    func = veval_ast(astc.c(astc.nast.func), local_field, graph)
    if func == None or not func.has_value():
        if config.show_warnings:
            print('Unknown function {} is called in L.{}'.format(
                get_ast_name_forcibly(astc.nast.func), astc.lineno))
        return None

    func_value = func.get_value()

    args = []
    fargs = []
    for arg in astc.nast.args:
        arg_ = veval_ast(astc.c(arg), local_field, graph)
        farg = functions.FunctionArg()
        farg.value = arg_.get_value()
        args.append(farg.value)
        fargs.append(farg)

    for keyword in astc.nast.keywords:
        arg_ = veval_ast(astc.c(keyword.value), local_field, graph)
        farg = functions.FunctionArg()
        farg.name = keyword.arg
        farg.value = arg_.get_value()
        args.append(farg.value)
        fargs.append(farg)

    lineprop = utils.LineProperty(astc.lineno)

    ret = None
    if isinstance(func_value, values.FuncValue):
        ret = func_value.func.vcall(local_field.module, graph,
                                    func_value.value, fargs, lineprop)

    elif isinstance(func_value, values.Instance) and func_value.callable:
        # __call__
        ret = func_value.func.func.vcall(local_field.module, graph, func_value,
                                         fargs, lineprop)

    else:
        if config.show_warnings:
            print('Unknown function is called in L.{}'.format(astc.lineno))
        return None

    return ret
예제 #7
0
def veval_ast_for_unroll(astc: 'AstContext', target_name,
                         iter_: 'values.ListValue',
                         local_field: 'values.Field', graph: 'Graph'):
    '''
    for target in iter: 
        ...
    with unroll
    '''
    assert (isinstance(astc.nast, gast.gast.For))
    lineprop = utils.LineProperty(astc.lineno)

    for element in iter_.get_constant_value():
        local_field.get_attribute(target_name).revise(element)
        veval_ast(astc.c(astc.nast.body), local_field, graph)

    return None
예제 #8
0
def veval_ast_return(astc: 'AstContext', local_field: 'values.Field',
                     graph: 'Graph') -> 'None':
    assert (isinstance(astc.nast, gast.gast.Return))
    lineprop = utils.LineProperty(astc.lineno)

    value = veval_ast(astc.c(astc.nast.value), local_field, graph)
    value_obj = try_get_ref(value, 'return', lineprop)
    value_value = try_get_value(value, 'return', lineprop)

    if value_value is None:
        if config.show_warnings:
            print('Returned values are not found. in L.{}'.format(astc.lineno))
        return None

    node = nodes.NodeReturn(value_value, astc.lineno)
    graph.add_node(node)
    return value_obj
예제 #9
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)

    # TODO : adhoc code
    if isinstance(left_value, values.TupleValue):
        value = values.TupleValue()
        value.values = [
            try_get_value(v, 'compare', lineprop) for v in left_value.values
        ]
        left_value = value

    if isinstance(right_value, values.TupleValue):
        value = values.TupleValue()
        value.values = [
            try_get_value(v, 'compare', lineprop) for v in right_value.values
        ]
        right_value = value

    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.Object(ret_value)
예제 #10
0
def veval_ast_attribute(astc: 'AstContext', local_field: 'values.Field',
                        graph: 'Graph') -> 'Attribute':
    assert (isinstance(astc.nast, gast.gast.Attribute))
    lineprop = utils.LineProperty(astc.lineno)

    value = veval_ast(astc.c(astc.nast.value), local_field, graph)
    value_obj = try_get_obj(value, 'attribute', lineprop)
    attr = value_obj.get_field().get_attribute(astc.nast.attr)

    if attr.has_obj():
        return attr

    # if attr is not found
    gotten_obj = value_obj.try_get_and_store_obj(astc.nast.attr)
    if gotten_obj is not None:
        return attr

    return attr
예제 #11
0
def veval_ast_name_constant(astc: 'AstContext', local_field: 'values.Field',
                            graph: 'Graph'):
    '''
    Ex. True
    '''
    assert (isinstance(astc.nast, gast.gast.NameConstant))
    lineprop = utils.LineProperty(astc.lineno)
    ret = None
    if astc.nast.value == True:
        ret = values.ValueRef(values.BoolValue(True))
    if astc.nast.value == False:
        ret = values.ValueRef(values.BoolValue(False))
    if astc.nast.value is None:
        ret = values.ValueRef(values.NoneValue())

    name = values.create_ref_value_name_with_constant(ret)
    ret.name = name
    ret.get_value().name = name
    return ret
예제 #12
0
def veval_ast_compare(astc: 'AstContext', local_field: 'values.Field',
                      graph: 'Graph'):
    """
    eval Compare.
    Ex. a >= b, a != b, a is b, etc
    """
    assert (isinstance(astc.nast, gast.gast.Compare))
    lineprop = utils.LineProperty(astc.lineno)

    left = veval_ast(astc.c(astc.nast.left), local_field, graph)
    right = veval_ast(astc.c(astc.nast.comparators[0]), local_field, graph)

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

    compare = nodes.CompareType.unknown
    if isinstance(astc.nast.ops[0], gast.Eq):
        compare = nodes.CompareType.Eq
    if isinstance(astc.nast.ops[0], gast.NotEq):
        compare = nodes.CompareType.NotEq
    if isinstance(astc.nast.ops[0], gast.Is):
        compare = nodes.CompareType.Is
    if isinstance(astc.nast.ops[0], gast.IsNot):
        compare = nodes.CompareType.IsNot
    if isinstance(astc.nast.ops[0], gast.Gt):
        compare = nodes.CompareType.Gt
    if isinstance(astc.nast.ops[0], gast.GtE):
        compare = nodes.CompareType.GtE
    if isinstance(astc.nast.ops[0], gast.Lt):
        compare = nodes.CompareType.Lt
    if isinstance(astc.nast.ops[0], gast.LtE):
        compare = nodes.CompareType.LtE

    node_compare = nodes.NodeCompare(left_value, right_value, compare,
                                     astc.lineno)

    ret_value = values.BoolValue(None)
    ret_value.name = '@{}'.format(lineprop)
    node_compare.set_outputs([ret_value])
    graph.add_node(node_compare)

    return values.ValueRef(ret_value)
예제 #13
0
def veval_ast_list(astc : 'AstContext', local_field : 'values.Field', graph : 'Graph'):
    assert(isinstance(astc.nast, gast.gast.List))
    '''
    Ex. [],[x,y,z]
    TODO : Initializer
    '''
    lineprop = utils.LineProperty(astc.lineno)

    elts = []
    for elt in astc.nast.elts:
        elt_ = veval_ast(astc.c(elt), local_field, graph)
        elt_value = elt_.get_value()
        elts.append(elt_value)

    node = nodes.NodeGenerate('List', elts, lineprop)
    graph.add_node(node)
    value = values.ListValue()
    value.values.extend(elts)

    node.set_outputs([value])
    return value
예제 #14
0
def veval_ast_attribute(astc: 'AstContext',
                        local_field: 'values.Field',
                        graph: 'Graph',
                        option: 'VEvalOption' = None) -> 'Attribute':
    assert (isinstance(astc.nast, gast.gast.Attribute))
    lineprop = utils.LineProperty(astc.lineno)

    from_module = True
    if option is not None and option.eval_as_written_target:
        from_module = False

    value = veval_ast(astc.c(astc.nast.value), local_field, graph)
    value_ref = try_get_ref(value, 'attribute', lineprop)
    attr = value_ref.get_field().get_attribute(astc.nast.attr, from_module)

    # property(getter)
    if attr.has_obj() and isinstance(
            attr.get_ref(False).get_value(), values.FuncValue
    ) and attr.get_ref(False).get_value().func.is_property:
        func_value = attr.get_ref(False).get_value()
        ret = func_value.func.vcall(local_field.module, graph, func_value.obj,
                                    functions.FunctionArgInput(), lineprop)
        return ret

    if attr.has_obj():
        return attr

    # if attr is not found
    gotten_obj = value_ref.try_get_and_store_obj(astc.nast.attr,
                                                 graph.root_graph)
    if gotten_obj is not None:
        return value_ref.get_field().get_attribute(astc.nast.attr, from_module)

    if option is not None and option.eval_as_written_target:
        return attr

    # value is unknown
    if config.show_warnings:
        print('Assigning value is not found in L.{}'.format(astc.lineno))
    return None
예제 #15
0
def veval_ast_attribute(astc: 'AstContext',
                        local_field: 'values.Field',
                        graph: 'Graph',
                        option: 'VEvalOption' = None) -> 'Attribute':
    assert (isinstance(astc.nast, gast.gast.Attribute))
    lineprop = utils.LineProperty(astc.lineno)

    from_module = True
    if option is not None and option.eval_as_written_target:
        from_module = False

    value = veval_ast(astc.c(astc.nast.value), local_field, graph)
    value_obj = try_get_obj(value, 'attribute', lineprop)
    attr = value_obj.get_field().get_attribute(astc.nast.attr, from_module)

    if attr.has_obj():
        return attr

    # if attr is not found
    gotten_obj = value_obj.try_get_and_store_obj(astc.nast.attr)
    if gotten_obj is not None:
        return attr

    return attr
예제 #16
0
def veval_ast_aug_assign(astc: 'AstContext', local_field: 'values.Field',
                         graph: 'Graph'):
    assert (isinstance(astc.nast, gast.gast.AugAssign))
    lineprop = utils.LineProperty(astc.lineno)

    target = veval_ast(astc.c(astc.nast.target), local_field, graph)
    value = veval_ast(astc.c(astc.nast.value), local_field, graph)

    target_value = try_get_value(target, 'aug_assign', lineprop)
    value_value = try_get_value(value, 'aug_assign', 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

    node_aug_assign = nodes.NodeAugAssign(target_value, value_value, binop,
                                          astc.lineno)
    graph.add_node(node_aug_assign)

    new_value = functions.generate_value_with_same_type(target_value)
    node_aug_assign.set_outputs([new_value])
    target.get_obj().revise(new_value)
예제 #17
0
def veval_ast_for(astc: 'AstContext', local_field: 'values.Field',
                  graph: 'Graph'):
    '''
    for target in iter:
        ...
    '''
    assert (isinstance(astc.nast, gast.gast.For))
    lineprop = utils.LineProperty(astc.lineno)

    # for target in iter:
    iter_ = veval_ast(astc.c(astc.nast.iter), local_field, graph)
    input_iter_value = try_get_value(iter_, 'for', lineprop)
    body_iter_value = functions.generate_value_with_same_type(
        input_iter_value, suffix_type=functions.SuffixType.Input)

    # get target name
    target_name = ''
    if isinstance(astc.nast.target, gast.gast.Name):
        target_name = astc.nast.target.id
    else:
        if config.show_warnings:
            print('This for is not supported. in L.{}'.format(astc.lineno))
        return None

    # unroll?
    if isinstance(input_iter_value,
                  values.ListValue) and input_iter_value.has_constant_value(
                  ) and input_iter_value.dtype is None:
        return veval_ast_for_unroll(astc, target_name, input_iter_value,
                                    local_field, graph)

    for_guid = utils.get_guid()
    for_id = 'for_' + str(for_guid)
    body_id = 'body_' + str(for_guid)

    values.push_history(for_id)

    # body
    body_graph = Graph()
    body_graph.root_graph = graph.root_graph
    body_graph.name = 'Body_' + str(for_guid)

    # generate a node for input
    node_input = nodes.NodeInput('input')
    body_graph.add_node(node_input)

    body_counter_value = values.NumberValue(None)
    body_counter_value.dtype = np.array(0).dtype
    body_counter_value.name = 'for_counter_' + str(for_guid)

    body_cond_value = values.BoolValue(None)
    body_cond_value.name = 'for_cond_' + str(for_guid)

    # create a node to lookup a value from sequence
    node_forgen = nodes.NodeForGenerator(body_counter_value, body_iter_value)

    # generate iterator
    target_ref = input_iter_value.get_iterator()
    if target_ref is None:
        target_ref = values.ValueRef(values.UnknownValue())
        if config.show_warnings:
            print('unknown iteratable type in L.{}'.format(astc.lineno))
    target_value = target_ref.get_value()

    node_forgen.set_outputs([target_ref.get_value()])

    target_attribute = local_field.get_attribute(target_name)
    target_attribute.revise(target_ref)
    body_graph.add_node(node_forgen)

    # veval body
    body = veval_ast(astc.c(astc.nast.body), local_field, body_graph)

    value_inputs = values.get_inputs()
    value_outputs = values.get_outputs()

    values.pop_history()

    inputs = []
    outputs = []
    node_input_outputs = []

    # default input for subgraph's input
    body_graph.add_input_value(body_counter_value)
    body_graph.add_input_value(body_cond_value)
    body_graph.add_input_value(body_iter_value)

    # default output for subgraph's output
    body_graph.add_output_value(body_cond_value)
    body_graph.add_output_value(body_iter_value)

    # default output
    outputs.append(functions.generate_value_with_same_type(input_iter_value))

    # generate pairs
    value_pairs = {}
    for v in value_inputs:
        key = str(v.field.id) + '_' + v.name
        if not (key in value_pairs.keys()):
            value_pairs[key] = {}

        value_pairs[key]['field'] = v.field
        value_pairs[key]['name'] = v.name
        value_pairs[key]['input_value'] = v.input_value
        value_pairs[key]['input_body_value'] = v.value

    for v in value_outputs:
        key = str(v.field.id) + '_' + v.name
        if not (key in value_pairs.keys()):
            value_pairs[key] = {}

        value_pairs[key]['field'] = v.field
        value_pairs[key]['name'] = v.name
        value_pairs[key]['output_body_value'] = v.value

    for k, v in value_pairs.items():
        name = v['name']
        field = v['field']

        if 'input_body_value' in v:
            inputs.append(v['input_value'])

            body_graph.add_input_value(v['input_body_value'])
        else:
            temp_value1 = functions.generate_value_with_same_type(
                v['output_body_value'],
                is_dummy_value=True,
                suffix_type=functions.SuffixType.Dummy)
            temp_value2 = functions.generate_value_with_same_type(
                v['output_body_value'], suffix_type=functions.SuffixType.Dummy)
            inputs.append(temp_value1)

            body_graph.add_input_value(temp_value2)
            node_input_outputs.append(temp_value2)

        if 'output_body_value' in v:
            body_graph.add_output_value(v['output_body_value'])
            output_value = functions.generate_value_with_same_type(
                v['output_body_value'])
            outputs.append(output_value)
            if field.get_attribute(name).has_obj():
                field.get_attribute(name).get_ref().revise(output_value)
            else:
                field.get_attribute(name).revise(values.ValueRef(output_value))
        else:
            temp_value1 = v['input_body_value']
            temp_value2 = functions.generate_value_with_same_type(
                v['input_body_value'])
            body_graph.add_output_value(temp_value1)
            outputs.append(temp_value2)

    node = nodes.NodeFor(input_iter_value, inputs, body_graph, astc.lineno)
    node.set_outputs(outputs)
    node_input.set_outputs(node_input_outputs)

    graph.add_node(node)

    return None
예제 #18
0
def veval_ast_listcomp(astc: 'AstContext', local_field: 'values.Field',
                       graph: 'Graph'):
    '''
    Ex. [x for x in xx]
    [elt for target in iter]
    '''
    assert (isinstance(astc.nast, gast.gast.ListComp))
    lineprop = utils.LineProperty(astc.lineno)

    listcomp_id = 'listcomp_' + str(utils.get_guid())
    body_id = 'listcomp_body_' + str(utils.get_guid())

    values.commit(listcomp_id)

    generator = astc.nast.generators[0]
    iter_value = try_get_value(
        veval_ast(astc.c(generator.iter), local_field, graph), 'generator',
        lineprop)
    list_value = values.ListValue()

    node_generate_list = nodes.NodeGenerate('List', [], lineprop)
    graph.add_node(node_generate_list)
    node_generate_list.set_outputs([list_value])

    # body
    target_name = ''
    if isinstance(generator.target, gast.gast.Name):
        target_name = generator.target.id
    else:
        if config.show_warnings:
            print('This for is not supported. in L.{}'.format(astc.lineno))
        return None

    counter_value = values.NumberValue(0)
    counter_value.name = 'for_counter'

    node_forgen = nodes.NodeForGenerator(counter_value, iter_value)
    target_value = values.Value()
    node_forgen.set_outputs([target_value])

    body_field = values.Field()
    body_field.set_module(local_field.module)
    body_field.set_parent(local_field)
    body_field.get_attribute(target_name).revise(target_value)
    body_graph = Graph()
    body_graph.name = 'Body'

    body_graph.add_node(node_forgen)

    elt = veval_ast(astc.c(astc.nast.elt), body_field, body_graph)
    farg = functions.FunctionArg()
    farg.name = ''
    farg.value = elt
    list_value.append_func.func.vcall(local_field.module, body_graph,
                                      list_value, [farg], lineprop)

    values.commit(body_id)

    body_output_attributes = get_input_attritubtes(body_field, listcomp_id,
                                                   body_id)
    body_intput_attributes = get_output_attritubtes(body_field, listcomp_id,
                                                    body_id)

    # Exports
    values.checkout(listcomp_id)

    input_attributes = set(body_intput_attributes)
    inputs = [i.get_value() for i in input_attributes]

    output_attributes = set(body_output_attributes)
    outputs = []

    for attribute in output_attributes:
        value = values.Value()
        outputs.append(value)
        attribute.revise(value)

    node = nodes.NodeListcomp(iter_value, inputs, body_graph, astc.lineno)
    node.set_outputs(outputs)

    graph.add_node(node)

    # compare

    return list_value
예제 #19
0
def veval_ast_listcomp(astc: 'AstContext', local_field: 'values.Field',
                       graph: 'Graph'):
    '''
    Ex. [x for x in xx]
    [elt for target in iter]
    '''
    assert (isinstance(astc.nast, gast.gast.ListComp))
    lineprop = utils.LineProperty(astc.lineno)

    listcomp_guid = str(utils.get_guid())
    listcomp_id = 'listcomp_' + listcomp_guid
    body_id = 'listcomp_body_' + listcomp_guid
    internal_counter_id = '@internal/listcomp_counter_' + listcomp_guid
    internal_list_id = '@internal/listcomp_list_' + listcomp_guid
    internal_cond_id = '@internal/listcomp_cond_' + listcomp_guid

    generator = astc.nast.generators[0]
    iter_value = try_get_value(
        veval_ast(astc.c(generator.iter), local_field, graph), 'generator',
        lineprop)
    list_value = values.ListValue()
    list_obj = values.ValueRef(list_value)

    node_generate_list = nodes.NodeGenerate('List', [], lineprop)
    node_generate_list.set_outputs([list_value])
    graph.add_node(node_generate_list)

    # body
    target_name = ''
    if isinstance(generator.target, gast.gast.Name):
        target_name = generator.target.id
    else:
        if config.show_warnings:
            print('This for is not supported. in L.{}'.format(astc.lineno))
        return None

    counter_value = values.NumberValue(None)
    counter_value.dtype = np.array(0).dtype
    counter_value.name = internal_counter_id

    cond_value = values.BoolValue(None)
    cond_value.name = internal_cond_id

    # set values with internal name
    local_field.get_attribute(internal_list_id).revise(list_obj)

    values.push_history(listcomp_id)

    body_graph = Graph()
    body_graph.root_graph = graph.root_graph
    body_graph.name = 'Body_' + listcomp_guid

    node_forgen = nodes.NodeForGenerator(counter_value, iter_value)

    target_ref = iter_value.get_iterator()
    if target_ref is None:
        target_ref = values.ValueRef(values.UnknownValue())
        if config.show_warnings:
            print('unknown iteratable type in L.{}'.format(astc.lineno))
    target_value = target_ref.get_value()

    node_forgen.set_outputs([target_ref.get_value()])
    local_field.get_attribute(target_name,
                              from_module=False).revise(target_ref)

    body_graph.add_node(node_forgen)

    elt = veval_ast(astc.c(astc.nast.elt), local_field, body_graph)
    elt_obj = try_get_ref(elt, 'listcomp', lineprop)

    finput = functions.FunctionArgInput()
    finput.inputs.append(elt_obj)

    append_value = local_field.get_attribute(internal_list_id).get_ref(
    ).get_field().get_attribute('append').get_ref().get_value()
    append_value.func.vcall(
        local_field.module, body_graph,
        local_field.get_attribute(internal_list_id).get_ref(), finput,
        lineprop)

    value_inputs = values.get_inputs()
    value_outputs = values.get_outputs()

    values.pop_history()

    inputs = []
    outputs = []

    # default input for subgraph's input
    body_graph.add_input_value(counter_value)
    body_graph.add_input_value(cond_value)
    body_graph.add_input_value(iter_value)

    # default output for subgraph's output
    body_graph.add_output_value(cond_value)
    body_graph.add_output_value(iter_value)

    # default output
    outputs.append(functions.generate_value_with_same_type(iter_value))

    # generate pairs
    value_pairs = {}
    for v in value_inputs:
        key = str(v.field.id) + '_' + v.name
        if not (key in value_pairs.keys()):
            value_pairs[key] = {}

        value_pairs[key]['field'] = v.field
        value_pairs[key]['name'] = v.name
        value_pairs[key]['input_value'] = v.input_value
        value_pairs[key]['input_body_value'] = v.value

    for v in value_outputs:
        key = str(v.field.id) + '_' + v.name
        if not (key in value_pairs.keys()):
            value_pairs[key] = {}

        value_pairs[key]['field'] = v.field
        value_pairs[key]['name'] = v.name
        value_pairs[key]['output_body_value'] = v.value

    # remove iterator
    removed_name = str(local_field.id) + '_' + target_value.name
    del value_pairs[removed_name]

    for k, v in value_pairs.items():
        name = v['name']
        field = v['field']

        if 'input_body_value' in v:
            inputs.append(v['input_value'])
            body_graph.add_input_value(v['input_body_value'])

        else:
            temp_value1 = functions.generate_value_with_same_type(
                v['output_body_value'])
            temp_value2 = functions.generate_value_with_same_type(
                v['output_body_value'])
            inputs.append(temp_value1)
            body_graph.add_input_value(temp_value2)

        if 'output_body_value' in v:
            body_graph.add_output_value(v['output_body_value'])
            output_value = functions.generate_value_with_same_type(
                v['output_body_value'])
            outputs.append(output_value)
            if field.get_attribute(name).has_obj():
                field.get_attribute(name).get_ref().revise(output_value)
            else:
                field.get_attribute(name).revise(values.ValueRef(output_value))
        else:
            temp_value1 = v['input_body_value']
            temp_value2 = functions.generate_value_with_same_type(
                v['input_body_value'])
            body_graph.add_output_value(temp_value1)
            outputs.append(temp_value2)

    node = nodes.NodeListcomp(iter_value, inputs, body_graph, astc.lineno)
    node.set_outputs(outputs)

    graph.add_node(node)

    return local_field.get_attribute(internal_list_id).get_ref()
예제 #20
0
def veval_ast_subscript(astc: 'AstContext', local_field: 'values.Field',
                        graph: 'Graph'):
    '''
    Ex. x[1], x[y,z]
    '''
    assert (isinstance(astc.nast, gast.gast.Subscript))
    lineprop = utils.LineProperty(astc.lineno)

    def veval_with_default(nast, default_value):
        if nast is None:
            ret = values.NumberValue(default_value)
            ret.name = '@SliceDefault'
            return ret
        obj = veval_ast(astc.c(nast), local_field, graph)
        return try_get_value(obj, 'subscript', lineprop)

    def get_slice_indices(slice):
        if slice.lower is None and slice.upper is None and slice.step is None:
            return []
        indices = [
            veval_with_default(slice.lower, 0),
            veval_with_default(slice.upper, utils.slice_int_max)
        ]
        if slice.step is not None:
            indices.append(veval_with_default(slice.step, 1))
        return indices

    value = veval_ast(astc.c(astc.nast.value), local_field, graph)
    value_value = try_get_value(value, 'subscript', lineprop)

    if isinstance(astc.nast.slice, gast.gast.Index):
        slice_ = veval_ast(astc.c(astc.nast.slice.value), local_field, graph)
        slice_value = try_get_value(slice_, 'subscript', lineprop)

        if isinstance(slice_value, values.TupleValue):
            # ex. x[1,2]
            if slice_value.has_constant_value():
                values_ = [
                    try_get_value(x, 'subscript', lineprop)
                    for x in slice_value.get_constant_value()
                ]
                node = nodes.NodeGetItem(value_value, values_, line=lineprop)
            else:
                if config.show_warnings:
                    print('This subscript is not supported. in L.{}'.format(
                        astc.lineno))
                node = nodes.NodeInvalid(line=lineprop)
        else:
            # ex. x[1]
            node = nodes.NodeGetItem(value_value, [slice_value])
        ret_value = values.Value()
        node.set_outputs([ret_value])
        graph.add_node(node)
        return values.ValueRef(ret_value)

    elif isinstance(astc.nast.slice, gast.gast.Slice):

        indices = get_slice_indices(astc.nast.slice)

        node = nodes.NodeSlice(value_value, indices, [len(indices)])
        ret_value = functions.generate_value_with_same_type(value_value)
        node.set_outputs([ret_value])
        graph.add_node(node)
        return values.ValueRef(ret_value)

    elif isinstance(astc.nast.slice, gast.gast.ExtSlice):
        indices = []
        slice_specs = []
        for dim in astc.nast.slice.dims:
            if isinstance(dim, gast.gast.Index):
                indices.append(
                    try_get_value(
                        veval_ast(astc.c(dim.value), local_field, graph),
                        'subscript', lineprop))
                slice_specs.append(1)
            elif isinstance(dim, gast.gast.Slice):
                ni = get_slice_indices(dim)
                indices.extend(ni)
                slice_specs.append(len(ni))
            else:
                assert False, 'Unknown slice: %s in %s' % (dim, nast.slice)

        node = nodes.NodeSlice(value_value, indices, slice_specs)
        ret_value = functions.generate_value_with_same_type(value_value)
        node.set_outputs([ret_value])
        graph.add_node(node)
        return values.ValueRef(ret_value)

    return None
예제 #21
0
def veval_ast_if(astc: 'AstContext', local_field: 'values.Field',
                 graph: 'Graph'):
    assert (isinstance(astc.nast, gast.gast.If))
    lineprop = utils.LineProperty(astc.lineno)

    # if condition
    test = veval_ast(astc.c(astc.nast.test), local_field, graph)
    test_value = try_get_value(test, 'if', lineprop)

    id_str = str(utils.get_guid())
    if_id = 'if_' + id_str
    true_id = 'true_' + id_str
    false_id = 'false_' + id_str

    # True
    values.push_history(true_id)

    true_graph = Graph()
    true_graph.root_graph = graph.root_graph
    true_graph.name = 'True'
    body = veval_ast(astc.c(astc.nast.body), local_field, true_graph)

    true_value_inputs = values.get_inputs()
    true_value_outputs = values.get_outputs()

    values.pop_history()

    # False
    values.push_history(false_id)

    false_graph = Graph()
    false_graph.root_graph = graph.root_graph
    false_graph.name = 'False'
    body = veval_ast(astc.c(astc.nast.orelse), local_field, false_graph)

    false_value_inputs = values.get_inputs()
    false_value_outputs = values.get_outputs()

    values.pop_history()

    # generate pairs
    value_pairs = {}
    for v in true_value_inputs:
        key = str(v.field.id) + '_' + v.name
        if not (key in value_pairs.keys()):
            value_pairs[key] = {}

        value_pairs[key]['field'] = v.field
        value_pairs[key]['name'] = v.name
        value_pairs[key]['true_input_value'] = v.input_value
        value_pairs[key]['true_input_body_value'] = v.value

    for v in true_value_outputs:
        key = str(v.field.id) + '_' + v.name
        if not (key in value_pairs.keys()):
            value_pairs[key] = {}

        value_pairs[key]['field'] = v.field
        value_pairs[key]['name'] = v.name
        value_pairs[key]['true_output_body_value'] = v.value

    for v in false_value_inputs:
        key = str(v.field.id) + '_' + v.name
        if not (key in value_pairs.keys()):
            value_pairs[key] = {}

        value_pairs[key]['field'] = v.field
        value_pairs[key]['name'] = v.name
        value_pairs[key]['false_input_value'] = v.input_value
        value_pairs[key]['false_input_body_value'] = v.value

    for v in false_value_outputs:
        key = str(v.field.id) + '_' + v.name
        if not (key in value_pairs.keys()):
            value_pairs[key] = {}

        value_pairs[key]['field'] = v.field
        value_pairs[key]['name'] = v.name
        value_pairs[key]['false_output_body_value'] = v.value

    inputs = []
    outputs = []

    for k, v in value_pairs.items():
        name = v['name']
        field = v['field']

        input_value = None
        true_input_body_value = None
        false_input_body_value = None

        if 'true_input_value' in v:
            input_value = v['true_input_value']
        elif 'false_input_value' in v:
            input_value = v['false_input_value']

        if input_value is not None:
            if 'true_input_body_value' in v:
                true_input_body_value = v['true_input_body_value']
            else:
                true_input_body_value = functions.generate_value_with_same_type(
                    input_value)

            if 'false_input_body_value' in v:
                false_input_body_value = v['false_input_body_value']
            else:
                false_input_body_value = functions.generate_value_with_same_type(
                    input_value)

        true_output_body_value = None
        false_output_body_value = None
        output_value = None

        if 'true_output_body_value' in v:
            true_output_body_value = v['true_output_body_value']
        else:
            true_output_body_value = true_input_body_value

        if 'false_output_body_value' in v:
            false_output_body_value = v['false_output_body_value']
        else:
            false_output_body_value = false_input_body_value

        # TODO check types between true and false

        if true_output_body_value is not None or false_output_body_value is not None:
            output_value = functions.generate_value_with_same_type(
                true_output_body_value)

        if input_value is not None:
            inputs.append(input_value)

            true_graph.add_input_value(true_input_body_value)
            false_graph.add_input_value(false_input_body_value)

        if output_value is not None:
            outputs.append(output_value)
            true_graph.add_output_value(true_output_body_value)
            false_graph.add_output_value(false_output_body_value)

            if field.get_attribute(name).has_obj():
                field.get_attribute(name).get_ref().revise(output_value)
            else:
                field.get_attribute(name).revise(values.ValueRef(output_value))

    node = nodes.NodeIf(test_value, inputs, true_graph, false_graph,
                        astc.lineno)
    node.set_outputs(outputs)

    graph.add_node(node)

    return None
예제 #22
0
def veval_ast_for(astc: 'AstContext', local_field: 'values.Field',
                  graph: 'Graph'):
    '''
    for target in iter:
        ...
    '''
    assert (isinstance(astc.nast, gast.gast.For))
    lineprop = utils.LineProperty(astc.lineno)

    # for target in iter:
    iter_ = veval_ast(astc.c(astc.nast.iter), local_field, graph)

    # get target name
    target_name = ''
    if isinstance(astc.nast.target, gast.gast.Name):
        target_name = astc.nast.target.id
    else:
        if config.show_warnings:
            print('This for is not supported. in L.{}'.format(astc.lineno))
        return None

    for_guid = utils.get_guid()
    for_id = 'for_' + str(for_guid)
    body_id = 'body_' + str(for_guid)

    values.push_history(for_id)

    # body
    body_graph = Graph()
    body_graph.name = 'Body_' + str(for_guid)

    counter_value = values.NumberValue(0)
    counter_value.name = 'for_counter_' + str(for_guid)

    cond_value = values.BoolValue(True)
    cond_value.name = 'for_cond_' + str(for_guid)

    iter_value = try_get_value(iter_, 'for', lineprop)

    # create a node to lookup a value from sequence
    node_forgen = nodes.NodeForGenerator(counter_value, iter_value)

    # estimate type
    # TODO : more types
    if isinstance(iter_value, values.RangeValue):
        target_value = values.NumberValue(None)
        target_value.dtype = np.array(0).dtype
    else:
        target_value = values.Value()

    target_obj = values.Object(target_value)
    node_forgen.set_outputs([target_value])

    target_attribute = local_field.get_attribute(target_name)
    target_attribute.revise(target_obj)
    body_graph.add_node(node_forgen)

    # veval body
    body = veval_ast(astc.c(astc.nast.body), local_field, body_graph)

    value_inputs = values.get_inputs()
    value_outputs = values.get_outputs()

    values.pop_history()

    inputs = []
    outputs = []

    # default input for subgraph's input
    body_graph.add_input_value(counter_value)
    body_graph.add_input_value(cond_value)
    body_graph.add_input_value(iter_value)

    # default output for subgraph's output
    body_graph.add_output_value(cond_value)
    body_graph.add_output_value(iter_value)

    # default output
    outputs.append(functions.generate_value_with_same_type(iter_value))

    # generate pairs
    value_pairs = {}
    for v in value_inputs:
        key = str(v.field.id) + '_' + v.name
        if not (key in value_pairs.keys()):
            value_pairs[key] = {}

        value_pairs[key]['field'] = v.field
        value_pairs[key]['name'] = v.name
        value_pairs[key]['input_value'] = v.input_value
        value_pairs[key]['input_body_value'] = v.value

    for v in value_outputs:
        key = str(v.field.id) + '_' + v.name
        if not (key in value_pairs.keys()):
            value_pairs[key] = {}

        value_pairs[key]['field'] = v.field
        value_pairs[key]['name'] = v.name
        value_pairs[key]['output_body_value'] = v.value

    for k, v in value_pairs.items():
        name = v['name']
        field = v['field']

        if 'input_body_value' in v:
            inputs.append(v['input_value'])
            body_graph.add_input_value(v['input_body_value'])
        else:
            temp_value1 = functions.generate_value_with_same_type(
                v['output_body_value'])
            temp_value2 = functions.generate_value_with_same_type(
                v['output_body_value'])
            inputs.append(temp_value1)
            body_graph.add_input_value(temp_value2)

        if 'output_body_value' in v:
            body_graph.add_output_value(v['output_body_value'])
            output_value = functions.generate_value_with_same_type(
                v['output_body_value'])
            outputs.append(output_value)
            if field.get_attribute(name).has_obj():
                field.get_attribute(name).get_obj().revise(output_value)
            else:
                field.get_attribute(name).revise(values.Object(output_value))
        else:
            temp_value1 = v['input_body_value']
            temp_value2 = functions.generate_value_with_same_type(
                v['input_body_value'])
            body_graph.add_output_value(temp_value1)
            outputs.append(temp_value2)

    node = nodes.NodeFor(iter_value, inputs, body_graph, astc.lineno)
    node.set_outputs(outputs)

    graph.add_node(node)

    return None
예제 #23
0
def veval_ast_subscript(astc: 'AstContext', local_field: 'values.Field',
                        graph: 'Graph'):
    '''
    Ex. x[1], x[y,z]
    '''
    assert (isinstance(astc.nast, gast.gast.Subscript))
    lineprop = utils.LineProperty(astc.lineno)

    def veval_with_default(nast, default_value):
        if nast is None:
            return values.NumberValue(default_value)
        obj = veval_ast(astc.c(nast), local_field, graph)
        return try_get_value(obj, 'subscript', lineprop)

    def get_slice_indices(slice):
        if slice.lower is None and slice.upper is None and slice.step is None:
            return []
        indices = [
            veval_with_default(slice.lower, 0),
            veval_with_default(slice.upper, utils.slice_int_max)
        ]
        if slice.step is not None:
            indices.append(veval_with_default(slice.step, 1))
        return indices

    value = veval_ast(astc.c(astc.nast.value), local_field, graph)
    value_value = try_get_value(value, 'subscript', lineprop)

    if isinstance(astc.nast.slice, gast.gast.Index):
        slice_ = veval_ast(astc.c(astc.nast.slice.value), local_field, graph)
        slice_value = try_get_value(slice_, 'subscript', lineprop)

        if isinstance(slice_value, values.TupleValue):
            # ex. x[1,2]
            values_ = [
                try_get_value(x, 'subscript', lineprop)
                for x in slice_value.values
            ]
            node = nodes.NodeGetItem(value_value, values_)
        else:
            # ex. x[1]
            node = nodes.NodeGetItem(value_value, [slice_value])
        ret_value = values.Value()
        node.set_outputs([ret_value])
        graph.add_node(node)
        return values.Object(ret_value)

    elif isinstance(astc.nast.slice, gast.gast.Slice):

        indices = get_slice_indices(astc.nast.slice)

        node = nodes.NodeSlice(value_value, indices, [len(indices)])
        ret_value = values.Value()
        node.set_outputs([ret_value])
        graph.add_node(node)
        return values.Object(ret_value)

    elif isinstance(astc.nast.slice, gast.gast.ExtSlice):
        indices = []
        slice_specs = []
        for dim in astc.nast.slice.dims:
            if isinstance(dim, gast.gast.Index):
                indices.append(
                    try_get_value(
                        veval_ast(astc.c(dim.value), local_field, graph),
                        'subscript', lineprop))
                slice_specs.append(1)
            elif isinstance(dim, gast.gast.Slice):
                ni = get_slice_indices(dim)
                indices.extend(ni)
                slice_specs.append(len(ni))
            else:
                assert False, 'Unknown slice: %s in %s' % (dim, nast.slice)

        node = nodes.NodeSlice(value_value, indices, slice_specs)
        ret_value = values.Value()
        node.set_outputs([ret_value])
        graph.add_node(node)
        return values.Object(ret_value)

    return None
예제 #24
0
def veval_ast_for(astc: 'AstContext', local_field: 'values.Field',
                  graph: 'Graph'):
    '''
    for target in iter:
        ...
    '''
    assert (isinstance(astc.nast, gast.gast.For))
    lineprop = utils.LineProperty(astc.lineno)

    # for target in iter:
    iter_ = veval_ast(astc.c(astc.nast.iter), local_field, graph)

    # get target name
    target_name = ''
    if isinstance(astc.nast.target, gast.gast.Name):
        target_name = astc.nast.target.id
    else:
        if config.show_warnings:
            print('This for is not supported. in L.{}'.format(astc.lineno))
        return None

    for_id = 'for_' + str(utils.get_guid())
    body_id = 'body_' + str(utils.get_guid())
    values.commit(for_id)

    # body
    body_graph = Graph()
    body_graph.name = 'Body'

    counter_value = values.NumberValue(0)
    counter_value.name = 'for_counter'

    cond_value = values.BoolValue(True)
    cond_value.name = 'for_cond'

    iter_value = try_get_value(iter_, 'for', lineprop)

    # create a node to lookup a value from sequence
    node_forgen = nodes.NodeForGenerator(counter_value, iter_value)

    # estimate type
    # TODO : more types
    if isinstance(iter_value, values.RangeValue):
        target_value = values.NumberValue(None)
        target_value.dtype = np.array(0).dtype
    else:
        target_value = values.Value()

    target_obj = values.Object(target_value)
    node_forgen.set_outputs([target_value])

    target_attribute = local_field.get_attribute(target_name)
    target_attribute.revise(target_obj)
    body_graph.add_node(node_forgen)

    # veval body
    body = veval_ast(astc.c(astc.nast.body), local_field, body_graph)

    values.commit(body_id)

    # get changed attributes
    body_input_attributes = get_input_attritubtes(local_field, for_id, body_id)
    body_output_attributes = get_output_attritubtes(local_field, for_id,
                                                    body_id)

    body_input_attributes = filter_attributes(body_input_attributes)
    body_output_attributes = filter_attributes(body_output_attributes)

    # get objects whose values are changed
    value_changed_objs = get_output_objs(local_field, for_id, body_id)

    changed_values = {}

    for obj in value_changed_objs:
        in_value = obj.get_value_log(for_id)
        out_value = obj.get_value_log(body_id)
        changed_values[in_value] = out_value

    # get outputs
    output_attributes_2_values = {}

    for attribute in body_output_attributes:
        output_attributes_2_values[attribute] = attribute.get_obj().get_value()

    # get inputs
    values.checkout(for_id)

    input_attributes_2_values = {}

    for attribute in body_input_attributes:
        input_attributes_2_values[attribute] = attribute.get_obj().get_value()

    # export
    values.checkout(for_id)

    # generate attribute pairs
    name2attributes = {}

    for attribute in body_input_attributes:
        key = str(attribute.parent.id) + '_' + attribute.name

        if key in name2attributes.keys():
            name2attributes[key][0] = attribute
        else:
            name2attributes[key] = [attribute, None]

    for attribute in body_output_attributes:
        key = str(attribute.parent.id) + '_' + attribute.name

        if key in name2attributes.keys():
            name2attributes[key][1] = attribute
        else:
            name2attributes[key] = [None, attribute]

    # remove defaule values
    name_removing = []
    defaule_values = [counter_value, cond_value, iter_value]
    for k, v in name2attributes.items():
        if v[0] in defaule_values:
            name_removing.append(k)

        if v[1] in defaule_values:
            name_removing.append(k)

    for nr in name_removing:
        if nr in name2attributes.keys():
            name2attributes.pop(nr)

    #
    inputs = []
    outputs = []
    non_volatiles = []

    # default input for subgraph's input
    body_graph.add_input_value(counter_value)
    body_graph.add_input_value(cond_value)
    body_graph.add_input_value(iter_value)

    # default output for subgrap's output
    body_graph.add_output_value(cond_value)
    body_graph.add_output_value(iter_value)

    # default output
    outputs.append(functions.generate_value_with_same_type(iter_value))

    for attributes in name2attributes.values():
        name = ''
        parent = None
        input_value = None
        output_value = None

        if attributes[0] is not None:
            name = attributes[0].name
            parent = attributes[0].parent

        if attributes[1] is not None:
            name = attributes[1].name
            parent = attributes[1].parent

        if attributes[0] is not None:
            input_value = input_attributes_2_values[attributes[0]]
        else:
            # value with same type
            input_value = functions.generate_value_with_same_type(
                output_attributes_2_values[attributes[1]])

        if attributes[1] is not None:
            output_value = output_attributes_2_values[attributes[1]]
        else:
            if input_attributes_2_values[
                    attributes[0]] in changed_values.keys():
                # change only values
                output_value = changed_values[input_attributes_2_values[
                    attributes[0]]]
            else:
                # copy value
                output_value = input_attributes_2_values[attributes[0]]

        output_value_in_node = functions.generate_value_with_same_type(
            output_value)

        inputs.append(input_value)
        outputs.append(output_value_in_node)
        body_graph.add_input_value(input_value)
        body_graph.add_output_value(output_value)

        if attributes[1] is not None and attributes[1].is_non_volatile:
            non_volatiles.append(
                (attributes[1].initial_obj.get_value(), output_value_in_node))

        output_obj_in_node = values.Object(output_value_in_node)
        parent.get_attribute(name).revise(output_obj_in_node)

    for changed_value_in, changed_value_out in changed_values.items():
        if changed_value_in is None:
            continue
        if changed_value_in in inputs:
            continue

        inputs.append(changed_value_in)
        body_graph.add_input_value(changed_value_in)
        body_graph.add_output_value(changed_value_out)
        value = functions.generate_value_with_same_type(changed_value_out)
        obj.revise(value)
        outputs.append(value)

    node = nodes.NodeFor(iter_value, inputs, body_graph, astc.lineno)
    node.set_outputs(outputs)

    graph.add_node(node)

    # add non-volatiles
    for tv, v in non_volatiles:
        node_nv = nodes.NodeNonVolatileAssign(tv, v)
        graph.add_node(node_nv)

    return None
예제 #25
0
def veval_ast_listcomp(astc: 'AstContext', local_field: 'values.Field',
                       graph: 'Graph'):
    '''
    Ex. [x for x in xx]
    [elt for target in iter]
    '''
    assert (isinstance(astc.nast, gast.gast.ListComp))
    lineprop = utils.LineProperty(astc.lineno)

    listcomp_guid = str(utils.get_guid())
    listcomp_id = 'listcomp_' + listcomp_guid
    body_id = 'listcomp_body_' + listcomp_guid
    internal_iter_id = '@internal/iter_' + listcomp_guid

    generator = astc.nast.generators[0]
    iter_value = try_get_value(
        veval_ast(astc.c(generator.iter), local_field, graph), 'generator',
        lineprop)
    list_value = values.ListValue()
    list_obj = values.Object(list_value)

    node_generate_list = nodes.NodeGenerate('List', [], lineprop)
    node_generate_list.set_outputs([list_value])
    graph.add_node(node_generate_list)

    # body
    target_name = ''
    if isinstance(generator.target, gast.gast.Name):
        target_name = generator.target.id
    else:
        if config.show_warnings:
            print('This for is not supported. in L.{}'.format(astc.lineno))
        return None

    counter_value = values.NumberValue(0)
    counter_value.name = 'listcomp_counter_' + listcomp_guid

    cond_value = values.BoolValue(True)
    cond_value.name = 'listcomp_cond_' + listcomp_guid

    body_field = values.Field()
    body_field.set_module(local_field.module)
    body_field.set_parent(local_field)

    # set iter with internal name
    body_field.get_attribute(internal_iter_id).revise(list_obj)

    values.commit(listcomp_id)

    body_graph = Graph()
    body_graph.name = 'Body_' + listcomp_guid

    node_forgen = nodes.NodeForGenerator(counter_value, iter_value)
    target_value = values.Value()
    target_obj = values.Object(target_value)
    node_forgen.set_outputs([target_value])

    body_field.get_attribute(target_name).revise(target_obj)

    body_graph.add_node(node_forgen)

    elt = veval_ast(astc.c(astc.nast.elt), body_field, body_graph)
    elt_obj = try_get_obj(elt, 'listcomp', lineprop)

    farg = functions.FunctionArg()
    farg.name = ''
    farg.obj = elt_obj
    append_value = list_obj.get_field().get_attribute(
        'append').get_obj().get_value()
    append_value.func.vcall(local_field.module, body_graph, list_obj, [farg],
                            lineprop)

    values.commit(body_id)

    body_input_attributes = get_input_attritubtes(
        body_field, listcomp_id, body_id) + get_input_attritubtes(
            local_field, listcomp_id, body_id)
    body_output_attributes = get_output_attritubtes(
        body_field, listcomp_id, body_id) + get_output_attritubtes(
            local_field, listcomp_id, body_id)
    body_input_attributes = filter_attributes(body_input_attributes)
    body_output_attributes = filter_attributes(body_output_attributes)

    # get objects whose values are changed
    value_changed_objs = get_output_objs(body_field, listcomp_id,
                                         body_id) + get_output_objs(
                                             local_field, listcomp_id, body_id)

    changed_values = {}

    for obj in value_changed_objs:
        in_value = obj.get_value_log(listcomp_id)
        out_value = obj.get_value_log(body_id)
        changed_values[in_value] = (obj, out_value)

    output_attributes_2_values = {}

    for attribute in body_output_attributes:
        output_attributes_2_values[attribute] = attribute.get_obj().get_value()

    # get inputs
    values.checkout(listcomp_id)

    input_attributes_2_values = {}

    for attribute in body_input_attributes:
        input_attributes_2_values[attribute] = attribute.get_obj().get_value()

    # Exports
    values.checkout(listcomp_id)

    # generate attribute pairs
    name2attributes = {}

    for attribute in body_input_attributes:
        key = str(attribute.parent.id) + '_' + attribute.name

        if key in name2attributes.keys():
            name2attributes[key][0] = attribute
        else:
            name2attributes[key] = [attribute, None]

    for attribute in body_output_attributes:
        key = str(attribute.parent.id) + '_' + attribute.name

        if key in name2attributes.keys():
            name2attributes[key][1] = attribute
        else:
            name2attributes[key] = [None, attribute]

    # remove defaule values
    name_removing = []
    defaule_values = [counter_value, cond_value, iter_value]
    for k, v in name2attributes.items():
        if v[0] is not None and input_attributes_2_values[
                v[0]] in defaule_values:
            name_removing.append(k)

        if v[1] is not None and output_attributes_2_values[
                v[1]] in defaule_values:
            name_removing.append(k)

    for nr in name_removing:
        if nr in name2attributes.keys():
            name2attributes.pop(nr)

    #
    inputs = []
    outputs = []
    non_volatiles = []

    # default input for subgraph's input
    body_graph.add_input_value(counter_value)
    body_graph.add_input_value(cond_value)
    body_graph.add_input_value(iter_value)

    # default output for subgrap's output
    body_graph.add_output_value(cond_value)
    body_graph.add_output_value(iter_value)

    # default output
    outputs.append(functions.generate_value_with_same_type(iter_value))

    for attributes in name2attributes.values():
        name = ''
        parent = None
        input_value = None
        output_value = None

        if attributes[0] is not None:
            name = attributes[0].name
            parent = attributes[0].parent

        if attributes[1] is not None:
            name = attributes[1].name
            parent = attributes[1].parent

        if attributes[0] is not None:
            input_value = input_attributes_2_values[attributes[0]]
        else:
            # value with same type
            input_value = functions.generate_value_with_same_type(
                output_attributes_2_values[attributes[1]])

        if attributes[1] is not None:
            output_value = output_attributes_2_values[attributes[1]]
        else:
            if input_attributes_2_values[
                    attributes[0]] in changed_values.keys():
                # change only values
                output_value = changed_values[input_attributes_2_values[
                    attributes[0]]]
            else:
                # copy value
                output_value = input_attributes_2_values[attributes[0]]

        output_value_in_node = functions.generate_value_with_same_type(
            output_value)

        inputs.append(input_value)
        outputs.append(output_value_in_node)
        body_graph.add_input_value(input_value)
        body_graph.add_output_value(output_value)

        if attributes[1] is not None and attributes[1].is_non_volatile:
            non_volatiles.append(
                (attribute[1].initial_obj.get_value(), output_value_in_node))

        output_obj_in_node = values.Object(output_value_in_node)
        parent.get_attribute(name).revise(output_obj_in_node)

    for changed_value_in, changed_obj_value_out in changed_values.items():
        if changed_value_in is None:
            continue
        if changed_value_in in inputs:
            continue
        inputs.append(changed_value_in)
        body_graph.add_input_value(changed_value_in)
        body_graph.add_output_value(changed_obj_value_out[1])
        value = functions.generate_value_with_same_type(
            changed_obj_value_out[1])
        changed_obj_value_out[0].revise(value)
        outputs.append(value)

    node = nodes.NodeListcomp(iter_value, inputs, body_graph, astc.lineno)
    node.set_outputs(outputs)

    graph.add_node(node)

    # add non-volatiles
    for tv, v in non_volatiles:
        node_nv = nodes.NodeNonVolatileAssign(tv, v)
        graph.add_node(node_nv)

    if body_field.get_attribute(internal_iter_id).has_obj():
        return body_field.get_attribute(internal_iter_id).get_obj()
    else:
        return list_obj
예제 #26
0
def veval_ast_if(astc: 'AstContext', local_field: 'values.Field',
                 graph: 'Graph'):
    assert (isinstance(astc.nast, gast.gast.If))
    lineprop = utils.LineProperty(astc.lineno)

    # if condition
    test = veval_ast(astc.c(astc.nast.test), local_field, graph)
    test_value = try_get_value(test, 'if', lineprop)

    id_str = str(utils.get_guid())
    if_id = 'if_' + id_str
    true_id = 'true_' + id_str
    false_id = 'false_' + id_str

    values.commit(if_id)

    # True condition
    values.checkout(if_id)
    true_graph = Graph()
    true_graph.name = 'True'
    body = veval_ast(astc.c(astc.nast.body), local_field, true_graph)

    values.commit(true_id)
    true_input_attributes = get_input_attritubtes(local_field, if_id, true_id)
    true_output_attributes = get_output_attritubtes(local_field, if_id,
                                                    true_id)
    true_output_objs = get_output_objs(local_field, if_id, true_id)

    #TODO(durswd): improve
    true_input_attributes = filter_attributes(true_input_attributes)
    true_output_attributes = filter_attributes(true_output_attributes)

    true_output_attributes_2_values = {}

    for attribute in true_output_attributes:
        true_output_attributes_2_values[attribute] = attribute.get_obj(
        ).get_value()

    # False condition
    values.checkout(if_id)
    false_graph = Graph()
    false_graph.name = 'False'
    orelse = veval_ast(astc.c(astc.nast.orelse), local_field, false_graph)

    values.commit(false_id)
    false_input_attributes = get_input_attritubtes(local_field, if_id,
                                                   false_id)
    false_output_attributes = get_output_attritubtes(local_field, if_id,
                                                     false_id)
    false_output_objs = get_output_objs(local_field, if_id, false_id)

    #TODO(durswd): improve
    false_input_attributes = filter_attributes(false_input_attributes)
    false_output_attributes = filter_attributes(false_output_attributes)

    false_output_attributes_2_values = {}

    for attribute in false_output_attributes:
        false_output_attributes_2_values[attribute] = attribute.get_obj(
        ).get_value()

    # Merge
    values.checkout(if_id)

    # Input
    input_attributes = set(true_input_attributes) | set(false_input_attributes)

    # remove unexisting values
    input_attributes = [v for v in input_attributes if v.has_obj()]
    input_values = [i.get_obj().get_value() for i in input_attributes]

    # Output
    name2output_attributes = {}

    # generate attribute pairs
    for attribute in true_output_attributes:
        key = str(attribute.parent.id) + '_' + attribute.name

        if key in name2output_attributes.keys():
            name2output_attributes[key][0] = attribute
        else:
            name2output_attributes[key] = [attribute, None]

    for attribute in false_output_attributes:
        key = str(attribute.parent.id) + '_' + attribute.name

        if key in name2output_attributes.keys():
            name2output_attributes[key][1] = attribute
        else:
            name2output_attributes[key] = [None, attribute]

    obj2output_values = {}
    for obj in true_output_objs:
        if obj.get_value() == None:
            continue

        key = obj
        value = obj.get_value_log(true_id)

        if key in obj2output_values.keys():
            obj2output_values[key][0] = value
        else:
            obj2output_values[key] = [value, None]

    for obj in false_output_objs:
        if obj.get_value() == None:
            continue

        key = obj
        value = obj.get_value_log(false_id)

        if key in obj2output_values.keys():
            obj2output_values[key][1] = value
        else:
            obj2output_values[key] = [None, value]

    output_attributes = set(true_output_attributes) | set(
        false_output_attributes)
    output_values = []

    non_volatiles = []

    for attribute_pair in name2output_attributes.values():
        true_attribute, false_attribute = attribute_pair
        name = ''
        parent = None  # type: values.Field

        if true_attribute is not None:
            name = true_attribute.name
            parent = true_attribute.parent

        if false_attribute is not None:
            name = false_attribute.name
            parent = false_attribute.parent

        if true_attribute is not None:
            true_value = true_output_attributes_2_values[true_attribute]
        else:
            if parent.has_attribute(name):
                true_value = parent.get_attribute(name).get_obj().get_value()

                if not true_value in input_values:
                    input_values.append(true_value)
            else:
                # TODO : it should be better
                # case
                # if xxx:
                #     y = 10
                # print(y)
                true_value = false_output_attributes_2_values[false_attribute]

        if false_attribute is not None:
            false_value = false_output_attributes_2_values[false_attribute]
        else:
            if parent.has_attribute(name):
                false_value = parent.get_attribute(name).get_obj().get_value()

                if not false_value in input_values:
                    input_values.append(false_value)

            else:
                # TODO : it should be better
                # case
                # if xxx:
                #     y = 10
                # print(y)
                false_value = true_output_attributes_2_values[true_attribute]

        true_graph.add_output_value(true_value)
        false_graph.add_output_value(false_value)

        if true_attribute is not None and false_attribute is not None and true_attribute != false_attribute:
            # dynamic
            value = functions.generate_value_with_same_type(true_value)
            output_values.append(value)
            parent.get_attribute(name).revise(values.Object(value))

        elif true_attribute is not None and false_attribute is not None:
            # change both
            value = functions.generate_value_with_same_type(true_value)
            output_values.append(value)

            if parent.get_attribute(name).is_non_volatile:
                non_volatiles.append(
                    (parent.get_attribute(name).initial_obj.get_value(),
                     value))

            parent.get_attribute(name).revise(values.Object(value))

        elif true_attribute in input_attributes:
            value = functions.generate_value_with_same_type(true_value)

            if parent.get_attribute(name).is_non_volatile:
                non_volatiles.append(
                    (parent.get_attribute(name).initial_obj.get_value(),
                     value))

            output_values.append(value)
            parent.get_attribute(name).revise(values.Object(value))
        else:
            value = functions.generate_value_with_same_type(false_value)

            if parent.get_attribute(name).is_non_volatile:
                non_volatiles.append(
                    (parent.get_attribute(name).initial_obj.get_value(),
                     value))

            output_values.append(value)
            parent.get_attribute(name).revise(values.Object(value))

    for input_value in input_values:
        true_graph.add_input_value(input_value)
        false_graph.add_input_value(input_value)

    for obj, values_pairs in obj2output_values.items():
        if not obj.get_value() in input_values:
            input_values.append(obj.get_value())

        value = None
        true_value = None
        false_value = None

        if values_pairs[0] is not None:
            value = values_pairs[0]
            true_value = values_pairs[0]
        if values_pairs[1] is not None:
            value = values_pairs[1]
            false_value = values_pairs[1]

        if true_value is None:
            true_value = obj.get_value()

        if false_value is None:
            false_value = obj.get_value()

        value = functions.generate_value_with_same_type(value)
        obj.revise(value)
        output_values.append(value)

        true_graph.add_output_value(true_value)
        false_graph.add_output_value(false_value)

    node = nodes.NodeIf(test_value, input_values, true_graph, false_graph,
                        astc.lineno)
    node.set_outputs(output_values)

    graph.add_node(node)

    # add non-volatiles
    for tv, v in non_volatiles:
        node_nv = nodes.NodeNonVolatileAssign(tv, v)
        graph.add_node(node_nv)

    return None