示例#1
0
 def rotate_right(self, children) -> List[Command]:
     expr, = children
     if expr.type != Type.int() and expr.type != Type.decimal():
         raise CompileError(
             "Expression {} should have type int or decimal, but is {}".
             format(expr.value, expr.type))
     return [Command.rotate_right(radians(expr.value))]
示例#2
0
 def backward(self, children) -> List[Command]:
     expr, = children
     if expr.type != Type.int() and expr.type != Type.decimal():
         raise CompileError(
             "Expression {} should have type int or decimal, but is {}".
             format(expr.value, expr.type))
     return [Command.backward(expr.value)]
    def test_assign_list_elem_with_different_type_should_give_error(self):
        types = [
            Type.int(),
            Type.decimal(),
            Type.string(),
            Type.boolean(),
            Type.vector(),
            Type.list_of(Type.int()),
            Type.list_of(Type.list_of(Type.int()))
        ]
        for t1 in types:
            for t2 in types:
                if t1 == t2:
                    continue
                with self.assertRaises(CompileError) as context:
                    generate_commands("""
                    main () {{
                     {} a;
                     {} b;
                     list[{}] c <- [a];
                     c[0] <- b;
                    }}
                    """.format(t1, t2, t1))

                self.assertTrue(
                    "Assigned value {} should have type {}, but is {}".format(
                        t2.default_value, t1.type_name, t2.type_name) in str(
                            context.exception))
 def test_container_not_list_should_give_error(self):
     none_list_exprs = [Expression(Type.int(), 1, "a"), Expression(Type.decimal(), 1.0, "a"),
                        Expression(Type.boolean(), True, "a"), Expression(Type.string(), "abc", "a"),
                        Expression(Type.vector(), [1.0, 1.0, 1.0], "a")]
     for none_list_expr in none_list_exprs:
         with self.assertRaises(AssertionError) as context:
             ListElem("a", none_list_expr, 0)
    def test_declare_and_assign_with_different_type_should_give_error(self):
        types = [
            Type.int(),
            Type.decimal(),
            Type.string(),
            Type.boolean(),
            Type.vector(),
            Type.list_of(Type.int()),
            Type.list_of(Type.list_of(Type.int()))
        ]
        for t1 in types:
            for t2 in types:
                if t1 == t2:
                    continue
                with self.assertRaises(CompileError) as context:
                    generate_commands("""
                    main () {{
                     {} a;
                     {} b <- a;
                    }}
                    """.format(t1, t2))

                self.assertTrue(
                    "Identifier b has been declared as {}, but assigned as {}".
                    format(t2.type_name, t1.type_name) in str(
                        context.exception))
 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_to_expression_should_return_correct_value(self):
        expression = Expression(Type.vector(), [1.0, 2.0, 3.0], "a")
        expected = Expression(Type.decimal(), 1.0)
        self.assertEqual(expected, VectorElem("a", expression, 0).to_expression())

        expression = Expression(Type.vector(), [1.0, 2.0, 3.0], "b[1]")
        expected = Expression(Type.decimal(), 3.0)
        self.assertEqual(expected, VectorElem("b[1]", expression, 2).to_expression())
示例#8
0
 def test_empty_list(self):
     empty_list = Type.empty_list()
     self.assertEqual(Type.empty_list(), empty_list)
     self.assertEqual(EmptyList(), empty_list)
     self.assertEqual("list[]", empty_list.type_name)
     self.assertEqual([], empty_list.default_value)
     self.assertEqual(Type("all", 0), empty_list.elem_type)
     self.assertEqual("list[]", str(empty_list))
 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))
示例#10
0
 def list(self, children) -> Expression:
     exprs = children
     if len(exprs) == 0:
         return Expression(Type.empty_list(), [])
     if not all(e.type == exprs[0].type for e in exprs):
         raise CompileError(
             "Elements in list {} should have the same type".format(exprs))
     return Expression(Type.list_of(exprs[0].type),
                       [e.value for e in exprs])
 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))
示例#12
0
 def test_corrupted_type_should_not_affect_correct_type(self):
     int_type = Type.int()
     corrupted_type = Type.int()
     corrupted_type._type_name = "corrupted"
     self.assertNotEqual(int_type, corrupted_type)
     self.assertEqual("int", str(Type.int()))
     self.assertEqual(0, Type.int().default_value)
     self.assertEqual("corrupted", str(corrupted_type))
     self.assertEqual(0, corrupted_type.default_value)
示例#13
0
 def vector(self, children) -> Expression:
     expr1, expr2, expr3 = children
     for expr in [expr1, expr2, expr3]:
         if expr.type != Type.decimal():
             raise CompileError(
                 "Expression {} should have type decimal, but is {}".format(
                     expr, expr.type))
     return Expression(Type.vector(),
                       [expr1.value, expr2.value, expr3.value])
    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)
 def test_declare_and_assign_list_should_change_symbol_table(self):
     actual = SymbolTable()
     generate_commands(
         """
         main () { list[int] a <- [1, 2, -3]; }
         """, actual)
     expected = SymbolTable()
     expected.store(
         "a", Expression(Type.list_of(Type.int()), [1, 2, -3], 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];
         }
         """, actual)
     expected = SymbolTable()
     expected.store(
         "a", Expression(Type.list_of(Type.int()), [1, -1, 1], 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];
         }
         """, actual)
     expected = SymbolTable()
     expected.store(
         "a", Expression(Type.list_of(Type.int()), [1, 1, 1], ident="a"))
     self.assertEqual(expected, actual)
 def test_eq(self):
     identifiers1 = [None, Identifier("a", None), Identifier("a", Expression(Type.int(), 1, "a")),
                     Identifier("a", Expression(Type.decimal(), 1.1, "a")),
                     Identifier("b", Expression(Type.int(), 1, "b")),
                     Identifier("b", Expression(Type.list_of(Type.int()), [], "b"))]
     identifiers2 = [None, Identifier("a", None), Identifier("a", Expression(Type.int(), 1, "a")),
                     Identifier("a", Expression(Type.decimal(), 1.1, "a")),
                     Identifier("b", Expression(Type.int(), 1, "b")),
                     Identifier("b", Expression(Type.list_of(Type.int()), [], "b"))]
     for i in range(len(identifiers1)):
         for j in range(len(identifiers2)):
             if i == j:
                 self.assertEqual(identifiers1[i], identifiers2[j])
             else:
                 self.assertNotEqual(identifiers1[i], identifiers2[j])
