def interpret(text):
    lexer = Lexer(text)
    parser = Parser(lexer)
    tree = parser.parse()
    interpreter = Interpreter(tree)
    interpreter.interpret()
    return interpreter.get_var_value
예제 #2
0
파일: run.py 프로젝트: minibox24/berryberry
async def main():
    exe = Execute.ExecuteClass({})

    parse_res = Parser.parse(code)

    execute_res = await exe.execute(parse_res)
    if execute_res:
        print(toString(execute_res))
    def test_throws_exceptions_when_var_not_declared(self):
        program_code = r'''
        PROGRAM NameError1;
        VAR
            a : INTEGER;

        BEGIN
            a := 2 + b;
        END.
        '''

        lexer = Lexer(program_code)
        parser = Parser(lexer)
        tree = parser.parse()
        semantic_analyzer = SemanticAnalyzer()
        with self.assertRaises(Exception) as context:
            semantic_analyzer.visit(tree)
    def test_throws_exceptions_when_duplicate_declaration(self):
        print('run test')
        program_code = r'''
        PROGRAM NameError2;
            VAR a, b : INTEGER;
            VAR b : REAL;

        BEGIN
            a := a + b;
        END.
        '''

        lexer = Lexer(program_code)
        parser = Parser(lexer)
        tree = parser.parse()
        semantic_analyzer = SemanticAnalyzer()
        with self.assertRaises(Exception) as context:
            semantic_analyzer.visit(tree)
    def test_output_1(self):
        program_code = r'''
        PROGRAM Part10;
        VAR
           number     : INTEGER;
           a, b, c, x : INTEGER;
           y          : REAL;
        
        BEGIN {Part10}
           BEGIN
              number := 2;
              a := number;
              b := 10 * a + 10 * number DIV 4;
              c := a - - b
           END;
           x := 11;
           y := 20 / 7 + 3.14;
           { writeln('a = ', a); }
           { writeln('b = ', b); }
           { writeln('c = ', c); }
           { writeln('number = ', number); }
           { writeln('x = ', x); }
           { writeln('y = ', y); }
        END.  {Part10}
        '''

        lexer = Lexer(program_code)
        parser = Parser(lexer)
        tree = parser.parse()
        semantic_analyzer = SemanticAnalyzer()
        semantic_analyzer.visit(tree)
        interpreter = Interpreter(tree)
        interpreter.interpret()
        print(interpreter.get_var_value('a'))
        print(interpreter.get_var_value('b'))
        print(interpreter.get_var_value('c'))
        print(interpreter.get_var_value('number'))
        print(interpreter.get_var_value('x'))
        print(interpreter.get_var_value('y'))
    def test_proc_call_throws_error_when_params_count_mismatch(self):
        program_code = r'''
        program Main;

        procedure Alpha(a : integer; b : integer);
        var x : integer;
        begin
           x := (a + b ) * 2;
        end;

        begin { Main }

           Alpha(7);  { procedure call }

        end.  { Main }
        '''
        lexer = Lexer(program_code)
        parser = Parser(lexer)
        tree = parser.parse()
        semantic_analyzer = SemanticAnalyzer()

        with self.assertRaises(Exception) as context:
            semantic_analyzer.visit(tree)
    def test_proc_call(self):
        program_code = r'''
        program Main;

        procedure Alpha(a : integer; b : integer);
        var x : integer;
        begin
           x := (a + b ) * 2;
        end;

        begin { Main }

           Alpha(3 + 5, 7);  { procedure call }

        end.  { Main }
        '''
        lexer = Lexer(program_code)
        parser = Parser(lexer)
        tree = parser.parse()
        semantic_analyzer = SemanticAnalyzer()
        semantic_analyzer.visit(tree)
        interpreter = Interpreter(tree)
        interpreter.interpret()
예제 #8
0
def init_dialog_manager(max_clients, dialog_manager_port):
    """ Dialog Manager process that listens to messages from the classifier
    :param max_clients: maximum number of clients that can connect to the dialog manager at a time
    :param dialog_manager_port: port the dialogue manager socket is running on
    :return: None
    """
    global socket_conn
    host = socket.gethostname()
    dialog_manager_port = dialog_manager_port
    server_socket = socket.socket()
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
    server_socket.bind((host, dialog_manager_port))
    server_socket.listen(max_clients)

    USER_MEMORY = OrderedDict()
    USER_TREES = OrderedDict()
    USER_SLOTS = OrderedDict()

    while True:
        try:
            socket_conn, address = server_socket.accept()
            data = socket_conn.recv(1024).decode()

            if data:
                message_data = json.loads(data)
                session_id = message_data['sessionid']
                intent_name = message_data['intent']
                slot_data = message_data['slots']
                action_type = message_data['action_type']
                user_message = message_data['message']

                user_intent_slots = USER_SLOTS.get(session_id)
                if slot_data is not None:
                    if user_intent_slots is None:
                        user_intent_slots = slot_data
                        USER_SLOTS[session_id] = user_intent_slots
                    else:
                        user_intent_slots.extend(slot_data)

                if action_type is not None:
                    user_intent_memory = USER_MEMORY.get(session_id)
                    user_intent_tree = USER_TREES.get(session_id)
                    node_id = user_intent_tree[0]
                    tree = user_intent_tree[1]
                    interpreter = Interpreter(tree, user_message,
                                              user_intent_slots,
                                              user_intent_memory, node_id)
                    response_data = interpreter.interpret()
                else:
                    intent = get_intent(intent_name)
                    lexer = Lexer(intent)
                    parser = Parser(lexer)
                    tree = parser.parse()

                    interpreter = Interpreter(tree,
                                              user_message, user_intent_slots,
                                              OrderedDict(), -1)
                    response_data = interpreter.interpret()

                print('')
                print('Run-time GLOBAL_MEMORY contents:')
                for k, v in sorted(interpreter.GLOBAL_MEMORY.items()):
                    print('%s = %s' % (k, v))
                interpreter = None

                if response_data is not None:
                    response_text = response_data.response_text
                    tree = response_data.tree
                    node_id = response_data.node_id
                    global_memory = response_data.global_memory
                    action_type = response_data.action_type

                    USER_MEMORY[session_id] = global_memory
                    USER_TREES[session_id] = (node_id, tree)

                    message_data = {}
                    message_data['sessionid'] = session_id
                    message_data['response_text'] = response_text
                    message_data['action_type'] = action_type
                    message_data['intent'] = intent_name
                    message = json.dumps(message_data)
                    socket_conn.send(message.encode())

                    if action_type == ACTION_EXIT:
                        del USER_MEMORY[session_id]
                        del USER_TREES[session_id]
                        del USER_SLOTS[session_id]
                else:
                    message_data = {}
                    message_data['sessionid'] = session_id
                    message_data['response_text'] = None
                    message_data['action_type'] = None
                    message_data['intent'] = None
                    message = json.dumps(message_data)
                    socket_conn.send(message.encode())

        except KeyboardInterrupt:
            socket_conn.close()

    # intent = get_intent('PieceIntent')
    # lexer = Lexer(intent)
    # parser = Parser(lexer)
    # tree = parser.parse()
    # #
    # # semantic_analyser = SemanticAnalyser()
    # # try:
    # #     semantic_analyser.visit(tree)
    # # except Exception as e:
    # #     print(e)
    # #
    # interpreter = Interpreter(tree, 'test', USER_SLOTS)
    # response_data = interpreter.interpret()

    print('')
    print('Run-time GLOBAL_MEMORY contents:')
    for k, v in sorted(interpreter.GLOBAL_MEMORY.items()):
        print('%s = %s' % (k, v))