def __init__(self, environment, value): super().__init__(environment) self.type = SmallCParser.PRIMARY if isinstance(value, int): self.value = value self.operand_type = IntegerType() self.result_type = self.operand_type elif isinstance(value, float): self.value = value self.operand_type = FloatType() self.result_type = self.operand_type elif isinstance(value, bool): if value: self.value = 1 else: self.value = 0 self.operand_type = IntegerType() self.result_type = BooleanType() elif isinstance(value, str): self.value = "'" + value + "'" self.operand_type = CharacterType() self.result_type = self.operand_type else: raise C2PException("use of unrecognized primary value " + str(value))
def __init__(self, environment, expr_conjunction, expr_comparison): super().__init__(environment) self.type = SmallCParser.CONJUNCTION self.conjunction = expr_conjunction self.comparison = expr_comparison self.addChild(self.conjunction) self.addChild(self.comparison) self.operand_type = BooleanType() self.result_type = BooleanType()
def __init__(self, environment, factor, operator): super().__init__(environment) self.type = SmallCParser.FACTOR self.factor = factor self.operator = operator self.addChild(self.factor) if self.operator == "!": self.operand_type = BooleanType() else: self.operand_type = self.factor.result_type self.result_type = self.operand_type
class Factor(Expression): def __init__(self, environment, factor, operator): super().__init__(environment) self.type = SmallCParser.FACTOR self.factor = factor self.operator = operator self.addChild(self.factor) if self.operator == "!": self.operand_type = BooleanType() else: self.operand_type = self.factor.result_type self.result_type = self.operand_type def getDisplayableText(self): return self.operator def generateCode(self, out): # first get the operand on top of the stack self.factor.generateCode(out) self.cast(self.factor, out) # execute the relevant operation on the operand if self.operator == "-": self.writeInstruction("neg " + self.operand_type.getPSymbol(), out) elif self.operator == "!": self.writeInstruction("not ", out) else: raise C2PException(self.operator + " is not supported")
class Primary(Expression): def __init__(self, environment, value): super().__init__(environment) self.type = SmallCParser.PRIMARY if isinstance(value, int): self.value = value self.operand_type = IntegerType() self.result_type = self.operand_type elif isinstance(value, float): self.value = value self.operand_type = FloatType() self.result_type = self.operand_type elif isinstance(value, bool): if value: self.value = 1 else: self.value = 0 self.operand_type = IntegerType() self.result_type = BooleanType() elif isinstance(value, str): self.value = "'" + value + "'" self.operand_type = CharacterType() self.result_type = self.operand_type else: raise C2PException("use of unrecognized primary value " + str(value)) def getDisplayableText(self): return self.result_type.literalToPCode(self.value) def generateCode(self, out): p_type_operand = self.operand_type.getPSymbol() p_type_result = self.result_type.getPSymbol() p_code_operand = self.operand_type.literalToPCode(self.value) self.writeInstruction("ldc " + p_type_operand + " " + p_code_operand, out) # Implicitly cast boolean if self.operand_type.getName() is not self.result_type.getName(): self.writeInstruction( "conv " + p_type_operand + " " + p_type_result, out)
def __init__(self, environment, expr_relation1, expr_relation2, operator): super().__init__(environment) self.type = SmallCParser.COMPARISON self.relation1 = expr_relation1 self.relation2 = expr_relation2 self.operator = operator self.addChild(self.relation1) self.addChild(self.relation2) self.operand_type = self.relation1.result_type.getPoorest( self.relation2.result_type) self.result_type = BooleanType()
def visitType_specifier(self, parsetree: SmallCParser.Type_specifierContext): is_const = parsetree.CONST() is not None typetext = parsetree.getChild(int(is_const)).getText() if typetext == "bool": typename = BooleanType() elif typetext == "char": typename = CharacterType() elif typetext == "int": typename = IntegerType() elif typetext == "void": typename = VoidType() elif typetext == "float": typename = FloatType() else: line = parsetree.start.line column = parsetree.start.column msg = "'" + typename + "' is not a recognized type" MyErrorListener().semanticError(line, column, msg) typename.is_const = is_const return TypeSpecifier(self.environment, typename)