def vReturnStatement(self, node): if not node.expr: self.w('RETURN') else: tpe = node.symtab.getReturnType().resolveType() if isinstance(node.expr, ast.Constructor) and len( node.expr.params. children) > 0 and node.expr.params.children[0].resolveType( ).type == node.expr.resolveType().type: self.w('RETURN') self.visit(node.expr.params.children[0]) elif isinstance(node.expr, ast.Constructor): print node.expr.params.children[0].resolveType() self.pi('BEGIN') decl = ast.VariableDeclaration('auxaux') decl.type = node.expr.type decl.params = node.expr.params decl.symtab = node.expr.symtab self.visit(decl) self.p(';') self.w('RETURN') self.w('auxaux') self.w(';') self.pu() self.w('END') else: self.w('RETURN') if tpe.type != node.expr.resolveType().type: self.visit(ast.CastExpression(tpe, node.expr)) else: self.visit(node.expr)
def vAssignmentStatement(self, node): if isinstance( node.expr, ast.Constructor) and node.expr.type.resolveType().isVector(): self.w('New') self.w('(') self.visit(node.lval) cons = node.expr while isinstance(cons, ast.Constructor): self.w(',') #print cons.params self.visit(cons.params.children[0]) if len(cons.params.children) == 1: cons = None break else: cons = cons.params.children[1] if cons != None: print '****************Need to initialize******************' self.w(')') elif isinstance(node.expr, ast.AssignmentStatement): self.visit(node.expr) self.p(';') tmp = node.expr if isinstance(node.expr.lval, str): node.expr = ast.Identifier(node.expr.lval) else: node.expr = node.expr.lval node.expr.symtab = node.symtab self.visit(node) node.expr = tmp else: ident = node.lval ident.symtab = node.symtab oldexpr = node.expr if node.operator != '=': node.expr = ast.BinaryOp( ident, self.assignmentOperators[node.operator], ast.Parenthesis(node.expr)) node.expr.symtab = node.symtab tpe_ident = ident.resolveType().type tpe_expr = node.expr.resolveType().type #if isinstance(ident.resolveType(), ast.StructType): #print ident.resolveType().id #print tpe_ident #if isinstance(node.expr.resolveType().resolveType(), ast.StructType): #print node.expr.resolveType().resolveType().id #print tpe_expr if tpe_ident != tpe_expr and not ( (tpe_ident == 'float' and tpe_expr == 'double') or (tpe_ident == 'double' and tpe_expr == 'float')): node.expr = ast.CastExpression(ast.Type(tpe_ident), node.expr) node.expr.symtab = node.symtab self.visit(node.lval) self.w(':=') self.visit(node.expr) node.expr = oldexpr
def vBinaryOp(self, node): # falta and or xor bit a bit pascalOp = self.t[node.oper] if (pascalOp == 'AND') or ( pascalOp == 'OR' ): # podriamos necesitar para todos los operadores hacer un in! self.w('(') self.visit(node.left) self.w(')') self.w(pascalOp) self.w('(') self.visit(node.right) self.w(')') elif (pascalOp == 'DIV'): left_type = node.left.resolveType().type right_type = node.right.resolveType().type divOp = 'DIV' if (left_type == 'float' or left_type == 'double' or right_type == 'float' or right_type == 'double'): divOp = '/' self.visit(node.left) self.w(divOp) self.visit(node.right) elif pascalOp == '-' or pascalOp == '+' or pascalOp == '=': left_type = node.left.resolveType().type right_type = node.right.resolveType().type #print "left " + left_type #print "right " + right_type if (left_type == 'char'): node.left = ast.CastExpression(ast.Type('int'), node.left) if (right_type == 'char'): node.right = ast.CastExpression(ast.Type('int'), node.right) self.visit(node.left) self.w(pascalOp) self.visit(node.right) else: self.visit(node.left) self.w(pascalOp) self.visit(node.right)
def vWhileStatement(self, node): self.w('WHILE') if not node.cond.resolveType().isBool(): cast = ast.CastExpression(ast.Type('bool'), node.cond) cast.type.symtab = node.symtab cast.symtab = node.symtab self.visit(cast) else: self.visit(node.cond) self.w('DO') self.pi('BEGIN') self.visit(node.loop) self.pu() self.w('END')
def vIfStatement(self, node): self.w('IF') if not node.cond.resolveType().isBool(): cast = ast.CastExpression(ast.Type('bool'), node.cond) cast.type.symtab = node.symtab cast.symtab = node.symtab self.visit(cast) else: self.visit(node.cond) #self.visit(node.cond) self.w('THEN') self.pi('BEGIN') self.visit(node.then) self.pu(';') self.w('END') if node.elze: self.p() self.w('ELSE') self.pi('BEGIN') self.visit(node.elze) self.pu(';') self.w('END')
def p_cast_expression_02(t): ''' cast_expression : type LPAR lor_expression RPAR ''' t[0] = ast.CastExpression(t[1], t[3])
def p_factor_08(t): '''factor : LPAR type RPAR assignment_expression''' t[0] = ast.CastExpression(t[2], t[4])
def vVariableDeclaration(self, node): tpe = node.type.resolveType() tpe.symtab = node.symtab if tpe.isVector() and node.params: self.w('VAR') self.w(node.id) self.w(':') if tpe.isVector() or tpe.isString(): #print "initVectorType" + tpe.type tpe = self.initializeVectorType(tpe, node) tpe.symtab = node.symtab self.visit(tpe) if tpe.isVector() and node.params: self.p(';') self.w('New') self.w('(') self.w(node.id) t = tpe while t.isVector(): if t.size != None: self.w(',') self.visit(t.size) else: print 'Vectors need to be initialized fully at creation.' break t = t.subtype self.w(')') if (tpe.isVector() or tpe.isString()) and node.params and len( node.params.children) == 2: self.w(';') if self.needsInitialization(node): self.p(';') self.initializeVector(node, node.type.resolveType(), [], 1, node.id) return if tpe.constant and node.isTopLevel: self.w('CONST') else: self.w('VAR') self.w(node.id) self.w(':') if tpe.isVector() or tpe.isString(): #print "initVectorType" + tpe.type tpe = self.initializeVectorType(tpe, node) tpe.symtab = node.symtab self.visit(tpe) if tpe.isVector() and node.params: self.w('(') t = tpe while t.isVector(): self.visit(t.size) if t.subtype.isVector(): self.w(',') t = t.subtype self.w(')') elif tpe.isString(): if tpe.size: self.w('[') if not tpe.size.resolveType().isInt(): tpe_int = ast.Type('int') tpe_int.symtab = node.symtab cast = ast.CastExpression(tpe_int, tpe.size) cast.symtab = node.symtab self.visit(cast) else: self.visit(tpe.size) self.w(']') if (tpe.isVector() or tpe.isString()) and node.params and len( node.params.children) == 2: if self.needsInitialization(node): self.p(';') self.initializeVector(node, node.type.resolveType(), [], 1, node.id) elif node.init: expr = node.init tpe_expr = expr.resolveType() tpe_ident = node.type if tpe_ident.type != tpe_expr.type and not ( tpe_ident.isFloatingPoint() and tpe_expr.isFloatingPoint()): expr = ast.CastExpression(tpe_ident, node.init) self.w('=') self.visit(expr)