Skip to content

Instantly share code, notes, and snippets.

@muriloadriano
Created August 2, 2011 13:19
Show Gist options
  • Save muriloadriano/1120161 to your computer and use it in GitHub Desktop.
Save muriloadriano/1120161 to your computer and use it in GitHub Desktop.
CodeGen Visitor for FOR expressions
/**
* Generates the opcode for FOR expression
*/
AST_VISITOR(CodeGenVisitor, ForExpr) {
Value* value;
Opcode* jmpz;
Opcode* jmp;
unsigned int start_pos = 0;
if (!expr->isIteratorMode()) {
start_pos = getOpNum();
if (expr->getVarDecl() != NULL) {
expr->getVarDecl()->accept(*this);
expr->setInitializated();
}
if (expr->getCondition()) {
expr->getCondition()->accept(*this);
value = getValue(expr->getCondition());
value->addRef();
}
else {
value = new Value(true);
}
jmpz = emit(OP_JMPZ, &VM::jmpz_handler, value);
if (expr->hasBlock()) {
m_brks.push(OpcodeStack());
expr->getBlock()->accept(*this);
/**
* Points break statements to out of FOR block
*/
while (!m_brks.top().empty()) {
m_brks.top().top()->setJmpAddr1(getOpNum() + 1);
m_brks.top().pop();
}
m_brks.pop();
}
if (expr->getIncrement() != NULL) {
expr->getIncrement()->accept(*this);
}
jmp = emit(OP_JMP, &VM::jmp_handler);
jmp->setJmpAddr2(start_pos);
jmpz->setJmpAddr1(getOpNum());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment