Atari Compiler Problem
I own an Atari 800XL and frequently use Datasoft's BASIC Compiler to compile my own BASIC programs. I recently tried to compile a public domain terminal program called "Amodem 7.1," with unsatisfactory results. The compiler won't accept statements that GOTO or GOSUB a variable or expression. The author of the terminal program used the common memory-saving technique of defining often-used numbers as variables (C1=1, and so on). I have converted the variables back to numbers, but the GOTO and GOSUB statements still refer to expressions (for instance, GOTO 3*100 instead of GOTO C3*C100). Can you write a routine that will take me the rest of way, or lead me on the right track?
Dennis Brenner
Since we don't have the terminal program in question, we can't give a specific answer. However, it's not very practical to write a routine that will solve your problem automatically. You'll need to analyze each of the problem statements to determine whether it always branches to the same destination, or branches to different destinations depending on the controlling variable's value. To explain, say that you find the statement GOTO C3*C100 and discover that C3 = 3 and C100=100. If it's clear that the values of C3 and C100 never change, you can replace the statement with GOTO 300. However, the primary reason for using a variable expression with GOTO or GOSUB is to permit the program to branch to a variety of destinations depending on the variable's value.
For example, say that you find the same statement (GOTO C3*C100) and discover that C3 may have the values 1, 2, or 3 when this statement executes. Program flow will branch to line 100, 200, or 300, depending on the value of C3. In this case, you can't replace the expression with a constant, since that would limit the branch to only one destination. The best alternative is to substitute ON-GOTO and ON-GOSUB. For instance, the statement ON C3 GOTO 100, 200, 300 branches to line 100 when C3 = 1, line 200 when C3 = 2, and so on. To make this work, you must determine all the possible values that the controlling variable (C3 in this case) might have, and compute all the destinations that might be generated by that expression. Once that's done, you'll know which line numbers to put at the end of the ON-GOTO or ON-GOSUB statement.
Most BASIC compilers accept only a subset of all the commands in BASIC, so it's possible that yours might not handle ON-GOTO or ON-GOSUB, either. If that's the case, you could replace the original statement with a string of IF-THEN-GOTO statements (IF C3 = 1 THEN GOTO 100, IF C3 = 2 THEN GOTO 200, etc.). This construction is less efficient, but should work with almost any compiler. Tradeoffs of this sort are inevitable when compiling programs that weren't designed to be compiled.