How do I refactor out GOTO statements in GWBASIC code?

400 views Asked by At

I'm currently refactoring a heap of old GWBASIC code into python so it can be run on newer machines. They're all old engineering mathematics programs which are quite maths heavy as well as using the GOTO statement massively throughout.

I'm just wondering how you refactor the statement out? Do you just declare all the variables at global scope and write functions which will modify them or do I just have to rewrite the program logic so it makes sense passing values around or is there a better way of doing it?

Snippet of code below:

25 INPUT "EXISTING FILE (Y/N)";EF$
30 IF EF$="Y" OR EF$="y" THEN 4000
40 LOCATE 4,1:PRINT USING "<######> NUMBER OF JOINTS";NON+1;:GOSUB 1400:IF D$="" THEN 50 ELSE NON=VAL (D$)-1
50 PRINT:PRINT USING "<######> NUMBER OF MEMBER GROUPS";MG+1;:GOSUB 1400:IF D$="" THEN 60 ELSE MG=VAL (D$)-1
60 PRINT:PRINT USING "<######> NUMBER OF MEMBERS";M+1;:GOSUB 1400:IF D$="" THEN 70 ELSE M=VAL (D$)-1
70 PRINT:PRINT USING "<######> NUMBER OF SUPPORTS";NS+1;:GOSUB 1400:IF D$="" THEN 75 ELSE NS=VAL (D$)-1
75 E=200000!:PRINT:PRINT USING "<#########> YOUNGS MODULUS (MPa)";E;:GOSUB 1400:IF D$="" THEN 80 ELSE E=VAL (D$)
80 TEC=.0000117:PRINT:PRINT USING "<##.##^^^^> THERMAL EXPANSION COEFFICIENT";TEC;:GOSUB 1400:IF D$="" THEN 90 ELSE TEC=VAL (D$)
90 GRAV=7850:PRINT:PRINT USING "<#########> MATERIAL DENSITY (kg/m^3)";GRAV;:GOSUB 1400:IF D$="" THEN 180 ELSE GRAV=VAL (D$)
180 DIM N(NON,1),MG(MG,1),M(M,3),ID(M,10),NC(NON,2),SD(NS,3),FE(M,5),R(5),C(5,5),E(5),T(5),SR(NS,2),LD(30,7),LN$(30),TE(M),MD$(MG)
190 F=-1:GOTO 200
195 CLS:F=-1:PRINT:PRINT USING "<###> JOINT NUMBER";F+2;:GOSUB 1410:IF D$="" THEN 200 ELSE F=VAL (D$)-2
200 CLS:PRINT:PRINT"JOINT DATA"
205 F=F+1:IF F>NON THEN 250
210 PRINT:PRINT"JOINT  ";F+1;:PRINT TAB(15);:PRINT USING "<#######>  X CO-ORD (mm)=";N(F,0)*1000!;:INPUT ;D$:IF D$="R" OR D$="r" THEN 1500
220 IF D$="" THEN 230 ELSE N(F,0)=VAL (D$)/1000
230 PRINT TAB(50);:PRINT USING "<#######> Y CO-ORD (mm)=";N(F,1)*1000!;:GOSUB 1410::IF D$="" THEN 240 ELSE N(F,1)=VAL (D$)/1000!
240 GOTO 205
250 IF FL=1 THEN 1500
290 F=-1:GOTO 300
2

There are 2 answers

0
Andrea Scarcella On BEST ANSWER

My two cents:

  1. start from GOSUB statements and identify blocks of code that implement functions (starting from small ones that do not depend on other blocks)
  2. write a python test for it (based on the expected/actual output of the portion of gwbasic code you are porting)
  3. port function (gwbasic block identified in point 1) to Python
  4. run test to check whether it passes/fails

The idea is to use tests to prove that the new system behaves as the old one.

HTH.

Reference: paper on test-driven porting

2
jochietoch On

If all you want to do is run the code on modern machines, you can run it with PC-BASIC and save yourself the refactoring work.

Of course that won't make the code any more understandable or maintainable...