faqts : Computers : Programming : Languages : Bbcbasic

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

Entry

BBCBASIC: Windows: Interpreter: Command: Line: How to create a simple command line interpreter? 3

Dec 2nd, 2008 02:52
ejac Teje, 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? 3
---
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 further automate by reading your
        combination of commands itself from a 4th file.
        This is then 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 3 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
 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 main program
        1. -Save this file as
             InterpreterRunMain
--- cut here: begin --------------------------------------------------
REM save file as 'InterpreterRunMain.bbc'
I% = 0
minI% = 1
maxI% = 3
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%
:
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
--- cut here: end ----------------------------------------------------
    2. -Create the second 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 "quit", "FNInterpreterRunQuitB("
ENDPROC
:
--- cut here: end ----------------------------------------------------
    3. -Create the third 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 FNInterpreterRunQuitB( s$ )
 quitB% = TRUE
= TRUE
:
--- cut here: end ----------------------------------------------------
===
    4. -If you run the main file it will ask you to input a command
        1. -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
===
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
----------------------------------------------------------------------
http://partyideas1.blogspot.com/
http://healthfitness121.blogspot.com/
http://chinesenewyear1.blogspot.com/
http://beautytips151.blogspot.com/
http://viajesturismoperu.blogspot.com/
-----------------------------------------------------------------------