def test_visit_declaration_list(self): vd1 = VarDeclaration(TypeIndicator("str"), Identifier("name")) vd2 = VarDeclaration(TypeIndicator("int"), Identifier("age")) vd3 = VarDeclaration(TypeIndicator("bool"), Identifier("isAlive")) dl = DeclarationList() dl.declarations.extend([vd1, vd2, vd3]) self.c.visit_declaration_list(dl) id_table = self.c.idTable.table self.assertEqual(id_table[0].attr, vd1) self.assertEqual(id_table[1].attr, vd2) self.assertEqual(id_table[2].attr, vd3)
def test_visit_var_declaration(self): vd = VarDeclaration(TypeIndicator("str"), Identifier("variable")) self.c.visit_var_declaration(vd) id_entry = self.c.idTable.table[0] self.assertEqual(id_entry.attr, vd)
def test_visit_var_expression(self): identifier = Identifier("variable") var_type = TypeIndicator("str") self.c.idTable.insert(identifier=identifier.spelling, attr=VarDeclaration(var_type, identifier)) exp_type: ExpressionType = self.c.visit_var_expression( VarExpression(identifier)) self.assertFalse(exp_type.is_rvalue)
def visit_var_declaration(self, vd: VarDeclaration, *args) -> object: address = args[0] vd.address = address register = self.__display_register(self.current_level, address.level) size: int = vd.type_indicator.visit(self) self.__emit(operation=Machine.PUSHop, length=0, register_n=register, displacement=size) return Address.from_address(address=address, increment=1)
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)