Exemple #1
0
def run_lir_tests():
    """
    Прогоняет тесты линеаризации канонических IR деревьев
    в папке samples/good
    :return:
    """
    sys.setrecursionlimit(1500)

    print("### Тесты LIR ###")
    print()

    if not os.path.exists('../tests/lir_tree'):
        os.mkdir('../tests/lir_tree')

    if not os.path.exists('../tests/lir_tree/good'):
        os.mkdir('../tests/lir_tree/good')

    good_samples = os.listdir('../samples/good')
    for sample in good_samples:
        print(f'Разбираем хорошую программу {sample} ...')
        program = parse_program(Path("../samples/good") / Path(sample))
        print()

        table = Table()
        filler = TableFiller(table, verbose=False)
        filler.fill_table(program)
        table = filler.table

        type_checker = TypeChecker(table)
        type_checker.check_ast_st(program)

        filler.fill_class_struct()
        table = filler.table

        frame_filler = FrameFiller(table, verbose=False)
        frame_filler.fill()

        builder = IRBuilder(table)
        builder.parse(program)
        trees = builder.trees

        canonizer = EseqCanonizer()
        canonized_trees = dict()
        for key, tree in trees.items():
            canonized_tree = canonizer.canonize(tree)
            canonized_trees[key] = canonized_tree

        linearizer = Linearizer()
        linearized = dict()
        for tree_key, tree_value in canonized_trees.items():
            linearized[tree_key] = []
            linearized[tree_key] = linearizer.linearize(
                tree_value, linearized[tree_key])

        printer = IRPrinter(
            Path('../tests/lir_tree/good') /
            Path(sample.replace('.java', '.gv')))
        printer.create_linearized_graph(linearized)
        printer.print_to_file()
        print()
Exemple #2
0
def run_ar_tests():
    """
    Прогоняет тесты записей активаций для всех программ
    в папках samples/good и samples/bad
    :return:
    """
    print("### Тесты AR ###")
    print()

    if not os.path.exists('../tests/st'):
        os.mkdir('../tests/st')

    if not os.path.exists('../tests/st/good'):
        os.mkdir('../tests/st/good')

    good_samples = os.listdir('../samples/good')
    for sample in good_samples:
        print(f'Разбираем хорошую программу {sample} ...')
        program = parse_program(Path('../samples/good') / Path(sample))

        table = Table()
        filler = TableFiller(table, verbose=False)
        filler.fill_table(program)

        frame_filler = FrameFiller(table)
        frame_filler.fill()
        print()
Exemple #3
0
def run_tc_tests():
    """
    Прогоняет тесты проверки типов тайпчекером для всех программ
    в папках samples/good и samples/bad
    :return:
    """
    print("### Тесты TC ###")
    print()

    if not os.path.exists('../tests/st'):
        os.mkdir('../tests/st')

    if not os.path.exists('../tests/st/good'):
        os.mkdir('../tests/st/good')

    good_samples = os.listdir('../samples/good')
    for sample in good_samples:
        print(f'Разбираем хорошую программу {sample} ...')
        program = parse_program(Path('../samples/good') / Path(sample))

        table = Table()
        filler = TableFiller(table)
        filler.fill_table(program)

        type_checker = TypeChecker(table)
        type_checker.check_ast_st(program)
        print()

    if not os.path.exists('../tests/st/bad'):
        os.mkdir('../tests/st/bad')

    bad_samples = os.listdir('../samples/bad')
    for sample in bad_samples:
        print(f'Разбираем плохую программу {sample} ...')
        try:
            program = parse_program(Path('../samples/bad') / Path(sample))

            table = Table()
            filler = TableFiller(table, verbose=False)
            filler.fill_table(program)

            type_checker = TypeChecker(table)
            type_checker.check_ast_st(program)
        except SyntaxError as error:
            print(error)
        print()