示例#19
0
 def vector_z(self, children) -> VectorElem:
     expr, = children
     if expr.type != Type.vector():
         raise CompileError(
             "Expression {} should have type vector, but is {}".format(
                 expr, expr.type))
     return VectorElem(expr.ident, expr, 2)
 def test_declare_int_should_change_symbol_table(self):
     actual = SymbolTable()
     generate_commands("""
         main () { int a; }
         """, actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.int(), 0, 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; }
         """, actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.decimal(), 1.0, ident="a"))
     self.assertEqual(expected, actual)
示例#22
0
 def repeat(self, children) -> List[Command]:
     expr = children[0]
     commands = children[1:]
     if expr.type != Type.int():
         raise CompileError(
             "Expression {} should have type int, but is {}".format(
                 expr.value, expr.type))
     times = expr.value
     return commands * times
 def test_declare_and_assign_string_should_change_symbol_table(self):
     actual = SymbolTable()
     generate_commands(
         """
         main () { string a <- "\0a"; }
         """, actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.string(), "\0a", ident="a"))
     self.assertEqual(expected, actual)
 def test_declare_and_assign_boolean_should_change_symbol_table(self):
     actual = SymbolTable()
     generate_commands(
         """
         main () { boolean a <- true; }
         """, actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.boolean(), True, ident="a"))
     self.assertEqual(expected, actual)
 def test_assign_list_elem_with_vector_should_update_symbol_table(self):
     actual = SymbolTable()
     generate_commands(
         """
         main () {
          list[vector] a <- [(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)];
          a[0] <- (7.0, 8.0, 9.0);
          a[1].x <- 10.0;
          a[1].y <- 11.0;
          a[1].z <- 12.0;
         }
         """, actual)
     expected = SymbolTable()
     expected.store(
         "a",
         Expression(Type.list_of(Type.vector()), [[7, 8, 9], [10, 11, 12]],
                    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); }
         """, actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.vector(), [1, 2, -3], ident="a"))
     self.assertEqual(expected, actual)
示例#27
0
 def test_nested_list(self):
     inner = Type.list_of(Type.int())
     outer = Type.list_of(inner)
     self.assertEqual(ListType(Type.list_of(Type.int())), outer)
     self.assertEqual("list[list[int]]", outer.type_name)
     self.assertEqual([], outer.default_value)
     self.assertEqual(Type.list_of(Type.int()), outer.elem_type)
     self.assertEqual("list[list[int]]", str(outer))
 def test_assign_list_elem_to_variable_nested_should_update_symbol_table(
         self):
     actual = SymbolTable()
     generate_commands(
         """
         main () {
          list[list[int]] a <- [[0, 1], [2, 3]];
          a[0] <- [4, 5];
          a[1][0] <- 6;
          a[1][1] <- 7;
         }
         """, actual)
     expected = SymbolTable()
     expected.store(
         "a",
         Expression(Type.list_of(Type.list_of(Type.int())),
                    [[4, 5], [6, 7]],
                    ident="a"))
     self.assertEqual(expected, actual)
 def test_eq(self):
     vector_elems1 = [None,
                      VectorElem("a", Expression(Type.vector(), [1.0, 2.0, 3.0], "a"), 0),
                      VectorElem("b", Expression(Type.vector(), [1.0, 2.0, 3.0], "b"), 0),
                      VectorElem("a", Expression(Type.vector(), [2.0, 2.0, 3.0], "a"), 0),
                      VectorElem("a", Expression(Type.vector(), [1.0, 2.0, 3.0], "a"), 1),
                      VectorElem("b[1]", Expression(Type.vector(), [1.0, 2.0, 3.0], "a"), 0)]
     vector_elems2 = [None,
                      VectorElem("a", Expression(Type.vector(), [1.0, 2.0, 3.0], "a"), 0),
                      VectorElem("b", Expression(Type.vector(), [1.0, 2.0, 3.0], "b"), 0),
                      VectorElem("a", Expression(Type.vector(), [2.0, 2.0, 3.0], "a"), 0),
                      VectorElem("a", Expression(Type.vector(), [1.0, 2.0, 3.0], "a"), 1),
                      VectorElem("b[1]", Expression(Type.vector(), [1.0, 2.0, 3.0], "a"), 0)]
     for i in range(len(vector_elems1)):
         for j in range(len(vector_elems2)):
             if i == j:
                 self.assertEqual(vector_elems1[i], vector_elems2[j])
             else:
                 self.assertNotEqual(vector_elems1[i], vector_elems2[j])
示例#30
0
 def assign_vector_elem(self, children) -> List[Command]:
     vector_elem, expr = children
     ident = vector_elem.ident
     vector = vector_elem.container
     index = vector_elem.index
     if expr.type != Type.decimal():
         raise CompileError(
             "Assigned value {} should have type decimal, but is {}".format(
                 expr.value, expr.type))
     self._update_nested_ident(ident, expr, index)
     return []