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_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_vector_should_give_error(self):
     none_vector_exprs = [Expression(Type.int(), 1, "a"), Expression(Type.decimal(), 1.0, "a"),
                          Expression(Type.boolean(), True, "a"), Expression(Type.string(), "abc", "a"),
                          Expression(Type.list_of(Type.decimal()), [1.0, 1.0, 1.0], "a"),
                          Expression(Type.list_of(Type.list_of(Type.decimal())), [[1.0, 1.0, 1.0]], "a")]
     for none_vector_expr in none_vector_exprs:
         with self.assertRaises(AssertionError) as context:
             VectorElem("a", none_vector_expr, 0)
Example #4
0
 def test_list(self):
     list_type = Type.list_of(Type.int())
     self.assertEqual(Type.list_of(Type.int()), list_type)
     self.assertEqual(ListType(Type.int()), list_type)
     self.assertEqual("list[int]", list_type.type_name)
     self.assertEqual([], list_type.default_value)
     self.assertEqual(Type.int(), list_type.elem_type)
     self.assertEqual("list[int]", str(list_type))
Example #5
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_to_expression_should_return_correct_value(self):
     expressions = [None, Expression(Type.int(), 1, "a"), Expression(Type.decimal(), 1.1, "a"),
                    Expression(Type.boolean(), False, "a"),
                    Expression(Type.vector(), [1.1, 2.2, -1.1], "a"),
                    Expression(Type.list_of(Type.int()), [1, 2, 3, 4], "a"),
                    Expression(Type.list_of(Type.int()), [], "a"),
                    Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"),
                    Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]], "a")]
     for expression in expressions:
         self.assertEqual(expression, Identifier("a", expression).to_expression())
 def test_declare_nested_list_should_change_symbol_table(self):
     actual = SymbolTable()
     generate_commands(
         """
         main () { list[list[int]] a; }
         """, actual)
     expected = SymbolTable()
     expected.store(
         "a",
         Expression(Type.list_of(Type.list_of(Type.int())), [], ident="a"))
     self.assertEqual(expected, actual)
    def test_to_expression_should_return_correct_value(self):
        expression = Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a")
        expected = Expression(Type.decimal(), 1.0, "a[0]")
        self.assertEqual(expected, ListElem("a", expression, 0).to_expression())

        expression = Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]], "a")
        expected = Expression(Type.list_of(Type.vector()), [[1.1, 2.2, -1.1], [1, 2, -1]], "a[0]")
        self.assertEqual(expected, ListElem("a", expression, 0).to_expression())

        expression = Expression(Type.list_of(Type.boolean()), [True, False], "b[1]")
        expected = Expression(Type.boolean(), False, "b[1][1]")
        self.assertEqual(expected, ListElem("b[1]", expression, 1).to_expression())
    def test_str(self):
        self.assertEqual("Expression: { type: int, value: 1, ident: None }", str(Expression(Type.int(), 1)))
        self.assertEqual("Expression: { type: decimal, value: 1.1, ident: None }", str(Expression(Type.decimal(), 1.1)))
        self.assertEqual("Expression: { type: boolean, value: False, ident: None }",
                         str(Expression(Type.boolean(), False)))
        self.assertEqual("Expression: { type: vector, value: [1.1, 2.2, -1.1], ident: None }",
                         str(Expression(Type.vector(), [1.1, 2.2, -1.1])))
        self.assertEqual("Expression: { type: list[int], value: [1, 2, 3, 4], ident: None }",
                         str(Expression(Type.list_of(Type.int()), [1, 2, 3, 4])))
        self.assertEqual("Expression: { type: list[int], value: [], ident: None }",
                         str(Expression(Type.list_of(Type.int()), [])))
        self.assertEqual(
            "Expression: { type: list[list[vector]], value: [[[1.1, 2.2, -1.1], [1, 2, -1]]], ident: None }",
            str(Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]])))

        self.assertEqual("Expression: { type: int, value: 1, ident: a }", str(Expression(Type.int(), 1, "a")))
        self.assertEqual("Expression: { type: decimal, value: 1.1, ident: a }",
                         str(Expression(Type.decimal(), 1.1, "a")))
        self.assertEqual("Expression: { type: boolean, value: False, ident: a }",
                         str(Expression(Type.boolean(), False, "a")))
        self.assertEqual("Expression: { type: vector, value: [1.1, 2.2, -1.1], ident: a }",
                         str(Expression(Type.vector(), [1.1, 2.2, -1.1], "a")))
        self.assertEqual("Expression: { type: list[int], value: [1, 2, 3, 4], ident: a }",
                         str(Expression(Type.list_of(Type.int()), [1, 2, 3, 4], "a")))
        self.assertEqual("Expression: { type: list[int], value: [], ident: a }",
                         str(Expression(Type.list_of(Type.int()), [], "a")))
        self.assertEqual("Expression: { type: list[list[vector]], value: [[[1.1, 2.2, -1.1], [1, 2, -1]]], ident: a }",
                         str(Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]],
                                        "a")))
 def test_to_expression_should_equal_self(self):
     expressions = [Expression(Type.int(), 1), Expression(Type.decimal(), 1.1), Expression(Type.boolean(), False),
                    Expression(Type.vector(), [1.1, 2.2, -1.1]), Expression(Type.list_of(Type.int()), [1, 2, 3, 4]),
                    Expression(Type.list_of(Type.int()), []),
                    Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0]),
                    Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]]),
                    Expression(Type.int(), 1, "a"), Expression(Type.decimal(), 1.1, "a"),
                    Expression(Type.boolean(), False, "a"),
                    Expression(Type.vector(), [1.1, 2.2, -1.1], "a"),
                    Expression(Type.list_of(Type.int()), [1, 2, 3, 4], "a"),
                    Expression(Type.list_of(Type.int()), [], "a"),
                    Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"),
                    Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]], "a")]
     for expression in expressions:
         self.assertEqual(expression, expression.to_expression())
 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])
 def test_assign_ident_nested_list_should_update_symbol_table(self):
     actual = SymbolTable()
     generate_commands(
         """
         main () {
          list[list[int]] a;
          a <- [[1], [-1], [+1]];
         }
         """, actual)
     expected = SymbolTable()
     expected.store(
         "a",
         Expression(Type.list_of(Type.list_of(Type.int())),
                    [[1], [-1], [1]],
                    ident="a"))
     self.assertEqual(expected, actual)