Exemple #4
0
def run_ast_tests():
    """
    Прогоняет тесты построения AST для всех программ
    в папках samples/good и samples/bad
    :return:
    """
    print("### Тесты AST ###")
    print()

    if not os.path.exists('../tests/ast'):
        os.mkdir('../tests/ast')

    if not os.path.exists('../tests/ast/good'):
        os.mkdir('../tests/ast/good')

    good_samples = os.listdir('../samples/good')
    for sample in good_samples:
        print(f'Разбираем хорошую программу {sample} ...')
        program = parse_program(Path('../samples/good') / Path(sample))
        printer = Printer(
            Path('../tests/ast/good') / Path(sample.replace('.java', '.gv')))
        printer.visit(program)
        printer.print_to_file()
        print()

    if not os.path.exists('../tests/ast/bad'):
        os.mkdir('../tests/ast/bad')

    bad_samples = os.listdir('../samples/bad')
    for sample in bad_samples:
        print(f'Разбираем плохую программу {sample} ...')
        try:
            program = parse_program(Path('../samples/bad') / Path(sample))
            printer = Printer(
                Path('../tests/ast/bad') /
                Path(sample.replace('.java', '.gv')))
            printer.visit(program)
            printer.print_to_file()
        except SyntaxError as error:
            print(error)
        print()
Exemple #5
0
def run_st_tests():
    """
    Прогоняет тесты получения символьной таблицы для всех программ
    в папках samples/good и samples/bad
    :return:
    """
    print("### Тесты ST ###")
    print()

    if not os.path.exists('../tests/st'):
        os.mkdir('../tests/st')

    if not os.path.exists('../tests/st/good'):
        os.mkdir('../tests/st/good')

    good_samples = os.listdir('../samples/good')
    for sample in good_samples:
        print(f'Разбираем хорошую программу {sample} ...')
        program = parse_program(Path('../samples/good') / Path(sample))

        table = Table()
        filler = TableFiller(table, verbose=True)
        filler.fill_table(program)
        print()

    if not os.path.exists('../tests/st/bad'):
        os.mkdir('../tests/st/bad')

    bad_samples = os.listdir('../samples/bad')
    for sample in bad_samples:
        print(f'Разбираем плохую программу {sample} ...')
        try:
            program = parse_program(Path('../samples/bad') / Path(sample))

            table = Table()
            filler = TableFiller(table, verbose=True)
            filler.fill_table(program)
        except SyntaxError as error:
            print(error)
        print()
Exemple #6
0
def run_ir_tests():
    """
    Прогоняет тесты записей активаций для всех программ
    в папках samples/good и samples/bad
    :return:
    """
    print("### Тесты IR ###")
    print()

    if not os.path.exists('../tests/ir_tree'):
        os.mkdir('../tests/ir_tree')

    if not os.path.exists('../tests/ir_tree/good'):
        os.mkdir('../tests/ir_tree/good')

    good_samples = os.listdir('../samples/good')
    for sample in good_samples:
        print(f'Разбираем хорошую программу {sample} ...')
        program = parse_program(Path("../samples/good") / Path(sample))
        print()

        table = Table()
        filler = TableFiller(table, verbose=False)
        filler.fill_table(program)
        table = filler.table

        type_checker = TypeChecker(table)
        type_checker.check_ast_st(program)

        filler.fill_class_struct()
        table = filler.table

        frame_filler = FrameFiller(table, verbose=False)
        frame_filler.fill()

        builder = IRBuilder(table)
        builder.parse(program)
        trees = builder.trees

        printer = IRPrinter(
            Path('../tests/ir_tree/good') /
            Path(sample.replace('.java', '.gv')))
        printer.create_graph(trees)
        printer.print_to_file()
        print()
Exemple #7
0
from ir_tree.translate.linearizer import Linearizer
from ir_tree.translate.no_jump_block import NoJumpBlocksForest, NoJumpTree
from symbol_table.table import Table
from symbol_table.table_filler import TableFiller
from syntax_tree import Printer
from type_checker.type_checker import TypeChecker
from x86.x86_code_generation import Muncher
from yacc import parse_program

if __name__ == '__main__':
    if not os.path.exists('../tests'):
        os.mkdir('../tests')

    # строим абстрактное синтаксическое дерево
    print('### Построение абстрактного синтаксического дерева ###')
    program = parse_program("../samples/good/Factorial.java")
    print()

    # распечатываем абстрактное синтаксическое дерево
    print('### Печать абстрактного синтаксического дерева ###')
    printer = Printer('../tests/ast_tree.gv')
    printer.visit(program)
    printer.print_to_file()
    print()

    # отображаем символьную таблицу
    print('### Символьная таблица ###')
    table = Table()
    filler = TableFiller(table, verbose=True)
    filler.fill_table(program)
    table = filler.table