1 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; and now
for something completely different
...
3 ;; UrForth
/C Forth Engine
!
4 ;; Copyright
(C
) 2023 Ketmar Dark
// Invisible Vector
6 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 ALSO COMPILER DEFINITIONS
15 3 CONSTANT PAIR
-IFELSE
23 666 CONSTANT PAIR
-CBLOCK
25 0 VARIABLE
(CHAIN
-BREAK)
26 0 VARIABLE
(CHAIN
-CONT
)
27 0 VARIABLE
(LOOP
-PAIR
)
31 ;; ( saved
-break saved
-cont saved
-lastpair begin
-id branchcfa
-- saved
-break saved
-cont saved
-lastpair begin
-id
)
32 ;; all failed whiles will terminate the loop
34 ?COMP SWAP PAIR
-BEGIN ?PAIRS
35 COMPILE
, (CHAIN
-BREAK) @
(CHAIN
-J
>) (CHAIN
-BREAK) !
40 ;; ( saved
-break saved
-cont saved
-lastpair begin
-id branchcfa
-- )
41 ;; ( -- saved
-break saved
-cont saved
-lastpair begin
-id
)
43 ?COMP SWAP PAIR
-BEGIN ?PAIRS
44 COMPILE
, (CHAIN
-CONT
) @
(<J
-RESOLVE
)
45 (CHAIN
-BREAK) @
(RESOLVE
-J
>)
46 (LOOP
-PAIR
) ! (CHAIN
-CONT
) ! (CHAIN
-BREAK) !
49 : (COUNTED
-LOOP?
) ( n
-- bool
)
50 DUP PAIR
-DO = OVER PAIR
-QDO
= OR SWAP PAIR
-FOR = OR
54 PREVIOUS DEFINITIONS ALSO COMPILER
56 : (IF-COMMON) ( branch
-cfa
-- )
57 ?COMP COMPILE
, (MARK
-J
>)
61 : IF ( -- addr id
) ['] FORTH:(0BRANCH) (IF-COMMON) ; IMMEDIATE
62 : IFNOT ( -- addr id ) ['] FORTH
:(TBRANCH
) (IF-COMMON) ; IMMEDIATE
63 : -IF ( -- addr id
) ['] FORTH:(+0BRANCH) (IF-COMMON) ; IMMEDIATE -- if negative
64 : +IF ( -- addr id ) ['] FORTH
:(-0BRANCH
) (IF-COMMON) ; IMMEDIATE
-- if positive
(but not zero
)
65 : -0IF ( -- addr id
) ['] FORTH:(+BRANCH) (IF-COMMON) ; IMMEDIATE -- if negative or 0
66 : +0IF ( -- addr id ) ['] FORTH
:(-BRANCH
) (IF-COMMON) ; IMMEDIATE
-- if positive or
0
70 ?COMP PAIR
-IF PAIR
-IFELSE ?
2PAIRS
82 COMPILE FORTH
:(BRANCH
) (MARK
-J
>)
89 ;; ( -- saved
-break saved
-cont saved
-lastpair begin
-jmark begin
-id
)
92 (CHAIN
-BREAK) @
(CHAIN
-CONT
) @
(LOOP
-PAIR
) @
93 (CHAIN
-BREAK) 0! (<J
-MARK
) (CHAIN
-CONT
) !
94 PAIR
-BEGIN DUP
(LOOP
-PAIR
) !
98 ;; all failed whiles will terminate the loop
99 : WHILE ['] FORTH:(0BRANCH) (COMPILE-WHILE) ; IMMEDIATE
100 : NOT-WHILE ['] FORTH
:(TBRANCH
) (COMPILE
-WHILE) ; IMMEDIATE
101 : -WHILE ['] FORTH:(+0BRANCH) (COMPILE-WHILE) ; IMMEDIATE
102 : +WHILE ['] FORTH
:(-0BRANCH
) (COMPILE
-WHILE) ; IMMEDIATE
103 : -0WHILE ['] FORTH:(+BRANCH) (COMPILE-WHILE) ; IMMEDIATE
104 : +0WHILE ['] FORTH
:(-BRANCH
) (COMPILE
-WHILE) ; IMMEDIATE
107 : AGAIN
['] FORTH:(BRANCH) (COMPILE-UNTIL) ; IMMEDIATE
108 : REPEAT [COMPILE] AGAIN ; IMMEDIATE
110 : UNTIL ['] FORTH
:(0BRANCH
) (COMPILE
-UNTIL
) ; IMMEDIATE
111 : NOT
-UNTIL
['] FORTH:(TBRANCH) (COMPILE-UNTIL) ; IMMEDIATE
112 : -UNTIL ['] FORTH
:(+0BRANCH
) (COMPILE
-UNTIL
) ; IMMEDIATE
113 : +UNTIL
['] FORTH:(-0BRANCH) (COMPILE-UNTIL) ; IMMEDIATE
114 : -0UNTIL ['] FORTH
:(+BRANCH
) (COMPILE
-UNTIL
) ; IMMEDIATE
115 : +0UNTIL
['] FORTH:(-BRANCH) (COMPILE-UNTIL) ; IMMEDIATE
119 : (DO) ( limit start -- | limit counter )
120 ;; index = 0x8000_0000 - to + from
121 ;; limit = 0x8000_0000 - to
122 R> NROT ( ret to from )
123 0x8000_0000 ROT - ( ret from 0x8000_0000-to )
128 : (?DO) ( limit start -- | limit counter )
130 ;; index = 0x8000_0000 - to + from
131 ;; limit = 0x8000_0000 - to
132 R> NROT ( ret to from )
133 0x8000_0000 ROT - ( ret from 0x8000_0000-to )
135 4+ ( skip branch address )
140 ; (HIDDEN) COMPILER:(WARG-BRANCH) COMPILER:SET-WARG
142 ;; exit when counter crosses a limit
143 : (+LOOP) ( add -- | limit index )
145 R@ + ( ret add+index )
146 R@ OVER XOR 0< IF ( ret add+index )
149 ELSE ( ret add+index )
154 ; (HIDDEN) COMPILER:(WARG-BRANCH) COMPILER:SET-WARG
155 \ DEBUG:DECOMPILE FORTH:(+LOOP)
157 : (I) ( | limit index -- real-index | limit index )
161 : (J) ( | limit0 index0 limit1 index1 -- real-index0 | limit0 index0 limit1 index1 )
172 (CHAIN-BREAK) @ (CHAIN-CONT) @ (LOOP-PAIR) @
173 (CHAIN-BREAK) 0! (<J-MARK) (CHAIN-CONT) !
174 PAIR-DO DUP (LOOP-PAIR) !
180 (CHAIN-BREAK) @ (CHAIN-CONT) @ (LOOP-PAIR) @
181 (MARK-J>) (CHAIN-BREAK) ! (<J-MARK) (CHAIN-CONT) !
182 PAIR-QDO DUP (LOOP-PAIR) !
185 : (LOOP-FINISH) ( loop-id -- )
186 DROP COMPILE FORTH:(+LOOP)
187 (CHAIN-CONT) @ (<J-RESOLVE)
188 (CHAIN-BREAK) @ (RESOLVE-J>)
189 (LOOP-PAIR) ! (CHAIN-CONT) ! (CHAIN-BREAK) !
193 ?COMP DUP PAIR-DO PAIR-QDO ?2PAIRS
194 1 LITERAL FORTH:(LOOP-FINISH)
198 ?COMP DUP PAIR-DO PAIR-QDO ?2PAIRS
203 ?COMP (LOOP-PAIR) @ (COUNTED-LOOP?) " 'I
' out of a loop" ?NOT-ERROR
208 ?COMP (LOOP-PAIR) @ (COUNTED-LOOP?) " 'J
' out of a loop" ?NOT-ERROR
214 : (FOR) ( limit -- | limit counter )
216 ;; index = 0x8000_0000 - to + from
217 ;; limit = 0x8000_0000 - to
218 0 R> NROT ( ret to from )
219 0x8000_0000 ROT - ( ret from 0x8000_0000-to )
221 4+ ( skip branch address )
226 ; (HIDDEN) COMPILER:(WARG-BRANCH) COMPILER:SET-WARG
228 : (ENDFOR) ( -- | limit counter )
230 R@ 1+ ( ret add+index )
231 R@ OVER XOR 0< IF ( ret add+index )
234 ELSE ( ret add+index )
239 ; (HIDDEN) COMPILER:(WARG-BRANCH) COMPILER:SET-WARG
244 (CHAIN-BREAK) @ (CHAIN-CONT) @ (LOOP-PAIR) @
245 (MARK-J>) (CHAIN-BREAK) ! (CHAIN-CONT) 0!
246 (<J-MARK) PAIR-FOR DUP (LOOP-PAIR) !
250 ?COMP PAIR-FOR ?PAIRS
251 ;; continue points here
252 (CHAIN-CONT) @ (RESOLVE-J>)
253 COMPILE (ENDFOR) (<J-RESOLVE)
255 (CHAIN-BREAK) @ (RESOLVE-J>)
256 (LOOP-PAIR) ! (CHAIN-CONT) ! (CHAIN-BREAK) !
269 ELSE PAIR-CASE ?PAIRS
272 ;; jump out of the cases
273 COMPILER:(RESOLVE-J>)
278 : (COMPILE-OF) ( cmpcfa -- )
279 ?COMP SWAP PAIR-CASE ?PAIRS
280 COMPILE, COMPILE FORTH:(0BRANCH) COMPILER:(MARK-J>)
285 : (OF=) OVER = ; (HIDDEN)
286 : (OF<>) OVER <> ; (HIDDEN)
287 : (OF<) OVER SWAP < ; (HIDDEN)
288 : (OF<=) OVER SWAP <= ; (HIDDEN)
289 : (OF>) OVER SWAP > ; (HIDDEN)
290 : (OF>=) OVER SWAP >= ; (HIDDEN)
291 : (OF-U<) OVER SWAP < ; (HIDDEN)
292 : (OF-U<=) OVER SWAP <= ; (HIDDEN)
293 : (OF-U>) OVER SWAP > ; (HIDDEN)
294 : (OF-U>=) OVER SWAP >= ; (HIDDEN)
295 : (OF-AND) OVER AND ; (HIDDEN)
296 : (OF-~AND) OVER SWAP BITNOT AND ; (HIDDEN)
297 : (OF-WITHIN) >R >R DUP R> R> WITHIN ; (HIDDEN)
298 : (OF-UWITHIN) >R >R DUP R> R> UWITHIN ; (HIDDEN)
299 : (OF-BOUNDS) >R >R DUP R> R> BOUNDS? ; (HIDDEN)
301 PREVIOUS DEFINITIONS ALSO COMPILER
303 : OF ['] COMPILER
:(OF
=) COMPILER
:(COMPILE
-OF
) ; IMMEDIATE
304 : NOT
-OF
['] COMPILER:(OF<>) COMPILER:(COMPILE-OF) ; IMMEDIATE
305 : <OF ['] COMPILER
:(OF
<) COMPILER
:(COMPILE
-OF
) ; IMMEDIATE
306 : <=OF
['] COMPILER:(OF<=) COMPILER:(COMPILE-OF) ; IMMEDIATE
307 : >OF ['] COMPILER
:(OF
>) COMPILER
:(COMPILE
-OF
) ; IMMEDIATE
308 : >=OF
['] COMPILER:(OF>=) COMPILER:(COMPILE-OF) ; IMMEDIATE
309 : U<OF ['] COMPILER
:(OF
-U
<) COMPILER
:(COMPILE
-OF
) ; IMMEDIATE
310 : U
<=OF
['] COMPILER:(OF-U<=) COMPILER:(COMPILE-OF) ; IMMEDIATE
311 : U>OF ['] COMPILER
:(OF
-U
>) COMPILER
:(COMPILE
-OF
) ; IMMEDIATE
312 : U
>=OF
['] COMPILER:(OF-U>=) COMPILER:(COMPILE-OF) ; IMMEDIATE
313 : &OF ['] COMPILER
:(OF
-AND
) COMPILER
:(COMPILE
-OF
) ; IMMEDIATE
314 : AND
-OF
['] COMPILER:(OF-AND) COMPILER:(COMPILE-OF) ; IMMEDIATE
315 : ~AND-OF ['] COMPILER
:(OF
-~AND
) COMPILER
:(COMPILE
-OF
) ; IMMEDIATE
316 : WITHIN
-OF
['] COMPILER:(OF-WITHIN) COMPILER:(COMPILE-OF) ; IMMEDIATE
317 : UWITHIN-OF ['] COMPILER
:(OF
-UWITHIN
) COMPILER
:(COMPILE
-OF
) ; IMMEDIATE
318 : BOUNDS
-OF
['] COMPILER:(OF-BOUNDS) COMPILER:(COMPILE-OF) ; IMMEDIATE
322 ;; jump out of the case
323 SWAP COMPILE FORTH:(BRANCH) COMPILER:(CHAIN-J>)
324 ;; fix previous OF jump
325 SWAP COMPILER:(RESOLVE-J>)
330 ?COMP PAIR-CASE ?PAIRS
338 (LOOP-PAIR) @ PAIR-BEGIN = IF
339 COMPILE FORTH:(BRANCH) (CHAIN-BREAK) @ (CHAIN-J>) (CHAIN-BREAK) !
340 ELSE (LOOP-PAIR) @ (COUNTED-LOOP?) IF
341 COMPILE FORTH:(UNLOOP)
342 COMPILE FORTH:(BRANCH) (CHAIN-BREAK) @ (CHAIN-J>) (CHAIN-BREAK) !
343 ELSE " 'BREAK' out of loop" ERROR
350 (LOOP-PAIR) @ PAIR-BEGIN = IF
351 COMPILE FORTH:(BRANCH) (CHAIN-CONT) @ (<J-RESOLVE)
352 ELSE (LOOP-PAIR) @ PAIR-FOR = IF
353 ;; in "FOR", "CONTINUE" is a forward jump to "ENDFOR"
354 COMPILE FORTH:(BRANCH) (CHAIN-CONT) @ (CHAIN-J>) (CHAIN-CONT) !
355 ELSE (LOOP-PAIR) @ (COUNTED-LOOP?) " 'CONTINUE' out of loop" ?NOT-ERROR
356 1 LITERAL COMPILE FORTH:(+LOOP) (CHAIN-CONT) @ (<J-RESOLVE)
357 COMPILE FORTH:(BRANCH) (CHAIN-BREAK) @ (CHAIN-J>) (CHAIN-BREAK) !