diff --git a/ueforth/common/testing.fs b/ueforth/common/testing.fs index 47087b6..0dcd28d 100644 --- a/ueforth/common/testing.fs +++ b/ueforth/common/testing.fs @@ -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 ; : replace-line 13 emit clear-to-eol ; : 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 else green ." OK" normal 1 tests-passed +! then 1 tests-run +! ; : show-test-results diff --git a/ueforth/common/vocabulary.fs b/ueforth/common/vocabulary.fs index 2155280..7965829 100644 --- a/ueforth/common/vocabulary.fs +++ b/ueforth/common/vocabulary.fs @@ -1,6 +1,9 @@ ( Implement Vocabularies ) -: forth [ current @ ] literal context ! ; -: vocabulary ( "name" ) create 0 , current @ 2 cells + , does> cell+ context ! ; +variable last-vocabulary +current @ constant forth-wordlist +: forth forth-wordlist context ! ; +: vocabulary ( "name" ) create 0 , current @ 2 cells + , current @ @ last-vocabulary ! + does> cell+ context ! ; : definitions context @ current ! ; : >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 ; @@ -15,14 +18,16 @@ ( Watered down versions of these ) : only forth 0 context cell+ ! ; -: last-voc ( -- a) context begin dup @ while cell+ repeat ; -: also context context cell+ last-voc over - cell+ cmove> ; -: sealed 0 current @ ! ; +: voc-stack-end ( -- a ) context begin dup @ while cell+ repeat ; +: also context context cell+ voc-stack-end over - 2 cells + cmove> ; +: 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 ) vocabulary internals internals definitions transfer{ - transfer-xt last-voc + transfer-xt voc-stack-end forth-wordlist voc. branch 0branch donext dolit 'context 'notfound notfound immediate? input-buffer ?echo ?echo-prompt diff --git a/ueforth/common/vocabulary_tests.fs b/ueforth/common/vocabulary_tests.fs index 7262a5b..8a69e95 100644 --- a/ueforth/common/vocabulary_tests.fs +++ b/ueforth/common/vocabulary_tests.fs @@ -29,3 +29,61 @@ e: test-vlist-empty forth definitions out: ;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 diff --git a/ueforth/site/index.html b/ueforth/site/index.html index 74da687..103300f 100644 --- a/ueforth/site/index.html +++ b/ueforth/site/index.html @@ -113,13 +113,31 @@ VARIABLE ECHO -- Determines if commands are echoed
Vocabularies
+

+µ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, ALSO +can be used to add vocabularies to a vocabulary stack of which +CONTEXT @ is the first item. +The word ONLY clears the vocabulary stack, but as there is +no separate ONLY vocabulary, it also sets CONTEXT +to the FORTH vocabulary. +The word SEALED modifies the most recently defined vocabulary +such that it does not chain. Note, this must be done before words are added to it. +

 VOCABULARY ( "name" ) Create a vocabulary with the current vocabulary as parent
 FORTH ( -- ) Make the FORTH vocabulary the context 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
                     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
 
Block Editor