Entry
BBCBASIC: Windows: Interpreter: Command: Line: How to create a simple command line interpreter? 2
Feb 5th, 2006 17:40
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 01 February 2021 - 07:21 pm -------------------
BBCBASIC: Windows: Interpreter: Command: Line: How to create a simple
command line interpreter? 2
---
You can use a REPEAT UNTIL loop,
containing
1. -Some input mechanism for commands
2. -You find the command by searching
in the command 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
e.g. DATA statements
(or even more flexible from a
file)
2. -The functions itself you could
add 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.
===
In general
--- cut here: begin --------------------------------------------------
I% = 0
minI% = 1
maxI% = 3
s$ = ""
command$ = ""
function$ = ""
quitB% = FALSE
foundB% = FALSE
:
DIM commandWord$( maxI% )
DIM commandFunction$( maxI% )
:
FOR I% = minI% TO maxI%
READ commandWord$( I% )
READ commandFunction$( I% )
NEXT I%
:
REPEAT
PRINT "command = ";
INPUT s$
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" )
UNTIL foundB% OR ( I% >= maxI% )
UNTIL quitB%
:
END
:
:
:
DEF FNInterpreterRunCommand1B( s$ )
REM do something 1
= TRUE
:
DEF FNInterpreterRunCommand2B( s$ )
REM do something 2
= TRUE
:
DEF FNInterpreterRunCommand3B( s$ )
REM do something 3
= TRUE
:
...
DEF FNInterpreterRunCommandLastS( s$ )
REM do something last
= TRUE
:
DATA "command1", "FNInterpreterRunCommand1B("
DATA "command2", "FNInterpreterRunCommand2B("
DATA "command3", "FNInterpreterRunCommand3B("
...
DATA "command3", "FNInterpreterRunCommandLastB("
--- cut here: end ----------------------------------------------------
===
Steps: Overview:
1. -Create e.g. the following example program
--- cut here: begin --------------------------------------------------
I% = 0
minI% = 1
maxI% = 3
s$ = ""
command$ = ""
function$ = ""
quitB% = FALSE
foundB% = FALSE
maxB% = FALSE
:
DIM commandWord$( maxI% )
DIM commandFunction$( maxI% )
:
FOR I% = minI% TO maxI%
READ commandWord$( I% )
READ commandFunction$( I% )
NEXT I%
:
REPEAT
PRINT "command = ";
INPUT s$
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
:
:
:
DEF FNInterpreterRunClearScreenB( s$ )
CLS
= TRUE
:
DEF FNInterpreterRunBeepB( s$ )
VDU 7
= TRUE
:
DEF FNInterpreterRunQuitB( s$ )
quitB% = TRUE
= TRUE
:
DATA "cls", "FNInterpreterRunClearScreenB("
DATA "beep", "FNInterpreterRunBeepB("
DATA "quit", "FNInterpreterRunQuitB("
:
--- cut here: end ----------------------------------------------------
2. -If you run this program it will ask for inputting a command
3. -If this command is lower case cls, beep 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
---
---
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
----------------------------------------------------------------------