File:  [gforth] / gforth / quotations.fs
Revision 1.3: download - view: text, annotated - select for diffs
Thu Feb 9 17:27:37 2012 UTC (12 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
bugfix: local names are now freed correctly even with quotations.

    1: \ anonymous definitions in a definition
    2: 
    3: :noname  false :noname ;
    4: :noname  locals-wordlist last @ lastcfa @
    5:     postpone AHEAD
    6:     locals-list @ locals-list off
    7:     postpone SCOPE
    8:     true  :noname  ;
    9: interpret/compile: [: ( -- quotation-sys )
   10: \G Starts a quotation
   11: 
   12: : ;] ( compile-time: quotation-sys -- ; run-time: -- xt )
   13:     \g ends a quotation
   14:     POSTPONE ; >r IF
   15: 	]  postpone ENDSCOPE
   16: 	locals-list !
   17: 	postpone THEN
   18: 	lastcfa ! last ! to locals-wordlist
   19: 	r> postpone ALiteral
   20:     ELSE  r>  THEN ( xt ) ; immediate
   21: 
   22: 0 [IF] \ tests
   23: : if-else ( ... f xt1 xt2 -- ... )
   24: \ Postscript-style if-else
   25:     rot IF
   26:        drop
   27:     ELSE
   28:        nip
   29:     THEN
   30:     execute ;
   31: 
   32: : test ( f -- )
   33:     [: ." true" ;]
   34:     [: ." false" ;]
   35:     if-else ;
   36:    
   37: 1 test cr \ writes "true"
   38: 0 test cr \ writes "false"
   39: 
   40: \ locals within quotations
   41: 
   42: : foo { a b } a b
   43:     [: { x y } x y + ;] execute . a . b . ;
   44: 2 3 foo
   45: [THEN]

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>