コード例 #1
0
 def test_update(self):
     st = SymbolTable()
     self.assertRaises(AssertionError, st.update, "a", 1)
     st.store("a", Expression(Type.int(), 1))
     st.update("a", 2)
     self.assertEqual(Type.int(), st.get_expression("a").type)
     self.assertEqual(2, st.get_expression("a").value)
コード例 #2
0
 def test_get_expression(self):
     st = SymbolTable()
     self.assertRaises(AssertionError, st.get_expression, "a")
     st.store("a", Expression(Type.list_of(Type.int()), [1]))
     self.assertEqual([1], st.get_expression("a").value)
     self.assertEqual(1, st.get_expression("a").value[0])
     self.assertEqual(Type.list_of(Type.int()), st.get_expression("a").type)
コード例 #3
0
 def test_store(self):
     st = SymbolTable()
     st.store("a", Expression(Type.int(), 1))
     self.assertTrue("a" in st)
     self.assertEqual(Type.int(), st.get_expression("a").type)
     self.assertEqual(1, st.get_expression("a").value)
     self.assertRaises(AssertionError, st.store, "a",
                       Expression(Type.int(), 1))
コード例 #4
0
 def test_declare_drone_should_change_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () { drone a; }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.drone(), NullDrone(), ident="a"))
     self.assertEqual(expected, actual)
コード例 #5
0
 def test_str(self):
     st = SymbolTable()
     st.store("a", Expression(Type.int(), 0))
     st.store("b", Expression(Type.list_of(Type.boolean()), [True]))
     expected = "SymbolTable: {\n" + \
                "  a -> Expression: { type: int, value: 0, ident: None }\n" + \
                "  b -> Expression: { type: list[boolean], value: [True], ident: None }\n" + \
                "}"
     self.assertEqual(expected, str(st))
コード例 #6
0
 def test_declare_and_assign_decimal_should_change_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () { decimal a <- 1.0; }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.decimal(), 1.0, ident="a"))
     self.assertEqual(expected, actual)
コード例 #7
0
 def test_declare_and_assign_vector_should_change_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () { vector a <- (1.0, 2.0, -3.0); }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.vector(), [1, 2, -3], ident="a"))
     self.assertEqual(expected, actual)
コード例 #8
0
 def test_declare_and_assign_boolean_should_change_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () { boolean a <- true; }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.boolean(), True, ident="a"))
     self.assertEqual(expected, actual)
コード例 #9
0
 def test_declare_and_assign_string_should_change_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () { string a <- "\0a"; }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.string(), "\0a", ident="a"))
     self.assertEqual(expected, actual)
コード例 #10
0
 def test_declare_list_should_change_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () { list[int] a; }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.list_of(Type.int()), [],
                                    ident="a"))
     self.assertEqual(expected, actual)
コード例 #11
0
    def test_eq(self):
        st1 = SymbolTable()
        st2 = SymbolTable()
        self.assertTrue(st1 == st2)
        st1.store("a", Expression(Type.int(), 0))
        self.assertFalse(st1 == st2)
        st2.store("a", Expression(Type.int(), 0))
        self.assertTrue(st1 == st2)

        self.assertNotEqual(SymbolTable(), None)
コード例 #12
0
 def test_assign_ident_vector_should_update_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () {
          vector a;
          a <- (1.0, -1.0, +1.0);
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.vector(), [1, -1, 1], ident="a"))
     self.assertEqual(expected, actual)
コード例 #13
0
 def test_assign_ident_string_should_update_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () {
          string a;
          a <- "1";
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.string(), "1", ident="a"))
     self.assertEqual(expected, actual)
コード例 #14
0
 def test_assign_ident_decimal_should_update_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () {
          decimal a;
          a <- -1.5e10;
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.decimal(), -1.5e10, ident="a"))
     self.assertEqual(expected, actual)
