Пример #1
0
def var_dec(obj):
    global _venv
    access = [_level, frame.alloc_local(frame.get_frame(_level), [True])]

    t = env.look(_tenv, obj.children[1])
    value = build_IR_tree_exp(obj.children[2])
    if obj.children[1] is None:
        t = value.ty
    elif t is None:
        error(obj.pos,
              "variable type error, no shch type : %s" % (obj.children[1]))
        return []
    elif t != value.ty:
        error(
            obj.pos, "type error, the exprression is %s type not %s type" %
            (value.ty, t))
        return []

    if env.look(_venv, '_var_' + obj.children[0]):
        error(obj.pos, "variable redefined : %s" % (obj.children[0]))
# 保存数组大小
    if (t[0] == 'TY_ARRAY') and (len(t) == 2):
        # t += [obj.children[2].children[1].children[0]]
        t += [build_IR_tree_exp(obj.children[2].children[1])]
    env.enter(_venv, '_var_' + obj.children[0], [access, t])
    return tree.Move(simple_var(obj), value)
Пример #2
0
def fun_dec(obj):
    global _level
    _level += 1
    if env.look(_venv, '_func_' + obj.children[0]):
        error(obj.pos, "function redefined : %s" % (obj.children[0]))

    fun_label = env.new_label()
    formals = para_list(obj.children[1])
    if obj.children[2] is None:
        result_type = 'TY_VOID'
    else:
        result_type = env.look(_tenv, obj.children[2])
    frame.new_level_item(_level, fun_label, form_escape_list(obj.children[1]))
    env.enter(_venv, '_func_' + obj.children[0],
              [_level, fun_label, formals, result_type])

    env.begin_scope(_venv)
    # fieldList = []
    # for item in obj.children[1]:
    # build_IR_tree_dec(item)
    i = 0
    while i < len(obj.children[1]):
        access = [_level, frame.get_frame(_level)['formals'][i]]
        env.enter(
            _venv, '_var_' + obj.children[1][i].children[0],
            [access, env.look(_tenv, obj.children[1][i].children[1])])
        i += 1
    body = build_IR_tree_exp(obj.children[3])
    env.end_scope(_venv)

    frame.add_function(obj.children[0], _level - 1, frame.get_frame(_level),
                       body)
    _level -= 1
    return []
Пример #3
0
def for_exp(obj):
    start = env.new_label()
    loop = env.new_label()
    done = env.new_label()

    env.begin_scope(_venv)
    access = [_level, frame.alloc_local(frame.get_frame(_level), [True])]
    if env.look(_venv, '_var_' + obj.children[0]):
        error(obj.pos, "for's variable redefined : %s" % (obj.children[0]))

    env.enter(_venv, '_var_' + obj.children[0], [access, 'TY_INT'])
    var = frame.expr(access[1], frame.fp())

    e1 = build_IR_tree_exp(obj.children[1])
    e2 = build_IR_tree_exp(obj.children[2])
    e3 = build_IR_tree_exp(obj.children[3])
    if e3.ty != 'TY_VOID':
        error(obj.pos,
              "for's body must be TY_VOID, unsupport %s type" % (e2.ty))

    env.end_scope(_venv)
    return ir_seq_list([
        tree.Move(var, e1),
        tree.Label(start),
        tree.Cjump(['<='], var, e2, tree.Name(loop), tree.Name(done)),
        tree.Label(loop), e3,
        tree.Move(var, tree.Binop('+', var, tree.Const(1))),
        tree.Jump(tree.Name(start), [start]),
        tree.Label(done)
    ])
Пример #4
0
def record_create(obj):
    t = env.look(_tenv, obj.children[0])
    addr = tree.Temp(env.new_temp())

    if not t:
        error(obj.pos, 'no such type : %s' % (obj.children[0]))
        return None
    if t[0] != 'TY_RECORD':
        error(obj.pos, 'type is not record : %s' % (obj.children[0]))
        return None
    size = len(t[1])
    if len(obj.children[1]) != size:
        error(
            obj.pos, 'need %d items, but give %d items in record: %s' %
            (len(obj.children[1]), size, obj.children[0]))

    i = 0
    record = []
    while i < size:
        index = find_record_index(t[1], obj.children[1][i].children[0])
        if index is None:
            error(
                obj.pos,
                "record create error, type %s has no such item : \'%s\'" %
                (obj.children[0], obj.children[1][i].children[0]))
        value = build_IR_tree_exp(obj.children[1][i])
        location = tree.Binop('+', addr, tree.Const(index * frame.WORD_SIZE))
        record.append(tree.Move(tree.Mem(location), value))
        i += 1

    alloc = tree.Call(tree.Name("__recordCreate__"), [size])
    r = [tree.Move(addr, alloc)] + record
    return tree.Eseq(ir_seq_list(r), addr, t)
