home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_BAS / BS2FR202.ZIP / BAS2FOR.DOC < prev    next >
Text File  |  1994-01-30  |  13KB  |  344 lines

  1. BAS2FOR.DOC                      by:  Ira F. Kavaler - July, 1992
  2. Version 2.02 - 1/30/94
  3.  
  4.  
  5.      The BASIC to FORTRAN Language Translator.
  6.      -----------------------------------------
  7.  
  8.      This program takes a BASIC program written under BASICA or by one
  9. of my other programs, PROGRAM, and translates it into FORTRAN.
  10.  
  11.      During the 1960's and 1970's my favorite computer language was
  12. FORTRAN IV.  When I got my second home computer, a TRS-80 Model I, I
  13. had to concentrate on writing my programs in BASIC.  BASIC is a great
  14. language;  it is far more powerful than FORTRAN, then and now.
  15.  
  16.      I love writing utilities.  One day I wanted to see how difficult
  17. it would be to write a language translator, so I wrote BAS2FOR.  It is
  18. by no means complete nor perfectly accurate.  That's my fault!  I have
  19. so many interests that I roam from problem to problem, usually I switch
  20. developing solutions to these problems when I get tired of the last.
  21. In time I do get back and clean up the mess I started.  So, don't be
  22. discouraged if you find one of the many opened ends in this program.
  23. When you see an updated version it means I got back to it and cleaned-
  24. up some.
  25.  
  26.  
  27.      BASIC is the Child of FORTRAN.
  28.      ------------------------------
  29.  
  30.      Although the boys at Dartmouth will disagree, BASIC is an
  31. outgrowth of FORTRAN.  Once you learn both languages you will also
  32. agree.  In fact, if you learn ALGOL (another ancient scientific
  33. computer language) you will discover it is also very similar to
  34. FORTRAN.
  35.  
  36.  
  37.      The Process.
  38.      ------------
  39.  
  40.      In order to translate a BASIC program into FORTRAN a few steps
  41. must be taken:
  42.  
  43. 1.   The BASIC program must be saved in the ASCII format.
  44. 2.   The ASCII formatted BASIC program file is converted so that it
  45.      does not contain any multi-instruction lines.  (This is a form of
  46.      Structured BASIC, but it does not have the indenting
  47.      characteristic of true Structured BASIC .)
  48. 3.   The Structured BASIC program file is then translated into FORTRAN
  49.      program file, which will be compatible with MicroSoft FORTRAN
  50.      Compiler.
  51.  
  52.  
  53.      BASIC program files that are saved as non-ASCII files (using the
  54. usual SAVE "filename" command are called "tokenized" or "compressed"
  55. files.  The term "compressed" should not be confused with compression
  56. techniques used by other compression programs such as PKZIP by PKWARE,
  57. etc.
  58.  
  59.      BASIC has its own special compression scheme that reduces the
  60. number of bytes required to store a BASIC program, not only on tape or
  61. disk, but also in memory!  Most BASIC commands are stores as one byte
  62. rather than one byte per letter of the command.  Some more advanced
  63. commands and certain line number references are stores as two bytes,
  64. again rather than one byte per digit.  In fact, the EDIT, LIST and
  65. LLIST commands are probably the only routines requiring that these
  66. tokens be expanded into ASCII characters.
  67.  
  68.      I have included a number of sample program files that I wrote as
  69. separate teaching and demonstration examples that can be translated:
  70.  
  71. 1.   WALLPAPR.BAS is a standard "tokenized" BASIC program file that
  72.      calculates the amount of wallpaper needed to cover the walls of a
  73.      normal rectanglar shaped room.
  74.  
  75. 2.   RECHKBK.BAS is a standard "tokenized" BASIC program file that
  76.      performs the same steps required to reconcile a check book against
  77.      the monthly statement.
  78.  
  79. 3.   PRIME.BAS is a standard "tokenized" BASIC program file that can do
  80.      two things;  first, if you give it a positive number it will
  81.      determine if that number is prime, or if you give it a negative
  82.      number it will display all prime number between 1 and that
  83.      absolute value of that number.  (A prime number is an positive
  84.      integer that is only evenly divisible by one and itself.)
  85.  
  86.  
  87.      Here's what you do.
  88.      -------------------
  89.  
  90.      Let's say we want to translate WALLPAPR into FORTRAN.  The
  91. tokenized program filename is WALLPAPR.BAS
  92.  
  93. 1.   Save the BASIC file in ASCII format:
  94.      (TXT is the customary file extension used for ASCII files.)
  95.  
  96.      BASICA                   Load your BASIC interpreter.
  97.      LOAD "WALLPAPR"          Load the program file.
  98.      SAVE "WALLPAPR.TXT",A    Save the ASCII formatted file.
  99.      SYSTEM                   Exit your BASIC interpreter.
  100.  
  101. 2.   Translate the ASCII formatted BASIC file to FORTRAN:
  102.      (FOR is the customary file extension used for FORTRAN files.)
  103.  
  104.      BAS2FOR                  Start the BASIC to FORTRAN translator.
  105.      WALLPAPR.TXT             Give the filename of the ASCII formatted
  106.                               program file.program
  107.      {ENTER}                  Specify the same path for the
  108.                               intermediate Structured BASIC file.
  109.      y                        Choose yes to print the Structured BASIC
  110.                               file along side the translated file.
  111.      {ENTER}                  Specify the same path for the file
  112.                               FORTRAN file.
  113.  
  114.      You can now use a FORTRAN compiler, such as MicroSoft's, to
  115. compile the FORTRAN source code into machine language object code.
  116.  
  117.  
  118.      Here's a sample of what to expect.
  119.      ----------------------------------
  120.  
  121.      The source BASIC program is called "wallpapr" (WALLPAPR.BAS).  It
  122. calculates the number of single rolls of wallpaper required for a
  123. rectangular shaped room.  The following is the original program as it
  124. was saved in ASCII as WALLPAPR.TXT:
  125.  
  126. 10 ' WALLPAPR.BAS - 1/25/94
  127. 15 ' --- Calculate the number of single rolls of wallpaper needed
  128. 17 '     for a rectangular room of known dimensions.
  129. 20 INPUT "Room width (ft.) ";W: INPUT "Room length (ft.) ";L
  130. 40 INPUT "Ceiling height (ft.) ";H: P=2*L+2*W: A=P*H: N=A/30
  131. 70 INPUT "Number of windows";NW: INPUT "Number of doors";ND
  132. 90 OP=NW+ND: SR=N-OP/2: IF INT(SR)<>SR THEN SR=INR(SR)+1
  133. 110 PRINT: PRINT SR;"single rolls": PRINT "without the ceiling."
  134. 130 A=A+L*W: N=A/30: SR=N-OP/2: IF INT(SR)<>SR THEN SR=INT(SR)+1
  135. 140 PRINT: PRINT SR;"single rolls": PRINT "including the ceiling.": END
  136.  
  137.  
  138.      The above program contains many multi-instruction lines.  The
  139. Structured BASIC file, as it was converted, is listed below, with the
  140. multi-instruction lines replaced by single instruction lines:
  141.  
  142. 10 ' WALLPAPR.BAS - 1/25/94
  143. 15 ' --- Calculate the number of single rolls of wallpaper needed
  144. 17 '     for a rectangular room of known dimensions.
  145. 20 INPUT "Room width (ft.) ";W
  146.    INPUT "Room length (ft.) ";L
  147. 40 INPUT "Ceiling height (ft.) ";H
  148.    P=2*L+2*W
  149.    A=P*H
  150.    N=A/30
  151. 70 INPUT "Number of windows";NW
  152.    INPUT "Number of doors";ND
  153. 90 OP=NW+ND
  154.    SR=N-OP/2
  155.    IF INT(SR)<>SR THEN SR=INR(SR)+1
  156. 110 PRINT
  157.     PRINT SR;"single rolls"
  158.     PRINT "without the ceiling."
  159. 130 A=A+L*W
  160.     N=A/30
  161.     SR=N-OP/2
  162.     IF INT(SR)<>SR THEN SR=INT(SR)+1
  163. 140 PRINT
  164.     PRINT SR;"single rolls"
  165.     PRINT "including the ceiling."
  166.     END
  167.  
  168.  
  169.      The output file translated to FORTRAN is shown below.  Where
  170. necessary BAS2FOR inserts any additional instructions needed to support
  171. the translation:
  172.  
  173. C     WALLPAPR.BAS - 1/25/94
  174. 10    CONTINUE
  175. C     --- Calculate the number of single rolls of wallpaper needed
  176. 15    CONTINUE
  177. C     for a rectangular room of known dimensions.
  178. 17    CONTINUE
  179. 20    WRITE (6,5)
  180. 5     FORMAT (1X,17HRoom width (ft.) )
  181.       READ (5,25) W
  182. 25    FORMAT (F16.6)
  183.       WRITE (6,45)
  184. 45    FORMAT (1X,18HRoom length (ft.) )
  185.       READ (5,35) L
  186. 35    FORMAT (I10)
  187. 40    WRITE (6,65)
  188. 65    FORMAT (1X,21HCeiling height (ft.) )
  189.       READ (5,55) H
  190. 55    FORMAT (F16.6)
  191.       P=2*L+2*W
  192.       A=P*H
  193.       N=A/30
  194. 70    WRITE (6,85)
  195. 85    FORMAT (1X,17HNumber of windows)
  196.       READ (5,75) NW
  197. 75    FORMAT (I10)
  198.       WRITE (6,105)
  199. 105   FORMAT (1X,15HNumber of doors)
  200.       READ (5,95) ND
  201. 95    FORMAT (I10)
  202. 90    OP=NW+ND
  203.       SR=N-OP/2
  204.       SR=INR(SR)+1
  205.       IF (.NOT.(AINT(SR).NE.SR)) GO TO 50
  206.       SR=INR(SR)+1
  207. 50    CONTINUE
  208. 110   WRITE (6,115)
  209. 115   FORMAT (1X)
  210.       WRITE (6,125) SR
  211. 125   FORMAT (1X,F16.6,12Hsingle rolls)
  212.       WRITE (6,135)
  213. 135   FORMAT (1X,20Hwithout the ceiling.)
  214. 130   A=A+L*W
  215.       N=A/30
  216.       SR=N-OP/2
  217.       SR=AINT(SR)+1
  218.       IF (.NOT.(AINT(SR).NE.SR)) GO TO 60
  219.       SR=AINT(SR)+1
  220. 60    CONTINUE
  221. 140   WRITE (6,145)
  222. 145   FORMAT (1X)
  223.       WRITE (6,155) SR
  224. 155   FORMAT (1X,F16.6,12Hsingle rolls)
  225.       WRITE (6,165)
  226. 165   FORMAT (1X,22Hincluding the ceiling.)
  227.       STOP
  228.       END
  229.  
  230.  
  231.      Why translate?
  232.      --------------
  233.  
  234.      Compiled program files run faster than interpreter based program
  235. files.  Why not try an experiment.  Process the PRIME.BAS file into
  236. FORTRAN.  Run the original BASIC version giving the number as -1000.
  237. Using a stop watch time the period it takes for the answers to be
  238. displayed.  This will cause the program to generate all prime numbers
  239. between one and 1,000.  If you have a FORTRAN compiler, compile the
  240. translated file into the object file PRIME.EXE.  Run the EXE file with
  241. the same -1000 argument.  Again time the primes number generation.  If
  242. should be much quicker!
  243.  
  244.  
  245.      Present Limitations.
  246.      --------------------
  247.  
  248.      I have not completed this program.  I have written just enough
  249. code to translate most simple and some intermediate BASIC instructions.
  250. Although I said that BASIC is an outgrowth of FORTRAN, ther are serious
  251. differences:
  252.  
  253. 1.   FORTRAN uses the first character of variable names to indicate its
  254. precision; A to H and O to Z are real or single precision variables;
  255. while, I to N are integer variable.  I have not implemented this except
  256. for the INPUT / READ and and PRINT / WRITE commands.
  257.  
  258. 2.   BASIC uses global variables;  while, FORTRAN uses local variables.
  259. A global variable has the same value in the main program and all
  260. subroutines.  A local variable can have different values at the same
  261. time in each program module it appears: main program, subroutine, and
  262. function.  (This is similar to BASIC's DEF FN instruction.)  I have not
  263. implemented a FORTRAN COMMON statement to pass arguments to
  264. subroutines, although the subroutine structure has been implemented.
  265.  
  266. 3.   Only BASIC commands that translate into a single FORTRAN c ommand
  267. can be part of the IF...THEN command.
  268.  
  269.  
  270.      Here's the small print.
  271.      -----------------------
  272.  
  273.      All versions of this program including its related files are being
  274. distributed on an "AS IS" basis.  There is absolutely no stated or
  275. implied guarantee or warrantee of usability for any purpose or
  276. correctness of the formulas and procedures contained in any file.
  277.  
  278.      If you happen to discover an error in the program I will make
  279. every attempt to correct the error as quickly as possible.  I am under
  280. no obligation to replace nor make refunds for defective full versions
  281. or demonstration/trial versions of the program.  I have to take this
  282. posture as my cost to make even the simplest of corrections far
  283. outweighs any monetary compensation received for the full version of
  284. the program.
  285.  
  286.      If you require any special modifications to the program I will be
  287. happy to discuss on an individual basis the cost of supplying modified
  288. programs and documentation.
  289.  
  290.      The program was tested on a Tandy model 1000 SX using MS-DOS 3.3;
  291. IBM PS/2 model 80, using PC-DOS 3.3; and a Compaq Desk-Pro 486/33 using
  292. MS-DOS 6.0.
  293.  
  294.  
  295.      And now a word from our sponsor.
  296.      --------------------------------
  297.  
  298.      You can get the latest version of this program by registering the
  299. program.  When you register I will also include any other demonstration
  300. / trail programs that I have available.  Please send $20.00 for an IBM
  301. compatible 5-1/4 or 3-1/2 inch 720 DS/DD or HD disk(s), your choice,
  302. to:
  303.  
  304.                                 IRA F. KAVALER
  305.                               671 East 78 Street
  306.                            Brooklyn, New York 11236
  307.  
  308.  
  309.      All inquiries that do not include the registration fee must be
  310. accompanied by a stamped self addressed return envelope.
  311.  
  312.      I reserve the right to discontinue support for, change the terms,
  313. or withdraw any part or all of this offer including but not limited to
  314. the programs and its associated files at any time without giving prior
  315. notice.
  316.  
  317.      No form of this program, registered or unregistered,  may be used
  318. in commercial, educational, nor governmental applications without
  319. written authorization or a site lease from the author;  such
  320. authorization and/or site lease may require that a substantial fee be
  321. paid to the author.
  322.  
  323.  
  324.      73's, de WA2ZIR.
  325.      ----------------
  326.  
  327.      I welcome your suggestions and comments about this product and
  328. others.  I won't promise that good suggestions will be added to the
  329. program, but they will be considered.
  330.  
  331.  
  332. Thank you.
  333.  
  334.  
  335.      Appendix.
  336.      ---------
  337.  
  338. The operating systems, programs and companies mentioned in this file:
  339. PKWARE, PKZIP, PKUNZIP, MS-DOS, PC-DOS, LIST, BROWSE, SIMCGA, Hercules,
  340. MicroSoft, Tandy Compaq, Arche, and IBM are all copyrights, trademarks,
  341. and/or service marks of other individuals or other corporations.
  342.  
  343.  
  344. >>>>>  End of File  <<<<<