home *** CD-ROM | disk | FTP | other *** search
/ Ahoy 1987 May / Ahoy_Magazine_87-05_1987_Double_L_Side_B.d64 / basic2comal.txt < prev    next >
Text File  |  2022-10-26  |  6KB  |  202 lines

  1. BASIC TO COMAL TRANSLATOR
  2.  
  3. by Sol Katz
  4.  
  5. Breathes there a hacker
  6. with soul so dead,
  7. who in his own heart hath not said,
  8. "There has got to be some easy way to
  9. convert all those BASIC programs to
  10. COMAL."
  11.  
  12. (apologies to poetry lovers
  13. everywhere.)
  14.  
  15. Two COMAL 0.14 programs comprise the
  16. basic2comal system. This two part
  17. system is designed to help
  18. programmers convert C64 BASIC
  19. programs to COMAL. Before you get too
  20. excited, it only does about 80% of
  21. the text conversion and none of the
  22. conversion necessary to get rid of
  23. the tortured logic inherent in BASIC
  24. "spaghetti" code.
  25.  
  26. To run the programs, the following
  27. condition must be met:
  28.  
  29. Both COMAL programs and the BASIC
  30. program to be converted must be on
  31. the same disk with enough free disk
  32. space to write several files.
  33.  
  34. The basic2comal system does the
  35. following:
  36.  
  37. * converts BASIC tokens to keywords
  38. * puts spaces around keywords
  39. * converts GOTO # to GOTO GT# (a
  40.   label) and then puts GT#: in the
  41.   right place;
  42. * converts GOSUB # to EX# (a proc
  43.   name) and puts the proc name in the
  44.   right place
  45. * strips BASIC line numbers and puts
  46.   in sequential COMAL line numbers
  47. * converts ON..GOTO and ON..GOSUB to
  48.   CASE structure
  49. * inserts a GOTO in IF expression
  50.   THEN #
  51. * converts user defined BASIC
  52.   functions (DEF FN) to COMAL format
  53. * partially converts LEFT$, MID$ and
  54.   RIGHT$ to COMAL string format,
  55.   using the LEN function and a dummy
  56.   string variable, XX$
  57. * converts RETURN to ENDPROC
  58. * converts all IF..THEN statements to
  59.   IF..THEN..ENDIF block format
  60. * inserts some commas in some PRINT
  61.   statements (BASIC isn't as strict
  62.   as COMAL when separating variables
  63.   from functions like TAB and SPC)
  64. * converts REM to //
  65. * converts NEXT to ENDFOR
  66. * hides BASIC keywords that have no
  67.   COMAL equivalents
  68. * appends the string "OF 10" to all
  69.   DIM variable$(number) statements
  70. * splits all multiple statements on
  71.   one line to separate lines
  72. * several other minor conversions
  73.  
  74. The program leaves the following for
  75. the user to do:
  76.  
  77. * DIM string variables that are not
  78.   BASIC arrays
  79. * convert OPEN statements to COMAL
  80.   format
  81. * initialize numeric variables before
  82.   they are used in logic statements
  83. * insert occasional commas in print
  84.   statements
  85. * substitute the correct string
  86.   variable in the converted LEFT$,
  87.   MID$ and RIGHT$ functions
  88. * figure out how to get the SPC
  89.   function (converted to SPCS) to
  90.   work
  91. * figure out how to get SYS #,# to
  92.   work in COMAL (an undocumented
  93.   BASIC feature that showed up in
  94.   some public domain programs from
  95.   Commodore)
  96. * convert TI, TI$, VAL, GET and other
  97.   functions to COMAL functions (this
  98.   will be much easier in COMAL 2.0
  99.   then in COMAL 0.14)
  100. * change PEEK and POKE to work with
  101.   COMAL addresses
  102. * put quotes around strings in some
  103.   DATA statements
  104. * and last but most important,
  105.   untangle the logic that uses
  106.   subroutine line numbers as targets
  107.   for GOTO statements
  108.  
  109. Basic2comal asks for the name of the
  110. BASIC program file to be translated.
  111. It writes output to two files after
  112. deleting older versions. The first is
  113. called structured'basic. It contains
  114. an ASCII file of the converted BASIC
  115. program (including the original BASIC
  116. line numbers as comments). The second
  117. file (called gotos'and'procs)
  118. contains the line numbers that were
  119. part of GOTO and GOSUB statements.
  120. The GOSUB line numbers were made
  121. negative to overcome the C64
  122. limitation of allowing a maximum of 3
  123. files open at once (the BASIC file,
  124. structured'basic, and
  125. gotos'and'procs). I had to store the
  126. line numbers in a file and split the
  127. program into 2 parts to overcome the
  128. 12K available memory limit in COMAL
  129. 0.14.
  130.  
  131. The first program then chains the
  132. second. It reads the line numbers
  133. into 2 arrays, the positive numbers
  134. into the goto array and the negative
  135. numbers, after convertion to
  136. positive, into the proc array. The
  137. arrays are then sorted. It then opens
  138. structured'basic, compares the sorted
  139. GOTO #s and GOSUB #s with the BASIC
  140. line numbers and inserts them as
  141. LABELs or PROC names in the correct
  142. order. When LABELs and PROC names
  143. occur at the same BASIC line number,
  144. the PROC name is entered first. The
  145. BASIC line numbers are striped off
  146. and new line numbers added. Then each
  147. line is written to a file called
  148. almost'comal.
  149.  
  150. The last step in the automated
  151. conversion is to find (and hopefully
  152. correct) the remaining syntax errors.
  153. I used the dynamic keyboard technique
  154. to put the commands:
  155.  
  156. new
  157. enter "almost'comal"
  158.  
  159. on the screen, and POKEd returns
  160. (CHR$(13)) into the keyboard buffer.
  161. This automatically clears memory and
  162. feeds each line of ASCII code to
  163. COMAL's syntax checking routines. It
  164. is as if you entered the line
  165. invisibly at the keyboard. If the
  166. line is correct it goes on to the
  167. next line. If the line has an error,
  168. the line and standard COMAL error
  169. message will be displayed. You must
  170. deal with the syntax error. If you
  171. don't know what's wrong, make the
  172. whole line a comment by putting a !
  173. right after the line number (a ! is
  174. coverted to // by COMAL for you). If
  175. you use the up or down cursor keys,
  176. the program will stop being entered
  177. at the previous line. If you
  178. accidentally stop the automatic
  179. entering, you can start over by
  180. typing:
  181.  
  182. enter "almost'comal"
  183.  
  184. After the last line has been
  185. automatically entered from the file,
  186. you have a COMAL program that is
  187. ready for debugging. I suggest you
  188. save it under a new name since the
  189. file almost'comal is deleted when
  190. basic2comal is run again.
  191.  
  192. The rest is up to you. Good luck and
  193. don't forget to send copies of your
  194. converted programs to COMAL Users
  195. Group, U.S.A., Limited so that we can
  196. all share them.
  197.  
  198. [This system provides an excellent
  199. starting point for anyone wishing to
  200. improve on it. Please let us know if
  201. you do!]
  202.