1 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; and now
for something completely different
...
3 ;; UrForth
/C Forth Engine
!
4 ;; Copyright
(C
) 2023 Ketmar Dark
// Invisible Vector
6 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12 ;; worker
for the following words
13 : (DO-ALLOT
) ( n
-- ) FORTH
:(DP
-TEMP
) @
IF FORTH
:(DP
-TEMP
) ELSE FORTH
:(DP
) ENDIF +! ; (HIDDEN
)
15 ;; contrary
to other systems
, alloting negative number of bytes is prohibited
.
16 ;; use
"UNALLOT" to "deallot" dictionary space
.
17 : N
-ALLOT
( n
-- start
-addr
) DUP
0< " negative allot" ?ERROR HERE
>R
(DO-ALLOT
) R
> ;
19 ;; contrary
to other systems
, alloting negative number of bytes is prohibited
.
20 ;; use
"UNALLOT" to "deallot" dictionary space
.
21 : ALLOT
( n
-- ) N
-ALLOT DROP
;
23 ;; "deallot" given number of bytes
.
24 : UNALLOT
( n
-- ) DUP
0< " negative unallot" ?ERROR NEGATE
(DO-ALLOT
) ;
29 ;; create new headerless wordlist
30 : (NEW
-WORDLIST
) ( parentvocid need
-hashtable?
-- vocid
)
31 ;; typeid
, used by Forth code
(structs and such
)
33 HERE
>R
;; result
-- vocid
34 ;; create wordlist struct
36 HERE
>R
;; for voclink
37 FORTH
:(VOC
-LINK
) @
, ;; voclink
41 IF (VOC
-HTABLE
-SIZE
) FOR 0 , ENDFOR
42 ELSE (VOC
-HTABLE
-NOFLAG
) ,
45 R@
(ADDR
-TEMP
-BIT
) AND
IF RDROP \
." NO-LINK!\n"
46 ELSE R
> FORTH
:(VOC
-LINK
) !
52 ;; create vocabulary word
53 : (CREATE
-NAMED
-VOCAB
) ( vocid addr count
-- )
56 (WFLAG
-SMUDGE
) OR
(WFLAG
-VOCAB
) OR
59 (CFAIDX
-DO-VOC
) , ;; cfa
60 DUP
, ;; wordlist address
62 (VOCOFS
-HEADER
) + LATEST
-NFA SWAP
!
65 ;; create vocabulary word
66 : (CREATE
-VOCAB
) ( vocid
-- ) \ vocname
67 PARSE
-NAME
(CREATE
-NAMED
-VOCAB
)
70 ;; check
if the given CFA defined as a vocabulary header
71 : (IS
-VOC
-WORD?
) ( cfa
-- bool
)
72 DUP @
(CFAIDX
-DO-VOC
) =
73 SWAP CFA
->NFA @
(WFLAG
-VOCAB
) (WFLAG
-SMUDGE
) OR AND LOGOR
76 ;; doesn
't check arguments
77 : (WORD->VOCID) ( cfa -- vocid )
81 ;; doesn't check arguments
82 : (VOCID
-PARENT@
) ( vocid
-- vocid
) (VOCOFS
-PARENT
) + @
;
83 : (VOCID
-PARENT
!) ( parent
-vocid vocid
-- ) (VOCOFS
-PARENT
) + ! ;
85 : (VOCID
-TYPEID@
) ( vocid
-- typeid
) CELL
- @
;
86 : (VOCID
-TYPEID
!) ( typeid vocid
-- ) CELL
- ! ;
90 : VOCID
-HEADNFA@
( vocid
-- head
-nfa
/ FALSE
)
91 COMPILER
:(VOCOFS
-HEADER
) + @
94 ;; useful low
-level words
to create vocabs with already parsed names
95 : (VOCABULARY
-EX
) ( addr count parent need
-hashtbl?
-- )
96 (NEW
-WORDLIST
) NROT
(CREATE
-NAMED
-VOCAB
)
99 : (VOCABULARY
) ( addr count
-- ) 0 TRUE
(VOCABULARY
-EX
) ;
100 : (SIMPLE
-VOCABULARY
) ( addr count
-- ) 0 FALSE
(VOCABULARY
-EX
) ;
101 : (NESTED
-VOCABULARY
) ( addr count
-- ) CURRENT @ TRUE
(VOCABULARY
-EX
) ;
102 : (SIMPLE
-NESTED
-VOCABULARY
) ( addr count
-- ) CURRENT @ FALSE
(VOCABULARY
-EX
) ;
105 : VOCABULARY
( -- ) \ vocname
106 0 TRUE
(NEW
-WORDLIST
) (CREATE
-VOCAB
)
109 ;; vocabulary without a hash table
110 : SIMPLE
-VOCABULARY
( -- ) \ vocname
111 0 FALSE
(NEW
-WORDLIST
) (CREATE
-VOCAB
)
114 : NESTED
-VOCABULARY
( -- ) \ vocname
115 CURRENT @ TRUE
(NEW
-WORDLIST
) (CREATE
-VOCAB
)
118 : SIMPLE
-NESTED
-VOCABULARY
( -- ) \ vocname
119 CURRENT @ TRUE
(NEW
-WORDLIST
) (CREATE
-VOCAB
)
122 : VOCID
: ( -- vocid
) \ vocname
123 -FIND
-REQUIRED DUP
(IS
-VOC
-WORD?
) " vocabulary name expected" ?NOT
-ERROR
127 ;; compile vocid literal
128 COMPILE FORTH
:(LITVOCID
) ,
132 : VOC
-LATEST
( vocid
-- latest
)
133 COMPILER
:(VOCOFS
-LATEST
) + @
137 ;; ALSO
-DEFS
: vocname
138 ;; does
"ALSO vocname DEFINITIONS"
139 : ALSO
-DEFS
: ( -- ) \ vocname
140 -FIND
-REQUIRED DUP
(IS
-VOC
-WORD?
) " vocabulary name expected" ?NOT
-ERROR
141 ALSO
(WORD
->VOCID
) CONTEXT
! DEFINITIONS
144 ;; does
"PREVIOUS DEFINITIONS"
150 ;; create vocabulary
if we don
't have such word yet
151 : VOCAB-IF-NONE ( -- ) \ name
152 PARSE-NAME 2DUP FIND-WORD IF 2DROP DROP
157 ;; create vocabulary if we don't have such word yet
158 : SIMPLE
-VOCAB
-IF-NONE
( -- ) \ name
159 PARSE
-NAME
2DUP FIND
-WORD
IF 2DROP DROP
160 ELSE (SIMPLE
-VOCABULARY
)
164 ;; create nested vocabulary
if we don
't have such word yet
165 : NESTED-VOCAB-IF-NONE ( -- ) \ name
166 PARSE-NAME 2DUP FIND-WORD IF 2DROP DROP
167 ELSE (NESTED-VOCABULARY)
171 ;; create nested vocabulary if we don't have such word yet
172 : SIMPLE
-NESTED
-VOCAB
-IF-NONE
( -- ) \ name
173 PARSE
-NAME
2DUP FIND
-WORD
IF 2DROP DROP
174 ELSE (SIMPLE
-NESTED
-VOCABULARY
)