Example #13
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))
 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_declare_and_assign_ident_empty_list_should_update_symbol_table(
         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 type in types:
         actual = SymbolTable()
         generate_commands(
             """
             main () {{
              list[{}] a <- [];
             }}
             """.format(type.type_name), actual)
         expected = SymbolTable()
         expected.store("a", Expression(Type.list_of(type), [], 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_declare_and_then_assign_vector_elem_with_different_type_should_give_error(
            self):
        for type in [
                Type.int(),
                Type.string(),
                Type.boolean(),
                Type.vector(),
                Type.list_of(Type.int()),
                Type.list_of(Type.list_of(Type.int()))
        ]:
            with self.assertRaises(CompileError) as context:
                generate_commands("""
                    main () {{
                     vector a;
                     {} b;
                     a.x <- b;
                    }}
                """.format(type.type_name))

            self.assertTrue(
                "Assigned value {} should have type decimal, but is {}".format(
                    type.default_value, type.type_name) in str(
                        context.exception))
 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_get_expression(self):
     st = SymbolTable()
     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)
Example #21
0
    def test_eq(self):
        types1 = [
            None,
            Type.int(),
            Type.decimal(),
            Type.string(),
            Type.boolean(),
            Type.vector(),
            Type.list_of(Type.int()),
            Type.list_of(Type.decimal()),
            Type.list_of(Type.list_of(Type.int())),
            Type.list_of(Type.list_of(Type.decimal()))
        ]
        types2 = [
            None,
            Type.int(),
            Type.decimal(),
            Type.string(),
            Type.boolean(),
            Type.vector(),
            Type.list_of(Type.int()),
            Type.list_of(Type.decimal()),
            Type.list_of(Type.list_of(Type.int())),
            Type.list_of(Type.list_of(Type.decimal()))
        ]
        for i in range(len(types1)):
            for j in range(len(types2)):
                if i == j:
                    self.assertEqual(types1[i], types2[j])
                else:
                    self.assertNotEqual(types1[i], types2[j])

        self.assertEqual(Type.empty_list(), Type.empty_list())
        for type in types1:
            if isinstance(type, ListType):
                self.assertEqual(Type.empty_list(), type)
                self.assertEqual(type, Type.empty_list())
            else:
                self.assertNotEqual(Type.empty_list(), type)
                self.assertNotEqual(type, Type.empty_list())
Example #22
0
 def test_corrupted_type_should_not_equal_to_list_type(self):
     self.assertNotEqual(Type("list[int]", []), Type.list_of(Type.int()))
     self.assertNotEqual(Type("list[int]", []), ListType(Type.int()))
 def test_eq(self):
     expressions1 = [None,
                     Expression(Type.int(), 1), Expression(Type.decimal(), 1.1), Expression(Type.boolean(), False),
                     Expression(Type.vector(), [1.1, 2.2, -1.1]), Expression(Type.list_of(Type.int()), [1, 2, 3, 4]),
                     Expression(Type.list_of(Type.int()), []),
                     Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0]),
                     Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]]),
                     Expression(Type.int(), 1, "a"), Expression(Type.decimal(), 1.1, "a"),
                     Expression(Type.boolean(), False, "a"),
                     Expression(Type.vector(), [1.1, 2.2, -1.1], "a"),
                     Expression(Type.list_of(Type.int()), [1, 2, 3, 4], "a"),
                     Expression(Type.list_of(Type.int()), [], "a"),
                     Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"),
                     Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]], "a")]
     expressions2 = [None,
                     Expression(Type.int(), 1), Expression(Type.decimal(), 1.1), Expression(Type.boolean(), False),
                     Expression(Type.vector(), [1.1, 2.2, -1.1]), Expression(Type.list_of(Type.int()), [1, 2, 3, 4]),
                     Expression(Type.list_of(Type.int()), []),
                     Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0]),
                     Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]]),
                     Expression(Type.int(), 1, "a"), Expression(Type.decimal(), 1.1, "a"),
                     Expression(Type.boolean(), False, "a"),
                     Expression(Type.vector(), [1.1, 2.2, -1.1], "a"),
                     Expression(Type.list_of(Type.int()), [1, 2, 3, 4], "a"),
                     Expression(Type.list_of(Type.int()), [], "a"),
                     Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"),
                     Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]], "a")]
     for i in range(len(expressions1)):
         for j in range(len(expressions2)):
             if i == j:
                 self.assertEqual(expressions1[i], expressions2[j])
             else:
                 self.assertNotEqual(expressions1[i], expressions2[j])
