wfroth reference : Control Structures

This chapter presents a complete reference on wfroth.

Last update:

31-December-1998

State (estimation)

100%


This chapter includes the following sections:


BEGIN ... AGAIN control structure

This control structure has the next 'syntax':

BEGIN

...
[?break]
[?continue]
...

AGAIN

 

Each word of the control structure operate as:

Word

Stack protocol
(run-time word)

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

(;)

 

BEGIN ... UNTIL control structure

This control structure has the next 'syntax':

BEGIN

...
[?break]
[?continue]
...

<flag> UNTIL

 

Each word of the control structure operate as:

Word

Stack protocol
(run-time word)

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)
if popped flag was false

?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

(;)

 

BEGIN ... WHILE ... REPEAT control structure

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
(run-time word)

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)
if popped flag was false.

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

(;)

 

IF ... [ELSE] ... ENDIF control structure

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
(run-time word)

Description

IF

( f --- )

IF..ENDIF

control is given to the next word after ENDIF if popped flag was false

IF..ELSE..ENDIF

control is given to the next word after ELSE if popped flag was false

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
(run-time word)

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
given to the first word after the next ENDOF.

ENDOF

 

mark the end of OF...ENDOF: control if given to the next word after
ENDCASE (ie end of the structure).

TIPOF

 

mark the end of OF...ENDOF: control if given to the next word after
the next OF (ie fall into the next OF...ENDOF).

ENDCASE

( n --- n )

mark the end of the structure.

 

Generated code (host):

: test1
cr key
CASE

 

'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
drop ;

 

 

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

(:)

 

 

 

 

 

 

 

 

 

 

 

TIME ... ENDTIMES control structure

This control structure has the next 'syntax':

<value> TIMES

...
[I]
...
[?break] [?continue]
...

ENDCASE

 

Each word of the control structure operate as:

Word

Stack protocol
(run-time word)

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
ENDTIMES (ie end of the structure).

?continue

 

if popped flag is true, control if given to the next word after
TIMES (ie re-enter into the loop), but only if end of loop is not reached.

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

...
I
?break
?continue
...

ENDDO

<limit> <start> DO

...
I
?break
?continue
...

<step> +ENDDO

Each word of the control structure operate as:

Word

Stack protocol
(run-time word)

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
ENDDO (ie end of the loop).

?continue

( n --- )

if popped flag is true, control if given to the next word after
DO (ie re-enter into the loop), but only if end of loop is not reached.

ENDDO

 

mark the end of the loop. Increment by one the current index and:

  • if the value is greater than the value popped by DO,
    control is given to next word after ENDDO (exit loop)
  • if the value is lower or equal than the value popped by DO,
    control is given to next word after DO (redo loop)

+ENDDO

 

mark the end of the loop. Increment by the popped value the current index and:

  • the value popped by +ENDDO is positive (same as ENDDO):

o        if the value is greater than the value popped by DO,
control is given to next word after ENDDO (exit loop)

o        if the value is lower or equal than the value popped by DO,
control is given to next word after DO (redo loop)

  • the value popped by +ENDDO is negative:

o        if the value is lower than the value popped by DO,
control is given to next word after ENDDO (exit loop)

o        if the value is greater or equal than the value popped by DO,
control is given to next word after DO (redo loop)

 

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