1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; UrForth level
1: self
-hosting
32-bit Forth compiler
3 ;; Copyright
(C
) 2020 Ketmar Dark
// Invisible Vector
5 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7 ;; index of the next CLI arg
to process
8 ;; set
to 0, negative or ARGC
to stop further processing
9 $value
"CLI-ARG-NEXT" 1
11 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15 envp @ begin dup c@
while dup type
-asciiz cr zcount
+ 1+ repeat drop
18 : get
-env
( addr count
-- addr count true
// false
)
22 >r
2dup r@ zcount
[char
] = str
-trim
-at
-char s
=ci
if
23 2drop r
> zcount
[char
] = str
-skip
-after
-char true exit
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 : ARGV
-STR
( argnum
-- addr count
)
34 dup
0< over argc
>= or
if
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 ;; filename is in tloader
:fnpad
44 : (cold
-try
-load
-file
) ( -- successflag
)
45 \ endcr
." ::: trying: " tloader
:fnpad count type cr
46 tloader
:fnpad
-file? ifnot false exit
endif
47 tloader
:fnpad count os
:o
-rdonly
0 os
:open dup
-if drop false exit
endif
49 tload
-verbose
-rc
to tload
-verbose
;; be silent
50 ['] tloader:tload-fd catch
57 : (COLD-LOAD-RC) ( -- ??? )
58 true tloader:build-liblist-name (cold-try-load-file) drop ;; try list at home dir
59 false tloader:build-liblist-name (cold-try-load-file) drop ;; try list at binary dir
60 ;; check for "--naked"
63 \ TODO: create special dictionary for cli args, and simply execute them
64 i argv-str " --naked" s=ci if unloop exit endif
66 i argv-str " --verbose-libs" s=ci if 1 to tload-verbose-libs endif
67 i argv-str " --quiet-libs" s=ci if 0 to tload-verbose-libs endif
68 i argv-str " --verbose-rc" s=ci if 1 to tload-verbose-rc endif
69 i argv-str " --quiet-rc" s=ci if 0 to tload-verbose-rc endif
70 i argv-str " --eval" s=ci if drop 2 endif
71 i argv-str " -e" s=ci if drop 2 endif
74 ;; no "--naked", load .rc
75 tloader:build-rc-name (cold-try-load-file) drop
80 cli-arg-next 1+ to cli-arg-next
83 : (COLD-CLI) ( -- ??? )
84 ;; load "urforth.rc" (if present)
87 ;; don't use
DO ... LOOP here
, use CLI
-ARG
-NEXT
93 cli
-arg
-next argv
-str cli
-arg
-skip
100 ;; "-e" or
"--eval" ?
103 ;; note that argv is dropped here
104 if ;; set TIB and eval
105 cli
-arg
-next argv
-str cli
-arg
-skip evaluate
107 ;; unknown
"-..." args are skipped
116 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
117 : (URVERSION
->PAD
) ( -- addr count
)
119 " UrForth level " pad c4s
:copy
-counted
120 (urforth
-level
) <#u bl hold #s #
> pad c4s
:cat
-counted
121 " v" pad c4s
:cat
-counted
122 (urforth
-version
) 24 rshift
0xff and
<#u
[char
] . hold #s #
> pad c4s
:cat
-counted
123 (urforth
-version
) 16 rshift
0xff and
<#u
[char
] . hold #s #
> pad c4s
:cat
-counted
124 (urforth
-version
) 0x7fff and
<#u #s #
> pad c4s
:cat
-counted
125 (urforth
-version
) 0x8000 and
if " -beta" pad c4s
:cat
-counted
endif
136 (urforth
-version
) 24 rshift
0xff and
0 .r
[char
] . emit
137 (urforth
-version
) 16 rshift
0xff and
0 .r
[char
] . emit
138 (urforth
-version
) 0x7fff and
0 .r
139 (urforth
-version
) 0x8000 and
if ." -beta" endif
142 (urversion
->pad
) type
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 ;; called
to print a banner after processing CLI args
152 $defer
".BANNER" cfa
"(.banner)"
154 ;; called
(once
!) to process CLI args
155 $defer
"PROCESS-CLI-ARGS" cfa
"(cold-cli)"
157 ;; main
program loop
, should never
return
158 $defer
"MAIN-LOOP" cfa
"quit" ;; this word should never
return
161 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
166 ;; just in case it returns
170 : (COLD
-FIRSTTIME
) ( ???
)
179 here
(dp
-protect
) ;; why not
181 ;; just in case it returns
183 ; (hidden
) (noreturn
)