I've got an large bunch of legacy code in an old self-conceived scripting language that we compile/translate into javascript.
That language has a conditional jump, jumping to a label. Difference to common goto statement is, that no backward jumps are possible. There are no nested if statements nor loops in that language.
As goto does not exist in javascript, I'm looking for an algorithm that transforms goto mylabel
and mylabel:
into semantically equivalent structure.
I thought of using ifs
but found it not trivial because of the arbitrary nesting of the goto labels.
Example:
if cond1 goto a
do something1
if cond2 goto b
do something2
a:
do something3
if cond3 goto c
do something4
c:
do something5
b:
Could be rewritten as:
lbl_b=false;
lbl_c=false;
lbl_a = cond1;
if (!cond1) {
do something1;
lbl_b = cond2;
if (!lbl_b) {
do something2;
}
}
if (!lbl_b) {
do something3;
lbl_c = cond3;
if (!lbl_c) {
do something4;
}
do something5;
}
However, I was not able to derive a general algorithm from that.