Need help updating old BASIC program to Xojo

Emulating GOTO and GOSUB from line-numbered BASICs from 1970’s and 1980’s

I’m working on changing a simple line-number BASIC program to one that will run into XOJO (REALbasic).

Here’s what I’ve done so far (you’ll have to add three TextFields for the example below).

(1) Start with the line-number BASIC program (let’s assume that the word “GOTO” is not optional in the starting program):

120 REM Greatest Common Divisor
130 Dim Number1, Number2, Quotient, Remainder As Single
140 Number1 = Val ( TextFIeld1.Text )
150 Number2 = Val ( TextFIeld2.Text )
160 Quotient = Floor ( Number1 / Number2 )
170 R = Number1 - Quotient * Number2
180 Number1 = Number2
190 Number2B = Remainder
200 If Remainder > 0 Then GOTO 160
210 TextField3.Text = Str (Number1 )

(2) If you see a “GOTO” followed by a line number, change that line number to “L” plus the line number.

120 REM Greatest Common Divisor
130 Dim Number1, Number2, Quotient, Remainder As Single
140 Number1 = Val ( TextFIeld1.Text )
150 Number2 = Val ( TextFIeld2.Text )
160: Quotient = Floor ( Number1 / Number2 )
170 R = Number1 - Quotient * Number2
180 Number1 = Number2
190 Number2 = Remainder
200 If Remainder > 0 Then GOTO L160
210 TextField3.Text = Str (Number1 )

(3) Find that same line number elsewhere in the program at the beginning of a line. If you have not already done so, change the line number to “L” plus the line number plus colon, and put the rest of that line on a brand-new line: Repeat as needed.

120 REM Greatest Common Divisor
130 Dim Number1, Number2, Quotient, Remainder As Single
140 Number1 = Val ( TextFIeld1.Text )
150 Number2 = Val ( TextFIeld2.Text )
160:
Quotient = Floor ( Number1 / Number2 )
170 Remainder = Number1 - Quotient * Number2
180 Number1 = Number2
190 Number2B = Remainder
200 If Remainder > 0 Then GOTO L160
210 TextField3.Text = Str (Number1 )

For simple programs, that should be all you need to do to handle GOTO.

Now for two questions: (1) What code would need to be used to handle GOSUB? (A simple sample progran would be great!) (2) Am I correct in assuming that the line numbers in line-number BASIC programs are essentially only used for GOTO and GOSUB?

Barry Traver

“Remember2B” in the previous post should be “Remember2”. (I couldn’t figure out how to edit my previous post.)

Barry Traver

Converting a GOSUB would require copying that section of code - from the target line number to the next return - into a separate method. This can get really tricky given the spaghetti nature of old line-numbered BASIC code. You will likely have to duplicate code in multiple methods (not that that’s a big deal) if for example, the “subroutine” has multiple entry points. It could get messy in the case where the target GOSUB line is really just a dispatch table into other “subroutines”. But, really, copying large sections of code, much of which won’t be used, into multiple methods probably isn’t a big deal.

Please let me know if I any of the following comments are incorrect.

(1) In the old line-number BASICs, line numbers are needed ONLY for GOTOs and GOSUBs. Elsewhere the liune numbers are just "filler, so to speak. To put it another way, when converting a program from a line-numbered BASIC to XOJO, the line numbers can be simply omitted except for situations involving GOTOs or GOSUB’s.

