http://www.perlmonks.org?node_id=174889

clintp has asked for the wisdom of the Perl Monks concerning the following question:

Well, I've been beating my head into the ground for most of the afternoon trying to solve this particular problem. I've got Parrot BASIC nearly compatable with Microsoft's GW-BASIC. Some things are missing of course (SOUND, POKE, COLOR, etc..) but for the most part it works.

Simple scripts it runs fine. However native Parrot BASIC lacks two things that GW BASIC has: mutiple statements on a line and "ELSE". So the larger scripts I run through a Perl filter that "ports" them to Parrot BASIC clean syntax. It's only about 30 lines of Perl now (most of the work is pouring through thousands of lines of BASIC to make sure I got it right).

The last hurdle is IF-THEN-ELSE. What I need to do is take those statements and convert them to Parrot BASIC's simpler syntax. I can do this by hand just fine. Observe:

If you have an "ELSE" statement, things of the form:
LN IF X then Y ELSE Z
Can generally be re-written as:

LN IF X GOTO LN.A GOTO LN.B LN.A Y GOTO LN.C LN.B Z LN.C REM

Where A, B, C are fractional line numbers (assume they're allowed for a moment). Of course there's other ways to re-write that.

What I'm looking for is a bit of help. I've burned out my brain and can't quite seem to get the code correct. You see, the problem isn't just IF..THEN..ELSE, look at the following examples:

7130 IF B3<>0 THEN PRINT "FROM E TO S":W1=B4:X=B5:GOTO 6920 9810 IF K$="Y" AND RND(1) <.5 THEN GOTO 9820 ELSE GOTO 9770 8020 IF SO=0 THEN SO=1 ELSE SO=0 4835 IF V$="K" THEN A$="+K+" ELSE IF V$="M" THEN A$="!M!" ELSE IF V$=" +R" THEN A$="?R?" 1850 IF K3=0 AND EX(Q1,Q2)=0 THEN GOTO 8500 ELSE GOSUB 6000 1800 IF V$="K" THEN A$="+K+" ELSE IF V$="R" THEN A$="?R?" ELSE IF V$=" +M" THEN A$="!M!":Z1=R1:Z2=R2
My favorite is the last one of course. Note that the ELSE is an ELSE IF, there's two of them, and the final statement is actually a compound statement. WHEE. (And people call Perl write-only!).

If you've got some spare cycles, and want to help a fellow Perl Hacker whose lost his mind -- please do. :) Ultimately, a good test is if you can get the last line (1800) to look something like:

1800 IF V$="K" THEN GOTO 1800.2 1800.1 GOTO 1800.4 1800.2 A$="+K+" 1800.3 GOTO 1800.d 1800.4 IF V$="R" THEN GOTO 1800.6 1800.5 GOTO 1800.8 1800.6 A$="?R?" 1800.7 GOTO 1800.d 1800.8 IF V$="M" THEN GOTO 1800.a 1800.9 GOTO 1800.d 1800.a A$="!M!" 1800.b Z1=R1 1800.c Z2=R2 1800.d REM
Without chucking your sanity. Of course the other lines listed above should do something similar. Don't worry about the structure of *my* program: can you take a text line as shown above, and break it down properly? You don't have to do the compound-statement part (A:B:C). That's handled elsewhere in the code (but don't add more please!).

The only requirement for a solution is that I need to distribute this with a package that I can't alter significantly. So if you need a module, Reality says I'm gonna have to cut-and-paste the relevant bits or the module back into your snippet.