1 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; and now
for something completely different
...
3 ;; UrForth
/C Forth Engine
!
4 ;; Copyright
(C
) 2023 Ketmar Dark
// Invisible Vector
6 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7 ;; local variables support
8 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19 here
, "args:" will declare local variables
, and automatically fill them
20 with arguments from the data stack
. "locals:" declare
"free" locals
.
21 there can be more than one
"locals:" section
, but all such sections should
22 come before anything else. to use local, you must prepend its name with a
23 colon. the usual "TO" word is used to set local values.
26 vocabulary
(locals
-support
)
27 also
(locals
-support
) definitions
29 ;; offset in temp
(user
) dict
30 ;; should be enough
for now
;-)
31 \
65536 constant locals
-temp
-voc
-offset
33 0 value locals
-dp
-temp
-saved
36 : new
-locals
-wordlist
( -- )
37 forth
:(dp
-here
) @ dup forth
:(dp
-main
) <> " invalid (DP-HERE)" ?error
38 forth
:(dp
-temp
) dup @
to locals
-dp
-temp
-saved forth
:(dp
-here
) !
39 0 false forth
:(new
-wordlist
) to locals
-vocid
44 ;; low byte of loccount is total number of locals
45 ;; high byte is the number of args
47 : latest
-has
-locals?
( -- bool
)
48 latest
-pfa dup here
= if drop false
49 else @
['] forth:(l-enter) = endif
54 latest-has-locals? " \`locals:\` or \`args:\` should be the first word" ?not-error
56 new-locals-wordlist compile forth:(l-enter) 0 ,
60 : add-local ( addr count -- )
62 latest-pfa cell+ dup c@ 255 = " too many locals" ?error
63 1 over +! c@ >r ;; save current local number to rstack
64 ;; now create access word
65 forth:(dp-here) @ current @
66 forth:(dp-temp) forth:(dp-here) ! locals-vocid current !
67 \ ." DP: " forth:(dp) @ . cr
68 \ ." DP-TEMP: " forth:(dp-temp) @ . cr
69 \ ." HERE: " here . cr
72 compiler:(create-header)
73 compiler:(cfaidx-do-const) compiler:cfa, ;; why not
76 forth:(dp-temp) @ here <> " wtf?!" ?error ;; assertion
78 current ! forth:(dp-here) !
81 ;; create local access words, adjust "(L-ENTER)"
82 : add-arg ( addr count -- )
83 add-local 0x01_00 latest-pfa cell+ +!
86 : parse-loc-list ( regcfa -- )
88 parse-skip-line-comments
90 while r@ execute repeat
95 ..: forth:(exit-extender) ( -- )
97 compile forth:(l-leave)
98 locals-dp-temp-saved forth:(dp-temp) !
103 : find-local ( addr count -- addr count FALSE / idx TRUE )
105 2dup 1 > swap c@ [char] : = and if
106 latest-has-locals? if ( addr count | ret-flag )
107 over 1+ over 1- locals-vocid find-word-in-voc
109 >r 2drop r> cfa->pfa @
117 ..: forth:(to-extender) ( addr count FALSE -- addr count FALSE / TRUE )
119 compiler:comp? if drop
120 find-local if literal compile forth:(local!) true
128 ..: forth:(interpret-check-word) ( addr count FALSE -- addr count FALSE / TRUE )
130 compiler:comp? if drop
131 find-local if literal compile forth:(local@) true
142 : args: ['] (locals
-support
):add
-arg
(locals
-support
):parse
-loc
-list
; immediate
143 : locals
: ['] (locals-support):add-local (locals-support):parse-loc-list ; immediate