home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Ahoy 1987 May
/
Ahoy_Magazine_87-05_1987_Double_L_Side_B.d64
/
basic2comal.txt
< prev
next >
Wrap
Text File
|
2022-10-26
|
6KB
|
202 lines
BASIC TO COMAL TRANSLATOR
by Sol Katz
Breathes there a hacker
with soul so dead,
who in his own heart hath not said,
"There has got to be some easy way to
convert all those BASIC programs to
COMAL."
(apologies to poetry lovers
everywhere.)
Two COMAL 0.14 programs comprise the
basic2comal system. This two part
system is designed to help
programmers convert C64 BASIC
programs to COMAL. Before you get too
excited, it only does about 80% of
the text conversion and none of the
conversion necessary to get rid of
the tortured logic inherent in BASIC
"spaghetti" code.
To run the programs, the following
condition must be met:
Both COMAL programs and the BASIC
program to be converted must be on
the same disk with enough free disk
space to write several files.
The basic2comal system does the
following:
* converts BASIC tokens to keywords
* puts spaces around keywords
* converts GOTO # to GOTO GT# (a
label) and then puts GT#: in the
right place;
* converts GOSUB # to EX# (a proc
name) and puts the proc name in the
right place
* strips BASIC line numbers and puts
in sequential COMAL line numbers
* converts ON..GOTO and ON..GOSUB to
CASE structure
* inserts a GOTO in IF expression
THEN #
* converts user defined BASIC
functions (DEF FN) to COMAL format
* partially converts LEFT$, MID$ and
RIGHT$ to COMAL string format,
using the LEN function and a dummy
string variable, XX$
* converts RETURN to ENDPROC
* converts all IF..THEN statements to
IF..THEN..ENDIF block format
* inserts some commas in some PRINT
statements (BASIC isn't as strict
as COMAL when separating variables
from functions like TAB and SPC)
* converts REM to //
* converts NEXT to ENDFOR
* hides BASIC keywords that have no
COMAL equivalents
* appends the string "OF 10" to all
DIM variable$(number) statements
* splits all multiple statements on
one line to separate lines
* several other minor conversions
The program leaves the following for
the user to do:
* DIM string variables that are not
BASIC arrays
* convert OPEN statements to COMAL
format
* initialize numeric variables before
they are used in logic statements
* insert occasional commas in print
statements
* substitute the correct string
variable in the converted LEFT$,
MID$ and RIGHT$ functions
* figure out how to get the SPC
function (converted to SPCS) to
work
* figure out how to get SYS #,# to
work in COMAL (an undocumented
BASIC feature that showed up in
some public domain programs from
Commodore)
* convert TI, TI$, VAL, GET and other
functions to COMAL functions (this
will be much easier in COMAL 2.0
then in COMAL 0.14)
* change PEEK and POKE to work with
COMAL addresses
* put quotes around strings in some
DATA statements
* and last but most important,
untangle the logic that uses
subroutine line numbers as targets
for GOTO statements
Basic2comal asks for the name of the
BASIC program file to be translated.
It writes output to two files after
deleting older versions. The first is
called structured'basic. It contains
an ASCII file of the converted BASIC
program (including the original BASIC
line numbers as comments). The second
file (called gotos'and'procs)
contains the line numbers that were
part of GOTO and GOSUB statements.
The GOSUB line numbers were made
negative to overcome the C64
limitation of allowing a maximum of 3
files open at once (the BASIC file,
structured'basic, and
gotos'and'procs). I had to store the
line numbers in a file and split the
program into 2 parts to overcome the
12K available memory limit in COMAL
0.14.
The first program then chains the
second. It reads the line numbers
into 2 arrays, the positive numbers
into the goto array and the negative
numbers, after convertion to
positive, into the proc array. The
arrays are then sorted. It then opens
structured'basic, compares the sorted
GOTO #s and GOSUB #s with the BASIC
line numbers and inserts them as
LABELs or PROC names in the correct
order. When LABELs and PROC names
occur at the same BASIC line number,
the PROC name is entered first. The
BASIC line numbers are striped off
and new line numbers added. Then each
line is written to a file called
almost'comal.
The last step in the automated
conversion is to find (and hopefully
correct) the remaining syntax errors.
I used the dynamic keyboard technique
to put the commands:
new
enter "almost'comal"
on the screen, and POKEd returns
(CHR$(13)) into the keyboard buffer.
This automatically clears memory and
feeds each line of ASCII code to
COMAL's syntax checking routines. It
is as if you entered the line
invisibly at the keyboard. If the
line is correct it goes on to the
next line. If the line has an error,
the line and standard COMAL error
message will be displayed. You must
deal with the syntax error. If you
don't know what's wrong, make the
whole line a comment by putting a !
right after the line number (a ! is
coverted to // by COMAL for you). If
you use the up or down cursor keys,
the program will stop being entered
at the previous line. If you
accidentally stop the automatic
entering, you can start over by
typing:
enter "almost'comal"
After the last line has been
automatically entered from the file,
you have a COMAL program that is
ready for debugging. I suggest you
save it under a new name since the
file almost'comal is deleted when
basic2comal is run again.
The rest is up to you. Good luck and
don't forget to send copies of your
converted programs to COMAL Users
Group, U.S.A., Limited so that we can
all share them.
[This system provides an excellent
starting point for anyone wishing to
improve on it. Please let us know if
you do!]