improved indentation and whitespace for markdown code blocks

All code blocks in markdown have been intended by at least 4 spaces.

We add `--tab-stop=8` to all `pandoc(1)` executions in order to set
a better tab stop indentation, especially for tab intended code blocks.

Changed `bin/pandoc-wrapper.sh` VERSION from "1.5.3 2024-03-26"
to "1.6 2024-03-31".

Fixed `1991/brnstnd/sorta.README.md` so that the sorta code
is code blocks.

Removed unneeded paragraph group in copyright in `inc/footer.default.html`.

Fixed excess whitespace after list Author on entry `index.html` page.

Changed `bin/output-index-author.sh` VERSION from "1.5.2 2024-03-26"
to "1.6 2024-03-31".

Performed `make www` to perform the above and test under macOS.

TODO: Fix the problem of the initial line of code being extra intended.
TODO: Fix excess whitespace around code that is inline.
This commit is contained in:
Landon Curt Noll committed 2024-04-01 04:48:48 -07:00
1 parent b84c27ce02
commit a996f8d2b8
734 files changed
+16723 -16963

No files matched your search

+374 -378
View File
@@ -188,64 +188,64 @@ when they are used. Normal instructions compile a pointer to their
executable part onto the end of the dictionary. As we will see, this
means that by default FIRST simply compiles things.</p>
<h4 id="integer-operations">Integer Operations</h4>
<pre><code>SYMBOL NAME FUNCTION
<pre><code> SYMBOL NAME FUNCTION
- binary minus pop top 2 elements of stack, subtract, push
- binary minus pop top 2 elements of stack, subtract, push
* multiply pop top 2 elements of stack, multiply, push
* multiply pop top 2 elements of stack, multiply, push
/ divide pop top 2 elements of stack, divide, push
/ divide pop top 2 elements of stack, divide, push
&lt;0 less than 0 pop top element of stack, push 1 if &lt; 0 else 0</code></pre>
&lt;0 less than 0 pop top element of stack, push 1 if &lt; 0 else 0</code></pre>
<p>Note that we can synthesize addition and negation from binary minus,
but we cannot synthesize a time efficient divide or multiply from it.
<code>&lt;0</code> is synthesize-able, but only in a non-portable way.</p>
<h4 id="memory-operations">Memory Operations</h4>
<pre><code>SYMBOL NAME FUNCTION
<pre><code> SYMBOL NAME FUNCTION
@ fetch pop top of stack, treat as address to push contents of
@ fetch pop top of stack, treat as address to push contents of
! store top of stack is address, 2nd is value; store to memory
and pop both off the stack</code></pre>
! store top of stack is address, 2nd is value; store to memory
and pop both off the stack</code></pre>
<h4 id="inputoutput-operations">Input/Output Operations</h4>
<pre><code>NAME FUNCTION
<pre><code> NAME FUNCTION
echo output top of stack through C&#39;s putchar()
echo output top of stack through C&#39;s putchar()
key push C&#39;s getchar() onto top of stack
key push C&#39;s getchar() onto top of stack
_read read a space-delimited word, find it in the
dictionary, and compile a pointer to
that word&#39;s code pointer onto the
current end of the dictionary</code></pre>
_read read a space-delimited word, find it in the
dictionary, and compile a pointer to
that word&#39;s code pointer onto the
current end of the dictionary</code></pre>
<p>Although <code>_read</code> could be synthesized from key, we need <code>_read</code> to be able
to compile words to be able to start any syntheses.</p>
<h4 id="execution-operations">Execution Operations</h4>
<pre><code>NAME FUNCTION
<pre><code> NAME FUNCTION
exit leave the current function: pop the return stack
into the program counter</code></pre>
exit leave the current function: pop the return stack
into the program counter</code></pre>
<h4 id="immediate-compilation-operations">Immediate (compilation) Operations</h4>
<pre><code>SYMBOL NAME FUNCTION
<pre><code> SYMBOL NAME FUNCTION
: define read in the next space-delimited word, add it to
the end of our string storage, and generate
a header for the new word so that when it
is typed it compiles a pointer to itself
so that it can be executed.
: define read in the next space-delimited word, add it to
the end of our string storage, and generate
a header for the new word so that when it
is typed it compiles a pointer to itself
so that it can be executed.
immediate immediate when used immediately after a name following a &#39;:&#39;,
it makes the word being defined run whenever
it is typed.</code></pre>
immediate immediate when used immediately after a name following a &#39;:&#39;,
it makes the word being defined run whenever
it is typed.</code></pre>
<p><code>:</code> cannot be synthesized, because we could not synthesize anything.
<code>immediate</code> has to be an immediate operation, so it could not be
synthesized unless by default operations were immediate; but that
would preclude our being able to do any useful compilation.</p>
<h4 id="stack-operations">Stack Operations</h4>
<pre><code>NAME FUNCTION
<pre><code> NAME FUNCTION
pick pop top of stack, use as index into stack and copy up
that element</code></pre>
pick pop top of stack, use as index into stack and copy up
that element</code></pre>
<p>If the data stack were stored in main memory, we could synthesize <code>pick</code>;
but putting the stack and stack pointer in main memory would significantly
increase the C source code size.</p>
@@ -264,16 +264,16 @@ word internally that it executes as its main loop. This word calls
up a word on the return stack, so it will eventually trash things.
This is discussed some more in section 2.</p>
<p>Here’s a handy summary of all the FIRST words:</p>
<pre><code>WORD DESCRIPTION
<pre><code> WORD DESCRIPTION
- * / binary integer operations on the stack
&lt;0 is top of stack less than 0?
@ ! read from or write to memory
echo key output or input one character
_read read a word from input and compile a pointer to it
exit stop running the current function
: compile the header of a definition
immediate modify the header to create an immediate word</code></pre>
- * / binary integer operations on the stack
&lt;0 is top of stack less than 0?
@ ! read from or write to memory
echo key output or input one character
_read read a word from input and compile a pointer to it
exit stop running the current function
: compile the header of a definition
immediate modify the header to create an immediate word</code></pre>
<p>Here is a sample FIRST program. I’m assuming you’re using
the ASCII character set. FIRST does not depend upon ASCII, but
since FIRST has no syntax for character constants, one normally has
@@ -286,33 +286,33 @@ differently if you wanted.</p>
inside parentheses. FIRST programs cannot have FORTH comments; but I need
some device to indicate what’s going on. (THIRD programs are an entirely
different subject.)</p>
<pre><code> ( Our first line gives the symbols for the built-ins )
: immediate _read @ ! - * / &lt;0 exit echo key _pick
<pre><code> ( Our first line gives the symbols for the built-ins )
: immediate _read @ ! - * / &lt;0 exit echo key _pick
( now we define a simple word that will print out a couple characters )
( now we define a simple word that will print out a couple characters )
: L ( define a word named &#39;L&#39; )
108 echo ( output an ascii &#39;l&#39; )
exit
: L ( define a word named &#39;L&#39; )
108 echo ( output an ascii &#39;l&#39; )
exit
: hello ( define a word named &#39;hello&#39;)
72 echo ( output an ascii &#39;H&#39; )
101 echo ( output an ascii &#39;e&#39; )
111 ( push ascii &#39;o&#39; onto the stack )
L L ( output two ascii &#39;l&#39;s )
echo ( output the &#39;o&#39; we pushed on the stack before )
10 echo ( print a newline )
exit ( stop running this routine )
: hello ( define a word named &#39;hello&#39;)
72 echo ( output an ascii &#39;H&#39; )
101 echo ( output an ascii &#39;e&#39; )
111 ( push ascii &#39;o&#39; onto the stack )
L L ( output two ascii &#39;l&#39;s )
echo ( output the &#39;o&#39; we pushed on the stack before )
10 echo ( print a newline )
exit ( stop running this routine )
: test immediate ( define a word named &#39;test&#39; that runs whenever typed )
hello ( call hello )
exit
: test immediate ( define a word named &#39;test&#39; that runs whenever typed )
hello ( call hello )
exit
test
test
( The result of running this program should be:
Hello
)</code></pre>
( The result of running this program should be:
Hello
)</code></pre>
<h2 id="section-2-motivating-third">Section 2: Motivating THIRD</h2>
<p>What is missing from FIRST? There are a large number of
important primitives that aren’t implemented, but which are
@@ -350,47 +350,46 @@ be <code>0</code>–it’s a fake dictionary entry that means <code>pushint</cod
<h2 id="section-3-building-third">Section 3: Building THIRD</h2>
<p>In this section we’ll eventually have real comments.</p>
<p>The first thing we have to do is give the symbols for our built-ins.</p>
<pre><code>: immediate _read @ ! - * / &lt; exit echo key _pick</code></pre>
<pre><code> : immediate _read @ ! - * / &lt; exit echo key _pick</code></pre>
<p>Next we want to be mildly self commenting, so we define the word <code>r</code> to push
the <em>address of the return stack pointer</em> onto the stack–NOT the value of the
return stack pointer. (In fact, when we run <code>r</code>, the value of the return stack
pointer is temporarily changed.)</p>
<pre><code>: r 1 exit</code></pre>
<pre><code> : r 1 exit</code></pre>
<p>Next, we’re currently executing a short loop that contains <code>_read</code> and
recursion, which is slowly blowing up the return stack. So let’s define a new
word, from which you can never return. What it does is drops the top value off
the return stack, calls <code>_read</code>, then calls itself. Because it kills the top of
the return stack, it can recurse indefinitely.</p>
<pre><code>: ]
r @ Get the value of the return stack pointer
1 - Subtract one
r ! Store it back into the return stack pointer
_read Read and compile one word
] Start over</code></pre>
<pre><code> : ]
r @ Get the value of the return stack pointer
1 - Subtract one
r ! Store it back into the return stack pointer
_read Read and compile one word
] Start over</code></pre>
<p>Notice that we don’t need to exit, since we never come back. Also, it’s
possible that an immediate word may get run during <code>_read</code>, and that <code>_read</code>
will never return!</p>
<p>Now let’s get compile running.</p>
<pre><code>: main immediate ]
main</code></pre>
<pre><code> : main immediate ]
main</code></pre>
<p>Next off, I’m going to do this the easy but non-portable way, and put some
character constant definitions in. I wanted them at the top of the file, but
that would have burned too much of the return stack.</p>
<pre><code>: &#39;&quot;&#39; 34 exit
: &#39;)&#39; 41 exit
: &#39;\n&#39; 10 exit
: &#39;space&#39; 32 exit
: &#39;0&#39; 48 exit
: &#39;-&#39; 45 exit
<pre><code> : &#39;&quot;&#39; 34 exit
: &#39;)&#39; 41 exit
: &#39;\n&#39; 10 exit
: &#39;space&#39; 32 exit
: &#39;0&#39; 48 exit
: &#39;-&#39; 45 exit
: cr &#39;\n&#39; echo exit</code></pre>
: cr &#39;\n&#39; echo exit</code></pre>
<p>Next, we want to define some temporary variables for locations <code>3</code>, <code>4</code>, and
<code>5</code>, since this’ll make our code look clearer.</p>
<pre><code>: _x 3 @ exit
: _x! 3 ! exit
: _y 4 @ exit
: _y! 4 ! exit
</code></pre>
<pre><code> : _x 3 @ exit
: _x! 3 ! exit
: _y 4 @ exit
: _y! 4 ! exit</code></pre>
<p>OK. Now, we want to make THIRD look vaguely like FORTH,
so we’re going to define <code>;</code>. What <code>;</code> ought to do is
terminate a compilation, and turn control over to the
@@ -399,41 +398,39 @@ command-mode handler. We don’t have one, so all we want
current word. To do this we’ll need several other words.</p>
<p>Swap by writing out the top two elements into temps, and
then reading them back in the other order.</p>
<pre><code>: swap _x! _y! _x _y exit</code></pre>
<pre><code> : swap _x! _y! _x _y exit</code></pre>
<p>Take another look and make sure you see why that works,
since it LOOKS like I’m reading them back in the same
order–in fact, it not only looks like it, but I AM!</p>
<p>Addition might be nice to have. To add, we need to
negate the top element of the stack, and then subtract.
To negate, we subtract from <code>0</code>.</p>
<pre><code>: +
0 swap -
-
exit
</code></pre>
<pre><code> : +
0 swap -
-
exit</code></pre>
<p>Create a copy of the top of stack.</p>
<pre><code>: dup _x! _x _x exit</code></pre>
<pre><code> : dup _x! _x _x exit</code></pre>
<p>Get a mnemonic name for our dictionary pointer–we need
to compile stuff, so it goes through this.</p>
<pre><code>: h 0 exit</code></pre>
<pre><code> : h 0 exit</code></pre>
<p>We’re going to need to advance that pointer, so let’s
make a generic pointer-advancing function.
Given a pointer to a memory location, increment the value
at that memory location.</p>
<pre><code>: inc
dup @ Get another copy of the address, and get the value
so now we have value, address on top of stack.
1 + Add one to the value
swap Swap to put the address on top of the stack
! exit Write it to memory
</code></pre>
<pre><code> : inc
dup @ Get another copy of the address, and get the value
so now we have value, address on top of stack.
1 + Add one to the value
swap Swap to put the address on top of the stack
! exit Write it to memory</code></pre>
<p><code>,</code> is a standard FORTH word. It should write the top of
stack into the dictionary, and advance the pointer</p>
<pre><code>: ,
h @ Get the value of the dictionary pointer
! Write the top of stack there
h inc And increment the dictionary pointer
exit</code></pre>
<pre><code> : ,
h @ Get the value of the dictionary pointer
! Write the top of stack there
h inc And increment the dictionary pointer
exit</code></pre>
<p><code>'</code> is a standard FORTH word. It should push the address
of the word that follows it onto the stack. We could
do this by making <code>'</code> immediate, but then it’d need to
@@ -442,30 +439,29 @@ as normal. When <code>'</code> is executed, the top of the return
stack will point into the instruction stream immediately
after the <code>'</code>. We push the word there, and advance the
return stack pointer so that we don’t execute it.</p>
<pre><code>: &#39;
r @ Get the address of the top of return stack
We currently have a pointer to the top of return stack
@ Get the value from there
We currently have a pointer to the instruction stream
dup Get another copy of it--the bottom copy will stick
around until the end of this word
1 + Increment the pointer, pointing to the NEXT instruction
r @ ! Write it back onto the top of the return stack
We currently have our first copy of the old pointer
to the instruction stream
@ Get the value there--the address of the &quot;next word&quot;
exit
</code></pre>
<pre><code> : &#39;
r @ Get the address of the top of return stack
We currently have a pointer to the top of return stack
@ Get the value from there
We currently have a pointer to the instruction stream
dup Get another copy of it--the bottom copy will stick
around until the end of this word
1 + Increment the pointer, pointing to the NEXT instruction
r @ ! Write it back onto the top of the return stack
We currently have our first copy of the old pointer
to the instruction stream
@ Get the value there--the address of the &quot;next word&quot;
exit</code></pre>
<p>Now we’re set. <code>;</code> should be an immediate word that pushes
the address of <code>exit</code> onto the stack, then writes it out.</p>
<pre><code>: ; immediate
&#39; exit Get the address of exit
, Compile it
exit And we should return</code></pre>
<pre><code> : ; immediate
&#39; exit Get the address of exit
, Compile it
exit And we should return</code></pre>
<p>Now let’s test out <code>;</code> by defining a useful word:</p>
<pre><code>: drop 0 * + ;</code></pre>
<pre><code> : drop 0 * + ;</code></pre>
<p>Since we have <code>inc</code>, we ought to make <code>dec</code>:</p>
<pre><code>: dec dup @ 1 - swap ! ;</code></pre>
<pre><code> : dec dup @ 1 - swap ! ;</code></pre>
<p>Our next goal, now that we have <code>;</code>, is to implement
<code>if-then</code>. To do this, we’ll need to play fast and
loose with the return stack, so let’s make some
@@ -478,22 +474,22 @@ return stack–<code>tor</code>’s return address! So we have to pop
that off first… We better just bite the bullet and
code it out–but we can’t really break it into smaller
words, because that’ll trash the return stack.</p>
<pre><code>: tor
r @ @ Get the value off the top of the return stack
swap Bring the value to be pushed to the top of stack
r @ ! Write it over the current top of return stack
r @ 1 + r ! Increment the return stack pointer--but can&#39;t use inc
r @ ! Store our return address back on the return stack
;</code></pre>
<pre><code> : tor
r @ @ Get the value off the top of the return stack
swap Bring the value to be pushed to the top of stack
r @ ! Write it over the current top of return stack
r @ 1 + r ! Increment the return stack pointer--but can&#39;t use inc
r @ ! Store our return address back on the return stack
;</code></pre>
<p>Next we want the opposite routine, which pops the top
of the return stack, and puts it on the normal stack.</p>
<pre><code>: fromr
r @ @ Save old value
r @ 1 - r ! Decrement pointer
r @ @ Get value that we want off
swap Bring return address to top
r @ ! Store it and return
;</code></pre>
<pre><code> : fromr
r @ @ Save old value
r @ 1 - r ! Decrement pointer
r @ @ Get value that we want off
swap Bring return address to top
r @ ! Store it and return
;</code></pre>
<p>Now, if we have a routine that’s recursing, and we
want to be polite about the return stack, right before
we recurse we can run <code>{ fromr drop }</code> so the stack won’t
@@ -503,324 +499,324 @@ address–so when we’re done, we’ll return up two levels.
To save a little, we make <code>tail</code> mean <code>{ fromr drop }</code>;
however, it’s more complex since there’s a new value on
top of the return stack.</p>
<pre><code>: tail fromr fromr drop tor ;</code></pre>
<pre><code> : tail fromr fromr drop tor ;</code></pre>
<p>Now, we want to do <code>if</code>. To do this, we need to convert
values to boolean values. The next few words set this
up.</p>
<p><code>minus</code> gives us unary negation.</p>
<pre><code>: minus 0 swap - ;</code></pre>
<pre><code> : minus 0 swap - ;</code></pre>
<p>If top of stack is boolean, <code>bnot</code> gives us inverse</p>
<pre><code>: bnot 1 swap - ;</code></pre>
<pre><code> : bnot 1 swap - ;</code></pre>
<p>To compare two numbers, subtract and compare to <code>0</code>.</p>
<pre><code>: &lt; - &lt;0 ;</code></pre>
<pre><code> : &lt; - &lt;0 ;</code></pre>
<p><code>logical</code> turns the top of stack into either <code>0</code> or <code>1</code>.</p>
<pre><code>: logical
dup Get two copies of it
0 &lt; 1 if &lt; 0, 0 otherwise
swap minus Swap number back up, and take negative
0 &lt; 1 if original was &gt; 0, 0 otherwise
+ Add them up--has to be 0 or 1!
;</code></pre>
<pre><code> : logical
dup Get two copies of it
0 &lt; 1 if &lt; 0, 0 otherwise
swap minus Swap number back up, and take negative
0 &lt; 1 if original was &gt; 0, 0 otherwise
+ Add them up--has to be 0 or 1!
;</code></pre>
<p><code>not</code> returns 1 if top of stack is 0, and 0 otherwise</p>
<pre><code>: not logical bnot ;</code></pre>
<pre><code> : not logical bnot ;</code></pre>
<p>We can test equality by subtracting and comparing to <code>0</code>.</p>
<pre><code>: = - not ;</code></pre>
<pre><code> : = - not ;</code></pre>
<p>Just to show how you compute a branch: Suppose you’ve
compiled a call to branch, and immediately after it is
an integer constant with the offset of how far to branch.
To branch, we use the return stack to read the offset, and
add that on to the top of the return stack, and return.</p>
<pre><code>: branch
r @ Address of top of return stack
@ Our return address
@ Value from there: the branch offset
r @ @ Our return address again
+ The address we want to execute at
r @ ! Store it back onto the return stack
;</code></pre>
<pre><code> : branch
r @ Address of top of return stack
@ Our return address
@ Value from there: the branch offset
r @ @ Our return address again
+ The address we want to execute at
r @ ! Store it back onto the return stack
;</code></pre>
<p>For conditional branches, we want to branch by a certain
amount if true, otherwise we want to skip over the branch
offset constant–that is, branch by one. Assuming that
the top of the stack is the branch offset, and the second
on the stack is <code>1</code> if we should branch, and <code>0</code> if not, the
following computes the correct branch offset.</p>
<pre><code>: computebranch 1 - * 1 + ;</code></pre>
<pre><code> : computebranch 1 - * 1 + ;</code></pre>
<p>Branch if the value on top of the stack is <code>0</code>.</p>
<pre><code>: notbranch
not
r @ @ @ Get the branch offset
computebranch Adjust as necessary
r @ @ + Calculate the new address
r @ ! Store it
;</code></pre>
<pre><code> : notbranch
not
r @ @ @ Get the branch offset
computebranch Adjust as necessary
r @ @ + Calculate the new address
r @ ! Store it
;</code></pre>
<p><code>here</code> is a standard FORTH word which returns a pointer to
the current dictionary address–that is, the value of
the dictionary pointer.</p>
<pre><code>: here h @ ;</code></pre>
<pre><code> : here h @ ;</code></pre>
<p>We’re ALL SET to compile <code>if...else...then</code> constructs!
Here’s what we do. When we get <code>if</code>, we compile a call
to <code>notbranch</code>, and then compile a dummy offset, because
we don’t know where the <code>then</code> will be. On the <em>stack</em>
we leave the address where we compiled the dummy offset.
<code>then</code> will calculate the offset and fill it in for us.</p>
<pre><code>: if immediate
&#39; notbranch , Compile notbranch
here Save the current dictionary address
0 , Compile a dummy value
;</code></pre>
<pre><code> : if immediate
&#39; notbranch , Compile notbranch
here Save the current dictionary address
0 , Compile a dummy value
;</code></pre>
<p><code>then</code> expects the address to <code>fixup</code> to be on the stack.</p>
<pre><code>: then immediate
dup Make another copy of the address
here Find the current location, where to branch to
swap - Calculate the difference between them
swap ! Bring the address to the top, and store it.
;</code></pre>
<pre><code> : then immediate
dup Make another copy of the address
here Find the current location, where to branch to
swap - Calculate the difference between them
swap ! Bring the address to the top, and store it.
;</code></pre>
<p>Now that we can do <code>if...then</code> statements, we can do
some parsing! Let’s introduce real FORTH comments.
<code>find-)</code> will scan the input until it finds a <code>)</code>, and
exit.</p>
<pre><code>: find-)
key Read in a character
&#39;)&#39; = Compare it to close parentheses
not if If it&#39;s not equal
tail find-) repeat (popping R stack)
then Otherwise branch here and exit
;
<pre><code> : find-)
key Read in a character
&#39;)&#39; = Compare it to close parentheses
not if If it&#39;s not equal
tail find-) repeat (popping R stack)
then Otherwise branch here and exit
;
: ( immediate
find-)
;
: ( immediate
find-)
;
( we should be able to do FORTH-style comments now )
( we should be able to do FORTH-style comments now )
( now that we&#39;ve got comments, we can comment the rest of the code
in a legitimate [self parsing] fashion. Note that you can&#39;t
nest parentheses... )
( now that we&#39;ve got comments, we can comment the rest of the code
in a legitimate [self parsing] fashion. Note that you can&#39;t
nest parentheses... )
: else immediate
&#39; branch , ( compile a definite branch )
here ( push the backpatching address )
0 , ( compile a dummy offset for branch )
swap ( bring old backpatch address to top )
dup here swap - ( calculate the offset from old address )
swap ! ( put the address on top and store it )
;
: else immediate
&#39; branch , ( compile a definite branch )
here ( push the backpatching address )
0 , ( compile a dummy offset for branch )
swap ( bring old backpatch address to top )
dup here swap - ( calculate the offset from old address )
swap ! ( put the address on top and store it )
;
: over _x! _y! _y _x _y ;
: over _x! _y! _y _x _y ;
: add
_x! ( save the pointer in a temp variable )
_x @ ( get the value pointed to )
+ ( add the incremement from on top of the stack )
_x ! ( and save it )
;
: add
_x! ( save the pointer in a temp variable )
_x @ ( get the value pointed to )
+ ( add the incremement from on top of the stack )
_x ! ( and save it )
;
: allot h add ;
: allot h add ;
: maybebranch
logical ( force the TOS to be 0 or 1 )
r @ @ @ ( load the branch offset )
computebranch ( calculate the condition offset [either TOS or 1])
r @ @ + ( add it to the return address )
r @ ! ( store it to our return address and return )
;
: maybebranch
logical ( force the TOS to be 0 or 1 )
r @ @ @ ( load the branch offset )
computebranch ( calculate the condition offset [either TOS or 1])
r @ @ + ( add it to the return address )
r @ ! ( store it to our return address and return )
;
: mod _x! _y! ( get x then y off of stack )
_y _y _x / _x * ( y - y / x * x )
-
;
: mod _x! _y! ( get x then y off of stack )
_y _y _x / _x * ( y - y / x * x )
-
;
: printnum
dup
10 mod &#39;0&#39; +
swap 10 / dup
if
printnum
echo
else
drop
echo
then
;
: printnum
dup
10 mod &#39;0&#39; +
swap 10 / dup
if
printnum
echo
else
drop
echo
then
;
: .
dup 0 &lt;
if
&#39;-&#39; echo minus
then
printnum
&#39;space&#39; echo
;
: .
dup 0 &lt;
if
&#39;-&#39; echo minus
then
printnum
&#39;space&#39; echo
;
: debugprint dup . cr ;
: debugprint dup . cr ;
( the following routine takes a pointer to a string, and prints it,
except for the trailing quote. returns a pointer to the next word
after the trailing quote )
( the following routine takes a pointer to a string, and prints it,
except for the trailing quote. returns a pointer to the next word
after the trailing quote )
: _print
dup 1 +
swap @
dup &#39;&quot;&#39; =
if
drop exit
then
echo
tail _print
;
: _print
dup 1 +
swap @
dup &#39;&quot;&#39; =
if
drop exit
then
echo
tail _print
;
: print _print ;
: print _print ;
( print the next thing from the instruction stream )
: immprint
r @ @
print
r @ !
;
( print the next thing from the instruction stream )
: immprint
r @ @
print
r @ !
;
: find-&quot;
key dup ,
&#39;&quot;&#39; =
if
exit
then
tail find-&quot;
;
: find-&quot;
key dup ,
&#39;&quot;&#39; =
if
exit
then
tail find-&quot;
;
: &quot; immediate
key drop
&#39; immprint ,
find-&quot;
;
: &quot; immediate
key drop
&#39; immprint ,
find-&quot;
;
: do immediate
&#39; swap , ( compile &#39;swap&#39; to swap the limit and start )
&#39; tor , ( compile to push the limit onto the return stack )
&#39; tor , ( compile to push the start on the return stack )
here ( save this address so we can branch back to it )
;
: do immediate
&#39; swap , ( compile &#39;swap&#39; to swap the limit and start )
&#39; tor , ( compile to push the limit onto the return stack )
&#39; tor , ( compile to push the start on the return stack )
here ( save this address so we can branch back to it )
;
: i r @ 1 - @ ;
: j r @ 3 - @ ;
: i r @ 1 - @ ;
: j r @ 3 - @ ;
: &gt; swap &lt; ;
: &lt;= 1 + &lt; ;
: &gt;= swap &lt;= ;
: &gt; swap &lt; ;
: &lt;= 1 + &lt; ;
: &gt;= swap &lt;= ;
: inci
r @ 1 - ( get the pointer to i )
inc ( add one to it )
r @ 1 - @ ( find the value again )
r @ 2 - @ ( find the limit value )
&lt;=
if
r @ @ @ r @ @ + r @ ! exit ( branch )
then
fromr 1 +
fromr drop
fromr drop
tor
;
: inci
r @ 1 - ( get the pointer to i )
inc ( add one to it )
r @ 1 - @ ( find the value again )
r @ 2 - @ ( find the limit value )
&lt;=
if
r @ @ @ r @ @ + r @ ! exit ( branch )
then
fromr 1 +
fromr drop
fromr drop
tor
;
: loop immediate &#39; inci @ here - , ;
: loop immediate &#39; inci @ here - , ;
: loopexit
: loopexit
fromr drop ( pop off our return address )
fromr drop ( pop off i )
fromr drop ( pop off the limit of i )
; ( and return to the caller&#39;s caller routine )
fromr drop ( pop off our return address )
fromr drop ( pop off i )
fromr drop ( pop off the limit of i )
; ( and return to the caller&#39;s caller routine )
: execute
8 !
&#39; exit 9 !
8 tor
;
: execute
8 !
&#39; exit 9 !
8 tor
;
: :: ; ( :: is going to be a word that does &#39;:&#39; at runtime )
: :: ; ( :: is going to be a word that does &#39;:&#39; at runtime )
: fix-:: immediate 3 &#39; :: ! ;
fix-::
: fix-:: immediate 3 &#39; :: ! ;
fix-::
( Override old definition of &#39;:&#39; with a new one that invokes ] )
: : immediate :: ] ;
( Override old definition of &#39;:&#39; with a new one that invokes ] )
: : immediate :: ] ;
: command
here 5 ! ( store dict pointer in temp variable )
_read ( compile a word )
( if we get control back: )
here 5 @
= if
tail command ( we didn&#39;t compile anything )
then
here 1 - h ! ( decrement the dictionary pointer )
here 5 @ ( get the original value )
= if
here @ ( get the word that was compiled )
execute ( and run it )
else
here @ ( else it was an integer constant, so push it )
here 1 - h ! ( and decrement the dictionary pointer again )
then
tail command
;
: command
here 5 ! ( store dict pointer in temp variable )
_read ( compile a word )
( if we get control back: )
here 5 @
= if
tail command ( we didn&#39;t compile anything )
then
here 1 - h ! ( decrement the dictionary pointer )
here 5 @ ( get the original value )
= if
here @ ( get the word that was compiled )
execute ( and run it )
else
here @ ( else it was an integer constant, so push it )
here 1 - h ! ( and decrement the dictionary pointer again )
then
tail command
;
: make-immediate ( make a word just compiled immediate )
here 1 - ( back up a word in the dictionary )
dup dup ( save the pointer to here )
h ! ( store as the current dictionary pointer )
@ ( get the run-time code pointer )
swap ( get the dict pointer again )
1 - ( point to the compile-time code pointer )
! ( write run-time code pointer on compile-time pointer )
;
: make-immediate ( make a word just compiled immediate )
here 1 - ( back up a word in the dictionary )
dup dup ( save the pointer to here )
h ! ( store as the current dictionary pointer )
@ ( get the run-time code pointer )
swap ( get the dict pointer again )
1 - ( point to the compile-time code pointer )
! ( write run-time code pointer on compile-time pointer )
;
: &lt;build immediate
make-immediate ( make the word compiled so far immediate )
&#39; :: , ( compile &#39;::&#39;, so we read next word )
2 , ( compile &#39;pushint&#39; )
here 0 , ( write out a 0 but save address for does&gt; )
&#39; , , ( compile a push that address onto dictionary )
;
: &lt;build immediate
make-immediate ( make the word compiled so far immediate )
&#39; :: , ( compile &#39;::&#39;, so we read next word )
2 , ( compile &#39;pushint&#39; )
here 0 , ( write out a 0 but save address for does&gt; )
&#39; , , ( compile a push that address onto dictionary )
;
: does&gt; immediate
&#39; command , ( jump back into command mode at runtime )
here swap ! ( backpatch the build&gt; to point to here )
2 , ( compile run-code primitive so we look like a word )
&#39; fromr , ( compile fromr, which leaves var address on stack )
;
: does&gt; immediate
&#39; command , ( jump back into command mode at runtime )
here swap ! ( backpatch the build&gt; to point to here )
2 , ( compile run-code primitive so we look like a word )
&#39; fromr , ( compile fromr, which leaves var address on stack )
;
: _dump ( dump out the definition of a word, sort of )
dup &quot; (&quot; . &quot; , &quot;
dup @ ( save the pointer and get the contents )
dup &#39; exit
= if
&quot; ;)&quot; cr exit
then
. &quot; ), &quot;
1 +
tail _dump
;
: _dump ( dump out the definition of a word, sort of )
dup &quot; (&quot; . &quot; , &quot;
dup @ ( save the pointer and get the contents )
dup &#39; exit
= if
&quot; ;)&quot; cr exit
then
. &quot; ), &quot;
1 +
tail _dump
;
: dump _dump ;
: dump _dump ;
: # . cr ;
: # . cr ;
: var &lt;build , does&gt; ;
: constant &lt;build , does&gt; @ ;
: array &lt;build allot does&gt; + ;
: var &lt;build , does&gt; ;
: constant &lt;build , does&gt; @ ;
: array &lt;build allot does&gt; + ;
: [ immediate command ;
: _welcome &quot; Welcome to THIRD.
Ok.
&quot; ;
: [ immediate command ;
: _welcome &quot; Welcome to THIRD.
Ok.
&quot; ;
: ; immediate &#39; exit , command exit
: ; immediate &#39; exit , command exit
[
[
_welcome</code></pre>
_welcome</code></pre>
<!--
Copyright © 1984-2024 by Landon Curt Noll. All Rights Reserved.