|
|
|
|
 Bas7 interpreter starting info |
 Bas7 interpreter executing hamurabi |
 Bas7 interpreter executing startrek |
Bas7 is a BASIC interpreter which is compatible to GW-BASIC (without graphics)
and other old BASIC dialects.
The Bas7 interpreter shows that Seed7 can be used to write interpreters.
Programs from the line number era of BASIC can be executed without problems.
The primarily intend of Bas7 is to analyze spaghetti code.
Therefore it writes a log file while interpreting the BASIC program.
Since spaghetti code is very common in old BASIC programs, this is very useful.
As porting tool it hopefully helps to port historic programs to modern
programming languages like Seed7.
Bas7 can be called from the command line:
tm@penguin:~/seed7_5/prg$ ls -l hello.bas
-rw-r--r-- 1 tm tm 66 2008-01-17 08:54 hello.bas
tm@penguin:~/seed7_5/prg$ cat hello.bas
100 PRINT "Hello, world!"
110 INPUT "Press return to continue",a$
tm@penguin:~/seed7_5/prg$ ./hi bas7 hello
The command window is cleared and contains:
Hello, world!
Press return to continue
After pressing return the program is finished. The log can be found in the file bas7.log:
tm@penguin:~/seed7_5/prg$ cat bas7.log
100 PRINT "Hello, world!"
110 INPUT "Press return to continue",a$
load_prog finished
100 PRINT "Hello, world!"
110 INPUT "Press return to continue"
110 INPUT A$ <- ""
END OF PROGRAM REACHED
tm@penguin:~/seed7_5/prg$
To avoid adding an 'INPUT' statement at the end of a program it is
possible to call bas7 with the -p option:
./hi bas7 -p foo.bas
That way bas7 asks for a confirmation at the end of the program:
=== Program finished ===
Press return to continue
The Bas7 interpreter implements the following functions and
statements ( ** means recognized but not implemented):
ABS, ASC, ATN, **BEEP, **CALL, CASE, CDBL, CHAIN, CHR$, CINT,
**CIRCLE, CLEAR, CLOSE, CLS, **COLOR, **COMMON, CONST, COS,
CSNG, CSRLIN, CVI, CVL, DATA, DATE$, **DECLARE, DECR, DEF FN,
**DEF SEG, **DEFDBL, DEFINT, **DEFSNG, DEFSTR, DIM, DO,
DO UNTIL, DO WHILE, **DRAW, ELSE, ELSEIF, END, EOF, **ERASE,
ERR, ERROR, EXIT DO, EXIT FOR, EXP, FIX, FOR, FREEFILE, GET,
GET#, GOSUB, GOSUB OF, GOTO, GOTO OF, HEX$, IF, INCR, INKEY$,
**INP, INPUT, INPUT#, INPUT$, INSTR, INT, **KEY, LCASE$, LEFT$,
LEN, LET, LINE INPUT, LINE INPUT#, LOCATE, LOF, LOG, LOOP,
LOOP UNTIL, LOOP WHILE, LSET, LTRIM$, MID$, MKI$, MKL$, NEXT,
OCT$, ON ERROR, ON GOSUB, ON GOTO, **ON KEY, **ON TIMER, OPEN,
OPTION BASE, **OUT, **PAINT, **PALETTE, **PEEK, **PLAY,
**POINT, **POKE, POS, PRESET, PRINT, PRINT#, PRINT USING, PSET,
PUT, PUT#, RANDOMIZE, READ, REDIM, REM, RESET, RESTORE, RESUME,
RETURN, RIGHT$, RND, RSET, RTRIM$, SCREEN, SEEK#, SELECT, SGN,
SIN, SLEEP, **SOUND, SPACE$, SPC, SQR, STOP, STR$, **STRIG,
STRING$, **SUB, SWAP, SYSTEM, TAB, TAN, TIME$, TIMER, UCASE$,
VAL, WEND, WHILE, WRITE, WRITE#
The interpreter uses several technics to support various BASIC dialects:
- There is no limit for the length of: A line, a string, a variable name or a line number.
- Line numbers can contain a decimal point.
- The line number 0 is allowed.
- Lines without line number are allowed.
- The operators <=, >= and <> can also be written as =<, => and >< .
- Operators can be written with two symbols. E.g.: < = and > < instead of <= and >< .
- Hexadecimal numbers can be introduced with '&H' or '&h' and
octal numbers with '&O' or '&o'.
- Instead of 'GOTO' it is possible to write 'GO TO'.
- Keywords can be written adjacent to numbers. E.g.: 'IF I=0THEN 120ELSE 150'
- The BASIC program can contain garbage. As long as the garbage is not executed, it does not hurt.
- When executing a 'FOR' statement the matching 'NEXT' statement is recognized at runtime.
That way a 'FOR' statement can have several corresponding 'NEXT' statements.
This approach works as long as the 'FOR' loop is entered at least once.
When a 'FOR' loop is not entered a statically matching 'NEXT' statement is searched.
- A 'NEXT' statement can be shared by several 'FOR' loops.
- A 'NEXT' statement with a list of variables like 'NEXT X,Y,Z'
is responsible for 'FOR' loops which use one of these variables.
It is not necessary that the variables in the 'NEXT' statement are
ordered.
- A 'NEXT' statement without a variable is responsible for any
'FOR' loop.
- A 'NEXT' statement with a variable like 'NEXT A' causes 'FOR' loops
with other variables like 'FOR I' to be left automatically. An example using
this feature is 'FOR A=B TO C:FOR D=E TO F:IF G(D) THEN NEXT ELSE NEXT A'.
- Unfinished 'FOR' loops of a subprogram are left when the 'RETURN' statement is executed.
Therefore statements like 'FOR A=B TO C:IF D(A) THEN NEXT:RETURN ELSE RETURN' work.
- When a 'NEXT' statement decides to leave a 'FOR' loop the loop variable keeps
the last value. This means that after 'FOR N=1 TO 8:NEXT' the variable 'N' has
the value '8'.
- Computed gotos can be written in two forms. As 'ON ... GOTO labels' and
as 'GOTO ... OF labels'.
- Computed gosubs can be written in two forms. As 'ON ... GOSUB labels' and
as 'GOSUB ... OF labels'.
- A 'GOTO' to a nonexisting line is recognized and the next existing line is used instead.
- A ':' after a 'THEN' is silently ignored.
- Statements like 'IF A=0 THEN 100:PRINT' are executed as 'IF A=0 THEN 100 ELSE PRINT'.
- An 'ELSE' can directly follow a 'THEN'.
- It is not necessary to declare arrays with a 'DIM' statement.
Therefore 'DIM' statements are skipped.
- Array subscripts can be written with parenthesis (E.g.: 'A(10)') or
brackets (E.g.: 'A[10]').
- In 'PRINT' statements it is possible to omit the ';'.
- In 'PRINT' statements it is allowed that 'USING'
clauses come later in the parameter list. E.g.
'100 PRINT"The amount is " USING "##.##";XC'
- Some of the newer keywords such as 'DO' can be used as variables.
- Functions defined with 'DEF FN*' can be called also when
there is a space between 'FN' and the rest of the name.
E.g. After the line '100 DEF FNB(C)=-C' the function
'FNB' can be called with 110 A=FN B(35)'.
- Labels and variables can have the same name.
A statement like 'A:IF B=0 THEN A = 5' does not introduce an infinite loop.
- When a file is opened (with 'OPEN', 'CHAIN' or 'INCLUDE') the file name is
searched with a case insensitive search.
- In a 'FIELD' statement it is allowed that the keyword
'AS' is directly followed by a variable without an intermediate
space. E.g. '100 FIELD#1,128ASDX$'
- 'REM' can not only be used as statement, but also at places where ' is allowed.
- When a statement is starting with the letters 'REM' and cannot
be recognized as 'LET' statement it is handled as remark
(E.g.: '100 REMARKABLY GOOD' is a comment line).
- It is not necessary to put double quotation marks around string constants in
'DATA' statements, unless they contain commas, colons, or significant
leading or trailing spaces.
- Data statements which start with a hex or oct number without
a space as in '500 DATA&Habcd' are recogniced correctly.
- Commas at the end of a 'DATA' line are 'READ' as an empty string.
- Empty 'DATA' fields can be read as 0.0.
- The 'END' statement is allowed to appear in the middle of a program and its
execution stops the program (It works just like a 'STOP' statement).
- In a BASIC source file control-Z is interpreted as end of file.
The interpreter reads the lines of the source file into an array of strings.
There is no internal representation of the program.
|
|