mirror of
https://git.sr.ht/~crc_/retroforth
synced 2024-11-16 19:48:56 +01:00
minor updates to examples
FossilOrigin-Name: a87d738bdda655512aee7a836a49439bc71a716db99ce3722ad2d2a5cec834e3
This commit is contained in:
parent
e286415d2b
commit
39f995a3b4
2 changed files with 29 additions and 21 deletions
|
@ -1,21 +1,26 @@
|
|||
These are adapted from HerkForth.
|
||||
# Lightweight Flow Control
|
||||
|
||||
| 0= | n- | exit word if TOS = 0 |
|
||||
| <; | nn- | exit word if NOS < TOS |
|
||||
| >; | nn- | exit word if NOS > TOS |
|
||||
| <>; | nn- | exit word if NOS <> TOS |
|
||||
| if; | f- | exit word if TOS is TRUE |
|
||||
| ?; | f- | exit word if TOS is TRUE. Leave Flag on stack if TRUE. |
|
||||
These were adapted from HerkForth.
|
||||
|
||||
| 0=; | n- | exit word if TOS = 0 |
|
||||
| 0<>; | n- | exit word if TOS <> 0 |
|
||||
| <; | nn- | exit word if NOS < TOS |
|
||||
| >; | nn- | exit word if NOS > TOS |
|
||||
| <>; | nn- | exit word if NOS <> TOS |
|
||||
| if; | f- | exit word if TOS is TRUE |
|
||||
| ?; | f- | exit word if TOS is TRUE. Leave Flag on stack if TRUE. |
|
||||
|
||||
~~~
|
||||
:0=; n:zero? [ as{ 'popopodr i 'drdrre.. i }as ] if ;
|
||||
:<; lt? [ as{ 'popopodr i 'drdrre.. i }as ] if ;
|
||||
:>; gt? [ as{ 'popopodr i 'drdrre.. i }as ] if ;
|
||||
:<>; -eq? [ as{ 'popopodr i 'drdrre.. i }as ] if ;
|
||||
:if; [ as{ 'popopodr i 'drdrre.. i }as ] if ;
|
||||
:?; dup [ as{ 'popopodr i 'drdrre.. i }as ] if drop ;
|
||||
:0=; n:zero? [ as{ 'popopodr i 'drdrre.. i }as ] if ;
|
||||
:0<>; n:zero? [ as{ 'popopodr i 'drdrre.. i }as ] -if ;
|
||||
:<; lt? [ as{ 'popopodr i 'drdrre.. i }as ] if ;
|
||||
:>; gt? [ as{ 'popopodr i 'drdrre.. i }as ] if ;
|
||||
:<>; -eq? [ as{ 'popopodr i 'drdrre.. i }as ] if ;
|
||||
:if; [ as{ 'popopodr i 'drdrre.. i }as ] if ;
|
||||
:?; dup [ as{ 'popopodr i 'drdrre.. i }as ] if drop ;
|
||||
~~~
|
||||
|
||||
# Tests
|
||||
|
||||
```
|
||||
:test (n-) n:even? if; 'Odd! s:put nl ;
|
||||
|
@ -24,7 +29,6 @@ These are adapted from HerkForth.
|
|||
#2 test
|
||||
```
|
||||
|
||||
|
||||
```
|
||||
nl '----------------- s:put nl
|
||||
:test (n-) n:even? ?; 'Odd! s:put nl ;
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
~~~
|
||||
:sort-pair dup-pair lt? [ swap ] if ;
|
||||
:perform-sort sort-pair depth #2 gt? [ [ perform-sort ] dip ] if ;
|
||||
:sort depth [ perform-sort ] times ;
|
||||
~~~
|
||||
# Sorting Numbers on the Stack
|
||||
|
||||
This is a recursive approach to sorting values on the stack. I
|
||||
won't try to claim that this is efficient, but it works.
|
||||
|
||||
~~~
|
||||
:sort-pair dup-pair lt? &swap if ;
|
||||
:perform-sort sort-pair depth #2 gt? [ &perform-sort dip ] if ;
|
||||
:sort depth &perform-sort times ;
|
||||
~~~
|
||||
|
||||
```
|
||||
#3 #33 #22 #333 #5 sort
|
||||
~~~
|
||||
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue