home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / locals.seq < prev    next >
Text File  |  1988-05-24  |  3KB  |  90 lines

  1. \ LOCALS.SEQ    Local variables for F88         by Tom Zimmer
  2.  
  3. ANEW LOCALVARS
  4.  
  5. comment:
  6.  
  7.         Adds LOCAL variables to F88, usage is as follows:
  8.  
  9.         : localswap     ( n1 n2 --- n2 n1 )
  10.                         2 locals                \ we need 2 local variables
  11.                         locala ! localb !
  12.                         locala @ localb @ ;
  13.  
  14.         Local variables LOCALA through LOCALD are provided. Local
  15.         variables are WORD in size, if you want to use a double word
  16.         local variable, use LOCALB or LOCALD, and specify 2 or 4 LOCALS
  17.  
  18.         Local variables are automatically deallocated at the end of the
  19.         definition. You can mix the use of local variables with the
  20.         return stack operators, but be aware that local variabls place
  21.         an extra function on the return stack to clean up the local
  22.         variables on definition exit, so you CANNOT do things like
  23.         " R> DROP EXIT" to pop up one level..This would be bad practice
  24.         in any case since F88 puts two things on the return stack for
  25.         each nest level.
  26.  
  27. comment;
  28.  
  29. create lstack 512 allot         \ local variable stack
  30.  
  31. variable lstkptr                \ local stack pointer
  32.   lstack lstkptr !
  33.  
  34. code (0locals)  ( --- )
  35.                 mov bx, lstkptr
  36.                 mov ax, 0 [bx]
  37.                 sub lstkptr ax
  38.                 jmp ' exit
  39.                 end-code
  40.  
  41. : 0locals       ( --- )
  42.                 exit            \ protect against accidental execution
  43.                 (0locals)
  44.                 ;
  45.  
  46. code locals     ( n -- )
  47.                 pop ax
  48.                 shl ax, # 1
  49.                 add lstkptr ax                  \ allocate the space
  50.                 add ax, # 2                     \ +2 for count word
  51.                 inc lstkptr     inc lstkptr
  52.                 mov bx, lstkptr
  53.                 mov 0 [bx], ax                  \ save count in local stack
  54.                 mov ax, # ' 0locals >body @
  55.                 add ax, xseg                    \ convert to absolute
  56.                 dec rp          dec rp
  57.                 mov 0 [rp], ax                  \ push 0LOCALS
  58.                 mov ax, # 2
  59.                 dec rp          dec rp
  60.                 mov 0 [rp], ax                  \ push 2 offset, past exit
  61.                 next            end-code
  62.  
  63. code locala     ( --- a1 )
  64.                 mov ax, lstkptr
  65.                 sub ax, # 2     1push           end-code
  66.  
  67. code localb     ( --- a1 )
  68.                 mov ax, lstkptr
  69.                 sub ax, # 4     1push           end-code
  70.  
  71. code localc     ( --- a1 )
  72.                 mov ax, lstkptr
  73.                 sub ax, # 6     1push           end-code
  74.  
  75. code locald     ( --- a1 )
  76.                 mov ax, lstkptr
  77.                 sub ax, # 8     1push           end-code
  78.  
  79. \s      Sample usage of local variables.
  80.  
  81. : findXinString ( string --- n1 )     \ Search for the first occurance
  82.                 1 locals locala on    \ of the "x" character in string
  83.                 bl word count 0       \ following tt, return n1 offset to "x".
  84.                ?do      dup i + c@ ascii x =
  85.                         if      i locala ! leave
  86.                         then
  87.                 loop    drop locala @ ;
  88.  
  89.  
  90.