add a numeric ranges example

FossilOrigin-Name: c3d30eebc6f43188cd59c30aa4e339e4186bf457d1f7de0d46a165618d73df21
This commit is contained in:
crc 2020-05-06 11:21:34 +00:00
parent cd5c69448d
commit 15c0569404
3 changed files with 50 additions and 20 deletions

View file

@ -17,25 +17,24 @@ updated for RETRO12 by Charles Childers.
## Functions
+-------------------+--------+----------------------------+
| Function | Stack | Used For |
+===================+========+============================+
| b:to-byte-address | a-a | Return the byte address of |
| | | the first byte in a cell |
+-------------------+--------+----------------------------+
| b:unpack | c-bbbb | Given a byte-packed cell |
| | | on the stack, return the |
| | | bytes it contains |
+-------------------+--------+----------------------------+
| b:pack | bbbb-c | Pack four byes into a cell,|
| | | return the cell on the |
| | | stack |
+-------------------+--------+----------------------------+
| b:fetch | a-b | Fetch a byte |
+-------------------+--------+----------------------------+
| b:store | ba- | Store a byte into memory |
+-------------------+--------+----------------------------+
+-------------------+--------+----------------------------+
| Function | Stack | Used For |
+===================+========+============================+
| b:to-byte-address | a-a | Return the byte address of |
| | | the first byte in a cell |
+-------------------+--------+----------------------------+
| b:unpack | c-bbbb | Given a byte-packed cell |
| | | on the stack, return the |
| | | bytes it contains |
+-------------------+--------+----------------------------+
| b:pack | bbbb-c | Pack four byes into a cell,|
| | | return the cell on the |
| | | stack |
+-------------------+--------+----------------------------+
| b:fetch | a-b | Fetch a byte |
+-------------------+--------+----------------------------+
| b:store | ba- | Store a byte into memory |
+-------------------+--------+----------------------------+
## Code & Commentary

View file

@ -0,0 +1,31 @@
This implements some words to return a range of numbers on the stack.
It's probably best to capture these values in an array.
First is a word to return values from a lower to upper limit, incrementing
upwards. The returned values are inclusive of the limits.
~~~
:range<inc> (lh-a)
over - n:inc [ I over + swap ] times<with-index> drop ;
~~~
Next is a word to return values from an upper to lower limit, decrementing
upwards. The returned values are inclusive of the limits.
~~~
:range<dec> (hl-a)
over &- dip swap n:inc [ I over swap - swap ] times<with-index> drop ;
~~~
The last word takes the limits and calls the appropriate word.
~~~
:range (nn-a)
dup-pair gt? [ range<dec> ] [ range<inc> ] choose ;
~~~
As a simple test case:
```
{ #1 #5 range } [ n:put sp ] a:for-each
```

View file

@ -3,7 +3,7 @@
#define CELL int32_t
#endif
CELL ngaImageCells = 13827;
CELL ngaImage[] = { 1793,13738,13728,13826,202004,0,10,1,10,2,10,3,10,4,10,5,10,6,10,
CELL ngaImage[] = { 1793,13738,13728,13826,202007,0,10,1,10,2,10,3,10,4,10,5,10,6,10,
7,10,8,10,9,10,10,11,10,12,10,13,10,14,10,15,10,16,10,17,
10,18,10,19,10,20,10,21,10,22,10,23,10,24,10,25,10,68223234,1,2575,
85000450,1,656912,3215,3224,268505089,63,62,135205121,63,10,101384453,0,9,10,2049,56,25,459011,74,