示例#1
0
def eval_struct_node(node: ast.StructNode, env: en.Environment):
    struct = Struct(node.name)
    pos = 0
    for line in node.block.lines:
        type_node: ast.TypeNode = line.left
        tal = get_tal_of_defining_node(type_node.right, env)
        name: str = type_node.left.name
        struct.vars[name] = pos, tal
        total_len = tal.total_len()
        pos += total_len
    mem.MEMORY.add_type(node.name, pos)
    env.add_struct(node.name, struct)
示例#2
0
def get_tal_of_evaluated_node(node: ast.Node, env: en.Environment) -> en.Type:
    if node.node_type == ast.LITERAL:
        node: ast.Literal
        return LITERAL_TYPE_TABLE[node.lit_type]
    elif node.node_type == ast.STRING_LITERAL:
        node: ast.StringLiteralNode
        return en.Type("char", node.byte_length)
    elif node.node_type == ast.NAME_NODE:
        node: ast.NameNode
        return env.get_type_arr_len(node.name, (node.line_num, node.file))
    elif node.node_type == ast.UNARY_OPERATOR:
        node: ast.UnaryOperator
        tal = get_tal_of_evaluated_node(node.value, env)
        if node.operation == "unpack":
            if len(tal.type_name) > 1 and tal.type_name[0] == "*":
                return en.Type(tal.type_name[1:])
            else:
                raise lib.TypeException("Cannot unpack a non-pointer type")
        elif node.operation == "pack":
            return en.Type("*" + tal.type_name)
        else:
            return tal
    elif node.node_type == ast.BINARY_OPERATOR:
        node: ast.BinaryOperator
        return get_tal_of_evaluated_node(node.left, env)
    elif node.node_type == ast.FUNCTION_CALL:
        node: ast.FuncCall
        call_obj = node.call_obj
        if call_obj.node_type == ast.NAME_NODE:
            func_group: dict = env.get_function(call_obj.name,
                                                (node.line_num, node.file))
            arg_types = []
            for orig_arg in node.args.lines:
                tal = get_tal_of_evaluated_node(orig_arg, env)
                arg_types.append(tal)
            types_id = en.args_type_hash(arg_types)
            func = func_group[types_id]
            return func.r_tal
    elif node.node_type == ast.INDEXING_NODE:  # array
        node: ast.IndexingNode
        # return get_tal_of_ordinary_node(node.call_obj, env)
        tal_co = get_tal_of_evaluated_node(node.call_obj, env)
        if en.is_array(tal_co):
            return en.Type(tal_co.type_name, *tal_co.array_lengths[1:])
        elif tal_co.type_name[0] == "*":
            return en.Type(tal_co.type_name[1:])
        else:
            raise lib.TypeException()
    elif node.node_type == ast.IN_DECREMENT_OPERATOR:
        node: ast.InDecrementOperator
        return get_tal_of_evaluated_node(node.value, env)
    elif node.node_type == ast.NULL_STMT:
        return en.Type("*void")
示例#3
0
def eval_dot(node: ast.Dot, env: en.Environment):
    l_ptr = evaluate(node.left, env)
    l_tal = get_tal_of_evaluated_node(node.left, env)
    struct: Struct = env.get_struct(l_tal.type_name)
    attr: ast.NameNode = node.right
    pos_in_struct = struct.get_attr_pos(attr.name)
    return l_ptr + pos_in_struct
示例#4
0
def get_tal_of_node_self(node: ast.Node, env: en.Environment) -> en.Type:
    if node.node_type == ast.NAME_NODE:
        node: ast.NameNode
        return env.get_type_arr_len(node.name, (node.line_num, node.file))
    elif node.node_type == ast.INDEXING_NODE:
        node: ast.IndexingNode
        return get_tal_of_node_self(node.call_obj, env)
    elif node.node_type == ast.UNARY_OPERATOR:
        node: ast.UnaryOperator
        print(2223)
示例#5
0
def evaluate(node: ast.Node, env: en.Environment):
    if env.is_terminated():
        return env.returned_ptr()
    fn = NODE_TABLE[node.node_type]
    # print(fn)
    return fn(node, env)
示例#6
0
def eval_return(node: ast.ReturnStmt, env: en.Environment):
    r = evaluate(node.value, env)
    env.terminate(r)
    return r
示例#7
0
def eval_assignment_node(node: ast.AssignmentNode, env: en.Environment):
    r = evaluate(node.right, env)
    lf = node.line_num, node.file

    if node.left.node_type == ast.NAME_NODE:
        name: str = node.left.name
        if node.level == ast.FUNC_DEFINE:
            if not isinstance(r, Function):
                raise lib.TypeException("Unexpected function declaration")
            env.define_function(name, r)
        else:
            tal = get_tal_of_evaluated_node(node.left, env)
            total_len = tal.total_len()
            rv = mem.MEMORY.get(r, total_len)
            current_ptr = env.get(name, lf)
            mem.MEMORY.set(current_ptr, rv)
            # env.assign(name, r, lf)
    elif node.left.node_type == ast.TYPE_NODE:
        type_node: ast.TypeNode = node.left
        tal = get_tal_of_defining_node(type_node.right, env)
        name: str = type_node.left.name
        total_len = tal.total_len()
        if total_len == 0:  # array with undefined length
            tal = get_tal_of_evaluated_node(node.right, env)
            total_len = tal.total_len()

        ptr = mem.MEMORY.allocate_empty(total_len)
        if r != 0:  # is not undefined
            mem.MEMORY.mem_copy(r, ptr, total_len)

        if node.level == ast.VAR:
            env.define_var(name, tal, ptr)
        elif node.level == ast.CONST:
            env.define_const(name, tal, ptr)
    elif node.left.node_type == ast.DOT:
        dot: ast.Dot = node.left
        l_ptr = evaluate(dot.left, env)
        l_tal = get_tal_of_evaluated_node(dot.left, env)
        # print(l_tal)
        struct: Struct = env.get_struct(l_tal.type_name)
        attr: ast.NameNode = dot.right
        pos_in_struct = struct.get_attr_pos(attr.name)
        attr_tal = struct.get_attr_tal(attr.name)
        attr_len = attr_tal.total_len()
        rv = mem.MEMORY.get(r, attr_len)
        mem.MEMORY.set(l_ptr + pos_in_struct, rv)
    elif node.left.node_type == ast.INDEXING_NODE:
        eval_setitem(node.left, r, env)
    elif node.left.node_type == ast.UNARY_OPERATOR:
        uo: ast.UnaryOperator = node.left
        tal = get_tal_of_evaluated_node(uo, env)
        total_len = tal.total_len()
        l_ptr = evaluate(uo, env)
        rv = mem.MEMORY.get(r, total_len)
        if uo.operation == "pack" and uo.value.node_type == ast.NAME_NODE:
            ri = typ.bytes_to_int(rv)
            env.assign(uo.value.name, ri, lf)
        else:
            mem.MEMORY.set(l_ptr, rv)
    else:
        raise lib.TypeException("Currently unimplemented")
示例#8
0
def eval_name(node: ast.NameNode, env: en.Environment):
    if env.contains_ptr(node.name):
        r = env.get(node.name, (node.line_num, node.file))
    else:
        r = env.get_function(node.name, (node.line_num, node.file))
    return r