Exemplo n.º 1
0
def create_get_type_of(var_name, lineno, col_offset, test_unreferenced=True):
    get_type_of_comment = create_src_comment(
        "Getting the type of '{0}'".format(var_name), lineno)
    get_type_of_method = core_language_copy.create_attribute(
        default_module_type_store_var_name,
        "get_type_of",
        line=lineno,
        column=col_offset)
    localization = create_localization(lineno, col_offset)
    if test_unreferenced:
        get_type_of_call = functions_copy.create_call(
            get_type_of_method,
            [localization,
             core_language_copy.create_str(var_name)])
    else:
        get_type_of_call = functions_copy.create_call(get_type_of_method, [
            localization,
            core_language_copy.create_str(var_name),
            core_language_copy.create_Name('False')
        ])

    assign_stmts, temp_assign = create_temp_Assign(get_type_of_call, lineno,
                                                   col_offset)

    return flatten_lists(get_type_of_comment, assign_stmts), temp_assign
Exemplo n.º 2
0
def create_get_type_of_member(owner_var,
                              member_name,
                              lineno,
                              col_offset,
                              test_unreferenced=True):
    comment = create_src_comment(
        "Obtaining the member '{0}' of a type".format(member_name), lineno)
    localization = create_localization(lineno, col_offset)
    # TODO: Remove?
    # get_type_of_member_func = core_language_copy.create_Name('get_type_of_member')
    # get_type_of_member_call = functions_copy.create_call(get_type_of_member_func, [localization, owner_var,
    #                                                                           core_language_copy.create_str(
    #                                                                               member_name)])

    get_type_of_member_func = core_language_copy.create_attribute(
        owner_var, 'get_type_of_member')
    if not test_unreferenced:
        get_type_of_member_call = functions_copy.create_call(
            get_type_of_member_func, [
                localization,
                core_language_copy.create_str(member_name),
                core_language_copy.create_Name('False')
            ])
    else:
        get_type_of_member_call = functions_copy.create_call(
            get_type_of_member_func,
            [localization,
             core_language_copy.create_str(member_name)])

    member_stmts, member_var = create_temp_Assign(get_type_of_member_call,
                                                  lineno, col_offset)

    return flatten_lists(comment, member_stmts), member_var
Exemplo n.º 3
0
def __remove_type_from_union_implementation(if_test, type_, lineno,
                                            col_offset):
    param = __get_idiom_type_param(if_test)
    if type(param) is ast.Name:
        obj_type, obj_var = stypy_functions_copy.create_get_type_of(
            param.id, lineno, col_offset)
        remove_type_call = functions_copy.create_call(
            core_language_copy.create_Name("remove_type_from_union"),
            [obj_var, type_],
            line=lineno,
            column=col_offset)
        set_type = stypy_functions_copy.create_set_type_of(
            param.id, remove_type_call, lineno, col_offset)

        return stypy_functions_copy.flatten_lists(obj_type, set_type)
    if type(param) is ast.Attribute:
        # Get the owner of the attribute
        obj_type_stmts, obj_var = stypy_functions_copy.create_get_type_of(
            param.value.id, lineno, col_offset)
        # Get the current type of the owner of the attribute
        att_type_stmts, att_var = stypy_functions_copy.create_get_type_of_member(
            obj_var, param.attr, lineno, col_offset)
        remove_type_call = functions_copy.create_call(
            core_language_copy.create_Name("remove_type_from_union"),
            [att_var, type_],
            line=lineno,
            column=col_offset)
        set_member = stypy_functions_copy.create_set_type_of_member(
            obj_var, param.attr, remove_type_call, lineno, col_offset)
        return stypy_functions_copy.flatten_lists(obj_type_stmts,
                                                  att_type_stmts, set_member)

    return []
def create_unary_operator(op_name, op, line=0, column=0):
    localization = stypy_functions_copy.create_localization(line, column)

    unop_func = core_language_copy.create_Name("operator")
    unop = functions_copy.create_call(unop_func, [localization, op_name, op])

    return unop
Exemplo n.º 5
0
def create_join_type_store(join_func_name, type_stores_to_join):
    join_func = core_language_copy.create_Name(join_func_name)
    join_call = functions_copy.create_call(join_func, type_stores_to_join)

    left_hand_side = __new_temp_type_store_Name(right_hand_side=False)
    join_statement = ast.Assign([left_hand_side], join_call)

    return join_statement, left_hand_side
Exemplo n.º 6
0
def create_type_store(type_store_name=default_module_type_store_var_name):
    call_arg = core_language_copy.create_Name("__file__")
    call_func = core_language_copy.create_Name("TypeStore")
    call = functions_copy.create_call(call_func, call_arg)
    assign_target = core_language_copy.create_Name(type_store_name, False)
    assign = core_language_copy.create_Assign(assign_target, call)

    return assign
def create_binary_operator(op_name, op1, op2, line=0, column=0):
    localization = stypy_functions_copy.create_localization(line, column)

    binop_func = core_language_copy.create_Name("operator")
    binop = functions_copy.create_call(binop_func,
                                       [localization, op_name, op1, op2])

    return binop
Exemplo n.º 8
0
def create_print_errors():
    """
    Creates AST Nodes that encode "ErrorType.print_error_msgs()"
    """
    attribute = core_language_copy.create_attribute("ErrorType",
                                                    "print_error_msgs")
    expr = ast.Expr()
    expr.value = functions_copy.create_call(attribute, [])

    return expr
Exemplo n.º 9
0
def create_localization(line, col):
    """
    Creates AST Nodes that creates a new Localization instance
    """
    linen = core_language_copy.create_num(line)
    coln = core_language_copy.create_num(col)
    file_namen = core_language_copy.create_Name('__file__')
    loc_namen = core_language_copy.create_Name(
        'stypy.python_lib.python_types.type_inference.localization.Localization'
    )
    loc_call = functions_copy.create_call(loc_namen, [file_namen, linen, coln])

    return loc_call
Exemplo n.º 10
0
def create_set_type_store(type_store_param, clone=True):
    attribute = core_language_copy.create_attribute("type_store",
                                                    "set_type_store")

    if clone:
        clone_param = core_language_copy.create_Name("True")
    else:
        clone_param = core_language_copy.create_Name("False")

    set_call = functions_copy.create_call(attribute,
                                          [type_store_param, clone_param])

    set_expr = ast.Expr()
    set_expr.value = set_call

    return set_expr
Exemplo n.º 11
0
def create_clone_type_store():
    attribute = core_language_copy.create_attribute("type_store",
                                                    "clone_type_store")
    clone_call = functions_copy.create_call(attribute, [])

    return create_temp_type_store_Assign(clone_call)