def __init__(self, leftExpression, rightExpression, operation): Expression.__init__(self, None) self.operation = operation self.leftExpression = leftExpression self.rightExpression = rightExpression # Determine the type of the expression # check if types of 2 expressions are the same if (self.leftExpression.basetype != self.rightExpression.basetype): raise RuntimeError( "The two types of the expressions in the logic expression should be the same" ) if (not isinstance(self.leftExpression.basetype, BooleanType)): raise RuntimeError( "Left side of logic expression should be a bool, now: " + str(type(self.leftExpression.basetype))) if (not isinstance(self.rightExpression.basetype, BooleanType)): raise RuntimeError( "Right side of logic expression should be a bool, now: " + str(type(self.rightExpression.basetype))) # set the type of this expression self.basetype = BooleanType()
def build_for_statement(self, tree): if tree.getChildCount() < 6: raise RuntimeError("invalid FOR statement: '" + tree.getText() + "'") token = tree.getChild(0).getPayload() # for开头 if not isinstance(token, Token) or token.type != CXLexer.FOR: raise RuntimeError("Invalid FOR statement: '" + tree.getText() + "'") token = tree.getChild(1).getPayload() if not isinstance(token, Token) or token.type != CXLexer.LEFTPARENTHESIS: raise RuntimeError("Invalid FOR statement: '" + tree.getText() + "'") token = tree.getChild(tree.getChildCount() - 2).getPayload() if not isinstance(token, Token) or token.type != CXLexer.RIGHTPARENTHESIS: raise RuntimeError("Invalid FOR statement: '" + tree.getText() + "'") child_index = 2 semicolon_index = 0 init_expression = Expression(None) check_expression = Expression(None) update_expression = Expression(None) while True: token = tree.getChild(child_index).getPayload() if isinstance(token, Token): if token.type == CXLexer.RIGHTPARENTHESIS: break # index=2为分号表示初始语句为空 elif token.type == CXLexer.SEMICOLON: semicolon_index += 1 child_index += 1 if semicolon_index > 2: raise RuntimeError("Invalid FOR statement: '" + tree.getText() + "'") else: raise RuntimeError("Invalid FOR statement: '" + tree.getText() + "'") else: if semicolon_index == 0: init_expression = self.build_expression( tree.getChild(child_index)) elif semicolon_index == 1: check_expression = self.build_expression( tree.getChild(child_index)) else: update_expression = self.build_expression( tree.getChild(child_index)) child_index += 1 self.symbol_table.open_scope() statement = self.build_compound_statement( tree.getChild(tree.getChildCount() - 1)) self.symbol_table.close_scope() return ForStatement(init_expression, check_expression, update_expression, statement, self.symbol_table)
def __init__(self, expression): Expression.__init__(self, None) self.expression = expression if (not isinstance(expression.basetype, AddressType)): raise RuntimeError("Tried to dereference non address type: " + str(type(self.expression.basetype))) self.basetype = self.expression.basetype.addressee
def __init__(self, basetype, expression): Expression.__init__(self, None) self.expression = expression if basetype == "int": self.basetype = IntegerType() elif basetype == "real": self.basetype = RealType() else: pass
def __init__(self, symbol): """Create variable call with symbol(Symbol)""" Expression.__init__(self, None) self.basetype = symbol.basetype self.symbol = symbol self.arraySize = None if(type(self.symbol) == type(ArrayType)): self.arraySize = self.symbol.basetype.getElementsCount()
def __init__(self, symbol, index): """Call variable call with symbol(Symbol) and index(int)""" Expression.__init__(self, None) self.symbol = symbol self.index = index self.basetype = symbol.basetype if(index != None): #Call to element in array so change the basetype to the array's basetype self.basetype = symbol.basetype.basetype
def __init__(self, left_expression, right_expression, operation): Expression.__init__(self, None) self.operation = operation self.left_expression = left_expression self.right_expression = right_expression # 判断运算符左右的类型是否相同,不支持real和int混合运算,提供强制转换符 if self.left_expression.basetype != self.right_expression.basetype: raise RuntimeError( "The two types of the expressions in the arithmetic expression should be the same" ) self.basetype = self.left_expression.basetype
def __init__(self, leftExpression, rightExpression, operation): Expression.__init__(self, None) self.operation = operation self.leftExpression = leftExpression self.rightExpression = rightExpression # Determine the type of the expression # check if types of 2 expressions are the same if(self.leftExpression.basetype != self.rightExpression.basetype): raise RuntimeError("The two types of the expressions in the arithmetic expression should be the same") # set the type of this expression self.basetype = BooleanType()
def __init__(self, left_expression, right_expression, operation): Expression.__init__(self, None) self.left_expression = left_expression self.right_expression = right_expression self.operation = operation # print(self.left_expression.basetype, self.right_expression.basetype) if self.left_expression.basetype != self.right_expression.basetype: raise RuntimeError( "left and right expression must be of the same type") if not isinstance(self.left_expression.basetype, BooleanType): raise RuntimeError( "logic expression must be bool op bool, but now is {}".format( self.left_expression.basetype)) if not isinstance(self.right_expression.basetype, BooleanType): raise RuntimeError( "logic expression must be bool op bool, but now is {}".format( self.left_expression.basetype)) self.basetype = BooleanType
def __init__(self, value, basetype): Expression.__init__(self, None) if(basetype == "bool"): self.basetype = BooleanType() self.value = BooleanData(value) elif(basetype == "char"): self.basetype = CharacterType() self.value = CharacterData(value) elif(basetype == "int"): self.basetype = IntegerType() self.value = IntegerData(value) elif(basetype == "float"): self.basetype = RealType() self.value = RealData(value) elif(basetype == "string"): self.basetype = AddressType(ArrayType(CharacterType(), len(value))) self.value = StringData(value + "\0") else: raise RuntimeError("Trying to create constantexpession with unkown type")
def __init__(self, value, basetype): Expression.__init__(self, None) self.basetype = basetype self.value = value self.compiled_codes = None if self.basetype == "bool": self.basetype = BooleanType() if self.value is True: self.compiled_codes = "ldc b t\n" elif self.value is False: self.compiled_codes = "ldc b f\n" else: raise RuntimeError("Wrong Boolean Value") elif self.basetype == "int": self.basetype = IntegerType() self.compiled_codes = "ldc i {}\n".format(str(self.value)) elif self.basetype == "real": self.basetype = RealType() self.compiled_codes = "ldc r {}\n".format(str(self.value)) else: raise RuntimeError("Type {} is not supported".format(self.basetype))
def __init__(self, leftExpression, rightExpression, operation): Expression.__init__(self, None) self.operation = operation self.leftExpression = leftExpression self.rightExpression = rightExpression # Determine the type of the expression # check if types of 2 expressions are the same if(self.leftExpression.basetype != self.rightExpression.basetype): raise RuntimeError("The two types of the expressions in the arithmetic expression should be the same") if(not isinstance(self.leftExpression.basetype, IntegerType) and not isinstance(self.leftExpression.basetype, RealType)): raise RuntimeError("Left side of arithmetic expression should be an integer or real, now: " + str(type(self.leftExpression.basetype))) if(not isinstance(self.rightExpression.basetype, IntegerType) and not isinstance(self.rightExpression.basetype, RealType)): raise RuntimeError("Right side of arithmetic expression should be an integer or real, now: " + str(type(self.rightExpression.basetype))) # set the type of this expression self.basetype = self.leftExpression.basetype
def __init__(self, function, parameters): Expression.__init__(self, None) self.function = function self.parameters = parameters self.basetype = function.returntype
def buildForStatement(self, tree): """Build For Statement""" # FOR LPAREN expression? SEMICOLON expression? SEMICOLON expression? RPAREN statement if (tree.getChildCount() < 6): raise RuntimeError("invalid FOR statement: `" + tree.getText() + "`") # Check if FOR at front token = tree.getChild(0).getPayload() if (not isinstance(token, Token) or token.type != CLexer.FOR): raise RuntimeError("Invalid FOR statement: '" + tree.getText() + "'") # Check if LPAREN at front token = tree.getChild(1).getPayload() if (not isinstance(token, Token) or token.type != CLexer.LPAREN): raise RuntimeError("Invalid FOR statement: '" + tree.getText() + "'") # Check for RPAREN token = tree.getChild(tree.getChildCount() - 2).getPayload() if (not isinstance(token, Token) or token.type != CLexer.RPAREN): raise RuntimeError("Invalid FOR statement: '" + tree.getText() + "'") childIndex = 2 semicolonIndex = 0 initExpression = Expression(None) checkExpression = Expression(None) updateExpression = Expression(None) while (True): token = tree.getChild(childIndex).getPayload() if (isinstance(token, Token)): if (token.type == CLexer.RPAREN): break elif (token.type == CLexer.SEMICOLON): semicolonIndex += 1 childIndex += 1 if (semicolonIndex > 2): raise RuntimeError("Invalid FOR statement: '" + tree.getText() + "'") else: raise RuntimeError("Invalid FOR statement: '" + tree.getText() + "'") else: if (semicolonIndex == 0): initExpression = self.buildExpression( tree.getChild(childIndex)) elif (semicolonIndex == 1): checkExpression = self.buildExpression( tree.getChild(childIndex)) else: updateExpression = self.buildExpression( tree.getChild(childIndex)) childIndex += 1 # build statement self.sym.openScope() statement = self.buildStatement(tree.getChild(tree.getChildCount() - 1)) self.sym.closeScope() return ForStatement(initExpression, checkExpression, updateExpression, statement, self.sym)
def __init__(self, variable): Expression.__init__(self, None) self.variable = variable self.basetype = self.variable.basetype
def __init__(self, variable, expression): Expression.__init__(self, None) self.variable = variable self.expression = expression self.basetype = self.expression.basetype
def __init__(self, expression): Expression.__init__(self, None) self.expression = expression self.basetype = self.expression.basetype
def __init__(self, symbol): """Call variable call with symbol(Symbol)""" Expression.__init__(self, None) self.symbol = symbol self.index = None self.basetype = symbol.basetype