quotations.fs 898 Bytes
Newer Older
pazsan's avatar
pazsan committed
1 2
\ anonymous definitions in a definition

3 4 5 6
: [: ( -- quotation-sys )
\G Starts a quotation
    false :noname ;
comp: drop  locals-wordlist last @ lastcfa @ leave-sp @
pazsan's avatar
pazsan committed
7
    postpone AHEAD
8 9 10
    locals-list @ locals-list off
    postpone SCOPE
    true  :noname  ;
pazsan's avatar
pazsan committed
11

pazsan's avatar
pazsan committed
12 13
: ;] ( compile-time: quotation-sys -- ; run-time: -- xt )
    \g ends a quotation
pazsan's avatar
pazsan committed
14
    POSTPONE ; >r IF
pazsan's avatar
pazsan committed
15
	]  postpone ENDSCOPE
16
	locals-list !
pazsan's avatar
pazsan committed
17
	postpone THEN
18
	leave-sp ! lastcfa ! last ! to locals-wordlist
pazsan's avatar
pazsan committed
19
	r> postpone ALiteral
pazsan's avatar
pazsan committed
20 21
    ELSE  r>  THEN ( xt ) ; immediate

pazsan's avatar
pazsan committed
22
0 [IF] \ tests
pazsan's avatar
pazsan committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
: 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"
pazsan's avatar
pazsan committed
39 40 41 42 43 44 45

\ locals within quotations

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