-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathselfboot-entry.rkt
More file actions
257 lines (228 loc) · 9.23 KB
/
Copy pathselfboot-entry.rkt
File metadata and controls
257 lines (228 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
;;
;; Selfboot entrypoint for Racket (INCOMPLETE)
;;
;; $ racket /path/to/selfboot-entry.rkt ....
;;
#lang racket
(require (prefix-in r6rs: r6rs/lang/reader))
(define (command-line) (cons
(path->string
(path->complete-path (find-system-path 'run-file)))
(vector->list (current-command-line-arguments))))
(define (%%extract-program-args args* entrypth)
(if (string=? (car args*) entrypth)
(cdr args*)
(%%extract-program-args (cdr args*) entrypth)))
(define (%%extract-entrypoint-path args*)
(define (checkone s)
(and (string? s)
(let ((len (string-length s)))
(and (< 4 len)
(string=? (substring s (- len 4) len) ".rkt")
s))))
(and (pair? args*)
(or (checkone (car args*))
(%%extract-entrypoint-path (cdr args*)))))
(define (%%pathslashfy pth)
(let* ((l (string->list pth))
(x (map (lambda (c) (if (char=? #\\ c) #\/ c)) l)))
(list->string x)))
(define (%%pathsimplify pth)
(define (pathcompose acc l)
(if (pair? l)
(pathcompose (if (string=? (car l) "")
acc
(string-append acc "/" (car l)))
(cdr l))
acc))
(define (pathcompose-start acc l)
(pathcompose (car l) (cdr l)))
(define (pathcomponent acc cur strq)
(if (string=? strq "")
(if (null? acc)
(reverse cur)
(reverse (cons (list->string (reverse acc)) cur)))
(let ((c (string-ref strq 0))
(r (substring strq 1 (string-length strq))))
(if (char=? c #\/)
(pathcomponent '() (cons (list->string (reverse acc)) cur) r)
(pathcomponent (cons c acc) cur r)))))
(define (simple cur m q)
(if (null? q)
(if (null? cur)
(reverse (cons m cur))
(reverse (cdr cur)))
(if (string=? m "..")
(simple (cdr cur) (car q) (cdr q))
(simple (cons m cur) (car q) (cdr q)))))
(define (start-simple cur m q)
;; Protect relative ../../../ sequence at beginning
(if (string=? m "..")
(start-simple (cons m cur) (car q) (cdr q))
(simple cur m q)))
(let ((r (pathcomponent '() '() pth)))
(pathcompose-start "" (start-simple '() (car r) (cdr r)))))
(define (%%locate-yuniroot-fromscmpath scmpath)
(write %%selfboot-orig-command-line) (newline)
(write %%selfboot-mypath) (newline)
(let ((npth (%%pathslashfy scmpath)))
(%%pathsimplify (string-append npth "/../../../.."))))
(define %%selfboot-orig-command-line (command-line))
(define %%selfboot-mypath (%%extract-entrypoint-path %%selfboot-orig-command-line))
(define %%selfboot-yuniroot (%%locate-yuniroot-fromscmpath %%selfboot-mypath))
(define %%selfboot-program-args (%%extract-program-args
%%selfboot-orig-command-line
%%selfboot-mypath))
(define myenv (make-base-namespace))
(define (xload pth)
(define import-done? #f)
(define (do-eval code)
(for-each (lambda (e)
(cond
((and (not import-done?) (pair? e) (eq? 'import (car e)))
(let ((reqclause* (map convert-import (cdr e))))
(eval-with-local-resolver `(require ,@reqclause*)))
(set! import-done? #t))
(else
;(write (list 'EVAL: e)) (newline)
(eval e myenv))))
code))
(write (list 'XLOAD: pth)) (newline)
(call-with-input-file
pth
(lambda (p)
(let loop ((code '()))
(let ((c (read p)))
(if (eof-object? c)
(do-eval (reverse code))
(loop (cons c code))))))))
(define (read-r6rs-source pth)
(call-with-input-file
pth
(lambda (p)
(r6rs:read-syntax (object-name p) p #'dummy 1 0 1))))
(define h-libnames (make-hash))
(define h-libsyms (make-hash))
(define (eval-with-local-resolver frm)
(let ((rr (current-module-name-resolver)))
(define res
(case-lambda
((r0 r1) 'ignored)
((mod src-name relative? unknown)
(let* ((q (hash-ref h-libsyms mod #f)))
(if q
(make-resolved-module-path mod)
(rr mod src-name relative? unknown))))))
(parameterize ((current-module-name-resolver res))
(eval frm myenv))))
(define (libname->symbol name)
(let loop ((cur "")
(q name))
(if (null? q)
(string->symbol cur)
(loop (string-append cur
(if (string=? "" cur) "" "_")
(symbol->string (car q)))
(cdr q)))))
(define (mlist->list x) (for/list ((y (in-mlist x))) y))
(define (load-libaliases truename alias* export*)
(let ((x (hash-ref h-libnames (mlist->list truename))))
(for-each (lambda (libname)
(write (list 'ALIAS x truename '=> libname)) (newline)
(hash-set! h-libnames (mlist->list libname) x))
(mlist->list alias*))))
(define (dumpsy x)
(for-each
(lambda (e)
(cond
((list? (syntax->datum e))
(dumpsy e))
(else
(write e) (newline) )))
(syntax->list x)))
(define (symbolic-identifier=? x y) ;; From TSPL
(eq? (syntax->datum x)
(syntax->datum y)))
(define (convert-import clause)
(let ((head (car clause)))
(case head
((only) `(only-in ,(convert-import (cadr clause)) . ,(cddr clause)))
((except) `(except-in ,(convert-import (cadr clause)) . ,(cddr clause)))
((rename) `(rename-in ,(convert-import (cadr clause)) . ,(cddr clause)))
;; FIXME: Implement multiple meta-levels
((for)
(unless (equal? '(run expand) (cddr clause))
(error 'xxx "Unknown phase level" clause))
`(for-syntax ,(convert-import (cadr clause))))
(else
(hash-ref h-libnames clause)))))
(define (loadlib pth libname0 imports exports)
(let* ((libname (mlist->list libname0))
(libsym (libname->symbol libname)))
(hash-set! h-libnames libname libsym)
(hash-set! h-libsyms libsym #t)
(write (list 'LOAD libname '=> libsym)) (newline)
(let ((x (read-r6rs-source pth)))
(syntax-case*
x (library export import) symbolic-identifier=?
((_ xxx dummy (module-begin (library libname
(export exports ...)
(import imports ...)
body ...)))
(let ((x-imports (syntax->datum #'(imports ...)))
(x-exports (syntax->datum #'(exports ...)))
(x-body (syntax->datum #'(body ...))))
(let ((code `(module ignored racket/base
(require ,@(map convert-import x-imports))
(provide ,@x-exports)
,@x-body)))
(parameterize
((current-namespace myenv)
(current-module-declare-name (make-resolved-module-path libsym)))
(eval-with-local-resolver code)))))))))
(define (set-top-level-value! sym val env)
;; Chez scheme API
;; map? = #f, as-constant? = #f
(namespace-set-variable-value! sym val #f env #f))
(define (launcher yuniroot program-args)
(parameterize ((current-namespace myenv))
(namespace-require 'rnrs)
(namespace-require 'rnrs/mutable-pairs-6))
(set-top-level-value! '%%selfboot-tmp-xload xload myenv)
(set-top-level-value! '%%selfboot-yuniroot yuniroot myenv)
;; Define program-args as mpair
(eval `(define %%selfboot-program-args (quote ,program-args)) myenv)
;; ... and same for core-libs
(eval '(define %%selfboot-core-libs (quote ((rnrs)
(rnrs mutable-pairs)
(rnrs mutable-strings)
(rnrs r5rs)
(racket base)
(srfi :1)
(srfi :9)
(srfi :39)
(srfi :98))))
myenv)
(for-each (lambda (e)
(let ((libname (car e))
(libsym (cdr e)))
(hash-set! h-libnames libname libsym)))
'(((rnrs) . rnrs)
((rnrs mutable-pairs) . rnrs/mutable-pairs-6)
((rnrs mutable-strings) . rnrs/mutable-strings-6)
((rnrs r5rs) . rnrs/r5rs-6)
((racket base) . racket/base)
((srfi :1) . srfi/%3a1)
((srfi :9) . srfi/%3a9)
((srfi :39) . srfi/%3a39)
((srfi :98) . srfi/%3a98)))
(set-top-level-value! '%%selfboot-impl-type 'racket myenv)
(set-top-level-value! '%%selfboot-load-aliaslib load-libaliases myenv)
(set-top-level-value! '%%selfboot-loadlib loadlib myenv)
(eval '(define load %%selfboot-tmp-xload) myenv)
(xload (string-append yuniroot "/lib-runtime/selfboot/racket/selfboot-runtime.scm"))
(xload (string-append yuniroot "/lib-runtime/selfboot/common/common.scm"))
(xload (string-append yuniroot "/lib-runtime/selfboot/common/run-program.scm")))
(when (string=? %%selfboot-yuniroot "")
(set! %%selfboot-yuniroot "."))
(launcher %%selfboot-yuniroot %%selfboot-program-args)