UrForth: fixed "(BASED-NUMBER)" with radix postfix
[urasm.git] / urflibs / init / bootstrap / 40-voc-creatori.f
blobf7ad52f75a9a6eb16cf8a22b83aa36bc52829e56
1 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; and now for something completely different...
3 ;; UrForth/C Forth Engine!
4 ;; Copyright (C) 2023 Ketmar Dark // Invisible Vector
5 ;; GPLv3 ONLY
6 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 ;; vocabulary creation
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) ;
27 ALSO COMPILER
29 ;; create new headerless wordlist
30 : (NEW-WORDLIST) ( parentvocid need-hashtable? -- vocid )
31 ;; typeid, used by Forth code (structs and such)
32 0 , ;; typeid
33 HERE >R ;; result -- vocid
34 ;; create wordlist struct
35 0 , ;; latest
36 HERE >R ;; for voclink
37 FORTH:(VOC-LINK) @ , ;; voclink
38 SWAP , ;; parent
39 0 , ;; header NFA
40 ;; create hashtable
41 IF (VOC-HTABLE-SIZE) FOR 0 , ENDFOR
42 ELSE (VOC-HTABLE-NOFLAG) ,
43 ENDIF
44 ;; link to voclink
45 R@ (ADDR-TEMP-BIT) AND IF RDROP \ ." NO-LINK!\n"
46 ELSE R> FORTH:(VOC-LINK) !
47 ENDIF
48 ;; done
50 ; (HIDDEN)
52 ;; create vocabulary word
53 : (CREATE-NAMED-VOCAB) ( vocid addr count -- )
54 ;; name flags
55 (GET-NEW-WORD-FLAGS)
56 (WFLAG-SMUDGE) OR (WFLAG-VOCAB) OR
57 (CREATE-WORD-HEADER)
58 ;; fill CFA and PFA
59 (CFAIDX-DO-VOC) , ;; cfa
60 DUP , ;; wordlist address
61 RESET-SMUDGE
62 (VOCOFS-HEADER) + LATEST-NFA SWAP!
63 ; (HIDDEN)
65 ;; create vocabulary word
66 : (CREATE-VOCAB) ( vocid -- ) \ vocname
67 PARSE-NAME (CREATE-NAMED-VOCAB)
68 ; (HIDDEN)
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 )
78 CFA->PFA @
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- ! ;
88 PREVIOUS
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
124 ;; vocid is in PFA
125 CFA->PFA @
126 STATE @ IF
127 ;; compile vocid literal
128 COMPILE FORTH:(LITVOCID) ,
129 ENDIF
130 ; IMMEDIATE
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"
145 : PREV-DEFS ( -- )
146 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
153 ELSE (VOCABULARY)
154 ENDIF
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)
161 ENDIF
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)
168 ENDIF
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)
175 ENDIF