Пример #1
0
    def test_file_source(self):

        text = ""
        file_source = TextSource(TEST_SOURCE_1_LINE)
        while not file_source.is_end_of_text():
            text += file_source.read_char()

        self.assertEqual("test Integer 5 Double 1;",
                         text,
                         msg='Error in first line.')
        self.assertEqual(True, file_source.is_end_of_text(),
                         'Error when checking EOF')
Пример #2
0
    def test_file_source_three_lines(self):

        text = ""
        file_source = TextSource(TEST_SOURCE_2_LINES)
        while not file_source.is_end_of_text():
            text += file_source.read_char()

        self.assertEqual("test1 sampleId1\ntest2 sample_2",
                         text,
                         msg='Error in first line.')
        self.assertEqual(True,
                         file_source.is_end_of_text(),
                         msg='Error when checking EOF')
Пример #3
0
    def test_file_source(self):
        from lexer.source_read import TextSource
        textSource = TextSource(
            '../test_files/test_parser_simple_function.txt')

        parser = Parser(64, 256, textSource)

        program = parser.parse()

        program_repr = program.__repr__()

        expected_repr = "Program:\nFunction:\nName:pow\nType:TokenType.K_VOID\nParameters:\nParameter:\nName:x\nType" \
                        ":TokenType.K_DOUBLE\nRefer:True\nInstructions:\nAssign:\nLeft assign " \
                        "operand:\nVariable:\nx\nRight assign operand:\nMul:\nLeft mul operand:\nVariable:\nx\nRight " \
                        "mul operand:\nVariable:\nx"

        self.assertEqual(program_repr, expected_repr)
Пример #4
0
    def test_init_operation(self):
        textSource = TextSource(TEST_SOURCE_1_LINE)

        parser = Parser(64, 256, textSource)

        tree = parser.parse()

        visitor = Visitor(tree)

        program = Interpreter(visitor, lib)

        program.interpret()

        printable = f'Returned {program.return_val}.'

        print(printable)

        self.assertEqual(printable, 'Returned 1296.')
Пример #5
0
 def setUp(self) -> None:
     self.parser = Parser(64, 256, TextSource(TEST_SOURCE_1_LINE))
Пример #6
0
    def setUp(self) -> None:

        self.lexer = LexerMain(maxIdentLength=64,
                               maxStringLength=256,
                               textSource=TextSource(TEST_SOURCE_1_LINE))
Пример #7
0
# Parses runtime arguments, like type of file input and file path
# (if text input is selected to be a file) and runs appropiate
# Lexer text input handlers to run the process of tokenization.

from argparse import ArgumentParser

from my_parser.parser import Parser
from objbrowser import browse

from lexer.source_read import TextSource

if __name__ == '__main__':
    arg_parser = ArgumentParser()

    arg_parser.add_argument('--file_path',
                            type=str,
                            default='../test_files/test_interpreter_code.txt')
    arg_parser.add_argument('--ident_length', type=int, default=64)
    arg_parser.add_argument('--string_length', type=int, default=256)

    args = arg_parser.parse_args()

    textSource = TextSource(args.file_path)

    parser = Parser(args.ident_length, args.string_length, textSource)

    program = parser.parse()

    browse(program)