[gforth] / gforth / quotations.fs  

gforth: gforth/quotations.fs


1 : pazsan 1.1 \ anonymous definitions in a definition
2 :    
3 :     :noname false :noname ;
4 :     :noname locals-wordlist last @ lastcfa @
5 : pazsan 1.2 postpone AHEAD
6 : anton 1.3 locals-list @ locals-list off
7 :     postpone SCOPE
8 :     true :noname ;
9 : pazsan 1.2 interpret/compile: [: ( -- quotation-sys )
10 :     \G Starts a quotation
11 : pazsan 1.1
12 : pazsan 1.2 : ;] ( compile-time: quotation-sys -- ; run-time: -- xt )
13 :     \g ends a quotation
14 : pazsan 1.1 POSTPONE ; >r IF
15 : pazsan 1.2 ] postpone ENDSCOPE
16 : anton 1.3 locals-list !
17 : pazsan 1.2 postpone THEN
18 : pazsan 1.1 lastcfa ! last ! to locals-wordlist
19 : pazsan 1.2 r> postpone ALiteral
20 : pazsan 1.1 ELSE r> THEN ( xt ) ; immediate
21 :    
22 : pazsan 1.2 0 [IF] \ tests
23 : pazsan 1.1 : 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 : pazsan 1.2
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]

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help