2017-10-16 18:09:39 +02:00
|
|
|
# RETRO 12
|
|
|
|
|
|
|
|
## Background
|
|
|
|
|
|
|
|
Retro is a dialect of Forth. It builds on the barebones Rx core,
|
|
|
|
providing a much more flexible and useful language.
|
|
|
|
|
|
|
|
Retro has a history going back many years. It began as a 16-bit
|
|
|
|
assembly implementation for x86 hardware, evolved into a 32-bit
|
|
|
|
system with cmForth and ColorForth influences, and eventually started
|
|
|
|
supporting mainstream OSes. Later it was rewritten for a small,
|
|
|
|
portable virtual machine. Over the years the language implementation
|
|
|
|
has varied substantially. This is the twelfth generation of Retro. It
|
|
|
|
now targets a new virtual machine (called Nga), and is built over a
|
|
|
|
barebones Forth kernel (called Rx).
|
|
|
|
|
|
|
|
### Namespaces
|
|
|
|
|
|
|
|
Various past releases have had different methods of dealing with the
|
|
|
|
dictionary. Retro 12 has a single global dictionary, with a convention
|
|
|
|
of using a namespace prefix for grouping related words.
|
|
|
|
|
|
|
|
| namespace | words related to |
|
|
|
|
| ---------- | ------------------ |
|
|
|
|
| ASCII | ASCII Constants |
|
|
|
|
| c | characters |
|
|
|
|
| compile | compiler functions |
|
|
|
|
| d | dictionary headers |
|
|
|
|
| err | error handlers |
|
|
|
|
| n | numbers |
|
|
|
|
| s | strings |
|
2017-10-25 03:22:49 +02:00
|
|
|
| set | sets (arrays) |
|
2017-10-16 18:09:39 +02:00
|
|
|
| v | variables |
|
|
|
|
|
|
|
|
### Prefixes
|
|
|
|
|
|
|
|
Prefixes are an integral part of Retro. These are single characters
|
|
|
|
added to the start of a word which indicate to Retro how it should
|
|
|
|
execute the word. These are processed at the start of interpreting a
|
|
|
|
token.
|
|
|
|
|
|
|
|
| prefix | used for |
|
|
|
|
| ------ | ---------------------- |
|
|
|
|
| : | starting a definition |
|
|
|
|
| & | obtaining pointers |
|
|
|
|
| ( | stack comments |
|
|
|
|
| ` | inlining bytecodes |
|
|
|
|
| ' | strings |
|
|
|
|
| # | numbers |
|
|
|
|
| $ | characters |
|
|
|
|
| @ | variable get |
|
|
|
|
| ! | variable set |
|
|
|
|
|
|
|
|
### Naming and Style Conventions
|
|
|
|
|
|
|
|
* Names should start with their namespace (if appropriate)
|
|
|
|
* Word names should be lowercase
|
|
|
|
* Variable names should be Title case
|
|
|
|
* Constants should be UPPERCASE
|
|
|
|
* Names may not start with a prefix character
|
|
|
|
* Names returning a flag should end with a ?
|
|
|
|
* Words with an effect on the stack should have a stack comment
|
|
|
|
|
|
|
|
## Code Begins
|
|
|
|
|
|
|
|
Memory Map
|
|
|
|
|
2017-10-23 21:27:14 +02:00
|
|
|
This assumes that the VM defines an image as being 524288 cells.
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
| range | contains |
|
|
|
|
| --------------- | ---------------------------- |
|
|
|
|
| 0 - 1024 | rx kernel |
|
|
|
|
| 1025 - 1535 | token input buffer |
|
|
|
|
| 1536 + | start of heap space |
|
2017-10-23 21:27:14 +02:00
|
|
|
| ............... | free memory for your use |
|
|
|
|
| 506879 | buffer for string evaluate |
|
|
|
|
| 507904 | temporary strings (32 * 512) |
|
2017-10-16 18:09:39 +02:00
|
|
|
| 524287 | end of memory |
|
|
|
|
|
|
|
|
I provide a word, `EOM`, which returns the last addressable location.
|
|
|
|
This will be used by the words in the `s:` namespace to allocate the
|
|
|
|
temporary string buffers at the end of memory.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:EOM (-n) #-3 fetch ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
... stack comments ...
|
|
|
|
|
|
|
|
(takes-returns)
|
|
|
|
|
|
|
|
I use a single character for each input and output item. These will
|
|
|
|
often (though perhaps not always) be:
|
|
|
|
|
|
|
|
n, m, x, y number
|
|
|
|
a, p pointer
|
|
|
|
q quotation (pointer)
|
|
|
|
d dictionary header (pointer)
|
|
|
|
s string
|
|
|
|
c character (ASCII)
|
|
|
|
|
|
|
|
I next define a few words in the `d:` namespace to make it easier
|
|
|
|
to operate on the most recent header in the dictionary. These return
|
|
|
|
pointers to specific fields in the header.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:d:last (-d) &Dictionary fetch ;
|
|
|
|
:d:last<xt> (-a) d:last d:xt fetch ;
|
|
|
|
:d:last<class> (-a) d:last d:class fetch ;
|
|
|
|
:d:last<name> (-s) d:last d:name ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
... reclass ...
|
|
|
|
|
|
|
|
This is used to change the class from `class:word` to `class:macro`.
|
|
|
|
Doing this is ugly and not very readable. I implement `reclass` to
|
|
|
|
change the class of the most recent word.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:reclass (a-) d:last d:class store ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
With this I can then define `immediate` (for state-smart words) and
|
|
|
|
`data` to tag data words.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:immediate (-) &class:macro reclass ;
|
2017-10-25 03:36:47 +02:00
|
|
|
:data (-) &class:data reclass ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2017-10-23 20:56:56 +02:00
|
|
|
`depth` returns the number of items on the data stack. This is
|
|
|
|
provided by the VM upon reading from address *-1*.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:depth (-n) #-1 fetch ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2017-10-24 23:00:50 +02:00
|
|
|
I have a `compile` namespace for some low level words that compile
|
|
|
|
specific Nga bytecode.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:compile:lit (a-) #1 , , ;
|
|
|
|
:compile:jump (a-) #1793 , , ;
|
|
|
|
:compile:call (a-) #2049 , , ;
|
|
|
|
:compile:ret (-) #10 , ;
|
|
|
|
~~~
|
|
|
|
|
2017-10-25 03:02:02 +02:00
|
|
|
The compiler state is stored in a value named `Compiler`. I have an
|
|
|
|
accessor word that aids in readability.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:compiling? (-f) &Compiler fetch ;
|
|
|
|
~~~
|
|
|
|
|
2017-10-23 20:56:56 +02:00
|
|
|
The next two are additional prefixes to make working with variables
|
|
|
|
a bit less painful. By default you have to do things like:
|
|
|
|
|
|
|
|
&Name fetch #10 * &Name store
|
|
|
|
|
|
|
|
Or use combinators:
|
|
|
|
|
|
|
|
&Name [ fetch #10 * ] sip store
|
|
|
|
|
|
|
|
With the @ and ! prefixes this can become:
|
|
|
|
|
|
|
|
@Name #10 * !Name
|
|
|
|
|
2017-10-24 23:03:00 +02:00
|
|
|
When compiling, these will generate packed Nga instructions
|
|
|
|
corresponding to:
|
|
|
|
|
|
|
|
lit + fetch + nop + nop 'life.... #3841
|
|
|
|
lit + store + nop + nop 'list.... #4097
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-24 23:00:50 +02:00
|
|
|
:prefix:@ (s-n)
|
|
|
|
d:lookup d:xt fetch
|
2017-10-25 03:02:02 +02:00
|
|
|
compiling? [ (life....) #3841 , , ] [ fetch ] choose ; immediate
|
2017-10-24 23:03:00 +02:00
|
|
|
|
2017-10-24 23:00:50 +02:00
|
|
|
:prefix:! (s-n)
|
|
|
|
d:lookup d:xt fetch
|
2017-10-25 03:02:02 +02:00
|
|
|
compiling? [ (list....) #4097 , , ] [ store ] choose ; immediate
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
It's sometimes useful to inline values directly. I use a backtick
|
|
|
|
prefix for this.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:prefix:` (s-)
|
|
|
|
compiling? [ s:to-number , ] [ drop ] choose ; immediate
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
It's traditional to have a word named `here` which returns the next
|
|
|
|
free address in memory.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:here (-a) @Heap ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
The next few words aren't useful until the `s:` namespace is defined.
|
|
|
|
With strings and the `'` prefix they allow creation of variables and
|
|
|
|
constants.
|
|
|
|
|
|
|
|
| To create a | Use a form like |
|
|
|
|
| ---------------------------- | ------------------ |
|
|
|
|
| Variable | `'Base var` |
|
|
|
|
| Variable, with initial value | `#10 'Base var<n>` |
|
|
|
|
| Constant | `#-1 'TRUE const` |
|
|
|
|
|
|
|
|
The first word creates a new header pointing to `here`. This is used
|
|
|
|
to build other data structures without invoking the `:` compiler.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:d:create (s-)
|
|
|
|
(s-) &class:data #0 d:add-header
|
|
|
|
here d:last d:xt store ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
And then the others are trivial.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:var (s-) d:create #0 , ;
|
|
|
|
:var<n> (ns-) d:create , ;
|
|
|
|
:const (ns-) d:create d:last d:xt store ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
The `const` word bears a tiny bit of explaination. It takes advantage
|
|
|
|
of Retro's word class model. It creates a header, with a class of
|
|
|
|
`class:data`, then sets the word pointer to the value. Since the data
|
|
|
|
class either leaves the word pointer on the stack or compiles it as
|
|
|
|
a literal into a definition, this allows constants to exist as just
|
|
|
|
a header with no special runtime code.
|
|
|
|
|
|
|
|
The core Rx language provides a few basic stack shuffling words: `push`,
|
|
|
|
`pop`, `drop`, `swap`, and `dup`. There are quite a few more that are
|
|
|
|
useful. Some of these are provided here.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:tuck (xy-yxy) dup push swap pop ;
|
|
|
|
:over (xy-xyx) push dup pop swap ;
|
|
|
|
:dup-pair (xy-xyxy) over over ;
|
|
|
|
:nip (xy-y) swap drop ;
|
|
|
|
:drop-pair (nn-) drop drop ;
|
|
|
|
:?dup (n-nn||n-n) dup 0; ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Retro makes use of anonymous functions called *quotations* for much of
|
|
|
|
the execution flow and stack control. The words that operate on these
|
|
|
|
quotations are called *combinators*.
|
|
|
|
|
|
|
|
`dip` executes a quotation after moving a value off the stack. The
|
|
|
|
value is restored after execution completes. These are equivilent:
|
|
|
|
|
|
|
|
#10 #12 [ #3 - ] dip
|
|
|
|
#10 #12 push #3 - pop
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:dip (nq-n) swap push call pop ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
`sip` is similar to dip, but leaves a copy of the value on the stack
|
|
|
|
while the quotation is executed. These are equivilent:
|
|
|
|
|
|
|
|
#10 [ #3 * ] sip
|
|
|
|
#10 dup push #3 * pop
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:sip (nq-n) push dup pop swap &call dip ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Apply each quote to a copy of x
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:bi (xqq-) &sip dip call ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Apply q1 to x and q2 to y
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:bi* (xyqq-) &dip dip call ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Apply q to x and y
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:bi@ (xyq-) dup bi* ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Apply each quote to a copy of x
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:tri (xqqq-) [ &sip dip sip ] dip call ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Apply q1 to x, q2 to y, and q3 to z
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:tri* (xyzqqq-) [ [ swap &dip dip ] dip dip ] dip call ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Apply q to x, y, and z
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:tri@ dup dup tri* ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
## Flow
|
|
|
|
|
|
|
|
Execute quote until quote returns a flag of 0.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:while (q-)
|
|
|
|
[ repeat dup dip swap 0; drop again ] call drop ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Execute quote until quote returns a flag of -1.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:until (q-)
|
|
|
|
[ repeat dup dip swap #-1 xor 0; drop again ] call drop ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
The `times` combinator runs a quote (n) times.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:times (q-)
|
|
|
|
swap [ repeat 0; #1 - push &call sip pop again ] call drop ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Taking a break from combinators for a bit, I turn to some words for
|
|
|
|
comparing things. First, constants for TRUE and FALSE.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:TRUE (-n) #-1 ;
|
|
|
|
:FALSE (-n) #0 ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
The basic Rx kernel doesn't provide two useful forms which I'll
|
|
|
|
provide here.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:lteq? (nn-f) dup-pair eq? [ lt? ] dip or ;
|
|
|
|
:gteq? (nn-f) dup-pair eq? [ gt? ] dip or ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
And then some numeric comparators.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:n:MAX (-n) #2147483647 ;
|
2017-10-25 03:36:47 +02:00
|
|
|
:n:MIN (-n) #-2147483648 ;
|
2017-10-16 18:09:39 +02:00
|
|
|
:n:zero? (n-f) #0 eq? ;
|
|
|
|
:n:-zero? (n-f) #0 -eq? ;
|
|
|
|
:n:negative? (n-f) #0 lt? ;
|
|
|
|
:n:positive? (n-f) #-1 gt? ;
|
|
|
|
:n:strictly-positive? (n-f) #0 gt? ;
|
|
|
|
:n:even? (n-f) #2 /mod drop n:zero? ;
|
|
|
|
:n:odd? (n-f) #2 /mod drop n:-zero? ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
And now back to combinators.
|
|
|
|
|
|
|
|
`case` is a conditional combinator. It's actually pretty useful. What
|
|
|
|
it does is compare a value on the stack to a specific value. If the
|
|
|
|
values are identical, it discards the value and calls a quote before
|
|
|
|
exiting the word. Otherwise it leaves the stack alone and allows
|
|
|
|
execution to continue.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
:c:vowel?
|
|
|
|
$a [ TRUE ] case
|
|
|
|
$e [ TRUE ] case
|
|
|
|
$i [ TRUE ] case
|
|
|
|
$o [ TRUE ] case
|
|
|
|
$u [ TRUE ] case
|
|
|
|
drop FALSE ;
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:case
|
|
|
|
[ over eq? ] dip swap
|
|
|
|
[ nip call TRUE ] [ drop FALSE ] choose 0; pop drop drop ;
|
|
|
|
:s:case
|
|
|
|
[ over s:eq? ] dip swap
|
|
|
|
[ nip call TRUE ] [ drop FALSE ] choose 0; pop drop drop ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Two more stack shufflers.
|
|
|
|
|
|
|
|
`rot` rotates the top three values.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:rot (abc-bca) [ swap ] dip swap ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Next is `tors`. Short for *top of return stack*, this returns the top
|
|
|
|
item on the address stack. As an analog to traditional Forth, this is
|
|
|
|
equivilent to `R@`.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:tors (-n) pop pop dup push swap push ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
The core Rx language provides addition, subtraction, multiplication,
|
|
|
|
and a combined division/remainder. Retro expands on this.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-25 03:42:06 +02:00
|
|
|
:/ (nq-d) /mod nip ;
|
2017-10-16 18:09:39 +02:00
|
|
|
:mod (nq-r) /mod drop ;
|
|
|
|
:not (n-n) #-1 xor ;
|
|
|
|
:n:pow (bp-n) #1 swap [ over * ] times nip ;
|
|
|
|
:n:negate (n-n) #-1 * ;
|
|
|
|
:n:square (n-n) dup * ;
|
|
|
|
:n:sqrt (n-n) #1 [ repeat dup-pair / over - #2 / 0; + again ] call nip ;
|
|
|
|
:n:min (nn-n) dup-pair lt? [ drop ] [ nip ] choose ;
|
|
|
|
:n:max (nn-n) dup-pair gt? [ drop ] [ nip ] choose ;
|
|
|
|
:n:abs (n-n) dup n:negate n:max ;
|
|
|
|
:n:limit (nlu-n) swap push n:min pop n:max ;
|
|
|
|
:n:inc (n-n) #1 + ;
|
|
|
|
:n:dec (n-n) #1 - ;
|
|
|
|
:n:between? (nul-) rot [ rot rot n:limit ] sip eq? ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Some of the above, like `n:inc`, are useful with variables. But it's
|
|
|
|
messy to execute sequences like:
|
|
|
|
|
|
|
|
@foo n:inc !foo
|
|
|
|
|
|
|
|
The `v:` namespace provides words which simplify the overall handling
|
|
|
|
of variables. With this, the above can become simply:
|
|
|
|
|
|
|
|
&foo v:inc
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:v:inc-by (na-) [ fetch + ] sip store ;
|
|
|
|
:v:dec-by (na-) [ fetch swap - ] sip store ;
|
|
|
|
:v:inc (n-n) #1 swap v:inc-by ;
|
|
|
|
:v:dec (n-n) #1 swap v:dec-by ;
|
|
|
|
:v:limit (alu-) push push dup fetch pop pop n:limit swap store ;
|
|
|
|
:v:on (a-) TRUE swap store ;
|
|
|
|
:v:off (a-) FALSE swap store ;
|
|
|
|
:v:preserve (aq-) swap dup fetch [ [ call ] dip ] dip swap store ;
|
|
|
|
:allot (n-) &Heap v:inc-by ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
If you need to update a stored variable there are two typical forms:
|
|
|
|
|
|
|
|
#1 'Next var<n>
|
|
|
|
@Next #10 * !Next
|
|
|
|
|
|
|
|
Or:
|
|
|
|
|
|
|
|
#1 'Next var<n>
|
|
|
|
&Next [ fetch #10 * ] sip store
|
|
|
|
|
|
|
|
The `v:update-using` replaces this with:
|
|
|
|
|
|
|
|
#1 'Next var<n>
|
|
|
|
&Next [ #10 * ] v:update-using
|
|
|
|
|
|
|
|
It takes care of preserving the variable address, fetching the stored
|
|
|
|
value, and updating with the resulting value.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:v:update-using (aq-) swap [ fetch swap call ] sip store ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
I have a simple word `copy` which copies memory to another location.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:copy (aan-) [ &fetch-next dip store-next ] times drop drop ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Now for something tricky: a system for lexical scoping.
|
|
|
|
|
|
|
|
The dictionary is a simple linked list. Retro allows for some control
|
|
|
|
over what is visible using the `{{`, `---reveal---`, and `}}` words.
|
|
|
|
|
|
|
|
As an example:
|
|
|
|
|
|
|
|
{{
|
|
|
|
:increment dup fetch n:inc swap store ;
|
2017-10-25 03:22:49 +02:00
|
|
|
:Value `0 ; data
|
2017-10-16 18:09:39 +02:00
|
|
|
---reveal---
|
|
|
|
:next-number @Value &Value increment ;
|
|
|
|
}}
|
|
|
|
|
|
|
|
Only the `next-number` function will remain visible once `}}` is
|
|
|
|
executed.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:ScopeList `0 `0 ;
|
|
|
|
:{{ (-)
|
|
|
|
d:last dup &ScopeList store-next store ;
|
|
|
|
:---reveal--- (-)
|
|
|
|
d:last &ScopeList n:inc store ;
|
|
|
|
:}} (-)
|
|
|
|
&ScopeList fetch-next swap fetch eq?
|
|
|
|
[ @ScopeList !Dictionary ]
|
|
|
|
[ @ScopeList [ &Dictionary repeat fetch dup fetch &ScopeList n:inc fetch -eq? 0; drop again ] call store ] choose ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
--> The scoping code is a bit messy. I'd like to simplify it.
|
|
|
|
|
|
|
|
|
|
|
|
A buffer is a linear memory buffer. Retro provides a `buffer:`
|
|
|
|
namespace for working with them.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
{{
|
|
|
|
:Buffer `0 ; data
|
|
|
|
:Ptr `0 ; data
|
|
|
|
:terminate (-) #0 @Ptr store ;
|
|
|
|
---reveal---
|
|
|
|
:buffer:start (-a) @Buffer ;
|
|
|
|
:buffer:end (-a) @Ptr ;
|
|
|
|
:buffer:add (c-) buffer:end store &Ptr v:inc terminate ;
|
|
|
|
:buffer:get (-c) &Ptr v:dec buffer:end fetch terminate ;
|
|
|
|
:buffer:empty (-) buffer:start !Ptr terminate ;
|
|
|
|
:buffer:size (-n) buffer:end buffer:start - ;
|
|
|
|
:buffer:set (a-) !Buffer buffer:empty ;
|
|
|
|
:buffer:preserve (q-)
|
|
|
|
@Buffer @Ptr [ [ call ] dip !Buffer ] dip !Ptr ;
|
|
|
|
}}
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
And now for strings. Traditional Forth systems have a messy mix of
|
|
|
|
strings. You have counted strings, address/length pairs, and sometimes
|
|
|
|
other forms.
|
|
|
|
|
|
|
|
Retro uses zero terminated strings. I know that counted strings are
|
|
|
|
better in many ways, but I've used these for years and they are a
|
|
|
|
workable approach.
|
|
|
|
|
|
|
|
Temporary strings are allocated in a circular pool (at STRINGS).
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-25 03:10:58 +02:00
|
|
|
:TempStrings ; data #32 !TempStrings
|
|
|
|
:TempStringMax ; data #512 !TempStringMax
|
2017-10-16 18:09:39 +02:00
|
|
|
:STRINGS EOM @TempStrings @TempStringMax * - ;
|
|
|
|
|
|
|
|
{{
|
|
|
|
:s:Current `0 ; data
|
|
|
|
|
2017-10-23 21:14:41 +02:00
|
|
|
:s:pointer (-p) @s:Current @TempStringMax * STRINGS + ;
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:next (-)
|
|
|
|
&s:Current v:inc
|
|
|
|
@s:Current @TempStrings eq? [ #0 !s:Current ] if ;
|
|
|
|
---reveal---
|
|
|
|
:s:temp (s-s) dup s:length n:inc s:pointer swap copy s:pointer s:next ;
|
|
|
|
:s:empty (-s) s:pointer s:next ;
|
|
|
|
}}
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Permanent strings are compiled into memory. To skip over them a helper
|
|
|
|
function is used. When compiled into a definition this will look like:
|
|
|
|
|
|
|
|
lit &s:skip
|
|
|
|
call
|
|
|
|
:stringbegins
|
|
|
|
.data 98
|
|
|
|
.data 99
|
|
|
|
.data 100
|
|
|
|
.data 0
|
|
|
|
lit &stringbegins
|
|
|
|
|
|
|
|
The `s:skip` adjusts the Nga instruction pointer to skip to the code
|
|
|
|
following the stored string.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:skip (-) pop [ fetch-next n:-zero? ] while n:dec push ;
|
|
|
|
:s:keep (s-s) compiling? [ &s:skip class:word ] if here [ s, ] dip class:data ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
And now a quick `'` prefix. (This will be replaced later). What this
|
|
|
|
does is either move the string token to the temporary buffer or compile
|
|
|
|
it into the current definition.
|
|
|
|
|
|
|
|
This doesn't support spaces. I use underscores instead. E.g.,
|
|
|
|
|
|
|
|
'Hello_World!
|
|
|
|
|
|
|
|
Later in the code I'll add a better implementation which can handle
|
|
|
|
conversion of _ into spaces.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:prefix:' compiling? [ s:keep ] [ s:temp ] choose ; immediate
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
`s:chop` removes the last character from a string.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:chop (s-s) s:temp dup s:length over + n:dec #0 swap store ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
`s:reverse` reverses the order of a string. E.g.,
|
|
|
|
|
|
|
|
'hello' -> 'olleh'
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:reverse (s-s)
|
|
|
|
[ dup s:temp buffer:set &s:length [ dup s:length + n:dec ] bi swap
|
|
|
|
[ dup fetch buffer:add n:dec ] times drop buffer:start s:temp ]
|
|
|
|
buffer:preserve ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Trimming removes leading (`s:trim-left`) or trailing (`s:trim-right`)
|
|
|
|
spaces from a string. `s:trim` removes both leading and trailing spaces.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:trim-left (s-s) s:temp [ fetch-next [ #32 eq? ] [ n:zero? ] bi and ] while n:dec ;
|
|
|
|
:s:trim-right (s-s) s:temp s:reverse s:trim-left s:reverse ;
|
|
|
|
:s:trim (s-s) s:trim-right s:trim-left ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
`s:prepend` and `s:append` for concatenating strings together.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:prepend (ss-s)
|
|
|
|
s:temp [ dup s:length + [ dup s:length n:inc ] dip swap copy ] sip ;
|
|
|
|
:s:append (ss-s) swap s:prepend ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
`s:for-each` executes a quote once for each cell in string. It is
|
|
|
|
a key part of building the other high-level string operations.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:for-each (sq-)
|
|
|
|
[ repeat
|
|
|
|
over fetch 0; drop
|
|
|
|
dup-pair
|
|
|
|
[ [ [ fetch ] dip call ] dip ] dip
|
|
|
|
[ n:inc ] dip
|
|
|
|
again
|
|
|
|
] call drop-pair ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
`s:filter` returns a new string, consisting of the characters from
|
|
|
|
another string that are filtered by a quotation.
|
|
|
|
|
|
|
|
'This_is_a_test [ c:-vowel? ] s:filter
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:filter (sq-s)
|
|
|
|
[ s:empty buffer:set swap
|
|
|
|
[ dup-pair swap call
|
|
|
|
[ buffer:add ]
|
|
|
|
[ drop ] choose
|
|
|
|
] s:for-each drop buffer:start
|
|
|
|
] buffer:preserve ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
`s:map` Return a new string resulting from applying a quotation to each
|
|
|
|
character in a source string.
|
|
|
|
|
|
|
|
'This_is_a_test [ $_ [ ASCII:SPACE ] case ] s:map
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:map (sq-s)
|
|
|
|
[ s:empty buffer:set swap
|
|
|
|
[ over call buffer:add ]
|
|
|
|
s:for-each drop buffer:start
|
|
|
|
] buffer:preserve ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
`s:substr` returns a subset of a string. Provide it with a string,
|
|
|
|
a starting offset, and a length.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:substr (sfl-s)
|
|
|
|
[ + s:empty ] dip [ over [ copy ] dip ] sip
|
|
|
|
over [ + #0 swap store ] dip ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
`s:right` and `s:left` are similar to `s:substr`, but operate
|
|
|
|
from fixed ends of the string.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:right (sn-s) over s:length over - swap s:substr ;
|
|
|
|
:s:left (sn-s) #0 swap s:substr ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Hash (using DJB2)
|
|
|
|
|
2017-10-23 20:56:56 +02:00
|
|
|
I use the djb2 hash algorithm for computing hashes from strings.
|
|
|
|
There are certainly better hashes out there, but this is pretty
|
|
|
|
simple and works well for my limited hash needs. The implementation
|
|
|
|
was adapted from the C example at http://www.cse.yorku.ca/~oz/hash.html
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:hash (s-n) #5381 swap [ swap #33 * + ] s:for-each ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Copy a string, including the terminator.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:copy (ss-) over s:length n:inc copy ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2017-10-25 03:10:58 +02:00
|
|
|
RETRO provides string constants for several ranges of characters that
|
|
|
|
are of some general interest.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:DIGITS (-s) '0123456789 ;
|
|
|
|
:s:ASCII-LOWERCASE (-s) 'abcdefghijklmnopqrstuvwxyz ;
|
|
|
|
:s:ASCII-UPPERCASE (-s) 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ;
|
|
|
|
:s:ASCII-LETTERS (-s) 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ;
|
|
|
|
:s:PUNCTUATION (-s) '_!"#$%&'()*+,-./:;<=>?@[\]^`{|}~ $_ over store ;
|
|
|
|
's:WHITESPACE d:create #32, #9 , #10 , #13 , #0 ,
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Not all characters can be obtained via the $ prefix. ASCII has many
|
|
|
|
characters that aren't really intended to be printable. Retro has an
|
|
|
|
`ASCII` namespace providing symbolic names for these.
|
|
|
|
|
|
|
|
Note that `ASCII:HT` is the horizontal tab character.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-11-07 14:40:15 +01:00
|
|
|
#0 'ASCII:NUL const #1 'ASCII:SOH const
|
|
|
|
#2 'ASCII:STX const #3 'ASCII:ETX const
|
|
|
|
#4 'ASCII:EOT const #5 'ASCII:ENQ const
|
|
|
|
#6 'ASCII:ACK const #7 'ASCII:BEL const
|
|
|
|
#8 'ASCII:BS const #9 'ASCII:HT const
|
|
|
|
#10 'ASCII:LF const #11 'ASCII:VT const
|
|
|
|
#12 'ASCII:FF const #13 'ASCII:CR const
|
|
|
|
#14 'ASCII:SO const #15 'ASCII:SI const
|
|
|
|
#16 'ASCII:DLE const #17 'ASCII:DC1 const
|
|
|
|
#18 'ASCII:DC2 const #19 'ASCII:DC3 const
|
|
|
|
#20 'ASCII:DC4 const #21 'ASCII:NAK const
|
|
|
|
#22 'ASCII:SYN const #23 'ASCII:ETB const
|
|
|
|
#24 'ASCII:CAN const #25 'ASCII:EM const
|
|
|
|
#26 'ASCII:SUB const #27 'ASCII:ESC const
|
|
|
|
#28 'ASCII:FS const #29 'ASCII:GS const
|
|
|
|
#30 'ASCII:RS const #31 'ASCII:US const
|
|
|
|
#32 'ASCII:SPACE const #127 'ASCII:DEL const
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
These words operate on character values. Retro currently deals with
|
|
|
|
ASCII, though cells are 32 bits in length, so Unicode values can be
|
|
|
|
stored.
|
|
|
|
|
|
|
|
First are a bunch of words to help identify character values.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:c:letter? (c-f) $A $z n:between? ;
|
|
|
|
:c:lowercase? (c-f) $a $z n:between? ;
|
|
|
|
:c:uppercase? (c-f) $A $Z n:between? ;
|
|
|
|
:c:digit? (c-f) $0 $9 n:between? ;
|
|
|
|
:c:whitespace? (c-f)
|
|
|
|
ASCII:SPACE [ TRUE ] case
|
|
|
|
ASCII:HT [ TRUE ] case
|
|
|
|
ASCII:LF [ TRUE ] case
|
|
|
|
ASCII:CR [ TRUE ] case
|
|
|
|
drop FALSE ;
|
|
|
|
:c:visible? (c-f) #31 #126 n:between? ;
|
|
|
|
:c:vowel? (c-f)
|
|
|
|
$a [ TRUE ] case
|
|
|
|
$e [ TRUE ] case
|
|
|
|
$i [ TRUE ] case
|
|
|
|
$o [ TRUE ] case
|
|
|
|
$u [ TRUE ] case
|
|
|
|
$A [ TRUE ] case
|
|
|
|
$E [ TRUE ] case
|
|
|
|
$I [ TRUE ] case
|
|
|
|
$O [ TRUE ] case
|
|
|
|
$U [ TRUE ] case
|
|
|
|
drop FALSE ;
|
|
|
|
:c:consonant? (c-f)
|
|
|
|
dup c:letter? [ c:vowel? not ] [ drop FALSE ] choose ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
And the inverse forms. (These are included for readability and
|
|
|
|
orthiginal completion).
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-25 03:32:19 +02:00
|
|
|
:c:-lowercase? (c-f) c:lowercase? not ;
|
|
|
|
:c:-uppercase? (c-f) c:uppercase? not ;
|
|
|
|
:c:-digit? (c-f) c:digit? not ;
|
|
|
|
:c:-whitespace? (c-f) c:whitespace? not ;
|
|
|
|
:c:-visible? (c-f) c:visible? not ;
|
|
|
|
:c:-vowel? (c-f) c:vowel? not ;
|
|
|
|
:c:-consonant? (c-f) c:consonant? not ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
The next few words perform simple transformations.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:c:to-upper (c-c) dup c:lowercase? 0; drop ASCII:SPACE - ;
|
|
|
|
:c:to-lower (c-c) dup c:uppercase? 0; drop ASCII:SPACE + ;
|
|
|
|
:c:toggle-case (c-c) dup c:lowercase? [ c:to-upper ] [ c:to-lower ] choose ;
|
|
|
|
:c:to-string (c-s) '. s:temp [ store ] sip ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
With the character transformations a few more string words are
|
|
|
|
possible.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:to-upper (s-s) [ c:to-upper ] s:map ;
|
|
|
|
:s:to-lower (s-s) [ c:to-lower ] s:map ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Convert a decimal (base 10) number to a string.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
{{
|
2017-10-25 03:32:19 +02:00
|
|
|
'Value var
|
2017-10-16 18:09:39 +02:00
|
|
|
:correct (c-c)
|
|
|
|
dup $0 lt? [ $0 over - #2 * + ] if ;
|
|
|
|
---reveal---
|
|
|
|
:n:to-string (n-s)
|
|
|
|
[ here buffer:set dup !Value n:abs
|
|
|
|
[ #10 /mod swap $0 + correct buffer:add dup n:-zero? ] while drop
|
|
|
|
@Value n:negative? [ $- buffer:add ] if
|
|
|
|
buffer:start s:reverse s:temp ] buffer:preserve ;
|
|
|
|
}}
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Now replace the old prefix:' with this one that can optionally turn
|
|
|
|
underscores into spaces.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
TRUE 'RewriteUnderscores var<n>
|
|
|
|
|
|
|
|
{{
|
|
|
|
:sub (c-c) $_ [ ASCII:SPACE ] case ;
|
|
|
|
:rewrite (s-s)
|
|
|
|
@RewriteUnderscores [ [ sub ] s:map ] if &prefix:' call ;
|
|
|
|
---reveal---
|
|
|
|
:prefix:' rewrite ; immediate
|
|
|
|
}}
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Building on `s:for-each`, I am able to implement `s:index-of`, which
|
|
|
|
finds the first instance of a character in a string.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:index-of (sc-n)
|
|
|
|
swap [ repeat
|
|
|
|
fetch-next 0; swap
|
|
|
|
[ over -eq? ] dip
|
|
|
|
swap 0; drop
|
|
|
|
again
|
|
|
|
] sip
|
|
|
|
[ - n:dec nip ] sip
|
|
|
|
s:length over eq? [ drop #-1 ] if ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
`s:contains-char?` returns a flag indicating whether or not a given
|
|
|
|
character is in a string.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:contains-char? (sc-f) s:index-of #-1 -eq? ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
`s:contains-string?` returns a flag indicating whether or not a given
|
|
|
|
substring is in a string.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
{{
|
|
|
|
'Src var
|
|
|
|
'Tar var
|
|
|
|
'Pad var
|
|
|
|
'I var
|
|
|
|
'F var
|
|
|
|
|
|
|
|
:terminate (-)
|
|
|
|
#0 @Pad @Tar s:length + store ;
|
|
|
|
|
|
|
|
:extract (-)
|
|
|
|
@Src @I + @Pad @Tar s:length copy ;
|
|
|
|
|
|
|
|
:compare (-)
|
|
|
|
@Pad @Tar s:eq? @F or !F ;
|
|
|
|
|
|
|
|
:next (-)
|
|
|
|
&I v:inc ;
|
|
|
|
---reveal---
|
|
|
|
:s:contains-string? (ss-f)
|
|
|
|
!Tar !Src s:empty !Pad #0 !I #0 !F
|
|
|
|
@Src s:length
|
|
|
|
[ extract terminate compare next ] times
|
|
|
|
@F ;
|
|
|
|
}}
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
The `s:split` splits a string on the first instance of a given
|
|
|
|
character. Results are undefined if the character can not be
|
|
|
|
located.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:split (sc-ss)
|
|
|
|
dup-pair s:index-of nip dup-pair s:left [ + ] dip ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Ok, This is a bit of a hack, but very useful at times.
|
|
|
|
|
|
|
|
Assume you have a bunch of values:
|
|
|
|
|
|
|
|
#3 #1 #2 #5
|
|
|
|
|
|
|
|
And you want to reorder them into something new:
|
|
|
|
|
|
|
|
#1 #3 #5 #5 #2 #1
|
|
|
|
|
|
|
|
Rather than using a lot of shufflers, `reorder` simplfies this into:
|
|
|
|
|
|
|
|
#3 #1 #2 #5
|
|
|
|
'abcd 'baddcb reorder
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
{{
|
|
|
|
'Values var #27 allot
|
|
|
|
:from s:length dup [ [ &Values + store ] sip n:dec ] times drop ;
|
|
|
|
:to dup s:length [ fetch-next $a - n:inc &Values + fetch swap ] times drop ;
|
|
|
|
---reveal---
|
|
|
|
:reorder (...ss-?) [ from ] dip to ;
|
|
|
|
}}
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2017-10-25 03:10:58 +02:00
|
|
|
`does` is intended to be paired with `d:create` to attach an action to a
|
|
|
|
newwly created data structure. An example use might be something like:
|
|
|
|
|
|
|
|
:constant (ns-) d:create , [ fetch ] does ;
|
|
|
|
|
|
|
|
In a traditional Forth this is similar in spirit to DOES>.
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:curry (vp-p) here [ swap compile:lit compile:call compile:ret ] dip ;
|
|
|
|
:does (q-) d:last<xt> swap curry d:last d:xt store &class:word reclass ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
`d:for-each` is a combinator which runs a quote once for each header in
|
|
|
|
the dictionary. A pointer to each header will be passed to the quote as
|
|
|
|
it is run.
|
|
|
|
|
2017-10-25 03:22:49 +02:00
|
|
|
This can be used for implementing `words`:
|
|
|
|
|
|
|
|
[ d:name puts sp ] d:for-each
|
|
|
|
|
|
|
|
Or finding the length of the longest name in the dictionary:
|
|
|
|
|
|
|
|
#0 [ d:name s:length n:max ] d:for-each
|
|
|
|
|
|
|
|
It's a handy combinator that lets me quickly walk though the entire
|
|
|
|
dictionary in a very clean manner.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:d:for-each (q-)
|
|
|
|
&Dictionary [ repeat fetch 0;
|
|
|
|
dup-pair [ [ swap call ] dip ] dip again ] call drop ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2017-10-29 03:09:56 +01:00
|
|
|
Using `d:for-each`, I implement a means of looking up a dictionary
|
|
|
|
header by the `d:xt` field.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:d:lookup-xt (a-d)
|
|
|
|
#0 swap [ dup-pair d:xt fetch eq?
|
|
|
|
[ swap [ nip ] dip ] [ drop ] choose ] d:for-each drop ;
|
|
|
|
~~~
|
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
Use `s:with-format` to construct a string from multiple items. This
|
|
|
|
can be illustrated with:
|
|
|
|
|
|
|
|
#4 #6 #10 '%n-%n=%n\n s:with-format
|
|
|
|
|
|
|
|
The format language is simple:
|
|
|
|
|
|
|
|
| \n | Replace with a LF |
|
|
|
|
| \t | Replace with a TAB |
|
2017-11-08 15:42:48 +01:00
|
|
|
| \\ | Replace with a single \ |
|
|
|
|
| \ | Replace with an underscore (_) |
|
2017-10-16 18:09:39 +02:00
|
|
|
| %c | Replace with a character on the stack |
|
|
|
|
| %s | Replace with a string on the stack |
|
|
|
|
| %n | Replace with the next number on the stack |
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
{{
|
|
|
|
:char (c-)
|
2017-11-08 15:42:48 +01:00
|
|
|
ASCII:SPACE [ $_ buffer:add ] case
|
2017-10-16 18:09:39 +02:00
|
|
|
$n [ ASCII:LF buffer:add ] case
|
|
|
|
$t [ ASCII:HT buffer:add ] case
|
|
|
|
buffer:add ;
|
|
|
|
|
|
|
|
:string (a-a)
|
|
|
|
repeat fetch-next 0; buffer:add again ;
|
|
|
|
|
|
|
|
:type (aac-)
|
|
|
|
$c [ swap buffer:add ] case
|
|
|
|
$s [ swap string drop ] case
|
|
|
|
$n [ swap n:to-string string drop ] case
|
|
|
|
drop ;
|
|
|
|
|
|
|
|
:handle (ac-a)
|
|
|
|
$\ [ fetch-next char ] case
|
|
|
|
$% [ fetch-next type ] case
|
|
|
|
buffer:add ;
|
|
|
|
---reveal---
|
|
|
|
:s:with-format (...s-s)
|
|
|
|
[ s:empty [ buffer:set
|
|
|
|
[ repeat fetch-next 0; handle again ]
|
|
|
|
call drop ] sip ] buffer:preserve ;
|
|
|
|
}}
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2017-10-20 15:30:31 +02:00
|
|
|
~~~
|
|
|
|
:s:const (ss-) [ s:keep ] dip const ;
|
|
|
|
~~~
|
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
## Sets
|
|
|
|
|
|
|
|
Sets are statically sized arrays. They are represented in memory as:
|
|
|
|
|
|
|
|
count
|
|
|
|
data #1 (first)
|
|
|
|
...
|
|
|
|
data #n (last)
|
|
|
|
|
|
|
|
Since the count comes first, a simple `fetch` will suffice to get it,
|
|
|
|
but for completeness (and to allow for future changes), we wrap this
|
|
|
|
as `set:length`:
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:set:length (a-n) fetch ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
The first couple of words are used to create sets. The first,
|
|
|
|
`set:from-results` executes a quote and constructs a set from the
|
|
|
|
returned values.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:set:from-results (q-a)
|
|
|
|
depth [ call ] dip depth swap -
|
|
|
|
here [ dup , [ , ] times ] dip ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
The second, `set:from-string`, creates a new string with the characters
|
|
|
|
in given a string.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:set:from-string (s-a)
|
|
|
|
s:reverse [ [ ] s:for-each ] curry
|
|
|
|
set:from-results ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
A very crucial piece is `set:for-each`. This runs a quote once against
|
|
|
|
each value in a set. This will be leveraged to implement additional
|
|
|
|
combinators.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
{{
|
|
|
|
'Q var
|
|
|
|
---reveal---
|
|
|
|
:set:for-each (aq-)
|
2017-10-25 03:22:49 +02:00
|
|
|
&Q [ !Q fetch-next
|
2017-10-16 18:09:39 +02:00
|
|
|
[ fetch-next swap [ @Q call ] dip ] times drop
|
2017-10-25 03:22:49 +02:00
|
|
|
] v:preserve ;
|
2017-10-16 18:09:39 +02:00
|
|
|
}}
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
With this I can easily define `set:dup` to make a copy of a set.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:set:dup (a-a)
|
|
|
|
here [ dup fetch , [ , ] set:for-each ] dip ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Next is `set:filter`, which is extracts matching values from a set. This
|
|
|
|
is used like:
|
|
|
|
|
|
|
|
[ #1 #2 #3 #4 #5 #6 #7 #8 ] set:from-results
|
|
|
|
[ n:even? ] set:filter
|
|
|
|
|
|
|
|
It returns a new set with the values that the quote returned a `TRUE`
|
|
|
|
flag for.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:set:filter (aq-)
|
|
|
|
[ over [ call ] dip swap [ , ] [ drop ] choose ] curry
|
|
|
|
here [ over fetch , set:for-each ] dip here over - n:dec over store ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Next are `set:contains?` and `set:contains-string?` which compare a given
|
|
|
|
value to each item in the array and returns a flag.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
{{
|
|
|
|
'F var
|
|
|
|
---reveal---
|
|
|
|
:set:contains? (na-f)
|
|
|
|
&F v:off
|
|
|
|
[ over eq? @F or !F ] set:for-each
|
|
|
|
drop @F ;
|
|
|
|
|
|
|
|
:set:contains-string? (na-f)
|
|
|
|
&F v:off
|
|
|
|
[ over s:eq? @F or !F ] set:for-each
|
|
|
|
drop @F ;
|
|
|
|
}}
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
I implemented `set:map` to apply a quotation to each item in a set and
|
|
|
|
construct a new set from the returned values.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
[ #1 #2 #3 ] set:from-results
|
|
|
|
[ #10 * ] set:map
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:set:map (aq-a)
|
|
|
|
[ call , ] curry
|
|
|
|
here [ over fetch , set:for-each ] dip ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
You can use `set:reverse` to make a copy of a set with the values
|
|
|
|
reversed. This can be useful after a `set:from-results`.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:set:reverse (a-a)
|
|
|
|
here [ fetch-next [ + n:dec ] sip dup ,
|
|
|
|
[ dup fetch , n:dec ] times drop
|
|
|
|
] dip ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
`set:nth` provides a quick means of adjusting a set and offset into an
|
|
|
|
address for use with `fetch` and `store`.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:set:nth (an-a)
|
|
|
|
+ n:inc ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
`set:reduce` takes a set, a starting value, and a quote. It executes
|
|
|
|
the quote once for each item in the set, passing the item and the value
|
|
|
|
to the quote. The quote should consume both and return a new value.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:set:reduce (pnp-n)
|
|
|
|
[ swap ] dip set:for-each ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
## Muri: an assembler
|
|
|
|
|
|
|
|
Muri is my minimalist assembler for Nga. This is an attempt to
|
|
|
|
implement something similar in Retro.
|
|
|
|
|
2017-10-23 20:45:49 +02:00
|
|
|
This requires some knowledge of the Nga architecture to be
|
|
|
|
useful. The major elements are:
|
|
|
|
|
|
|
|
**Instruction Set**
|
|
|
|
|
|
|
|
Nga has 27 instructions. These are:
|
|
|
|
|
|
|
|
0 nop 7 jump 14 gt 21 and
|
|
|
|
1 lit <v> 8 call 15 fetch 22 or
|
|
|
|
2 dup 9 ccall 16 store 23 xor
|
|
|
|
3 drop 10 return 17 add 24 shift
|
|
|
|
4 swap 11 eq 18 sub 25 zret
|
|
|
|
5 push 12 neq 19 mul 26 end
|
|
|
|
6 pop 13 lt 20 divmod
|
|
|
|
|
|
|
|
The mnemonics allow for each name to be reduced to just two
|
|
|
|
characters. In the same order as above:
|
|
|
|
|
|
|
|
no ju gt an
|
|
|
|
li ca fe or
|
|
|
|
du cc st xo
|
|
|
|
dr re ad sh
|
|
|
|
sw eq su zr
|
|
|
|
pu ne mu en
|
|
|
|
po lt di
|
|
|
|
|
|
|
|
Up to four instructions can be packed into a single memory
|
|
|
|
location. (You can only use *no*p after a *ju*mp, *ca*ll,
|
|
|
|
*cc*all, *re*t, or *zr*et as these alter the instruction
|
|
|
|
pointer.)
|
|
|
|
|
|
|
|
So a bundled sequence like:
|
|
|
|
|
|
|
|
lit 100
|
|
|
|
lit 200
|
|
|
|
add
|
|
|
|
ret
|
|
|
|
|
|
|
|
Would look like:
|
|
|
|
|
|
|
|
'liliadre i
|
|
|
|
100 d
|
|
|
|
200 d
|
|
|
|
|
|
|
|
And:
|
|
|
|
|
|
|
|
lit s:eq?
|
|
|
|
call
|
|
|
|
|
|
|
|
Would become:
|
|
|
|
|
|
|
|
'lica.... i
|
|
|
|
's:eq? r
|
|
|
|
|
|
|
|
Note the use of `..` instead of `no` for the nop's; this is
|
|
|
|
done to improve readability a little.
|
|
|
|
|
|
|
|
Instruction bundles are specified as strings, and are converted
|
|
|
|
to actual instructions by the `i` word. As in the standard Muri
|
|
|
|
assembler, the RETRO version uses `d` for decimal values and `r`
|
|
|
|
for references to named functions.
|
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
This is kept in the global namespace, but several portions are
|
|
|
|
kept private.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
{{
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
I allocate a small buffer for each portion of an instruction
|
|
|
|
bundle.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
'I0 d:create #3 allot
|
|
|
|
'I1 d:create #3 allot
|
|
|
|
'I2 d:create #3 allot
|
|
|
|
'I3 d:create #3 allot
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
The `opcode` word maps a two character instruction to an opcode
|
|
|
|
number.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:opcode (s-n)
|
|
|
|
'.. [ #0 ] s:case 'li [ #1 ] s:case
|
|
|
|
'du [ #2 ] s:case 'dr [ #3 ] s:case
|
|
|
|
'sw [ #4 ] s:case 'pu [ #5 ] s:case
|
|
|
|
'po [ #6 ] s:case 'ju [ #7 ] s:case
|
|
|
|
'ca [ #8 ] s:case 'cc [ #9 ] s:case
|
|
|
|
're [ #10 ] s:case 'eq [ #11 ] s:case
|
|
|
|
'ne [ #12 ] s:case 'lt [ #13 ] s:case
|
|
|
|
'gt [ #14 ] s:case 'fe [ #15 ] s:case
|
|
|
|
'st [ #16 ] s:case 'ad [ #17 ] s:case
|
|
|
|
'su [ #18 ] s:case 'mu [ #19 ] s:case
|
|
|
|
'di [ #20 ] s:case 'an [ #21 ] s:case
|
|
|
|
'or [ #22 ] s:case 'xo [ #23 ] s:case
|
|
|
|
'sh [ #24 ] s:case 'zr [ #25 ] s:case
|
|
|
|
'en [ #26 ] s:case drop #0 ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
I use `pack` to combine the individual parts of the instruction
|
|
|
|
bundle into a single cell.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:pack (-n)
|
|
|
|
&I0 opcode
|
|
|
|
&I1 opcode
|
|
|
|
&I2 opcode
|
|
|
|
&I3 opcode
|
|
|
|
#-24 shift swap
|
|
|
|
#-16 shift + swap
|
|
|
|
#-8 shift + swap + ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Switch to the public portion of the code.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
---reveal---
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
With this it's pretty easy to implement the instruction bundle
|
|
|
|
handler. Named `i`, this takes a string with four instruction
|
|
|
|
names, splits it into each part, calls `opcode` on each and
|
|
|
|
then `pack` to combine them before using `,` to write them into
|
|
|
|
the `Heap`.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:i (s-)
|
|
|
|
dup &I0 #2 copy #2 +
|
|
|
|
dup &I1 #2 copy #2 +
|
|
|
|
dup &I2 #2 copy #2 +
|
|
|
|
&I3 #2 copy
|
|
|
|
pack , ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
The `d` word inlines a data item.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:d (n-)
|
|
|
|
, ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
And `r` inlines a reference (pointer).
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:r (s-)
|
|
|
|
d:lookup d:xt fetch , ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
The final bits are `as{` and `}as`, which start and stop the
|
|
|
|
assembler. (Basically, they just turn the `Compiler` on and
|
|
|
|
off, restoring its state as needed).
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:as{ (-f)
|
|
|
|
@Compiler &Compiler v:off ; immediate
|
|
|
|
|
|
|
|
:}as (f-?)
|
|
|
|
!Compiler ; immediate
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
This finishes by sealing off the private words.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
}}
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
## Evaluating Source
|
|
|
|
|
|
|
|
The standard interfaces have their own approaches to getting and
|
|
|
|
dealing with user input. Sometimes though it'd be nicer to have a
|
|
|
|
way of evaluating code from within RETRO itself. This provides an
|
|
|
|
`evaluate` word.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
{{
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
First, create a buffer for the string to be evaluated. This is sized
|
|
|
|
to allow for a standard FORTH block to fit, or to easily fit a RETRO
|
|
|
|
style 512 character block. It's also long enough for most source lines
|
|
|
|
I expect to encounter when working with files.
|
|
|
|
|
2017-10-23 21:27:14 +02:00
|
|
|
I allocate this immediately prior to the temporary string buffers.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-23 21:27:14 +02:00
|
|
|
:current-line (-a) STRINGS #1025 - ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
To make use of this, we need to know how many tokens are in the input
|
|
|
|
string. The `count-tokens` word will do this, by filtering out anything
|
|
|
|
other than spaces and getting the size of the remaining string.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:count-tokens (s-n)
|
|
|
|
[ ASCII:SPACE eq? ] s:filter s:length ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
The `next-token` word splits the remainimg string on SPACE and returns
|
|
|
|
both parts.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:next-token (s-ss)
|
|
|
|
ASCII:SPACE s:split ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
And then the `process-tokens` uses `next-token` and `interpret` to go
|
|
|
|
through the string, processing each token in turn. Using the standard
|
|
|
|
`interpret` word allows for proper handling of the prefixes and classes
|
|
|
|
so everything works just as if entered directly.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:process-tokens (sn-)
|
|
|
|
[ next-token swap
|
|
|
|
[ dup s:length n:-zero? [ interpret ] [ drop ] choose ] dip n:inc
|
|
|
|
] times interpret ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
---reveal---
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
And finally, tie it all together into the single exposed word
|
|
|
|
`evaluate`.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:s:evaluate (s-...)
|
2017-10-23 21:27:14 +02:00
|
|
|
current-line s:copy
|
|
|
|
current-line dup count-tokens process-tokens ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
}}
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
## I/O
|
|
|
|
|
|
|
|
Retro really only provides one I/O function in the standard interface:
|
|
|
|
pushing a character to the output log.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:putc (c-) `1000 ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
This can be used to implement words that push other item to the log.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:nl (-) ASCII:LF putc ;
|
|
|
|
:sp (-) ASCII:SPACE putc ;
|
|
|
|
:tab (-) ASCII:HT putc ;
|
|
|
|
:puts (s-) [ putc ] s:for-each ;
|
|
|
|
:putn (n-) n:to-string puts ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
Different inteface layers may provide additional I/O words.
|
|
|
|
|
|
|
|
## Debugging Aids
|
|
|
|
|
|
|
|
I provide just a few debugging aids.
|
|
|
|
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
:words (-) [ d:name puts sp ] d:for-each ;
|
|
|
|
:reset (...-) depth repeat 0; push drop pop #1 - again ;
|
|
|
|
:dump-stack (-) depth 0; drop push dump-stack pop dup putn sp ;
|
2017-10-20 03:10:38 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2017-10-23 21:27:14 +02:00
|
|
|
~~~
|
|
|
|
:FREE (-n) STRINGS #1025 - here - ;
|
|
|
|
~~~
|
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
## The End
|
|
|
|
|
|
|
|
## Legalities
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for
|
|
|
|
any purpose with or without fee is hereby granted, provided that the
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
|
|
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
|
|
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
Copyright (c) 2008 - 2017, Charles Childers
|
|
|
|
Copyright (c) 2012 - 2013, Michal J Wallace
|
|
|
|
Copyright (c) 2009 - 2011, Luke Parrish
|
|
|
|
Copyright (c) 2009 - 2010, JGL
|
|
|
|
Copyright (c) 2010 - 2011, Marc Simpson
|
|
|
|
Copyright (c) 2011 - 2012, Oleksandr Kozachuk
|
|
|
|
Copyright (c) 2010, Jay Skeer
|
|
|
|
Copyright (c) 2010, Greg Copeland
|
|
|
|
Copyright (c) 2011, Aleksej Saushev
|
|
|
|
Copyright (c) 2011, Foucist
|
|
|
|
Copyright (c) 2011, Erturk Kocalar
|
|
|
|
Copyright (c) 2011, Kenneth Keating
|
|
|
|
Copyright (c) 2011, Ashley Feniello
|
|
|
|
Copyright (c) 2011, Peter Salvi
|
|
|
|
Copyright (c) 2011, Christian Kellermann
|
|
|
|
Copyright (c) 2011, Jorge Acereda
|
|
|
|
Copyright (c) 2011, Remy Moueza
|
|
|
|
Copyright (c) 2012, John M Harrison
|
|
|
|
Copyright (c) 2012, Todd Thomas
|