This chapter presents a complete reference on wfroth.
Last update: |
31-December-1998 |
State (estimation) |
100% |
This chapter includes the following sections:
This control structure has the next 'syntax':
BEGIN
...
[?break]
[?continue]
...
AGAIN
Each word of the control structure operate as:
Word |
Stack
protocol |
Description |
BEGIN |
|
just marks the top of the loop. |
AGAIN |
( --- ) |
control is given to the next word after BEGIN (ie the top if the loop). |
?break |
( f --- ) |
if flag is true, control is given to the next word after AGAIN (ie exit the loop). |
?continue |
( f --- ) |
if flag is true, control is given to the next word after BEGIN (ie re-enter the loop). |
Generated code (host):
: test1
cr BEGIN key 1+ emit AGAIN ;
cr |
key |
1+ |
emit |
(branch) |
-4 |
(;) |
: test2
cr BEGIN key dup 'X == ?break 1+ emit AGAIN drop ;
cr |
key |
dup |
(lit) |
$58 |
== |
(1branch) |
5 |
1+ |
emit |
(branch) |
-10 |
drop |
(;) |
: test3
cr BEGIN key dup 'X == ?continue 1+ emit AGAIN ;
cr |
key |
dup |
(lit) |
$58 |
== |
(1branch) |
-7 |
1+ |
emit |
(branch) |
-10 |
(;) |
This control structure has the next 'syntax':
BEGIN
...
[?break]
[?continue]
...
<flag> UNTIL
Each word of the control structure operate as:
Word |
Stack
protocol |
Description |
BEGIN |
|
just marks the top of the loop. |
UNTIL |
( f --- ) |
control is given to the
next word after BEGIN (ie the top if the loop) |
?break |
( f --- ) |
if flag is true, control is given to the next word after UNTIL (ie exit the loop). |
?coontinue |
( f --- ) |
if flag is true, control is given to the next word after BEGIN (ie re-enter the loop). |
Generated code (host):
: test1
cr BEGIN key dup 1+ emit 'X == UNTIL cr ;
cr |
key |
dup |
1+ |
emit |
(lit) |
$58 |
== |
(0branch) |
-8 |
cr |
(;) |
This control structure has the next 'syntax':
BEGIN
...
[?break]
[?continue]
...
<flag> WHILE
...
[?break]
[?continue]
...
REPEAT
Each word of the control structure operate as:
Word |
Stack
protocol |
Description |
BEGIN |
|
just marks the top of the loop. |
WHILE |
( f --- ) |
control is given to the
next word after REPEAT (ie exit of the loop) |
REPEAT |
( --- ) |
control is given to the next word after BEGIN (ie the top if the loop). |
?break |
( f --- ) |
if flag is true, control is given to the next word after REPEAT (ie exit the loop). |
?coontinue |
( f --- ) |
if flag is true, control is given to the next word after BEGIN (ie top of the loop). |
Generated code (host):
: test1
cr BEGIN key dup 'X != WHILE 1+ emit REPEAT drop ;
cr |
key |
dup |
(lit) |
$58 |
!= |
(0branch) |
5 |
1+ |
emit |
(branch) |
-10 |
drop |
(;) |
This control structure has the next two 'syntaxes':
<flag> IF ... ENDIF |
<flag> IF ... ELSE ... ENDIF |
Each word of the control structure operate as:
Word |
Stack
protocol |
Description |
||||
IF |
( f --- ) |
|
||||
ELSE |
|
sequence of words executed if initial condition popped by IF was false. |
||||
ENDIF |
|
mark the end of the structure. |
Generated code (host):
: test1
cr key '0 '9 [within] IF " is a digit" type ENDIF cr ;
cr |
key |
(lit) |
$30 |
(lit) |
$31 |
[within] |
(0branch) |
7 |
(") |
10 |
"is a" |
"digi" |
"t" |
type |
cr |
(;) |
: test2
cr key '0 '9 [within] IF " is " ELSE " not " ENDIF type
" a digit" type ;
cr |
key |
(lit) |
$30 |
(lit) |
$31 |
[within] |
(0branch) |
6 |
(") |
3 |
"is " |
(branch) |
5 |
(") |
4 |
"not " |
"" |
type |
(") |
7 |
"a di" |
"git" |
type |
(;) |
CASE ... [OF ... ENDOF] ENDCASE control structure
This control structure has the next 'syntax':
<value> CASE
<value> OF ... ENDOF | TIPOF
...
ENDCASE
Each word of the control structure operate as:
Word |
Stack
protocol |
Description |
CASE |
( n --- n ) |
mark the end of the structure: value used by OF stays on top of stack. |
OF |
( n1 n2 --- n1 ) |
pop value from stack and compare
with value on top: if not equal, control is |
ENDOF |
|
mark the end of OF...ENDOF:
control if given to the next word after |
TIPOF |
|
mark the end of OF...ENDOF:
control if given to the next word after |
ENDCASE |
( n --- n ) |
mark the end of the structure. |
Generated code (host):
:
test1 |
|
|
'0 OF " zero" type ENDOF |
|
'1 OF " one" type ENDOF |
|
'a OF " lower " type TIPOF |
|
'A OF " HA!" type ENDOF |
|
'2 of " two" type ENDOF |
|
" unknow" type |
ENDCASE |
|
100 |
cr |
key |
||||||||||
102 |
(lit) |
$30 |
(of) |
8 (113) |
(") |
4 |
"zero" |
"" |
type |
(branch) |
47 (159) |
|
113 |
(lit) |
$31 |
(of) |
7 (123) |
(") |
3 |
"one" |
type |
(branch) |
37 (159) |
|
|
123 |
(lit) |
$61 |
(of) |
8 (134) |
(") |
6 |
"lowe" |
"r " |
type |
(branch) |
5 (138) |
|
134 |
(lit) |
$41 |
(of) |
7 (144) |
(") |
3 |
"Ha!" |
type |
(branch) |
16 (159) |
|
|
144 |
(lit) |
$32 |
(of) |
7 (154) |
(") |
3 |
"two" |
type |
(branch) |
6 (159) |
|
|
154 |
(") |
6 |
"Unkn" |
"ow" |
type |
|
|
|
|
|
|
|
159 |
drop |
(:) |
|
|
|
|
|
|
|
|
|
|
This control structure has the next 'syntax':
<value> TIMES
...
[I]
...
[?break] [?continue]
...
ENDCASE
Each word of the control structure operate as:
Word |
Stack
protocol |
Description |
TIMES |
( n --- ) |
mark the top of the loop: value popped is the number of iteration. |
I |
( --- n ) |
push current index loop (starting from 0 to limit-1) |
?break |
|
if popped flag is true,
control if given to the next word after |
?continue |
|
if popped flag is true,
control if given to the next word after |
ENDTIMES |
|
mark the end of the structure. |
If the value popped by TIMES is 0, the loop is not executed.
Generated code (host):
: test1
cr 10 TIMES I . space ENDTIMES cr ;
cr |
(lit) |
10 |
(times) |
6 |
I |
. |
space |
(endtimes) |
-4 |
cr |
(;) |
Result:
0 1 2 3 4 5 6 7 8 9
: test2
cr 10 TIMES key dup emit 'X ?break space ENDTIMES cr ;
cr |
(lit) |
10 |
(times) |
11 |
key |
dup |
emit |
(lit) |
'X |
(timebreak) |
4 |
space |
(endtimes) |
-10 |
cr |
(;) |
: test3
cr 10 TIMES key '0 '9 [within] ?continue '- emit ENDTIMES CR ;
cr |
(lit) |
10 |
(times) |
15 |
key |
(lit) |
$30 |
lit |
$31 |
'X |
(timecontinue) |
-7 |
(lit) |
'- |
emit |
(endtimes) |
-12 |
cr |
(;) |
DO ... ENDDO control structure
<limit> <start> DO ... ENDDO |
<limit> <start> DO ... <step> +ENDDO |
Each word of the control structure operate as:
Word |
Stack
protocol |
Description |
DO |
( n1 n2 --- ) |
mark the top of the loop: n1 is the limit, n2 is the starting value. |
I |
( --- n ) |
push current index loop |
J |
( --- n ) |
push current index external-loop |
K |
( --- n ) |
push current index external-loop |
?break |
( n --- ) |
if popped flag is true,
control if given to the next word after |
?continue |
( n --- ) |
if popped flag is true,
control if given to the next word after |
ENDDO |
|
mark the end of the loop. Increment by one the current index and:
|
+ENDDO |
|
mark the end of the loop. Increment by the popped value the current index and:
o
if the value is
greater than the value popped by DO, o
if the value is lower
or equal than the value popped by DO,
o
if the value is lower
than the value popped by DO, o
if the value is
greater or equal than the value popped by DO, |
Generated code (host):
: test1
cr 10 1 DO I . space ENDDO cr ;
cr |
(lit) |
10 |
(lit) |
1 |
(do) |
I |
. |
space |
(loop) |
-4 |
cr |
(;) |
Result:
0 1 2 3 4 5 6 7 8 9
: test2
3 -5 DO I . space ENDDO ;
(lit) |
3 |
(lit) |
-5 |
(do) |
I |
. |
space |
(loop) |
-4 |
(;) |
Result:
-5 -4 -3 -2 -1 0 1 2
: test3
20 3 DO I . space 2 +ENDDO ;
(lit) |
20 |
(lit) |
3 |
(do) |
I |
. |
space |
(lit) |
2 |
(+loop) |
-6 |
(;) |
Result:
3 5 7 9 11 13 15 17 19