(2) With GOTO, XOJO and the old line-number work essentially the same way except for the formatting of the line number (XOJO requires that a line number label must begin with a letter and – when the line number is used simply at a line number label – end with a colon. Thus XOJO doesn’t like “100” but is happy with “L100” or “L100:” .

(3) My previous post shows how to convert the old line number BASIC to XOJO for situations involving GOTO. What remains is situations involving GOSUB.

(4) Tim Hare has some good comments on the situation for GOSUB. My approach is simpler, but it may not work, because it may be simplistic rather than simply simpler. For example, it assumes that line numbers are needed ONLY for GOTOs and GOSUBs, which may or may not be a correct assumption.

(5) The very last step in converting the program is to remove line numbers that serve no real purpose. The first step is to make sure the GOTOs work (by following XOJO requirements for line number labels). (See my previous post.) In between is where we handle GOSUB situations.

(6)There are (in my approach) only two significant changes to make in the original code: Replace “GOSUB” with “GOTO” and replace the related “RETURN” with GOTO. My idea here is to have the latter GOTO" (replacing RETURN) point to the line immediately after the GOSUB line.

(7) Some possible problems here… One is that GOSUBs can be nested, so the RETURN must point back to the correct GOTO. Another is that (again) I may have oversimplified the problem. TIm Hare’s mention of GOSUB routines with multiple entry points is very important here, because multiple points may possibly make it impossible to have a more simplified approach.

I’m not worried about nested GOSUBs. I wrote a program to change NEXT in SOJO programs to NEXT COUNTER (e.g., NEXT I, NEXT J, NEXT XMLLineNumber), so I’ve already worked with programs involving nesting and deciding what goes with what. It’s whether multiple entry points make the conversion impossibly complicated. (My desire is to have the conversion done in a totally automated way, a “black box” where you send in an old BASIC program and out comes a working XOJO program.

Barry Traver

SOmehow I missed adding a title for my thread. Is there a staff member who can make the first line of my first post into a title for the thread? That would be much appreciated. Thanks.

Barry Traver

Don’t forget “ON GOTO” and “ON GOSUB”

if your intent is to be able to take “retro code” from the internet and run it thru this process to make it executable then you have other things to worry about as well… such as PRINT, syntax differences in keywords/functions (SQR vs SQRT etc)

You can also have (in spagetti code)… situations where you “GOSUB XXX”, and maybe “just fall into XXX” where the RETURN is logically bypassed. This is a situation where OOP code cannot get into by the very nature of its design

[quote=12892:@Barry Traver]SOmehow I missed adding a title for my thread. Is there a staff member who can make the first line of my first post into a title for the thread? That would be much appreciated. Thanks.

Barry Traver[/quote]
You should be able to do it yourself as the original poster. I changed some of my titles yesterday.

Spaghetti code aside, in a well-formed program, a subroutine would be called from multiple places - you’d see “GOSUB 1000” in several places in the code. RETURN has to go back to the correct place, so replacint it with a simple GOTO won’t work. In spaghetti code, of course, you often see GOTO and GOSUB to the same line number.

[quote=12892:@Barry Traver]SOmehow I missed adding a title for my thread. Is there a staff member who can make the first line of my first post into a title for the thread? That would be much appreciated. Thanks.
[/quote]
I tweaked the title for you, but I believe you can also change the title of your own conversations by just clicking on the title when viewing the conversation.

It looks like my idea of using GOTO instead of a GOSUB plus using another GOTO instead of the related RETURN won’t work.

Would this work?: replacing an entire GOSUB routine in an old line-number BASIC program with a new SOJO SUB to do what the old GOSUB routine used to do.

Barry Traver

P.S. Originally I had planned to write a conversion program that would manipulate a simple text file representing the source code. If I use SUBs, it’s not that simple, but I still may be able to do it by manipulating a Project XML file.

probably not…

10 gosub 10000
20 quit
1000 ' this is a subroutine
1010  ' here is some code that does something
1015 x=x+1
1020  if x=3 then goto 1000
1030 return 

if you make that a method… what would you do with line 1020?

SUB method1000
' here is some code that does something
x=x+1
if x=3 then method1000
END SUB

can’t do that… as it creates a recursive call… what is the scope of X?

you would need something like this

SUB method1000
label1000:
' here is some code that does something
x=x+1
if x=3 then goto label1000
END SUB

but then again… perhaps the code DID need to be recursive calls… not gotos

Hello,
Please see if the following is of any help.

Replace a “conditional GOTO to a smaller line number” with “Do … Loop Until” and negate the condition.

So that the code below

120 REM Greatest Common Divisor
130 Dim Number1, Number2, Quotient, Remainder As Single
140 Number1 = Val ( TextFIeld1.Text )
150 Number2 = Val ( TextFIeld2.Text )
160 Quotient = Floor ( Number1 / Number2 )
170 R = Number1 - Quotient * Number2
180 Number1 = Number2
190 Number2 = Remainder
200 If Remainder > 0 Then GOTO 160
210 TextField3.Text = Str (Number1 )

Becomes

// Greatest Common Divisor
Dim Number1, Number2, Quotient, Remainder As Single
Number1 = Val ( TextFIeld1.Text )
Number2 = Val ( TextFIeld2.Text )
Do
Quotient = Floor ( Number1 / Number2 )
R = Number1 - Quotient * Number2
Number1 = Number2
Number2 = Remainder
Loop Until Remainder <= 0
TextField3.Text = Str (Number1 )

Replace a “conditional GOTO to a greater line number” with “If … End If”.

The best option for replacing a GOSUB is to use a separate method as already discussed by “Tim Hare”.

Regards,

Shahid.