xref: /titanic_50/usr/src/lib/efcode/fcode_test/loop.fth (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1\ #ident	"%Z%%M%	%I%	%E% SMI"
2\ Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3\ Use is subject to license terms.
4\
5\ CDDL HEADER START
6\
7\ The contents of this file are subject to the terms of the
8\ Common Development and Distribution License, Version 1.0 only
9\ (the "License").  You may not use this file except in compliance
10\ with the License.
11\
12\ You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
13\ or http://www.opensolaris.org/os/licensing.
14\ See the License for the specific language governing permissions
15\ and limitations under the License.
16\
17\ When distributing Covered Code, include this CDDL HEADER in each
18\ file and include the License file at usr/src/OPENSOLARIS.LICENSE.
19\ If applicable, add the following below this CDDL HEADER, with the
20\ fields enclosed by brackets "[]" replaced with your own identifying
21\ information: Portions Copyright [yyyy] [name of copyright owner]
22\
23\ CDDL HEADER END
24\
25
26." Interactive begin .. while .. repeat: "
27	" no loop (1) "		1 begin 0 while 1- repeat .passed?
28	" loop to 0 (1)"	9 begin dup while 1- repeat  0= .passed?
29cr
30." Compiled begin .. while .. repeat: "
31	: btest1		1 begin 0 while 1- repeat .passed? ;
32	: btest2		9 begin dup while 1- repeat 0= .passed? ;
33	" no loop (2) "		btest1
34	" loop to 0 (2)"	btest2
35cr
36." Interactive begin..until: "
37	" no loop (3)"		1 begin  dup until .passed?
38	" loop to 0 (3)"	9 begin  1- dup 0= until 0= .passed?
39cr
40." Compiled begin..until: "
41	: btest3		1 begin  dup until .passed? ;
42	: btest4		9 begin  1- dup 0= until 0= .passed? ;
43	" no loop (4)"		btest3
44	" loop to 0 (4)"	btest4
45cr
46." Interactive do .. loop: "
47	" loop (1)"	0 h# 10 0 do drop i loop h# f = .passed?
48	" no loop (1)"	1 0 0 ?do 1- loop .passed?
49	" leave (1)"	h# 10 0 do i 5 = if 1 leave drop 0 then loop .passed?
50cr
51." Compiled do .. loop: "
52	: loop1			do drop i loop h# f = .passed? ;
53	: loop2			?do 1- loop .passed? ;
54	: loop3			do i 3 = if drop i leave 0 then loop ;
55	: loop7			do i 4 = if drop i unloop exit then loop ;
56	" loop (2)"		0 h# 10 0 loop1
57	" no loop (2)"		1 0 0 loop2
58	" leave (2)"		3 4 0 loop3 3 = .passed?
59	" unloop"		5 6 0 loop7 4 = .passed?
60cr
61." Interactive do .. +loop: "
62	" loop by 2"		0 h# 10 0 do drop i 2 +loop h# e = .passed?
63	" loop down by 2"	0 -2 h# 10 do drop i -2 +loop h# -2 = .passed?
64cr
65." Compiled do .. +loop: "
66	: loop4			0 h# 10  0 do drop i 2 +loop h# e = .passed? ;
67	: loop5			0 -2 h# 10 do drop i -2 +loop -2 = .passed? ;
68	" loop (4)"		loop4
69	" loop (5)"		loop5
70cr
71." Nested loops: "
72	: loop6		0 h# 4 0 do 8 0 do 1 j 3 lshift i + lshift xor loop loop ;
73	" i,j sum"		loop6 lwsplit over = swap h# ffff = and .passed?
74cr
75." Negative Limit Loops: "
76	" loop.7"       h# 10 -37 8 bounds do drop i loop -30 = .passed?
77	" loop.8"       h# 10 -37 -30 do drop i -1 +loop  -37 = .passed?
78cr
79." Compiled begin...again: "
80	: loop9 begin true exit again false ; loop9 " loop9" rot .passed?
81	0 value in-count
82	0 value out-count
83	: loop10
84	   begin
85	      out-count 1+ to out-count
86 	      begin
87	         in-count 10 >= if
88	            exit
89	         then in-count 1+ to in-count
90	      again
91	      -1 to in-count
92	      exit
93	   again
94	   -2 to in-count
95	;
96	" loop.10" loop10 in-count 10 = out-count 1 = and  .passed?
97cr
98