-
Notifications
You must be signed in to change notification settings - Fork 9
Structures
Wolf Wejgaard edited this page Feb 22, 2018
·
15 revisions
TclForth provides the classic Forth structures as well as useful constructs courtesy of Tcl.
: test { n -- } n 0= if "is 0" else "not 0" then . ;
: test { item -- }
item case
77 of "is77" endof
abc of "isabc" endof
default of "none" endof
endcase . ;
: test { n -- } begin n incr n . n 10 > if break then again ;
: test { n -- } begin n incr n . n 10 > until ;
: test { n -- } begin n 10 < while n incr n . repeat ;
0 variable sum 101 1 do I sum add loop sum .
2 0 do 12 10 do 102 100 do I . J . K . cr loop loop loop
0 variable sum 100 0 do I 10 > if leave then I sum add loop sum .
3 times "onemoretime" . repeat
Use: {data} {args} foreach ... repeat
The args are locals defined in the stack diagram.
: do-foreach { list | x -- } list {x} foreach x x * . repeat ;
{11 22 33} do-foreach
For immediate use provide x as a global variable
0 variable x {1 2 3 4} {x} foreach x 7 * . repeat
: count { start limit -- }
begin start . start incr start limit > if exit then again ;
0 99 count