Пример #5
0
def name_type(obj):
    t = env.look(_tenv, obj.children[0])
    if (t == None):
        error(obj.pos, "type error : %s" % (obj.children[0]))
    else:
        env.pop(_tenv)
        return ['TY_NAME', t]
Пример #6
0
def call_exp(obj):
    fun_entry = env.look(_venv, '_func_' + obj.children[0])
    if not fun_entry:
        error(obj.pos, "call error, no such function: %s" % (obj.children[0]))
        return None

    i = 0
    field = []
    if len(obj.children[1]) == len(fun_entry[2]):
        while i < len(obj.children[1]):
            item = build_IR_tree_exp(obj.children[1][i])
            #类型检查
            field.append(item)
            i += 1
    elif len(obj.children[1]) <= len(fun_entry[2]):
        error(
            obj.pos, "call error, function except more arguments : %s" %
            (obj.children[0]))
    else:
        error(
            obj.pos, "call error, function except less arguments : %s" %
            (obj.children[0]))

    fr = frame.get_frame_by_name(fun_entry[0], fun_entry[1])
    return tree.Call(tree.Name(fun_entry[1]), [fr['formals'], field],
                     fun_entry[3])
Пример #7
0
def array_type(obj):
    t = env.look(_tenv, obj.children[0])
    if t == None:
        error(obj.pos,
              "type error, not exist arrayType : %s" % (obj.children[0]))
    else:
        env.pop(_tenv)
        return ['TY_ARRAY', t]
Пример #8
0
def record_type(obj):
    t = []
    for item in obj.children[0]:
        tt = env.look(_tenv, item.children[1])
        if tt == None:
            error(obj.pos, "type error, record : %s" % (item.children[1]))
        elif tt == 'Null':
            t.append([item.children[0], 'TY_RECORD'])
        else:
            t.append([item.children[0], tt])
    env.pop(_tenv)
    return ['TY_RECORD', t]
Пример #9
0
def record_var(obj):
    t = env.look(_venv, '_var_' + obj.children[0].children[0])
    if t is None:
        error(obj.pos, 'no such variable %s:' % (obj.children[0]))
    elif t[1][0] != 'TY_RECORD':
        error(obj.pos, 'variable is not record : %s' % (obj.children[0]))

    index = find_record_index(t[1][1], obj.children[1])
    if index is None:
        error(obj.pos, "no such record item : \'%s\'" % (obj.children[1]))
    return tree.Mem(
        tree.Binop(
            '+', build_IR_tree_exp(obj.children[0]),
            tree.Binop('*', tree.Const(index), tree.Const(frame.WORD_SIZE))),
        t[1][1][index][1])
Пример #10
0
def array_create(obj):
    t = env.look(_tenv, obj.children[0])
    if not t:
        error(obj.pos, 'no such type : %s' % (obj.children[0]))
        return None
    if t[0] != 'TY_ARRAY':
        error(obj.pos, '%s is not array' % (obj.children[0]))
        return None
    size = build_IR_tree_exp(obj.children[1])
    init = build_IR_tree_exp(obj.children[2])
    if size.ty != 'TY_INT':
        error(obj.pos, 'size of array unsupport %s' % (size.ty))
        return None
    if t[1] != init.ty:
        error(obj.pos, 'need %s not %s' % (t, init.ty))
    return tree.Call(tree.Name("__arrayCreate__"), [size, init], t)
Пример #11
0
def type_dec(obj):
    if (env.look(_tenv, obj.children[0])):
        error(obj.pos, "type error, redefined : %s" % (obj.children[0]))
    env.enter(_tenv, obj.children[0], 'Null')
    env.enter(_tenv, obj.children[0], build_IR_tree_type(obj.children[1]))
    return []
Пример #12
0
def simple_var(obj):
    var = env.look(_venv, '_var_' + obj.children[0])
    if not var:
        error(obj.pos, 'variable not declare : %s' % (obj.children[0]))
        return tree.Const(0, 'TY_INT')
    return frame.expr(var[0][1], frame.fp(), var[1])
Пример #13
0
def para_list(obj):
    r = []
    for item in obj:
        r.append(env.look(_tenv, item.children[1]))
        # r.append([item.children[0], item.children[1]])
    return r