Adding more tests and docs.

This commit is contained in:
Brad Nelson
2021-02-07 00:57:58 -08:00
parent 560264677a
commit f4be0ab2d9
4 changed files with 89 additions and 8 deletions

View File

@ -42,7 +42,7 @@ variable tests-found variable tests-run variable tests-passed
: red 1 fg ; : green 2 fg ; : hr 40 for [char] - emit next cr ; : red 1 fg ; : green 2 fg ; : hr 40 for [char] - emit next cr ;
: replace-line 13 emit clear-to-eol ; : replace-line 13 emit clear-to-eol ;
: label-test ( xt -- ) replace-line >name type ; : label-test ( xt -- ) replace-line >name type ;
: run-test ( xt -- ) dup label-test confirm{ ['] wrap-test catch }confirm : run-test ( xt -- ) dup label-test only forth confirm{ ['] wrap-test catch }confirm
if drop ( cause xt restored on throw ) red ." FAILED" normal cr if drop ( cause xt restored on throw ) red ." FAILED" normal cr
else green ." OK" normal 1 tests-passed +! then 1 tests-run +! ; else green ." OK" normal 1 tests-passed +! then 1 tests-run +! ;
: show-test-results : show-test-results

View File

@ -1,6 +1,9 @@
( Implement Vocabularies ) ( Implement Vocabularies )
: forth [ current @ ] literal context ! ; variable last-vocabulary
: vocabulary ( "name" ) create 0 , current @ 2 cells + , does> cell+ context ! ; current @ constant forth-wordlist
: forth forth-wordlist context ! ;
: vocabulary ( "name" ) create 0 , current @ 2 cells + , current @ @ last-vocabulary !
does> cell+ context ! ;
: definitions context @ current ! ; : definitions context @ current ! ;
: >name-length ( xt -- n ) dup 0= if exit then >name nip ; : >name-length ( xt -- n ) dup 0= if exit then >name nip ;
: vlist 0 context @ @ begin dup >name-length while onlines dup see. >link repeat 2drop cr ; : vlist 0 context @ @ begin dup >name-length while onlines dup see. >link repeat 2drop cr ;
@ -15,14 +18,16 @@
( Watered down versions of these ) ( Watered down versions of these )
: only forth 0 context cell+ ! ; : only forth 0 context cell+ ! ;
: last-voc ( -- a) context begin dup @ while cell+ repeat ; : voc-stack-end ( -- a ) context begin dup @ while cell+ repeat ;
: also context context cell+ last-voc over - cell+ cmove> ; : also context context cell+ voc-stack-end over - 2 cells + cmove> ;
: sealed 0 current @ ! ; : sealed 0 last-vocabulary @ >body cell+ ! ;
: voc. ( voc -- ) dup forth-wordlist = if ." FORTH " drop exit then 3 cells - see. ;
: order context begin dup @ while dup @ voc. cell+ repeat drop cr ;
( Hide some words in an internals vocabulary ) ( Hide some words in an internals vocabulary )
vocabulary internals internals definitions vocabulary internals internals definitions
transfer{ transfer{
transfer-xt last-voc transfer-xt voc-stack-end forth-wordlist voc.
branch 0branch donext dolit branch 0branch donext dolit
'context 'notfound notfound 'context 'notfound notfound
immediate? input-buffer ?echo ?echo-prompt immediate? input-buffer ?echo ?echo-prompt

View File

@ -29,3 +29,61 @@ e: test-vlist-empty
forth definitions forth definitions
out: out:
;e ;e
e: test-order
vocabulary foo
vocabulary bar
vocabulary baz
also foo also bar also baz
order
out: baz bar foo FORTH
only forth definitions
;e
e: test-vocab-define-order
vocabulary foo
foo definitions
: a ." AAAAAA" cr ;
forth definitions
: a ." BAD" cr ;
foo a
out: AAAAAA
only forth definitions
;e
e: test-vocabulary-chaining
vocabulary foo
foo definitions
vocabulary bar
bar definitions
: a ." aaaa" cr ;
foo definitions
: b ." bbbb" cr ;
forth definitions
: a ." BAD" cr ;
: b ." BAD" cr ;
foo a b
out: BAD
out: bbbb
bar a b
out: aaaa
out: bbbb
only forth definitions
;e
e: test-sealed
: aaa ." good" cr ;
vocabulary foo
foo definitions
: aaa ." bad" cr ;
vocabulary bar sealed
also bar definitions
: bbb ." b" cr ;
only forth definitions
also foo bar
aaa
bbb
out: good
out: b
only forth definitions
;e

View File

@ -113,13 +113,31 @@ VARIABLE ECHO -- Determines if commands are echoed
</pre> </pre>
<h5>Vocabularies</h5> <h5>Vocabularies</h5>
<p>
µEforth uses a hybrid of Forth-79 and Forth-83 style vocabularies.
By default vocabularies chain to the vocabulary in which they were defined,
as in Forth-79. However, like Forth-83, <code>ALSO</code>
can be used to add vocabularies to a vocabulary stack of which
<code>CONTEXT @</code> is the first item.
The word <code>ONLY</code> clears the vocabulary stack, but as there is
no separate ONLY vocabulary, it also sets <code>CONTEXT</code>
to the <code>FORTH</code> vocabulary.
The word <code>SEALED</code> modifies the most recently defined vocabulary
such that it does not chain. Note, this must be done before words are added to it.
</p>
<pre> <pre>
VOCABULARY ( "name" ) Create a vocabulary with the current vocabulary as parent VOCABULARY ( "name" ) Create a vocabulary with the current vocabulary as parent
FORTH ( -- ) Make the FORTH vocabulary the context vocabulary FORTH ( -- ) Make the FORTH vocabulary the context vocabulary
DEFINITIONS ( -- ) Make the context vocabulary the current vocabulary DEFINITIONS ( -- ) Make the context vocabulary the current vocabulary
VLIST ( -- ) List the words in the context vocabulary VLIST ( -- ) List the words in the context vocabulary (not chains)
WORDS ( -- ) List the words in the context vocabulary (including chains)
TRANSFER ( "name" ) Move a word from its current dictionary to the current vocabulary TRANSFER ( "name" ) Move a word from its current dictionary to the current vocabulary
Useful for "hiding" built-in words Useful for "hiding" built-in words
ALSO ( -- ) Duplicate the vocabulary at the top of the vocabulary stack
ONLY ( -- ) Reset context stack to one item, the FORTH dictionary
Non-standard, as there's no distinct ONLY vocabulary
ORDER ( -- ) Print the vocabulary search order
SEALED ( -- ) Alter the last vocabulary defined so it doesn't chain
</pre> </pre>
<h5>Block Editor</h5> <h5>Block Editor</h5>