Untitled
unknown
python
2 years ago
2.6 kB
8
Indexable
def visitFor(self, ctx, o):
# Enter the loop and get the labels for breaking out of it and continuing
o.frame.enterLoop()
labelLoop = o.frame.getNewLabel()
labelExit = o.frame.getBreakLabel()
labelContinue = o.frame.getContinueLabel()
# Generate code for the initialization expression
iniCode, typeIni = self.visit(ctx.init_expr, Access(o.frame, o.sym, False))
idxCode, typeIdx = self.visit(ctx.idxvar , Access(o.frame, o.sym, True))
index = o.frame.getNewIndex()
o.sym[0] += Symbol(ctx.name, typeIni, Index(index))
# Emit a label for the start of the loop
self.emit.printout(iniCode)
self.emit.printout(idxCode)
self.emit.printout(self.emit.emitLABEL(labelLoop, o.frame))
idxCode, typeIdx = self.visit(ctx.idxvar , Access(o.frame, o.sym, False))
self.emit.printout(idxCode)
# Evaluate the loop condition and jump to the end if false
endCode, _ = self.visit(ctx.end_expr, Access(o.frame, o.sym, False))
self.emit.printout(endCode)
self.emit.printout(self.emit.emitREOP('>=', typeIni, o.frame))
self.emit.printout(self.emit.emitIFTRUE(labelExit, o.frame))
# Visit the statement inside the loop
self.visit(ctx.stmt1 , o)
# Emit a label for the continue statement
self.emit.printout(self.emit.emitLABEL(labelContinue, o.frame))
# idxCode, _ = self.visit(ctx.idx, Access(o.frame, o.sym, True, False))
# self.emit.printout(idxCode)
# Generate code for the update expression
idxCodeUpd, typeIdxUpd = self.visit(ctx.idxvar , Access(o.frame, o.sym, False))
self.emit.printout(idxCodeUpd)
updCode, _ = self.visit(ctx.update_expr , Access(o.frame, o.sym, False))
self.emit.printout(updCode)
idxCode, typeIdx = self.visit(ctx.idxvar , Access(o.frame, o.sym, True))
self.emit.printout(self.emit.emitADDOP('+', typeIdx, o.frame))
self.emit.printout(idxCode)
# Jump back to the beginning of the loop
self.emit.printout(self.emit.emitGOTO(labelLoop, o.frame))
# Emit a label for exiting the loop and exit the frame
self.emit.printout(self.emit.emitLABEL(labelExit, o.frame))
o.frame.exitLoop()
for item in sym[0]:
if item.name.name == ctx.name.name:
self.visit(Assign(Id(item.name), x.init))
Editor is loading...
Leave a Comment