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)
 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)
 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)
 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)
 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)
 def test_list_remove_from_temp_list_should_not_update_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () {
           [1].remove();
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     self.assertEqual(expected, actual)
 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)
 def test_assign_vector_elem_to_temp_vector_should_not_change_symbol_table(
         self):
     actual = SymbolTable()
     generate_commands("""
         main () {
          (0.0, 0.0, 0.0).x <- 1.0;
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     self.assertEqual(expected, actual)
 def test_list_insert_to_temp_empty_list_should_not_update_symbol_table(
         self):
     actual = SymbolTable()
     generate_commands("""
         main () {
           [].insert(0);
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     self.assertEqual(expected, actual)
 def test_del_should_delete_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () {
          int a;
          del a;
         }
         """,
                       symbol_table=actual)
     expected = SymbolTable()
     self.assertEqual(expected, actual)
 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)
 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)
 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)
 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)
 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)
Ejemplo n.º 16
0
 def test_if_false_without_else_should_do_nothing(self):
     actual = SymbolTable()
     actual_commands = generate_commands("""
         main () {
           if false {
             int a <- 1;
           }
         }
         """, symbol_table=actual)
     expected = SymbolTable()
     self.assertEqual(expected, actual)
     expected_commands = []
     self.assertEqual(expected_commands, actual_commands)
 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)
 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)
 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)
 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)
Ejemplo n.º 21
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)
Ejemplo n.º 22
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)
 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)
 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)
 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)
 def test_assign_list_elem_to_temp_vector_should_not_change_symbol_table(
         self):
     for code in [
             "[0.0][0] <- 1.0", "[0][0] <- 1", "[[\"a\"]][0] <- [\"b\"]",
             "[0.0, 1.0][0] <- 1.0"
     ]:
         actual = SymbolTable()
         generate_commands("""
             main () {{
              {};
             }}
             """.format(code),
                           symbol_table=actual)
         expected = SymbolTable()
         self.assertEqual(expected, actual)
Ejemplo n.º 27
0
def generate_commands(program,
                      drone_config_map: Dict[str, DroneConfig] = None,
                      boundary_checker: BoundaryChecker = None,
                      collision_checker: CollisionChecker = None,
                      has_checks: bool = True,
                      symbol_table: SymbolTable = None,
                      function_table: FunctionTable = None):
    if drone_config_map is None:
        drone_config_map = {"DEFAULT": DefaultDroneConfig()}
    if boundary_checker is None:
        boundary_checker = BoundaryChecker(BoundaryConfig.no_limit())
    if collision_checker is None:
        collision_checker = CollisionChecker(drone_config_map,
                                             DefaultCollisionConfig())
    if symbol_table is None:
        symbol_table = SymbolTable()
    if function_table is None:
        function_table = FunctionTable()
    state_updater_map = {
        name: StateUpdater(config)
        for name, config in drone_config_map.items()
    }

    tree = _parse_program(program)

    drone_commands = Compiler(drone_config_map, symbol_table,
                              function_table).visit(tree)

    if has_checks:
        boundary_checker.check(drone_commands, state_updater_map)
        collision_checker.check(drone_commands, state_updater_map)

    return drone_commands
 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)
 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)
Ejemplo n.º 30
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)