bind all module-level variables lazily
[bpt/guile.git] / module / system / il / ghil.scm
1 ;;; Guile High Intermediate Language
2
3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
4
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
8 ;; any later version.
9 ;;
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
19
20 ;;; Code:
21
22 (define-module (system il ghil)
23 :use-syntax (system base syntax)
24 :use-module (ice-9 regex)
25 :export
26 (<ghil-void> make-ghil-void ghil-void?
27 ghil-void-env ghil-void-loc
28
29 <ghil-quote> make-ghil-quote ghil-quote?
30 ghil-quote-env ghil-quote-loc ghil-quote-obj
31
32 <ghil-quasiquote> make-ghil-quasiquote ghil-quasiquote?
33 ghil-quasiquote-env ghil-quasiquote-loc ghil-quasiquote-exp
34
35 <ghil-unquote> make-ghil-unquote ghil-unquote?
36 ghil-unquote-env ghil-unquote-loc ghil-unquote-exp
37
38 <ghil-unquote-splicing> make-ghil-unquote-splicing ghil-unquote-splicing?
39 ghil-unquote-env ghil-unquote-loc ghil-unquote-exp
40
41 <ghil-ref> make-ghil-ref ghil-ref?
42 ghil-ref-env ghil-ref-loc ghil-ref-var
43
44 <ghil-set> make-ghil-set ghil-set?
45 ghil-set-env ghil-set-loc ghil-set-var ghil-set-val
46
47 <ghil-define> make-ghil-define ghil-define?
48 ghil-define-env ghil-define-loc ghil-define-var ghil-define-val
49
50 <ghil-if> make-ghil-if ghil-if?
51 ghil-if-env ghil-if-loc ghil-if-test ghil-if-then ghil-if-else
52
53 <ghil-and> make-ghil-and ghil-and?
54 ghil-and-env ghil-and-loc ghil-and-exps
55
56 <ghil-or> make-ghil-or ghil-or?
57 ghil-or-env ghil-or-loc ghil-or-exps
58
59 <ghil-begin> make-ghil-begin ghil-begin?
60 ghil-begin-env ghil-begin-loc ghil-begin-exps
61
62 <ghil-bind> make-ghil-bind ghil-bind?
63 ghil-bind-env ghil-bind-loc ghil-bind-vars ghil-bind-vals ghil-bind-body
64
65 <ghil-lambda> make-ghil-lambda ghil-lambda?
66 ghil-lambda-env ghil-lambda-loc ghil-lambda-vars ghil-lambda-rest ghil-lambda-body
67
68 <ghil-inline> make-ghil-inline ghil-inline?
69 ghil-inline-env ghil-inline-loc ghil-inline-inline ghil-inline-args
70
71 <ghil-call> make-ghil-call ghil-call?
72 ghil-call-env ghil-call-loc ghil-call-proc ghil-call-args
73
74 <ghil-var> make-ghil-var ghil-var?
75 ghil-var-env ghil-var-name ghil-var-kind ghil-var-type ghil-var-value
76 ghil-var-index
77
78 <ghil-mod> make-ghil-mod ghil-mod?
79 ghil-mod-module ghil-mod-table ghil-mod-imports
80
81 <ghil-env> make-ghil-env ghil-env?
82 ghil-env-mod ghil-env-parent ghil-env-table ghil-env-variables
83
84 ghil-primitive-macro? ghil-env-add! ghil-lookup ghil-define
85 ghil-env-toplevel?
86 call-with-ghil-environment call-with-ghil-bindings))
87
88 \f
89 ;;;
90 ;;; Parse tree
91 ;;;
92
93 (define-type <ghil>
94 (|
95 ;; Objects
96 (<ghil-void> env loc)
97 (<ghil-quote> env loc obj)
98 (<ghil-quasiquote> env loc exp)
99 (<ghil-unquote> env loc exp)
100 (<ghil-unquote-splicing> env loc exp)
101 ;; Variables
102 (<ghil-ref> env loc var)
103 (<ghil-set> env loc var val)
104 (<ghil-define> env loc var val)
105 ;; Controls
106 (<ghil-if> env loc test then else)
107 (<ghil-and> env loc exps)
108 (<ghil-or> env loc exps)
109 (<ghil-begin> env loc exps)
110 (<ghil-bind> env loc vars vals body)
111 (<ghil-lambda> env loc vars rest body)
112 (<ghil-call> env loc proc args)
113 (<ghil-inline> env loc inline args)))
114
115 \f
116 ;;;
117 ;;; Procedures
118 ;;;
119
120 (define *core-primitives*
121 '(@void @quote @define @set! @if @begin @let @letrec @lambda))
122
123 (define *macro-module* (resolve-module '(system il macros)))
124
125 (define (ghil-primitive-macro? x)
126 (and (module-defined? *macro-module* x)
127 (procedure? (module-ref *macro-module* x))))
128
129 (define (ghil-macro-expander x)
130 (module-ref *macro-module* x))
131
132 (define (ghil-primitive? x)
133 (or (memq x *core-primitives*)
134 (ghil-primitive-macro? x)))
135
136 \f
137 ;;;
138 ;;; Variables
139 ;;;
140
141 (define-record (<ghil-var> env name kind (type #f) (value #f) (index #f)))
142
143 \f
144 ;;;
145 ;;; Modules
146 ;;;
147
148 (define-record (<ghil-mod> module (table '()) (imports '())))
149
150 \f
151 ;;;
152 ;;; Environments
153 ;;;
154
155 (define-record (<ghil-env> mod parent (table '()) (variables '())))
156
157 (define %make-ghil-env make-ghil-env)
158 (define (make-ghil-env e)
159 (record-case e
160 ((<ghil-mod>) (%make-ghil-env :mod e :parent e))
161 ((<ghil-env> mod) (%make-ghil-env :mod mod :parent e))))
162
163 (define (ghil-env-toplevel? e)
164 (eq? (ghil-env-mod e) (ghil-env-parent e)))
165
166 (define (ghil-env-ref env sym)
167 (assq-ref (ghil-env-table env) sym))
168
169 (define-macro (push! item loc)
170 `(set! ,loc (cons ,item ,loc)))
171 (define-macro (apush! k v loc)
172 `(set! ,loc (acons ,k ,v ,loc)))
173 (define-macro (apopq! k loc)
174 `(set! ,loc (assq-remove! ,loc ,k)))
175
176 (define (ghil-env-add! env var)
177 (apush! (ghil-var-name var) var (ghil-env-table env))
178 (push! var (ghil-env-variables env)))
179
180 (define (ghil-env-remove! env var)
181 (apopq! (ghil-var-name var) (ghil-env-table env)))
182
183 \f
184 ;;;
185 ;;; Public interface
186 ;;;
187
188 (define (fix-ghil-mod! mod for-sym)
189 ;;; So, these warnings happen for all instances of define-module.
190 ;;; Rather than fixing the problem, I'm going to suppress the common
191 ;;; warnings.
192 (if (not (eq? for-sym 'process-define-module))
193 (warn "during lookup of" for-sym ":"
194 (ghil-mod-module mod) "!= current" (current-module)))
195 (if (not (null? (ghil-mod-table mod)))
196 (warn "throwing away old variable table"
197 (ghil-mod-module) (ghil-mod-table mod)))
198 (set! (ghil-mod-module mod) (current-module))
199 (set! (ghil-mod-table mod) '())
200 (set! (ghil-mod-imports mod) '()))
201
202 ;; looking up a var has side effects?
203 (define (ghil-lookup env sym)
204 (or (ghil-env-ref env sym)
205 (let loop ((e (ghil-env-parent env)))
206 (record-case e
207 ((<ghil-mod> module table imports)
208 (cond ((not (eq? module (current-module)))
209 ;; FIXME: the primitive-eval in eval-case and/or macro
210 ;; expansion can have side effects on the compilation
211 ;; environment, for example changing the current
212 ;; module. We probably need to add a special case in
213 ;; compilation to handle define-module.
214 (fix-ghil-mod! e sym)
215 (loop e))
216 ((assq-ref table sym)) ;; when does this hit?
217 (else
218 ;; although we could bind the variable here, in
219 ;; practice further toplevel definitions in this
220 ;; compilation unit could change how we would resolve
221 ;; this binding, so punt and memoize the lookup at
222 ;; runtime always.
223 (let ((var (make-ghil-var (make-ghil-env e) sym 'module)))
224 (apush! sym var table)
225 var))))
226 ((<ghil-env> mod parent table variables)
227 (let ((found (assq-ref table sym)))
228 (if found
229 (begin (set! (ghil-var-kind found) 'external) found)
230 (loop parent))))))))
231
232 (define (ghil-define mod sym)
233 (if (not (eq? (ghil-mod-module mod) (current-module)))
234 (fix-ghil-mod! mod sym))
235 (or (assq-ref (ghil-mod-table mod) sym)
236 (let ((var (make-ghil-var (make-ghil-env mod) sym 'module)))
237 (apush! sym var (ghil-mod-table mod))
238 var)))
239
240 (define (call-with-ghil-environment e syms func)
241 (let* ((e (make-ghil-env e))
242 (vars (map (lambda (s)
243 (let ((v (make-ghil-var e s 'argument)))
244 (ghil-env-add! e v) v))
245 syms)))
246 (func e vars)))
247
248 (define (call-with-ghil-bindings e syms func)
249 (let* ((vars (map (lambda (s)
250 (let ((v (make-ghil-var e s 'local)))
251 (ghil-env-add! e v) v))
252 syms))
253 (ret (func vars)))
254 (for-each (lambda (v) (ghil-env-remove! e v)) vars)
255 ret))
256
257 \f
258 ;;;
259 ;;; Parser
260 ;;;
261
262 ;;; (define-public (parse-ghil x e)
263 ;;; (parse `(@lambda () ,x) (make-ghil-mod e)))
264 ;;;
265 ;;; (define (parse x e)
266 ;;; (cond ((pair? x) (parse-pair x e))
267 ;;; ((symbol? x)
268 ;;; (let ((str (symbol->string x)))
269 ;;; (case (string-ref str 0)
270 ;;; ((#\@) (error "Invalid use of IL primitive" x))
271 ;;; ((#\:) (let ((sym (string->symbol (substring str 1))))
272 ;;; (<ghil-quote> (symbol->keyword sym))))
273 ;;; (else (<ghil-ref> e (ghil-lookup e x))))))
274 ;;; (else (<ghil-quote> x))))
275 ;;;
276 ;;; (define (map-parse x e)
277 ;;; (map (lambda (x) (parse x e)) x))
278 ;;;
279 ;;; (define (parse-pair x e)
280 ;;; (let ((head (car x)) (tail (cdr x)))
281 ;;; (if (and (symbol? head) (eq? (string-ref (symbol->string head) 0) #\@))
282 ;;; (if (ghil-primitive-macro? head)
283 ;;; (parse (apply (ghil-macro-expander head) tail) e)
284 ;;; (parse-primitive head tail e))
285 ;;; (<ghil-call> e (parse head e) (map-parse tail e)))))
286 ;;;
287 ;;; (define (parse-primitive prim args e)
288 ;;; (case prim
289 ;;; ;; (@ IDENTIFIER)
290 ;;; ((@)
291 ;;; (match args
292 ;;; (()
293 ;;; (<ghil-ref> e (make-ghil-var '@ '@ 'module)))
294 ;;; ((identifier)
295 ;;; (receive (module name) (identifier-split identifier)
296 ;;; (<ghil-ref> e (make-ghil-var module name 'module))))))
297 ;;;
298 ;;; ;; (@@ OP ARGS...)
299 ;;; ((@@)
300 ;;; (match args
301 ;;; ((op . args)
302 ;;; (<ghil-inline> op (map-parse args e)))))
303 ;;;
304 ;;; ;; (@void)
305 ;;; ((@void)
306 ;;; (match args
307 ;;; (() (<ghil-void>))))
308 ;;;
309 ;;; ;; (@quote OBJ)
310 ;;; ((@quote)
311 ;;; (match args
312 ;;; ((obj)
313 ;;; (<ghil-quote> obj))))
314 ;;;
315 ;;; ;; (@define NAME VAL)
316 ;;; ((@define)
317 ;;; (match args
318 ;;; ((name val)
319 ;;; (let ((v (ghil-lookup e name)))
320 ;;; (<ghil-set> e v (parse val e))))))
321 ;;;
322 ;;; ;; (@set! NAME VAL)
323 ;;; ((@set!)
324 ;;; (match args
325 ;;; ((name val)
326 ;;; (let ((v (ghil-lookup e name)))
327 ;;; (<ghil-set> e v (parse val e))))))
328 ;;;
329 ;;; ;; (@if TEST THEN [ELSE])
330 ;;; ((@if)
331 ;;; (match args
332 ;;; ((test then)
333 ;;; (<ghil-if> (parse test e) (parse then e) (<ghil-void>)))
334 ;;; ((test then else)
335 ;;; (<ghil-if> (parse test e) (parse then e) (parse else e)))))
336 ;;;
337 ;;; ;; (@begin BODY...)
338 ;;; ((@begin)
339 ;;; (parse-body args e))
340 ;;;
341 ;;; ;; (@let ((SYM INIT)...) BODY...)
342 ;;; ((@let)
343 ;;; (match args
344 ;;; ((((sym init) ...) body ...)
345 ;;; (let* ((vals (map-parse init e))
346 ;;; (vars (map (lambda (s)
347 ;;; (let ((v (make-ghil-var e s 'local)))
348 ;;; (ghil-env-add! e v) v))
349 ;;; sym))
350 ;;; (body (parse-body body e)))
351 ;;; (for-each (lambda (v) (ghil-env-remove! e v)) vars)
352 ;;; (<ghil-bind> e vars vals body)))))
353 ;;;
354 ;;; ;; (@letrec ((SYM INIT)...) BODY...)
355 ;;; ((@letrec)
356 ;;; (match args
357 ;;; ((((sym init) ...) body ...)
358 ;;; (let* ((vars (map (lambda (s)
359 ;;; (let ((v (make-ghil-var e s 'local)))
360 ;;; (ghil-env-add! e v) v))
361 ;;; sym))
362 ;;; (vals (map-parse init e))
363 ;;; (body (parse-body body e)))
364 ;;; (for-each (lambda (v) (ghil-env-remove! e v)) vars)
365 ;;; (<ghil-bind> e vars vals body)))))
366 ;;;
367 ;;; ;; (@lambda FORMALS BODY...)
368 ;;; ((@lambda)
369 ;;; (match args
370 ;;; ((formals . body)
371 ;;; (receive (syms rest) (parse-formals formals)
372 ;;; (let* ((e (make-ghil-env e))
373 ;;; (vars (map (lambda (s)
374 ;;; (let ((v (make-ghil-var e s 'argument)))
375 ;;; (ghil-env-add! e v) v))
376 ;;; syms)))
377 ;;; (<ghil-lambda> e vars rest (parse-body body e)))))))
378 ;;;
379 ;;; ;; (@eval-case CLAUSE...)
380 ;;; ((@eval-case)
381 ;;; (let loop ((clauses args))
382 ;;; (cond ((null? clauses) (<ghil-void>))
383 ;;; ((or (eq? (caar clauses) '@else)
384 ;;; (and (memq 'load-toplevel (caar clauses))
385 ;;; (ghil-env-toplevel? e)))
386 ;;; (parse-body (cdar clauses) e))
387 ;;; (else
388 ;;; (loop (cdr clauses))))))
389 ;;;
390 ;;; (else (error "Unknown primitive:" prim))))
391 ;;;
392 ;;; (define (parse-body x e)
393 ;;; (<ghil-begin> (map-parse x e)))
394 ;;;
395 ;;; (define (parse-formals formals)
396 ;;; (cond
397 ;;; ;; (@lambda x ...)
398 ;;; ((symbol? formals) (values (list formals) #t))
399 ;;; ;; (@lambda (x y z) ...)
400 ;;; ((list? formals) (values formals #f))
401 ;;; ;; (@lambda (x y . z) ...)
402 ;;; ((pair? formals)
403 ;;; (let loop ((l formals) (v '()))
404 ;;; (if (pair? l)
405 ;;; (loop (cdr l) (cons (car l) v))
406 ;;; (values (reverse! (cons l v)) #t))))
407 ;;; (else (error "Invalid formals:" formals))))
408 ;;;
409 ;;; (define (identifier-split identifier)
410 ;;; (let ((m (string-match "::([^:]*)$" (symbol->string identifier))))
411 ;;; (if m
412 ;;; (values (string->symbol (match:prefix m))
413 ;;; (string->symbol (match:substring m 1)))
414 ;;; (values #f identifier))))