Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

void CodeGen::visitSWhile(SWhile *swhile) { int looploc = code.pos(); // Beginni

ID: 3572058 • Letter: V

Question

void CodeGen::visitSWhile(SWhile *swhile)

{

int looploc = code.pos(); // Beginning of test

swhile->exp_->accept(this);

code.add(I_JR_IF_FALSE); // Jump past the body.

code.add(0);

int patchloc = code.pos() - 1;

swhile->stm_->accept(this); // Body.

code.add(I_JR);

code.add(looploc - (code.pos() - 1)); // offset to looploc

code.at(patchloc) = code.pos() - (patchloc - 1);

}

if this code for while loop:

So what is the code for for loop?

Explanation / Answer

void CodeGen::visitSFor(SFor *sfor) { //DTJ int looploc = code.pos(); // Get start of loop sfor->exp_1->accept(this); code.add(I_JR_IF_FALSE); // Jump past the body code.add(0); int patchloc = code.pos() - 1; sfor->stm_->accept(this); // Body sfor->exp_2->accept(this); code.add(I_JR); code.add(looploc - (code.pos() - 1)); // offset to looploc code.at(patchloc) = code.pos() - (patchloc - 1); }