[futurebasic] Re: [FB] PRINT

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : August 2005 : Group Archive : Group : All Groups

From: Stu Cram <stu@...>
Date: Fri, 19 Aug 2005 18:56:02 -0600
Brian - Be sure to look at the answers to this and similar questions 
you had last fall on the list. For now. here is some food for thought.

1.  It appears you are trying to write a simple language and 
interpret its instructions. Typically, each line will have just one 
instruction which begins with a command keyword which may be 
followed, or not, by some data to work with.

2.  From your example and a previous post, you seem to be getting the 
group of instructions from and edit field. The first step is to get 
each line of the 'program' as you did in the loop with ...
      efH = TeHandle( efID )
      numLines = efH..teNLines%
      FOR i = 1 TO numLines
           lineStr = EDIT$( efID, i )
           '.....
      NEXT i

3.  Variable 'lineStr' will now have an entire line of information. 
If that line is a simple command like 'BEEP', then your SELECT/CASE 
code will find and interpret it. However, for an command like 'Print 
something', your code fails because variable 'linestr' is not the 
keyword 'PRINT'; rather it's the entire line, remember.  So you need 
to add a step that gets the leading keyword and base your SELECT/CASE 
decisions on the keyword.
      The method shown below uses INSTR() to find where the first 
space is on a line. Then LEFT$() is used to get the keyword before 
that spaces and MID$() is used to get the rest of the command after 
the space. That way, your interpreter can decide what to do and what 
data to use.
      Here is a revised version of your function that should do this 
for you. You can add more CASE values and deal with each as needed

LOCAL FN ParseEFLines( efID as long )
DIM AS HANDLE  efH
DIM AS LONG   i, numLines, spacePosition
DIM AS str255  lineStr,  keyword,  restOfline

efH = TeHandle( efID )
numLines = efH..teNLines%

FOR i = 1 TO numLines
   lineStr = EDIT$( efID, i )
   spacePosition = INSTR( 1, lineStr, " " )
   LONG IF spacePosition > 0
     keyword = LEFT$( lineStr, spacePosition - 1 )
     restOfLine = MID$( ineStr, spacePosition + 1 )
   XELSE
      keyword = ""
     restOfLine = ""
  END IF

   SELECT keyword

     CASE "BEEP"
       BEEP

     CASE "PRINT"
       PRINT  restOfLine

   END SELECT

NEXT i

END FN


Hope this helps you get started.  Be sure to plan your work carefully 
and in detail so you know what needs to be done in your interpreter 
program.  And don't forget to look back at the answers by many to you 
questions from last year.

Best wishes,
- Stu

-- 
-------------------------------------------------------------------------------
As asked by...
Brian Heibert <bheibert@...>
To: futurebasic@...
Subject: [FB] PRINT

Hi,

I got a question:
How do I figure out what is at the right side of the PRINT statement
so I can do something with it?   See below

Brian

local fn ParseEFLines( efID as long )
dim as handle efH
dim as long   i, numLines
dim as str255 lineStr

efH = TeHandle( efID )
numLines = efH..teNLines%

for i = 1 to numLines
lineStr = edit$( efID, i )
SELECT CASE lineStr
CASE "BEEP"
beep

CASE "PRINT"
print  what goes here?
END SELECT

next i

end fn = numLines

--