Exemplo n.º 1
0
def tree(text):
    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    sem_an = SemanticAnalyzer(tree)
    sem_an.analyse()
    return tree
Exemplo n.º 2
0
def test_parser_expr_assign():
    text = 'a: int = (12 + 2) * 2;'
    lexer = Lexer(text)
    parser = Parser(lexer)
    node1 = parser.parse()

    assert isinstance(node1, Block)
    assert len(node1.children) == 2

    node2 = node1.children[0]
    assert isinstance(node2, Assign)
    assert isinstance(node2.name, Variable)
    assert node2.type.type == 'TYPE'  # TYPE Token as type
    assert node2.type.value == 'int'
    assert isinstance(node2.value, BinOp)

    node3 = node2.value
    assert isinstance(node3.left, BinOp)
    assert node3.op.value == '*'
    assert isinstance(node3.right, Number)

    node4 = node3.left
    assert isinstance(node4.left, Number)
    assert node4.op.value == '+'
    assert isinstance(node4.right, Number)
Exemplo n.º 3
0
def test_parser_func_decl3():
    text = """
        function function_name(a: int, b: int) {
            return (b + a);
        }
    """
    lexer = Lexer(text)
    parser = Parser(lexer)
    block = parser.parse()
    func_decl = block.children[0]
    assert isinstance(func_decl, FuncDecl)

    # Params
    params = func_decl.formal_params
    assert isinstance(params, list)
    assert len(params) == 2
    first_param = params[0]
    assert isinstance(first_param, Param)
    assert first_param.var_node.value == 'a'
    assert first_param.type_node.value == 'int'
    second_param = params[1]
    assert isinstance(second_param, Param)
    assert second_param.var_node.value == 'b'
    assert second_param.type_node.value == 'int'

    # Block
    func_block = func_decl.block_node
    assert isinstance(func_block, Block)
Exemplo n.º 4
0
 def testPrintVisitor(self):
     src = Source('./tests/test.minipl')
     scanner = Scanner(src)
     parser = Parser(scanner)
     ast = parser.program()
     pv = PrintVisitor()
     ast.accept(pv)
Exemplo n.º 5
0
 def testTypeCheckVisitor(self):
     return
     src = Source('./tests/test.minipl')
     scanner = Scanner(src)
     parser = Parser(scanner)
     ast = parser.program()
     tc = TypeCheck()
     ast.accept(tc)
Exemplo n.º 6
0
 def __createAstAndVisitor__(self, lines):
     src = MockSource(lines)
     scanner = Scanner(src)
     parser = Parser(scanner)
     ast = parser.program()
     tc = TypeCheck()
     ast.accept(tc)
     iv = InterpretingVisitor(tc.symbolTable)
     return ast, iv
Exemplo n.º 7
0
 def testInterpretingVisitorWithAFile(self, output, input):
     src = Source('./tests/test.minipl')
     scanner = Scanner(src)
     parser = Parser(scanner)
     ast = parser.program()
     tc = TypeCheck()
     ast.accept(tc)
     iv = InterpretingVisitor(tc.symbolTable)
     ast.accept(iv)
     output.assert_called_with('120')
Exemplo n.º 8
0
def test_parser_function_call1():
    text = """
        some_function();
    """
    lexer = Lexer(text)
    parser = Parser(lexer)
    block = parser.parse()
    func_call = block.children[0]
    assert isinstance(func_call, FuncCall)
    assert func_call.params == []
    assert func_call.func_name == 'some_function'
    assert func_call.token.value == 'some_function'
Exemplo n.º 9
0
def interpret(expression):
    try:
        parser = Parser(expression)
        node = parser.parse()
        result = node.traverse()

        namespace.set("_", result)
        return result
    except KeyError as key_error:
        print("Error: name {} is not defined".format(key_error))
    except Exception as exception:
        print("{}: {}".format(type(exception).__name__, str(exception)))
Exemplo n.º 10
0
def test_parser_statement_after_if():
    text = """
        if (a > 2) {
            print('yo');
        }
        b: int = 4; """
    lexer = Lexer(text)
    parser = Parser(lexer)
    main_block = parser.parse()

    if_node = main_block.children[0]
    assert isinstance(if_node, IfStatement)

    assgn_node = main_block.children[1]
    assert isinstance(assgn_node, Assign)
Exemplo n.º 11
0
def main():
    while True:
        try:
            text = input('rafmath: ')
        except (EOFError, KeyboardInterrupt):
            break
        if not text:
            continue

        if (text.lower() == 'exit'):
            break

        try:
            lexer = Lexer(text)
            parser = Parser(lexer)
            interpreter = Interpreter(parser)
            result = interpreter.interpret()

            if isinstance(result, int):
                print(result)
            if isinstance(result, float):
                result *= 1000
                result = int(result)
                result = result / 1000
                print(result)
        except:
            print('ERROR')
            pass
Exemplo n.º 12
0
def test_parser_statement_before_if():
    text = """  
        a: str = "hey";
        if (a == 2) {
            print('yes');
        }
    """
    lexer = Lexer(text)
    parser = Parser(lexer)
    main_block = parser.parse()

    assign_node = main_block.children[0]
    assert isinstance(assign_node, Assign)

    if_node = main_block.children[1]
    assert isinstance(if_node, IfStatement)
