def __init__(self): self.tbool = Type.get('bool') self.tchar = Type.get('char') self.tint = Type.get('int') self.tvoid = Type.get('void') self.curfn = None self.in_loop = False
def __init__(self): self.tbool = Type.get('bool') self.tchar = Type.get('char') self.tint = Type.get('int') self.tfloat = Type.get('float') #! added float self.tvoid = Type.get('void') self.curfn = None self.loop_count = 0
def visitFor(self, forNode): #! added Visitfor, how is desugarer called # from: for(VARDEF to END) BLOCK # to: {VARDEF VARDEF1 while (VARDEF.ID < "end"){ BLOCK ASSIGNMENT} # assignment: VARDEF.ID = VARDEF.ID + 1 # vardef1: (int, "end", END) self.visit_children(forNode) endVar = self.makevar("end") initEnd = VarDef(Type.get('int'), endVar, BinaryOp(forNode.end, Operator('-'), IntConst(1))) decrCount = Assignment( VarUse(forNode.start.name), (BinaryOp(VarUse(forNode.start.name), Operator('-'), IntConst(1)))) #initEnd = VarDef(Type.get('int'), endVar, IntConst(1)) endCheck = BinaryOp(VarUse(forNode.start.name), Operator('<'), VarUse(endVar)) incrCount = Assignment( VarUse(forNode.start.name), (BinaryOp(VarUse(forNode.start.name), Operator('+'), IntConst(1)))) return Block([ forNode.start, decrCount, initEnd, While(endCheck, Block([incrCount, forNode.body])) ])
def visitFor(self, node): # from: for(int node.name = node.expr1 to node.expr2) { node.body } # to: # Block wrapper { # Assignment: int node.name = node.expr1; # While type: while ( expression: node.name < node.expr2){ # new block: # Block: node.body; # : node.name = node.name + 1 # } self.visit_children(node) if isinstance(node.ref, str): ref = VarUse(node.ref) initial_assignment = VarDef(Type.get('int'), str(ref), node.expr1).at(node) incrementation = Assignment(ref, BinaryOp(ref, Operator('+'), IntConst(1))).at(node) while_body = Block([node.body, incrementation]).at(node) while_condition = BinaryOp(ref, Operator("<"), node.expr2).at(node) while_statement = While(while_condition, while_body).at(node) while_statement.set_desugared_for(ref) return Block([initial_assignment, while_statement]).at(node)