-
Notifications
You must be signed in to change notification settings - Fork 9
Structures
ww edited this page Dec 5, 2014
·
15 revisions
TclForth provides the classic Forth structures as well as useful constructs courtesy of Tcl.
###Flow Control##
####if else then#
: test { n -- } n 0= if "is 0" else "not 0" then . ;
####case of endof default endcase#
: test { item -- }
item case
77 of "is77" endof
abc of "isabc" endof
default of "none" endof
endcase . ;
###Conditional Loops#
####begin again break#
: test { n -- } begin n incr n . n 10 > if break then again ;
####begin until#
: test { n -- } begin n incr n . n 10 > until ;
####begin while repeat#
: test { n -- } begin n 10 < while n incr n . repeat ;
###Counted Loops#
####do loop I J K#
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
####leave#
0 variable sum 100 0 do I 10 > if leave then I sum add loop sum .
####times#
3 times "onemoretime" . repeat
####foreach#
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
####exit = return#
: count { start limit -- }
begin start . start incr start limit > if exit then again ;
0 99 count