I'm writing an interpreter for a simple DSL and wondering how to implement if-then-else evaluator elegantly. I found one example online from antlr.org but it actually used getNodeIndex() from CommonTreeNodeStream, which is protected, so no use. The best I could do was the following:
ifblock
@init
{
int t = 0;
int f = 0;
}
@after
{
if(c)
stream.push(t);
else
stream.push(f);
statements(); // handle the statements in then or else branch
stream.pop()
}
: ^(IF c=condition {t = input.mark();}. {f = input.mark();}.)
;
This is working, but I am not really satisfied somehow. Is there a better way?