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))
Ejemplo n.º 2
0
 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_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_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_assign_ident_string_should_update_symbol_table(self):
     actual = SymbolTable()
     generate_commands(
         """
         main () {
          string a;
          a <- "1";
         }
         """, actual)
     expected = SymbolTable()
     expected.store("a", Expression(Type.string(), "1", ident="a"))
     self.assertEqual(expected, actual)
Ejemplo n.º 6
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())
 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_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))
Ejemplo n.º 9
0
 def test_string(self):
     self.assertEqual(Type.string(), Type.string())
     self.assertEqual("string", Type.string().type_name)
     self.assertEqual("", Type.string().default_value)
     self.assertEqual("string", str(Type.string()))
Ejemplo n.º 10
0
 def string_expr(self, children) -> Expression:
     escaped_string, = children
     quotation_removed = str(escaped_string)[1:-1]
     return Expression(Type.string(), quotation_removed)
Ejemplo n.º 11
0
 def string_type(self, children) -> Type:
     return Type.string()