Exemplo n.º 13
0
def test_parser_print_bool():
    text = 'print(True);'
    lexer = Lexer(text)
    parser = Parser(lexer)
    node1 = parser.parse()

    # First node should be Block
    assert isinstance(node1, Block)
    assert len(node1.children) == 2  # Print node and Empty node

    node2 = node1.children[0]
    assert isinstance(node2, Print)
    assert len(node2.args) == 1

    node3 = node2.args[0]
    assert isinstance(node3, Boolean)
    assert node3.value == True
Exemplo n.º 14
0
def test_parser_print_str():
    text = 'print("Hello");'
    lexer = Lexer(text)
    parser = Parser(lexer)
    node1 = parser.parse()

    # First node should be Block
    assert isinstance(node1, Block)
    assert len(node1.children) == 2  # Print node and Empty node

    node2 = node1.children[0]
    assert isinstance(node2, Print)
    assert len(node2.args) == 1

    node3 = node2.args[0]
    assert isinstance(node3, String)
    assert node3.value == 'Hello'
Exemplo n.º 15
0
def test_parser_int_assign():
    text = 'a: int = 46;'
    lexer = Lexer(text)
    parser = Parser(lexer)
    node1 = parser.parse()

    assert isinstance(node1, Block)
    assert len(node1.children) == 2

    node2 = node1.children[0]
    assert isinstance(node2, Assign)
    assert isinstance(node2.name, Variable)
    assert node2.type.type == 'TYPE'  # TYPE Token as type
    assert node2.type.value == 'int'
    assert isinstance(node2.value, Number)

    node3 = node2.value
    assert node3.value == 46
Exemplo n.º 16
0
def test_parser_nested_func_call():
    text = """
    function some_function(a: int) {
        b: int = a + 1;
        return (b);
    }    
    print(some_function(2));
    """
    lexer = Lexer(text)
    parser = Parser(lexer)
    block = parser.parse()
    func_decl = block.children[0]
    assert isinstance(func_decl, FuncDecl)
    assert len(func_decl.formal_params) == 1
    assert func_decl.func_name.value == 'some_function'

    print_statement = block.children[1]
    assert isinstance(print_statement, Print)
Exemplo n.º 17
0
def test_parser_str_assign():
    text = 'a: str = "string";'
    lexer = Lexer(text)
    parser = Parser(lexer)
    node1 = parser.parse()

    assert isinstance(node1, Block)
    assert len(node1.children) == 2

    node2 = node1.children[0]
    assert isinstance(node2, Assign)
    assert isinstance(node2.name, Variable)
    assert node2.type.type == 'TYPE'  # TYPE Token as type
    assert node2.type.value == 'str'
    assert isinstance(node2.value, String)

    node3 = node2.value
    assert node3.value == 'string'
Exemplo n.º 18
0
def test_parser_bool_assign():
    text = 'variable: bool = True;'
    lexer = Lexer(text)
    parser = Parser(lexer)
    node1 = parser.parse()

    assert isinstance(node1, Block)
    assert len(node1.children) == 2

    node2 = node1.children[0]
    assert isinstance(node2, Assign)
    assert isinstance(node2.name, Variable)
    assert node2.type.type == 'TYPE'  # TYPE Token as type
    assert node2.type.value == 'bool'
    assert isinstance(node2.value, Boolean)

    node3 = node2.value
    assert node3.value == True
Exemplo n.º 19
0
    def visit_TukuyinAccessNode(self, node):
        func_name = node.func_name
        tukuyin = self.conductor.use_tukuyin(func_name)
        params = tukuyin[0]
        body = tukuyin[1]

        parser = Parser(body, split=False).parse(from_main=False)
        isolated_interpreter = Interpreter(parser, self.conductor)
        isolated_interpreter.interpret()
Exemplo n.º 20
0
def test_parser_if_comparison2():
    text = """
        if (a <= 2) {
            print('yo');
        } """
    lexer = Lexer(text)
    parser = Parser(lexer)
    block = parser.parse()
    if_node = block.children[0]
    assert isinstance(if_node, IfStatement)

    assert isinstance(if_node.value, Comparison)
    assert isinstance(if_node.block, Block)
    assert if_node.elseblock is None

    comp_node = if_node.value
    assert isinstance(comp_node.left, Variable)
    assert comp_node.op.value == '<='
    assert isinstance(comp_node.right, Number)
Exemplo n.º 21
0
def run_interpreter(text):
    result = {}
    result['output'] = []
    with Capturing() as output:
        try:
            lexer = Lexer(text)
            parser = Parser(lexer)
            tree = parser.parse()
            sem_an = SemanticAnalyzer(tree)
            sem_an.analyse()
            interpreter = Interpreter(tree)
            result['final_result'] = interpreter.interpret()
        except Exception as exc:
            result['exception'] = str(exc)

    if output:
        result['output'] = output

    return result
