def test_visit_binary_expression_invalid_left_side_of_the_expression(self): int_exp = IntLiteralExpression(IntegerLiteral("10")) operator = Operator("~") un_exp = UnaryExpression(Operator("-"), IntLiteralExpression(IntegerLiteral("10"))) with self.assertRaises(Exception): self.c.visit_binary_expression( BinaryExpression(operator, int_exp, un_exp))
def test_visit_int_literal_expression(self): int_lit = IntegerLiteral("123") exp_type: ExpressionType = self.c.visit_int_literal_expression( IntLiteralExpression(int_lit)) self.assertTrue(exp_type.is_rvalue)
def test_visit_expression_list_throws_on_undeclared_variable(self): el = ExpressionList() el.expressions.append(IntLiteralExpression(IntegerLiteral("10"))) el.expressions.append(BooleanLiteralExpression(BooleanLiteral("true"))) el.expressions.append(VarExpression(Identifier("number"))) with self.assertRaises(UndeclaredVariableException): self.c.visit_expression_list(el)
def test_visit_var_declaration_with_assignment(self): vda = VarDeclarationWithAssignment(type_indicator=TypeIndicator("str"), identifier=Identifier("variable"), operator=Operator("~"), expression=IntLiteralExpression( IntegerLiteral("10"))) self.c.visit_var_declaration_with_assignment(vda) id_entry = self.c.idTable.table[0] self.assertEqual(id_entry.attr, vda)
def test_visit_binary_expression(self): var_exp = VarExpression(Identifier("variable")) operator = Operator("+") un_exp = UnaryExpression(Operator("-"), IntLiteralExpression(IntegerLiteral("10"))) var_dec = VarDeclaration(TypeIndicator("str"), Identifier("variable")) self.c.idTable.insert("variable", var_dec) exp_type: ExpressionType = self.c.visit_binary_expression( BinaryExpression(operator, var_exp, un_exp)) self.assertTrue(exp_type.is_rvalue)
def test_visit_arguments_list(self): args = ArgumentsList() exp1 = IntLiteralExpression(IntegerLiteral("10")) exp2 = VarExpression(Identifier("variable")) args.expressions.extend([exp1, exp2]) self.c.idTable.insert(identifier=exp2.name.spelling, attr=VarDeclaration( TypeIndicator("str"), Identifier(exp2.name.spelling))) exp_types = self.c.visit_arguments_list(args) self.assertEqual(len(exp_types), 2)
def test_visit_func_declaration(self): args = ArgumentsList() args.expressions.append(IntLiteralExpression(IntegerLiteral("10"))) args.expressions.append( BooleanLiteralExpression(BooleanLiteral("true"))) vd = VarDeclaration(TypeIndicator("str"), Identifier("variable")) dl = DeclarationList() dl.declarations.append(vd) block = CommandList() block.commands.append(DeclarationCommand(dl)) fd = FuncDeclaration(identifier=Identifier("myFunc"), args=args, commands=block) self.c.visit_func_declaration(fd) id_entry = self.c.idTable.table[0] self.assertEqual(id_entry.attr, fd)
def test_visit_integer_literal(self): spelling = self.c.visit_integer_literal(IntegerLiteral("123")) self.assertEqual(spelling, "123")