Question

new topic     » goto parent     » topic index » view thread      » older message » newer message

*** Reply to note of 01/09/97 04:08
re: how do i write an interpreter.

this is not difficult. first, you need to convert your command into
seperate words. each word must be seperated by a space. here is a function
that will do that:


function split( sequence s )
   sequence word, words
   word = ""
   words = ""
   for i = 1 to length( words ) do
      if s[i] = ' ' or s[i] = '\n' then
         if compare( word, "" ) != 0 then
            words = append( words, word )
            word = ""
         end if
      else
         word = word & s[i]
      end if
   end for
   return words
end function

then you need to interpret the command. here is a simple procedure that
will interpret the command 'color X' and 'line X1 Y1 to X2 Y2'

procedure interpret( sequence s )
   integer x1, y1, x2, y2
   if compare( s[1], "color" ) = 0 then -- first word is 'color'
      x1 = value( [s2] ) -- convert word 2 to a number
      -- color setting code goes here
   elsif compare( s[1], "plot" ) = 0 then -- first word is 'plot'
      x1 = value( s[2] ) -- convert word 2 to number and store in x1
      y1 = value( s[3] ) -- convert word 3 to number and store in y1
      x2 = value( s[5] ) -- convert word 5 to number and store in x2
      y2 = value( s[6] ) -- convert word 5 to number and store in y2
      -- draw line command goes here
   else
      -- put your error routine here
      if compare( s, "" ) != 0 -- don't try to print word #1 if the sequence
                               -- is empty, or you get an error.
         puts( 1, "I don't know how to " & s[1] )
      end if
   end if
end procedure


if you use the function 'value', you'll need to include the file 'get.e'.

i hope this helps.

-- david cuny

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu