home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1994 #1
/
monster.zip
/
monster
/
PROG_BAS
/
BS2FR202.ZIP
/
BAS2FOR.DOC
< prev
next >
Wrap
Text File
|
1994-01-30
|
13KB
|
344 lines
BAS2FOR.DOC by: Ira F. Kavaler - July, 1992
Version 2.02 - 1/30/94
The BASIC to FORTRAN Language Translator.
-----------------------------------------
This program takes a BASIC program written under BASICA or by one
of my other programs, PROGRAM, and translates it into FORTRAN.
During the 1960's and 1970's my favorite computer language was
FORTRAN IV. When I got my second home computer, a TRS-80 Model I, I
had to concentrate on writing my programs in BASIC. BASIC is a great
language; it is far more powerful than FORTRAN, then and now.
I love writing utilities. One day I wanted to see how difficult
it would be to write a language translator, so I wrote BAS2FOR. It is
by no means complete nor perfectly accurate. That's my fault! I have
so many interests that I roam from problem to problem, usually I switch
developing solutions to these problems when I get tired of the last.
In time I do get back and clean up the mess I started. So, don't be
discouraged if you find one of the many opened ends in this program.
When you see an updated version it means I got back to it and cleaned-
up some.
BASIC is the Child of FORTRAN.
------------------------------
Although the boys at Dartmouth will disagree, BASIC is an
outgrowth of FORTRAN. Once you learn both languages you will also
agree. In fact, if you learn ALGOL (another ancient scientific
computer language) you will discover it is also very similar to
FORTRAN.
The Process.
------------
In order to translate a BASIC program into FORTRAN a few steps
must be taken:
1. The BASIC program must be saved in the ASCII format.
2. The ASCII formatted BASIC program file is converted so that it
does not contain any multi-instruction lines. (This is a form of
Structured BASIC, but it does not have the indenting
characteristic of true Structured BASIC .)
3. The Structured BASIC program file is then translated into FORTRAN
program file, which will be compatible with MicroSoft FORTRAN
Compiler.
BASIC program files that are saved as non-ASCII files (using the
usual SAVE "filename" command are called "tokenized" or "compressed"
files. The term "compressed" should not be confused with compression
techniques used by other compression programs such as PKZIP by PKWARE,
etc.
BASIC has its own special compression scheme that reduces the
number of bytes required to store a BASIC program, not only on tape or
disk, but also in memory! Most BASIC commands are stores as one byte
rather than one byte per letter of the command. Some more advanced
commands and certain line number references are stores as two bytes,
again rather than one byte per digit. In fact, the EDIT, LIST and
LLIST commands are probably the only routines requiring that these
tokens be expanded into ASCII characters.
I have included a number of sample program files that I wrote as
separate teaching and demonstration examples that can be translated:
1. WALLPAPR.BAS is a standard "tokenized" BASIC program file that
calculates the amount of wallpaper needed to cover the walls of a
normal rectanglar shaped room.
2. RECHKBK.BAS is a standard "tokenized" BASIC program file that
performs the same steps required to reconcile a check book against
the monthly statement.
3. PRIME.BAS is a standard "tokenized" BASIC program file that can do
two things; first, if you give it a positive number it will
determine if that number is prime, or if you give it a negative
number it will display all prime number between 1 and that
absolute value of that number. (A prime number is an positive
integer that is only evenly divisible by one and itself.)
Here's what you do.
-------------------
Let's say we want to translate WALLPAPR into FORTRAN. The
tokenized program filename is WALLPAPR.BAS
1. Save the BASIC file in ASCII format:
(TXT is the customary file extension used for ASCII files.)
BASICA Load your BASIC interpreter.
LOAD "WALLPAPR" Load the program file.
SAVE "WALLPAPR.TXT",A Save the ASCII formatted file.
SYSTEM Exit your BASIC interpreter.
2. Translate the ASCII formatted BASIC file to FORTRAN:
(FOR is the customary file extension used for FORTRAN files.)
BAS2FOR Start the BASIC to FORTRAN translator.
WALLPAPR.TXT Give the filename of the ASCII formatted
program file.program
{ENTER} Specify the same path for the
intermediate Structured BASIC file.
y Choose yes to print the Structured BASIC
file along side the translated file.
{ENTER} Specify the same path for the file
FORTRAN file.
You can now use a FORTRAN compiler, such as MicroSoft's, to
compile the FORTRAN source code into machine language object code.
Here's a sample of what to expect.
----------------------------------
The source BASIC program is called "wallpapr" (WALLPAPR.BAS). It
calculates the number of single rolls of wallpaper required for a
rectangular shaped room. The following is the original program as it
was saved in ASCII as WALLPAPR.TXT:
10 ' WALLPAPR.BAS - 1/25/94
15 ' --- Calculate the number of single rolls of wallpaper needed
17 ' for a rectangular room of known dimensions.
20 INPUT "Room width (ft.) ";W: INPUT "Room length (ft.) ";L
40 INPUT "Ceiling height (ft.) ";H: P=2*L+2*W: A=P*H: N=A/30
70 INPUT "Number of windows";NW: INPUT "Number of doors";ND
90 OP=NW+ND: SR=N-OP/2: IF INT(SR)<>SR THEN SR=INR(SR)+1
110 PRINT: PRINT SR;"single rolls": PRINT "without the ceiling."
130 A=A+L*W: N=A/30: SR=N-OP/2: IF INT(SR)<>SR THEN SR=INT(SR)+1
140 PRINT: PRINT SR;"single rolls": PRINT "including the ceiling.": END
The above program contains many multi-instruction lines. The
Structured BASIC file, as it was converted, is listed below, with the
multi-instruction lines replaced by single instruction lines:
10 ' WALLPAPR.BAS - 1/25/94
15 ' --- Calculate the number of single rolls of wallpaper needed
17 ' for a rectangular room of known dimensions.
20 INPUT "Room width (ft.) ";W
INPUT "Room length (ft.) ";L
40 INPUT "Ceiling height (ft.) ";H
P=2*L+2*W
A=P*H
N=A/30
70 INPUT "Number of windows";NW
INPUT "Number of doors";ND
90 OP=NW+ND
SR=N-OP/2
IF INT(SR)<>SR THEN SR=INR(SR)+1
110 PRINT
PRINT SR;"single rolls"
PRINT "without the ceiling."
130 A=A+L*W
N=A/30
SR=N-OP/2
IF INT(SR)<>SR THEN SR=INT(SR)+1
140 PRINT
PRINT SR;"single rolls"
PRINT "including the ceiling."
END
The output file translated to FORTRAN is shown below. Where
necessary BAS2FOR inserts any additional instructions needed to support
the translation:
C WALLPAPR.BAS - 1/25/94
10 CONTINUE
C --- Calculate the number of single rolls of wallpaper needed
15 CONTINUE
C for a rectangular room of known dimensions.
17 CONTINUE
20 WRITE (6,5)
5 FORMAT (1X,17HRoom width (ft.) )
READ (5,25) W
25 FORMAT (F16.6)
WRITE (6,45)
45 FORMAT (1X,18HRoom length (ft.) )
READ (5,35) L
35 FORMAT (I10)
40 WRITE (6,65)
65 FORMAT (1X,21HCeiling height (ft.) )
READ (5,55) H
55 FORMAT (F16.6)
P=2*L+2*W
A=P*H
N=A/30
70 WRITE (6,85)
85 FORMAT (1X,17HNumber of windows)
READ (5,75) NW
75 FORMAT (I10)
WRITE (6,105)
105 FORMAT (1X,15HNumber of doors)
READ (5,95) ND
95 FORMAT (I10)
90 OP=NW+ND
SR=N-OP/2
SR=INR(SR)+1
IF (.NOT.(AINT(SR).NE.SR)) GO TO 50
SR=INR(SR)+1
50 CONTINUE
110 WRITE (6,115)
115 FORMAT (1X)
WRITE (6,125) SR
125 FORMAT (1X,F16.6,12Hsingle rolls)
WRITE (6,135)
135 FORMAT (1X,20Hwithout the ceiling.)
130 A=A+L*W
N=A/30
SR=N-OP/2
SR=AINT(SR)+1
IF (.NOT.(AINT(SR).NE.SR)) GO TO 60
SR=AINT(SR)+1
60 CONTINUE
140 WRITE (6,145)
145 FORMAT (1X)
WRITE (6,155) SR
155 FORMAT (1X,F16.6,12Hsingle rolls)
WRITE (6,165)
165 FORMAT (1X,22Hincluding the ceiling.)
STOP
END
Why translate?
--------------
Compiled program files run faster than interpreter based program
files. Why not try an experiment. Process the PRIME.BAS file into
FORTRAN. Run the original BASIC version giving the number as -1000.
Using a stop watch time the period it takes for the answers to be
displayed. This will cause the program to generate all prime numbers
between one and 1,000. If you have a FORTRAN compiler, compile the
translated file into the object file PRIME.EXE. Run the EXE file with
the same -1000 argument. Again time the primes number generation. If
should be much quicker!
Present Limitations.
--------------------
I have not completed this program. I have written just enough
code to translate most simple and some intermediate BASIC instructions.
Although I said that BASIC is an outgrowth of FORTRAN, ther are serious
differences:
1. FORTRAN uses the first character of variable names to indicate its
precision; A to H and O to Z are real or single precision variables;
while, I to N are integer variable. I have not implemented this except
for the INPUT / READ and and PRINT / WRITE commands.
2. BASIC uses global variables; while, FORTRAN uses local variables.
A global variable has the same value in the main program and all
subroutines. A local variable can have different values at the same
time in each program module it appears: main program, subroutine, and
function. (This is similar to BASIC's DEF FN instruction.) I have not
implemented a FORTRAN COMMON statement to pass arguments to
subroutines, although the subroutine structure has been implemented.
3. Only BASIC commands that translate into a single FORTRAN c ommand
can be part of the IF...THEN command.
Here's the small print.
-----------------------
All versions of this program including its related files are being
distributed on an "AS IS" basis. There is absolutely no stated or
implied guarantee or warrantee of usability for any purpose or
correctness of the formulas and procedures contained in any file.
If you happen to discover an error in the program I will make
every attempt to correct the error as quickly as possible. I am under
no obligation to replace nor make refunds for defective full versions
or demonstration/trial versions of the program. I have to take this
posture as my cost to make even the simplest of corrections far
outweighs any monetary compensation received for the full version of
the program.
If you require any special modifications to the program I will be
happy to discuss on an individual basis the cost of supplying modified
programs and documentation.
The program was tested on a Tandy model 1000 SX using MS-DOS 3.3;
IBM PS/2 model 80, using PC-DOS 3.3; and a Compaq Desk-Pro 486/33 using
MS-DOS 6.0.
And now a word from our sponsor.
--------------------------------
You can get the latest version of this program by registering the
program. When you register I will also include any other demonstration
/ trail programs that I have available. Please send $20.00 for an IBM
compatible 5-1/4 or 3-1/2 inch 720 DS/DD or HD disk(s), your choice,
to:
IRA F. KAVALER
671 East 78 Street
Brooklyn, New York 11236
All inquiries that do not include the registration fee must be
accompanied by a stamped self addressed return envelope.
I reserve the right to discontinue support for, change the terms,
or withdraw any part or all of this offer including but not limited to
the programs and its associated files at any time without giving prior
notice.
No form of this program, registered or unregistered, may be used
in commercial, educational, nor governmental applications without
written authorization or a site lease from the author; such
authorization and/or site lease may require that a substantial fee be
paid to the author.
73's, de WA2ZIR.
----------------
I welcome your suggestions and comments about this product and
others. I won't promise that good suggestions will be added to the
program, but they will be considered.
Thank you.
Appendix.
---------
The operating systems, programs and companies mentioned in this file:
PKWARE, PKZIP, PKUNZIP, MS-DOS, PC-DOS, LIST, BROWSE, SIMCGA, Hercules,
MicroSoft, Tandy Compaq, Arche, and IBM are all copyrights, trademarks,
and/or service marks of other individuals or other corporations.
>>>>> End of File <<<<<