def test_lexer(expression, expected_result): lexer = Lexer(expression) if isinstance(expected_result, Error): with pytest.raises(expected_result.__class__, match=re.escape(expected_result.message)): res = lexer.get_all_tokens() else: res = lexer.get_all_tokens() assert expected_result == res
def interpret(text): lexer = Lexer(text) parser = Parser(lexer) tree = parser.parse() interpreter = Interpreter(tree) interpreter.interpret() return interpreter.get_var_value
def part_2(data): total = 0 for line in data: result = Interpreter(Lexer(line)).expr() total += result return total
def test_expressions(expression, expected_result): tree = Parser( Lexer(f"PROGRAM test; var a : real; BEGIN a := {expression} END.") ).parse() SemanticAnalyzer().analyze(tree) interpreter = Interpreter() top_stack_frame = interpreter.run(tree) assert expected_result == top_stack_frame['a']
def test_semantic_analyzer(sources, expected_error): root = Parser(Lexer(sources)).parse() analyzer = SemanticAnalyzer() if expected_error: with pytest.raises(SemanticError, match=expected_error): analyzer.visit(root) else: assert analyzer.visit(root) is None
def main(): input_data = sys.stdin.readlines() intent_data = json.loads(input_data[0]) intent_name = intent_data['name'] intent = get_intent(intent_name) if intent is not '': lexer = Lexer(intent) parser = Parser(lexer) intent_data = parser.parse() else: intent_data = {'global_variables': None, 'nodes': None} print(json.dumps(intent_data))
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_parser_case_insensitive(): sources = ''' PROGRAM Part10; VAR x : INTEGER; y : REAL; PROCEDURE P1(z : REAL; i : integer); VAR A2 : REAL; procedure p2(a : INTEGER); begin a2 := a + 1; end; BEGIN {P1} p2(1); {2} x := x + a2 + z + i; {6} y := x * 2; {12} END; {P1} beGin {Part10} BEGIN x := 1; Y := x / 3; END; P1(1, 2); End. {Part10} ''' tree = Parser(Lexer(sources)).parse() SemanticAnalyzer().analyze(tree) interpreter = Interpreter() top_stack_frame = interpreter.run(tree) assert top_stack_frame['x'] == 6 assert top_stack_frame['y'] == 12
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()
def testExpr9(self): interpreter = Interpreter(Parser(Lexer("(2-7)+6*3"))) self.assertEqual(interpreter.interpret(), 13)
def testExpr7(self): interpreter = Interpreter(Parser(Lexer("100-100+100/10*8"))) self.assertEqual(interpreter.interpret(), 80)
def testExpr6(self): interpreter = Interpreter(Parser(Lexer("100/10*10/10*10+1"))) self.assertEqual(interpreter.interpret(), 101)
def testExpr5(self): interpreter = Interpreter(Parser(Lexer("1+2*3+4*5"))) self.assertEqual(interpreter.interpret(), 27)
def test_interpreter(): assert Interpreter(Lexer("1 + (2 * 3) + (4 * (5 + 6))")).expr() == 51 assert Interpreter(Lexer("2 * 3 + (4 * 5)")).expr() == 46
def testExpr12(self): interpreter = Interpreter(Parser(Lexer("7+3*(10/(12/(3+1)-1))"))) self.assertEqual(interpreter.interpret(), 22)
def test_parser(expression, expected_result): res = Parser(Lexer(expression)).parse() assert str(res) == expected_result
def test_invalid_expression(expression, error): with pytest.raises(Exception, match=re.escape(error)): tree = Parser( Lexer(f"PROGRAM test; BEGIN a := {expression} END.")).parse() Interpreter().run(tree)
def testExpr10(self): interpreter = Interpreter(Parser(Lexer("(2+3)"))) self.assertEqual(interpreter.interpret(), 5)
def testExpr2(self): interpreter = Interpreter(Parser(Lexer("8*10/8+1"))) self.assertEqual(interpreter.interpret(), 11)
def testExpr11(self): interpreter = Interpreter(Parser(Lexer("(2*2)+1"))) self.assertEqual(interpreter.interpret(), 5)
def testExpr3(self): interpreter = Interpreter(Parser(Lexer("8/2+40"))) self.assertEqual(interpreter.interpret(), 44)
def testExpr1(self): interpreter = Interpreter(Parser(Lexer("2+4-6"))) self.assertEqual(interpreter.interpret(), 0)
def testExpr4(self): interpreter = Interpreter(Parser(Lexer("1+2+3+4+5/5"))) self.assertEqual(interpreter.interpret(), 11)
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))
def test_source_to_source_translator(sources, expected_result): root = Parser(Lexer(sources)).parse() translator = SourceToSourceTranslator() result = translator.translate(root) assert result == expected_result