Exemplo n.º 1
0
def parse_dec_body(tokens: TokenStream):

    DEC_LIST = []

    while True:
        dec = parse_declaration(tokens)
        if dec is None:
            break
        DEC_LIST.append(dec)

    token_list_check(tokens, ['begin'])
    tokens.read()

    SMT_LIST = []

    while True:
        smt = parse_statement(tokens)
        if smt is None:
            break
        SMT_LIST.append(smt)

    token_list_check(tokens, ['end'])
    tokens.read()

    return (DEC_LIST, SMT_LIST)
Exemplo n.º 2
0
def parse_type_dec(tokens: TokenStream):
    global scope, table, level
    tokens.read()
    ty = ASTnode(ASTtype.TYPE_DEC)
    ty.DEC = {}

    while True:
        token_list_check(tokens, [TokenType.ID, TokenType.EQUAL])
        id = tokens.read().text
        tokens.read()
        kind = read_type(tokens)
        if kind is None:
            show_error(tokens.peek().row_number, "not valid type declaration~")
        if table_walk(id) >= 0:
            show_error(tokens.peek().row_number, f'symbol {id} redefinition')
        ty.DEC[id] = kind
        p = Sym_type(id, kind)
        table.append(p)
        if tokens != TokenType.COMMA:
            break
        tokens.read()

    token_list_check(tokens, [TokenType.COLON])
    tokens.read()
    return ty
Exemplo n.º 3
0
def parse_procedure_dec(tokens: TokenStream):
    global scope, table, level
    level += 1
    token_list_check(tokens, ['procedure', TokenType.ID, TokenType.LEFT_PAREN])
    proc_blk = ASTnode(ASTtype.PROCEDURE)
    tokens.read()
    proc_blk.PROC_NAME = tokens.read().text
    tokens.read()

    proc_blk.PARA_LIST = parse_para_list(tokens)
    p = Sym_proc(proc_blk.PROC_NAME, None, level, proc_blk.PARA_LIST)
    if table_walk(p.text) >= 0:
        print(f'redef pos {table_walk(p.text)}')
        show_error(tokens.peek().row_number, f'symbol {p.text} redefinition')
    table.append(p)
    scope.append(len(table))

    row = tokens.peek().row_number
    for para in proc_blk.PARA_LIST:
        if level_walk(para[0]) >= 0:
            show_error(row, f'symbol {para[0]} redefinition')
        arg = Sym_vari(para[0], para[1], level, None)
        table.append(arg)

    token_list_check(tokens, [TokenType.RIGHT_PAREN, TokenType.COLON])
    tokens.read()
    tokens.read()

    proc_blk.DEC_LIST, proc_blk.SMT_LIST = parse_dec_body(tokens)
    table.append(scope[-1] - 1)
    del scope[-1]
    level -= 1

    return proc_blk
Exemplo n.º 4
0
def parse_para_list(tokens: TokenStream):
    para_list = []
    if tokens == TokenType.RIGHT_PAREN:
        return para_list

    if tokens == 'var':
        tokens.read()
    kind = read_type(tokens)
    if kind is None:
        show_error(tokens.peek().row_number, "expect type here~")
    varis = parse_id_list(tokens)
    for var in varis:
        para_list.append((var.text, kind))

    while tokens != TokenType.RIGHT_PAREN:
        token_list_check(tokens, [TokenType.COLON])
        tokens.read()
        if tokens == 'var':
            tokens.read()
        kind = read_type(tokens)
        if kind is None:
            show_error(tokens.peek().row_number,
                       "expect at least one argument~")
        varis = parse_id_list(tokens)
        for var in varis:
            para_list.append((var.text, kind))
    return para_list
Exemplo n.º 5
0
def __llparse(t_stream: TokenStream) -> ASTnode:
    global scope, table, level
    token_list_check(t_stream, ['program', TokenType.ID])
    prog = ASTnode(ASTtype.PROGRAM)
    t_stream.read()
    prog.PROG_NAME = t_stream.read().text
    scope.append(0)
    p = Sym_vari(prog.PROG_NAME, None, 0)
    table.append(p)
    level += 1
    prog.DEC_LIST, prog.SMT_LIST = parse_dec_body(t_stream)
    table.append(0)
    return prog
Exemplo n.º 6
0
def read_array_type(tokens: TokenStream):
    token_list_check(tokens, [
        'array', TokenType.LEFT_SQUARE_BRACKET, TokenType.NUM, TokenType.DOT,
        TokenType.DOT, TokenType.NUM, TokenType.RIGHT_SQUARE_BRACKET, 'of',
        ('char', 'integer')
    ])
    ty = ASTnode(ASTtype.ARRAY)
    tokens.read()
    tokens.read()
    ty.LOWER_BOUND = tokens.read()
    tokens.read()
    tokens.read()
    ty.UPPER_BOUND = tokens.read()
    tokens.read()
    tokens.read()
    ty.BASE_TYPE = tokens.read()
    return ty
Exemplo n.º 7
0
def parse_variable_dec(tokens: TokenStream):
    global scope, table, level
    init_pos = tokens.pos
    token_list_check(tokens, ['var'])
    tokens.read()
    kind = read_type(tokens)
    if kind is None:
        tokens.pos = init_pos
        return None
    varis = parse_id_list(tokens)
    row = tokens.peek().row_number
    for v in varis:
        if level_walk(v.text) >= 0:
            show_error(row, f'symbol {v.text} redefinition')
        p = Sym_vari(v.text, kind, level, None)
        table.append(p)
    token_list_check(tokens, [TokenType.COLON])
    tokens.read()
    dec = ASTnode(ASTtype.VARI_DEC)
    dec.VARI = {}

    for vari in varis:
        dec.VARI[(vari.text, vari.row_number)] = kind
    return dec
Exemplo n.º 8
0
def read_record_type(tokens: TokenStream):
    token_list_check(tokens, ['record'])
    tokens.read()
    ty = ASTnode(ASTtype.RECORD)
    ty.VARIABLE = {}

    while tokens != 'end':
        kind = read_type(tokens)
        if kind is None:
            show_error(tokens.peek().row_number,
                       "expect at least one declaration~")
        varis = parse_id_list(tokens)
        token_list_check(tokens, [TokenType.COLON])
        tokens.read()
        for var in varis:
            ty.VARIABLE[var.text] = kind

    token_list_check(tokens, ['end'])
    tokens.read()
    return ty