Skip to content

Instantly share code, notes, and snippets.

@cedric-sun
Last active December 2, 2019 06:59
Show Gist options
  • Save cedric-sun/c388936c028cdc00e37fcbed16bb34f4 to your computer and use it in GitHub Desktop.
Save cedric-sun/c388936c028cdc00e37fcbed16bb34f4 to your computer and use it in GitHub Desktop.
the handling of a block in the recursive interpreter of a subgrammar of Lua
@Override
public Object visitBlock(Block block, Object arg) throws Exception {
for (int i = 0; i < block.stats.size(); i++) {
Stat stat = block.stats.get(i);
try {
stat.visit(this, arg);
} catch (BreakException e) {
// need to know if break inside a loop
if (stat instanceof StatWhile ||
stat instanceof StatRepeat) {
continue;
}
throw e;
} catch (GotoException e) {
StatLabel target = e.target;
if (target.enclosingBlock == block) {
i = target.index;
} else {
throw e;
}
} catch (ReThrownReturnException e) {
throw e;
} catch (ReturnException e) {
if (i != block.stats.size() - 1)
throw new LuaRuntimeException(
"Return statement is not the last statement in its block."
);
throw new ReThrownReturnException(e);
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment