Entry
BBCBASIC: Windows: Interpreter: Command: Line: How to create a simple command line interpreter? 4
Feb 5th, 2006 17:38
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 02 February 2021 - 08:03 pm -------------------
BBCBASIC: Windows: Interpreter: Command: Line: How to create a simple
command line interpreter? 4
---
You can use a REPEAT UNTIL loop,
containing
1. -Some input mechanism for commands
2. -Search for the commands in an array
1. -If the inputted commands are recognized,
do something (e.g. call a function or
procedure)
1. -Call the function as a parameter
1. -Read the commands and the
corresponding functions via
an external file
2. -The functions are added
from an external file
using
INSTALL @dir$+"mylib"
(see help INSTALL)
This will give maximum flexibility,
as you can change the commands, functionnames
and functionbodies from
outside of the main program, without
having the reprogram the main
interpreter loop.
2. You can create your own programs by putting
the statements in DATA statements in a 4th file
This is a specific program written in
your new little interpreted language.
By further adding control structures like IF/THEN, REPEAT/UNTIL
loops, FOR/NEXT loops, reading extra parameters (e.g.
separated by spaces) after the keywords, you can quickly build
e.g. a LOGO like interpreter.
e.g. something with a command like
REPEAT 10 times create circle 10 100 color yellow ENDREPEAT
===
Steps: Overview:
1. You create 4 programs
1. -The main program file
2. -The file containing the data for
the commands and function names
3. -The file containing the functions
itself
4. -The file containing your program
2. You use the INSTALL command
to include the information in
the main file
===
Steps: Worked out
1. -Create and save e.g. the following 1st main program
1. -Save this file as
InterpreterRunMain
--- cut here: begin --------------------------------------------------
REM save file as 'InterpreterRunMain.bbc'
:
I% = 0
minI% = 1
maxI% = 5
s$ = ""
command$ = ""
function$ = ""
quitB% = FALSE
foundB% = FALSE
maxB% = FALSE
:
DIM commandWord$( maxI% )
DIM commandFunction$( maxI% )
:
INSTALL @dir$ + "InterpreterRunDataFunctionLibraryFile"
:
INSTALL @dir$ + "InterpreterRunDataLibraryFile"
:
PROCInterpreterDataRestore : REM by design necessary for INSTALLed data
:
FOR I% = minI% TO maxI%
READ commandWord$( I% )
READ commandFunction$( I% )
NEXT I%
:
INSTALL @dir$ + "InterpreterRunDataProgramFile"
:
PROCInterpreterRunDataProgramFileRestore : REM by design necessary for
INSTALLed data
:
REPEAT
READ s$
PRINT "command = "; s$
REPEAT UNTIL GET
I% = minI% - 1
foundB% = FALSE
REPEAT
I% = I% + 1
command$ = commandWord$( I% )
IF s$ = command$ THEN function$ = commandFunction$( I% ) : function$
= function$ + " s$ )" : foundB% = TRUE : IF EVAL( function$ ) THEN
PRINT "command runned successful"
maxB% = ( I% >= maxI% )
UNTIL foundB% OR maxB% OR quitB%
IF NOT foundB% AND maxB% THEN PRINT "unknown command"
UNTIL quitB%
:
END
--- cut here: end ----------------------------------------------------
2. -Create the 2nd file containing the data for
the commands and function names
1. -Save this file as
InterpreterRunDataLibraryFile
--- cut here: begin --------------------------------------------------
REM save file as 'InterpreterRunDataLibraryFile.bbc'
:
DEF PROCInterpreterDataRestore
RESTORE +1
ENDPROC
:
DEF PROCInterpreterData
DATA "cls", "FNInterpreterRunClearScreenB("
DATA "beep", "FNInterpreterRunBeepB("
DATA "print", "FNInterpreterRunPrintB("
DATA "wait", "FNInterpreterRunPrintB("
DATA "quit", "FNInterpreterRunQuitB("
ENDPROC
:
--- cut here: end ----------------------------------------------------
3. -Create the 3rd file containing the functions
itself
1. -Save this file as
InterpreterRunDataFunctionLibraryFile
--- cut here: begin --------------------------------------------------
REM save file as 'InterpreterRunDataFunctionLibraryFile.bbc'
:
DEF FNInterpreterRunClearScreenB( s$ )
CLS
= TRUE
:
DEF FNInterpreterRunBeepB( s$ )
VDU 7
= TRUE
:
DEF FNInterpreterRunPrintB( s$ )
PRINT s$
= TRUE
:
DEF FNInterpreterRunWaitB( s$ )
REPEAT UNTIL GET
= TRUE
:
DEF FNInterpreterRunQuitB( s$ )
quitB% = TRUE
= TRUE
:
--- cut here: end ----------------------------------------------------
4. -Create the 4th file containing the functions
itself
1. -Save this file as
InterpreterRunDataProgramFile
--- cut here: begin --------------------------------------------------
REM save file as 'InterpreterRunDataProgramFile'
:
DEF PROCInterpreterRunDataProgramFileRestore
RESTORE +1
ENDPROC
:
DEF PROCInterpreterDataProgram
DATA "beep"
DATA "beep"
DATA "beep"
DATA "beep"
DATA "print"
DATA "print"
DATA "wait"
DATA "cls"
DATA "quit"
ENDPROC
:
--- cut here: end ----------------------------------------------------
===
4. -If you run the main file it will read the command statements
from the
DATA statements
1. -If this command is lower case cls, beep, print, wait or
quit
it will do something (e.g. clear the screen)
1. It searches for the command in the array
1. If found, it takes the corresponding function as a
string, builds the possible further parameters by
concatenating that to the end of the string and calls
that function (by using the EVAL function)
2. If not found, it will print a warning message
===
Tested successfully on
Microsoft Windows XP Professional (service pack 2),
running
BBCBASIC for Windows v5.01a
===
Internet: see also:
---
BBCBASIC: Windows: Interpreter: Link: Overview: Can you give an
overview of links?
http://www.faqts.com/knowledge_base/view.phtml/aid/39385/fid/768
----------------------------------------------------------------------