コード例 #15
0
 def test_vector_with_int_should_return_correct_value(self):
     actual = SymbolTable()
     generate_commands("""
         main () {
           vector a <- (1, 2.0, -3);
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store("a",
                    Expression(Type.vector(), [1.0, 2.0, -3.0], ident="a"))
     self.assertEqual(expected, actual)
コード例 #16
0
 def test_list_insert_to_empty_list_should_update_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () {
           list[int] a <- [];
           a.insert(0);
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.list_of(Type.int()), [0],
                                    ident="a"))
     self.assertEqual(expected, actual)
コード例 #17
0
 def test_assign_ident_list_should_update_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () {
          list[int] a;
          a <- [1, -1, +1];
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store(
         "a", Expression(Type.list_of(Type.int()), [1, -1, 1], ident="a"))
     self.assertEqual(expected, actual)
コード例 #18
0
 def test_list_remove_without_index_should_update_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () {
           list[int] a <- [1, 2, 3];
           a.remove();
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store("a",
                    Expression(Type.list_of(Type.int()), [1, 2], ident="a"))
     self.assertEqual(expected, actual)
コード例 #19
0
 def test_declare_with_different_variable_name_should_change_symbol_table(
         self):
     for name in [
             "a", "A", "_a", "_1", "_A", "abc", "Abc", "a12", "aA1", "_aA1"
     ]:
         actual = SymbolTable()
         generate_commands("""
             main () {{ int {}; }}
             """.format(name),
                           symbol_table=actual)
         expected = SymbolTable()
         expected.store(name, Expression(Type.int(), 0, ident=name))
         self.assertEqual(expected, actual)
コード例 #20
0
 def test_list_elem_assign_should_update_symbol_table_correctly(self):
     actual = SymbolTable()
     generate_commands("""
         main () {
           list[int] a <- [1, 2, 3];
           a[0] <- 4;
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store(
         "a", Expression(Type.list_of(Type.int()), [4, 2, 3], ident="a"))
     self.assertEqual(expected, actual)
コード例 #21
0
 def test_declare_and_assign_drone_should_change_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () { drone a <- DRONE1; }
         """,
                       drone_config_map={"DRONE1": DefaultDroneConfig()},
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store(
         "a",
         Expression(Type.drone(),
                    Drone("DRONE1", DefaultDroneConfig()),
                    ident="a"))
     self.assertEqual(expected, actual)
コード例 #22
0
 def test_assign_list_elem_to_variable_should_update_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () {
          list[int] a <- [0, 1, 2];
          a[0] <- 1;
          a[2] <- a[0];
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store(
         "a", Expression(Type.list_of(Type.int()), [1, 1, 1], ident="a"))
     self.assertEqual(expected, actual)
コード例 #23
0
 def test_list_remove_from_nested_list_should_update_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () {
           list[list[int]] a <- [[1], [2], [3]];
           a[0].remove();
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store(
         "a",
         Expression(Type.list_of(Type.list_of(Type.int())), [[], [2], [3]],
                    ident="a"))
     self.assertEqual(expected, actual)
コード例 #24
0
 def test_list_should_return_correct_value(self):
     actual = SymbolTable()
     generate_commands("""
         main () {
           list[int] a <- [];
           list[boolean] b <- [true, false];
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.list_of(Type.int()), [],
                                    ident="a"))
     expected.store(
         "b",
         Expression(Type.list_of(Type.boolean()), [True, False], ident="b"))
     self.assertEqual(expected, actual)
コード例 #25
0
 def test_list_remove_with_index_should_update_symbol_table(self):
     for index in [0, 1, 2]:
         actual = SymbolTable()
         generate_commands("""
             main () {{
               list[int] a <- [1, 2, 3];
               a.at({}).remove();
             }}
             """.format(index),
                           symbol_table=actual)
         expected = SymbolTable()
         list = [1, 2, 3]
         list.pop(index)
         expected.store(
             "a", Expression(Type.list_of(Type.int()), list, ident="a"))
         self.assertEqual(expected, actual)
コード例 #26
0
 def test_vector_elem_assign_with_int_should_update_symbol_table_correctly(
         self):
     actual = SymbolTable()
     generate_commands("""
         main () {
           vector a <- (10, 20, -30);
           a.x <- 1;
           a.y <- 2;
           a.z <- -3;
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     expected.store("a",
                    Expression(Type.vector(), [1.0, 2.0, -3.0], ident="a"))
     self.assertEqual(expected, actual)
コード例 #27
0
 def test_repeat_should_keep_updates_but_discard_new_variables(self):
     actual = SymbolTable()
     actual_commands = generate_commands("""
         main () {
           int a <- 0;
           repeat 5 times {
             int b <- 1;
             a <- a + 1;
           }
         }
         """, symbol_table=actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.int(), 5, ident="a"))
     self.assertEqual(expected, actual)
     expected_commands = []
     self.assertEqual(expected_commands, actual_commands)
コード例 #28
0
 def test_if_with_else_should_keep_updates_but_discard_new_variables(self):
     actual = SymbolTable()
     actual_commands = generate_commands("""
         main () {
           int a <- 1;
           if false {
           } else {
             int b <- 1;
             a <- 2;
           }
         }
         """, symbol_table=actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.int(), 2, ident="a"))
     self.assertEqual(expected, actual)
     expected_commands = []
     self.assertEqual(expected_commands, actual_commands)
 def test_return_correct_type_in_function_should_return_correct_value(self):
     types = [Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(),
              Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int()))]
     for type in types:
         actual = SymbolTable()
         generate_commands("""
             function func() return {} {{
               {} a;
               return a;
             }}
             main () {{
               {} a <- func();
             }}
             """.format(type, type, type), symbol_table=actual)
         expected = SymbolTable()
         expected.store("a", Expression(type, type.default_value, ident="a"))
         self.assertEqual(expected, actual)
 def test_scope(self):
     actual = SymbolTable()
     generate_commands("""
         procedure proc(int i) {
           int j <- 100;
           while i < 10 {
             i <- i + 1;
           }
         }
         main () {
           int i <- 1;
           proc(i);
         }
         """, symbol_table=actual)
     expected = SymbolTable()
     expected.store("i", Expression(Type.int(), 1, ident="i"))
     self.assertEqual(expected, actual)