def test_var_statement_array_expected_integer_size_of_array(self): tokens_list = [ Token("var", "", 1), Token("id", 1, 1), Token("left_bracket", "", 1), Token("number", 2, 1), Token("right_bracket", "", 1), Token("assignment", "", 1), Token("left_brace", "", 1), Token("number", 3, 1), Token("comma", "", 1), ] table = SymbolTable() table.entry("a", "var", "variable") table.entry("3.14", "float", "constant") self.__suppress_print() with self.assertRaises(SystemExit): _ = var_statement(tokens=tokens_list, i=1, table=table, func_ret_type={}) self.__release_print()
def test_var_statement_array_assign_no_error(self): tokens_list = [ Token("var", "", 1), Token("id", 1, 1), Token("left_bracket", "", 1), Token("number", 2, 1), Token("right_bracket", "", 1), Token("assignment", "", 1), Token("left_brace", "", 1), Token("number", 3, 1), Token("comma", "", 1), Token("number", 4, 1), Token("right_brace", "", 1), Token("newline", "", 1), ] table = SymbolTable() table.entry("a", "var", "variable") table.entry("2", "int", "constant") table.entry("1", "int", "constant") table.entry("2", "int", "constant") opcode, _, _ = var_statement(tokens=tokens_list, i=1, table=table, func_ret_type={}) self.assertEqual(opcode, OpCode("array_assign", "a---2---{1,2}", "int"))
def test_get_by_id_not_present(self): symbol_table = SymbolTable() _ = symbol_table.entry(value="my_var", type="int", typedata="variable") _ = symbol_table.entry(value="my_var_1", type="float", typedata="variable") entry = symbol_table.get_by_id(10) self.assertEqual(entry, [None, None, None])
def test_get_by_symbol_not_present(self): symbol_table = SymbolTable() _ = symbol_table.entry(value="my_var", type="int", typedata="variable") _ = symbol_table.entry(value="my_var_1", type="float", typedata="variable") id = symbol_table.get_by_symbol("my_variable") self.assertEqual(id, -1)
def test_for_statement_missing_to_keyword(self): tokens_list = [ Token("for", "", 1), Token("id", 1, 1), Token("in", "", 1), Token("number", 2, 1), Token("print", "", 1), ] table = SymbolTable() table.entry("a", "int", "variable") table.entry("1", "int", "variable") self.__test_error_case(tokens_list, table=table)
def test_array_initializer_cannot_find_type(self): tokens_list = [ Token("assignment", "", 1), Token("left_brace", "", 1), Token("number", 1, 1), Token("comma", "", 1), Token("number", 2, 1), Token("right_brace", "", 1), ] table = SymbolTable() table.entry("1", "var", "variable") table.entry("3.14", "var", "variable") self.__test_error_case(tokens_list, table)
def test_exit_no_error(self): tokens_list = [ Token("exit", "", 1), Token("left_paren", "", 1), Token("number", 1, 1), Token("right_paren", "", 1), ] table = SymbolTable() table.entry("0", "int", "constant") opcode, _, _ = exit_statement( tokens=tokens_list, i=1, table=table, func_ret_type={} ) self.assertEqual(opcode, OpCode("exit", "0", None))
def test_print_statement_no_error(self): tokens_list = [ Token("print", "", 1), Token("left_paren", "", 1), Token("string", 1, 1), Token("right_paren", "", 1), Token("newline", "", 1), ] table = SymbolTable() table.entry('"hello world"', "string", "constant") opcode, _, _ = print_statement( tokens=tokens_list, i=1, table=table, func_ret_type={}, num_opcodes=-1 ) self.assertEqual(opcode, OpCode("print", '"hello world"', None))
def test_entry(self): symbol_table = SymbolTable() id = symbol_table.entry(value="my_var", type="int", typedata="variable") self.assertEqual(id, 1) self.assertEqual(symbol_table.symbol_table, {1: ["my_var", "int", "variable"]})
def test_case_statement_missing_colon(self): tokens_list = [ Token("case", "", 1), Token("number", "", 1), Token("print", "", 1), ] table = SymbolTable() table.entry("1", "int", "variable") self.__suppress_print() with self.assertRaises(SystemExit): _, _, _ = case_statement( tokens=tokens_list, i=1, table=table, func_ret_type={} ) self.__release_print()
def test_case_statement_no_error(self): tokens_list = [ Token("case", "", 1), Token("number", 1, 1), Token("colon", "", 1), Token("newline", "", 1), Token("left_brace", "", 1), Token("print", "", 1), ] table = SymbolTable() table.entry("1", "bool", "variable") opcode, _, _ = case_statement( tokens=tokens_list, i=1, table=table, func_ret_type={} ) self.assertEqual(opcode, OpCode("case", "1", ""))
def test_assign_statement_variable_used_before_declaration(self): tokens_list = [ Token("id", 1, 1), Token("assignment", "", 1), Token("number", 2, 1), Token("newline", "", 1), ] table = SymbolTable() table.entry("a", "var", "variable") table.entry("2", "int", "constant") self.__suppress_print() with self.assertRaises(SystemExit): _ = parse(tokens_list, table) self.__release_print()
def test_array_initializer_number_of_entries_do_not_match(self): tokens_list = [ Token("assignment", "", 1), Token("left_brace", "", 1), Token("number", 1, 1), Token("comma", "", 1), Token("number", 2, 1), Token("comma", "", 1), Token("number", 2, 1), Token("right_brace", "", 1), ] table = SymbolTable() table.entry("1", "int", "variable") table.entry("3", "int", "variable") table.entry("4", "int", "variable") self.__test_error_case(tokens_list, table)
def test_struct_declaration_statement_expected_left_brace(self): tokens_list = [ Token("struct", "", 1), Token("id", 1, 1), Token("print", "", 1) ] table = SymbolTable() table.entry("my_struct", "var", "variable") self.__suppress_print() with self.assertRaises(SystemExit): _ = struct_declaration_statement(tokens=tokens_list, i=1, table=table) self.__release_print()
def test_var_statement_array_no_assign(self): tokens_list = [ Token("var", "", 1), Token("id", 1, 1), Token("left_bracket", "", 1), Token("number", 2, 1), Token("right_bracket", "", 1), ] table = SymbolTable() table.entry("a", "var", "variable") table.entry("2", "int", "constant") opcode, _, _ = var_statement(tokens=tokens_list, i=1, table=table, func_ret_type={}) self.assertEqual(opcode, OpCode('array_no_assign', 'a---2', None))
def test_if_statement_no_error(self): tokens_list = [ Token("if", "", 1), Token("left_paren", "", 1), Token("number", 1, 1), Token("right_paren", "", 1), Token("call_end", "", 1), Token("newline", "", 1), Token("print", "", 1), ] table = SymbolTable() table.entry("true", "bool", "variable") opcode, _, _ = if_statement( tokens=tokens_list, i=1, table=table, func_ret_type={} ) self.assertEqual(opcode, OpCode("if", "true"))
def test_for_statement_missing_missing_change_value(self): tokens_list = [ Token("for", "", 1), Token("id", 1, 1), Token("in", "", 1), Token("number", 2, 1), Token("to", "", 1), Token("number", 3, 1), Token("by", "", 1), Token("plus", "", 1), Token("print", "", 1), ] table = SymbolTable() table.entry("a", "int", "variable") table.entry("1", "int", "variable") table.entry("10", "int", "variable") self.__test_error_case(tokens_list, table=table)
def test_parse_empty_function_body_error(self): tokens_list = [ Token("fun", "", 1), Token("id", 1, 1), Token("left_paren", "", 1), Token("right_paren", "", 1), Token("newline", "", 1), Token("newline", "", 1), Token("MAIN", "", 2), ] table = SymbolTable() table.entry("func", "var", "variable") self.__suppress_print() with self.assertRaises(SystemExit): _ = parse(tokens=tokens_list, table=table) self.__release_print()
def test_while_statement_no_error_do_while(self): tokens_list = [ Token("while", "", 1), Token("left_paren", "", 1), Token("id", 1, 1), Token("right_paren", "", 1), Token("newline", "", 1), Token("print", "", 2), ] table = SymbolTable() table.entry("true", "bool", "variable") opcode, _, _ = while_statement(tokens=tokens_list, i=1, table=table, in_do=True, func_ret_type={}) self.assertEqual(opcode, OpCode("while_do", "true", None))
def test_while_statement_missing_right_paren(self): tokens_list = [ Token("while", "", 1), Token("left_paren", "", 1), Token("id", 1, 1), Token("print", "", 1), ] table = SymbolTable() table.entry("true", "bool", "variable") self.__suppress_print() with self.assertRaises(SystemExit): _, _, _ = while_statement(tokens=tokens_list, i=1, table=table, in_do=False, func_ret_type={}) self.__release_print()
def test_var_statement_array_size_of_array_needs_to_be_known_if_no_assign( self): tokens_list = [ Token("var", "", 1), Token("id", 1, 1), Token("left_bracket", "", 1), Token("right_bracket", "", 1), ] table = SymbolTable() table.entry("a", "var", "variable") table.entry("3", "int", "constant") self.__suppress_print() with self.assertRaises(SystemExit): _ = var_statement(tokens=tokens_list, i=1, table=table, func_ret_type={}) self.__release_print()
def test_array_initializer_no_errors(self): tokens_list = [ Token("assignment", "", 1), Token("left_brace", "", 1), Token("number", 1, 1), Token("comma", "", 1), Token("number", 2, 1), Token("right_brace", "", 1), ] table = SymbolTable() table.entry("1", "int", "variable") table.entry("2", "int", "variable") op_value, op_type, i = array_initializer(tokens=tokens_list, i=1, table=table, size_of_array=2, msg="Test message") self.assertEqual(op_value, "{1,2") self.assertEqual(op_type, 3)
def test_var_statement_array_invalid_syntax_for_declaration(self): tokens_list = [ Token("var", "", 1), Token("id", 1, 1), Token("left_bracket", "", 1), Token("number", 2, 1), Token("right_bracket", "", 1), Token("plus_equal", "", 1), Token("newline", "", 1), ] table = SymbolTable() table.entry("a", "var", "variable") table.entry("2", "int", "constant") self.__suppress_print() with self.assertRaises(SystemExit): _ = var_statement(tokens=tokens_list, i=1, table=table, func_ret_type={}) self.__release_print()
def test_structure_declaration_no_error(self): tokens_list = [ Token("struct", "", 1), Token("id", 1, 1), Token("left_brace", "", 1), Token("newline", "", 1), Token("var", "", 2), Token("id", 2, 2), Token("assignment", "", 2), Token("number", 3, 2), Token("newline", "", 2), Token("right_brace", "", 3), Token("newline", "", 3), ] table = SymbolTable() table.entry("my_struct", "var", "variable") table.entry("a", "int", "variable") table.entry("a", "int", "constant") opcode, _, _ = struct_declaration_statement(tokens=tokens_list, i=1, table=table) self.assertEqual(opcode, OpCode("struct_decl", "my_struct", ""))
class TestSymbolTableClass(unittest.TestCase): def setUp(self): self.table = SymbolTable() def test_entry(self): id_1 = self.table.entry("hello", "int", "variable") id_2 = self.table.entry("world", "float", "constant") self.assertEqual(id_1, 1) self.assertEqual(id_2, 2) self.assertEqual( self.table.symbol_table, { 1: ["hello", "int", "variable"], 2: ["world", "float", "constant"] }, ) def test_get_by_id(self): id_1 = self.table.entry("hello", "int", "variable") id_2 = self.table.entry("world", "float", "constant") value, type, typedata = self.table.get_by_id(2) self.assertEqual(value, "world") self.assertEqual(type, "float") self.assertEqual(typedata, "constant") value, type, typedata = self.table.get_by_id(10) self.assertEqual(value, None) self.assertEqual(type, None) self.assertEqual(typedata, None) def test_get_symbol(self): id_1 = self.table.entry("hello", "int", "variable") id_2 = self.table.entry("world", "float", "constant") id = self.table.get_by_symbol("world") self.assertEqual(id, 2) id = self.table.get_by_symbol("something") self.assertEqual(id, -1)
def test_for_statement_no_errors(self): tokens_list = [ Token("for", "", 1), Token("id", 1, 1), Token("in", "", 1), Token("number", 2, 1), Token("to", "", 1), Token("number", 3, 1), Token("by", "", 1), Token("plus", "", 1), Token("number", 4, 1), ] table = SymbolTable() table.entry("a", "int", "variable") table.entry("1", "int", "variable") table.entry("10", "int", "variable") table.entry("2", "int", "variable") opcode, _, _ = for_statement(tokens=tokens_list, i=1, table=table, func_ret_type={}) self.assertEqual(opcode, OpCode("for", "a&&&1&&&10&&&+&&&<&&&2", None))