Exemplo n.º 22
0
def evaluate(code: str, tape_size: int = 1000) -> str:
    """
            Helper function which combines the process of lexing,
            syntax analysis, and parsing/evaluation to return either
            the finaly output or an error message.

            Parameters:
                code : str
                    the brainfxck code to be evaluated. 

            Returns:
                str
        """

    # Pass the input code to the lexer, then to the syntax analyser
    # where if the is_faulty parameter is true, then the error messages
    # generated by the analyser method are returned, otherwise the
    # token stream is passed to the parser and the output generated
    # is returned

    # Lexical analysis to generate a token stream
    lexer = Lexer(code)
    token_stream = lexer.lex()

    syntax_analyser = SyntaxAnalyser(token_stream)
    error_dic = syntax_analyser.analyse()
    if error_dic["is_faulty"]:
        # Adding a newline character to the beginning of the error message
        op_string = "\n"
        # Traversing throught the faulty tokens in the token list
        for token in error_dic["faulty_token_list"]:
            op_string += (
                f"Error for {token.op_name} at :: line : {token.line_number} position : {token.posn_number}\n"
            )
        return op_string

    # Parsing the token stream to generate output
    parser = Parser(token_stream, tape_size)
    parser.parse()
    output = parser.output_string
    return output
Exemplo n.º 23
0
def test_parser_function_call_with_params():
    text = """
        some_function(12 + 4, 'string', 78);
    """
    lexer = Lexer(text)
    parser = Parser(lexer)
    block = parser.parse()
    func_call = block.children[0]
    assert isinstance(func_call, FuncCall)
    assert len(func_call.params) == 3
    assert func_call.func_name == 'some_function'
    assert func_call.token.value == 'some_function'

    first_param = func_call.params[0]
    assert isinstance(first_param, BinOp)

    second_param = func_call.params[1]
    assert isinstance(second_param, String)

    third_param = func_call.params[2]
    assert isinstance(third_param, Number)
Exemplo n.º 24
0
def main():
    argparser = argparse.ArgumentParser(
        description='Generate an AST DOT file.')
    argparser.add_argument('fname', help='Pascal source file')
    args = argparser.parse_args()
    fname = args.fname
    text = open(fname, 'r').read()

    lexer = Lexer(text)
    parser = Parser(lexer)
    viz = ASTVisualizer(parser)
    content = viz.gendot()
    print(content)
Exemplo n.º 25
0
def test_parser_if_true():
    text = """
        if (True) {
            print('yo');
        } """
    lexer = Lexer(text)
    parser = Parser(lexer)
    block = parser.parse()
    if_node = block.children[0]
    assert isinstance(if_node, IfStatement)

    assert isinstance(if_node.value, Comparison)
    comparison = if_node.value
    assert comparison.left.value == True
    assert comparison.op is None
    assert comparison.right is None

    assert isinstance(if_node.block, Block)
    assert if_node.elseblock is None

    block_node = if_node.block
    assert isinstance(block_node.children[0], Print)
Exemplo n.º 26
0
def main():
    arg = sys.argv[1]
    
    processtime_in_ms = time.time() * 1000

    with open(arg) as fs:

        # Generate Tokens
        lexer = Lexer(fs.read())
        tokens = lexer.generate_tokens()

        # Generate AST
        parser = Parser(tokens)
        ast = parser.parse()

        # Global conductor instance
        conductor = Conductor()

        # Interpret AST
        interpreter = Interpreter(ast, conductor)
        interpreter.interpret()

        print(f"\nPROCESS TIME: {time.time() * 1000 - processtime_in_ms}ms")
Exemplo n.º 27
0
    def visit_KungNode(self, node):
        expressions = node.expressions
        condition = node.condition
        body = node.body
        exprs = []

        for expression in expressions:
            expr = self.visit(expression)
            exprs.append(expr)

        if condition == TokenTypes.EQUALS:
            if exprs[0] == exprs[1]:
                parser = Parser(body, split=False).parse(from_main=False)
                isolated_interpreter = Interpreter(parser, self.conductor)
                isolated_interpreter.interpret()
Exemplo n.º 28
0
def main():
    while True:
        try:
            text = input('rafmath: ')
        except (EOFError, KeyboardInterrupt):
            break
        if text == 'exit':
            exit('Goodbye')
        if not text:
            continue
        text += ' '
        lexer = Lexer(text)
        parser = Parser(lexer)
        interpreter = Interpreter(parser)
        result = interpreter.interpret()
        print(result)
Exemplo n.º 29
0
def main():
    while True:
        try:
            text = input('>>>RAFMATH ')
        except (EOFError, KeyboardInterrupt):
            break
        if not text:
            continue

        lexer = Lexer(text)
        parser = Parser(lexer)
        interpreter = Interpreter(parser)
        result = interpreter.interpret()
        if result in (True, False):
            print(result)
        elif float(result).is_integer():
            print("%.0f" % result)
        else:
            print("%.3f" % result)
Exemplo n.º 30
0
def interpret(expression):
    """Interpret expression and return the result."""
    lexer = Lexer(expression)
    parser = Parser(lexer)
    interpreter = Interpreter(parser)
    return interpreter.interpret()