Example #24
0
 def list_type(self, children) -> Type:
     elem_type, = children
     return Type.list_of(elem_type)
 def test_str(self):
     self.assertEqual("ListElem: { ident: a, " +
                      "container: Expression: { type: list[int], value: [1, 2, 3, 4], ident: a }, " +
                      "index: 0, expression: Expression: { type: int, value: 1, ident: a[0] } }",
                      str(ListElem("a", Expression(Type.list_of(Type.int()), [1, 2, 3, 4], "a"), 0)))
 def test_eq(self):
     list_elems1 = [None,
                    ListElem("a", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"), 0),
                    ListElem("b", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "b"), 0),
                    ListElem("a", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0], "a"), 0),
                    ListElem("a", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"), 1),
                    ListElem("a", Expression(Type.list_of(Type.list_of(Type.int())), [[[1], [1]]], "a"), 0),
                    ListElem("b[1]", Expression(Type.list_of(Type.boolean()), [True, False], "b[1]"), 0)]
     list_elems2 = [None,
                    ListElem("a", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"), 0),
                    ListElem("b", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "b"), 0),
                    ListElem("a", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0], "a"), 0),
                    ListElem("a", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"), 1),
                    ListElem("a", Expression(Type.list_of(Type.list_of(Type.int())), [[[1], [1]]], "a"), 0),
                    ListElem("b[1]", Expression(Type.list_of(Type.boolean()), [True, False], "b[1]"), 0)]
     for i in range(len(list_elems1)):
         for j in range(len(list_elems2)):
             if i == j:
                 self.assertEqual(list_elems1[i], list_elems2[j])
             else:
                 self.assertNotEqual(list_elems1[i], list_elems2[j])