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

\ anonymous definitions in a definition

:noname  false :noname ;
:noname  locals-wordlist last @ lastcfa @
    postpone AHEAD
    locals-list @ locals-list off
    postpone SCOPE
    true  :noname  ;
interpret/compile: [: ( -- quotation-sys )
\G Starts a quotation

: ;] ( compile-time: quotation-sys -- ; run-time: -- xt )
    \g ends a quotation
    POSTPONE ; >r IF
	]  postpone ENDSCOPE
	locals-list !
	postpone THEN
	lastcfa ! last ! to locals-wordlist
	r> postpone ALiteral
    ELSE  r>  THEN ( xt ) ; immediate

0 [IF] \ tests
: if-else ( ... f xt1 xt2 -- ... )
\ Postscript-style if-else
    rot IF
       drop
    ELSE
       nip
    THEN
    execute ;

: test ( f -- )
    [: ." true" ;]
    [: ." false" ;]
    if-else ;
   
1 test cr \ writes "true"
0 test cr \ writes "false"

\ locals within quotations

: foo { a b } a b
    [: { x y } x y + ;] execute . a . b . ;
2 3 foo
[THEN]

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