Esempio n. 1
0
    def __init__(self, *, loop=None, **options):
        super().__init__(loop=loop, **options)

        create_log_folder()
        self.logger = logging.getLogger("DiceBot")
        self.init_logging()

        self.lexer = Lexer()
        self.interpreter = Interpreter(self.lexer)
Esempio n. 2
0
def test_lexer_var_names():
    var_names = ['a', 'Yo', 'variable', 'some_var', 'WhatEver']
    for name in var_names:
        lexer = Lexer(name)

        next_token = lexer.get_next_token()
        assert next_token is not None
        assert isinstance(next_token, Token)
        assert next_token.type == 'NAME'
        assert next_token.value == name
Esempio n. 3
0
def test_lexer_comments():
    text = """
        /*
        I am a comment
        */
        a: int = 1;
    """
    lexer = Lexer(text)

    token = lexer.get_next_token()
    assert isinstance(token, Token)
    assert token.type == 'NAME'
    assert token.value == 'a'
Esempio n. 4
0
def test_lexer_integers2():
    text = '87'
    lexer = Lexer(text)

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'INTEGER'
    assert next_token.value == 87

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'EOF'
    assert next_token.value == None
Esempio n. 5
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)
Esempio n. 6
0
def test_lexer_builtin_names_type():
    reserved_names = {
        'str': 'TYPE',
        'int': 'TYPE',
        'bool': 'TYPE',
        'float': 'TYPE'
    }

    for key, val in reserved_names.items():
        lexer = Lexer(key)

        next_token = lexer.get_next_token()
        assert next_token is not None
        assert isinstance(next_token, Token)
        assert next_token.type == val
        assert next_token.value == key
Esempio n. 7
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)
Esempio n. 8
0
def tree(text):
    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    sem_an = SemanticAnalyzer(tree)
    sem_an.analyse()
    return tree
Esempio n. 9
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
Esempio n. 10
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
Esempio n. 11
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'
Esempio n. 12
0
def test_lexer_int_decl():
    text = 'var: int = 45;'
    lexer = Lexer(text)

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'NAME'
    assert next_token.value == 'var'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'COLON'
    assert next_token.value == ':'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'TYPE'
    assert next_token.value == 'int'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'EQUAL'
    assert next_token.value == '='

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'INTEGER'
    assert next_token.value == 45

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'SCOLON'
    assert next_token.value == ';'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'EOF'
    assert next_token.value == None
Esempio n. 13
0
def test_lexer_str_decl():
    text = 'var: str = "I am s string";'
    lexer = Lexer(text)

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'NAME'
    assert next_token.value == 'var'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'COLON'
    assert next_token.value == ':'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'TYPE'
    assert next_token.value == 'str'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'EQUAL'
    assert next_token.value == '='

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'STRING'
    assert next_token.value == 'I am s string'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'SCOLON'
    assert next_token.value == ';'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'EOF'
    assert next_token.value == None
Esempio n. 14
0
def test_lexer_float_decl():
    text = 'var: float = 1.56;'
    lexer = Lexer(text)

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'NAME'
    assert next_token.value == 'var'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'COLON'
    assert next_token.value == ':'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'TYPE'
    assert next_token.value == 'float'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'EQUAL'
    assert next_token.value == '='

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'FLOAT'
    assert next_token.value == 1.56

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'SCOLON'
    assert next_token.value == ';'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'EOF'
    assert next_token.value == None
Esempio n. 15
0
def test_lexer_bool_decl():
    text = 'var: bool = True;'
    lexer = Lexer(text)

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'NAME'
    assert next_token.value == 'var'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'COLON'
    assert next_token.value == ':'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'TYPE'
    assert next_token.value == 'bool'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'EQUAL'
    assert next_token.value == '='

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'BOOL'
    assert next_token.value == True

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'SCOLON'
    assert next_token.value == ';'

    next_token = lexer.get_next_token()
    assert next_token is not None
    assert isinstance(next_token, Token)
    assert next_token.type == 'EOF'
    assert next_token.value == None
Esempio n. 16
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)
Esempio n. 17
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")
Esempio n. 18
0
def main():
    argparser = argparse.ArgumentParser(
        description='Generate a Parse Tree DOT file.')
    argparser.add_argument(
        'text', help='Arithmetic expression (in quotes): "1 + 2 * 3"')
    args = argparser.parse_args()
    text = args.text

    lexer = Lexer(text)
    parser = Parser(lexer)

    viz = ParseTreeVisualizer(parser)
    content = viz.gendot()
    print(content)
Esempio n. 19
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)
Esempio n. 20
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)
Esempio n. 21
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)
Esempio n. 22
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'
Esempio n. 23
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
Esempio n. 24
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'
Esempio n. 25
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
Esempio n. 26
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)
Esempio n. 27
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
Esempio n. 28
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
Esempio 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)
Esempio n. 30
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)