fix recording of source locations
[bpt/guile.git] / module / system / il / ghil.scm
CommitLineData
17e90c5e
KN
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)
17e90c5e 23 :use-syntax (system base syntax)
17e90c5e
KN
24 :use-module (ice-9 regex)
25 :export
01967b69 26 (<ghil-void> make-ghil-void ghil-void?
bdaffda2 27 ghil-void-env ghil-void-loc
01967b69
AW
28
29 <ghil-quote> make-ghil-quote ghil-quote?
bdaffda2 30 ghil-quote-env ghil-quote-loc ghil-quote-obj
01967b69
AW
31
32 <ghil-quasiquote> make-ghil-quasiquote ghil-quasiquote?
bdaffda2 33 ghil-quasiquote-env ghil-quasiquote-loc ghil-quasiquote-exp
01967b69
AW
34
35 <ghil-unquote> make-ghil-unquote ghil-unquote?
bdaffda2 36 ghil-unquote-env ghil-unquote-loc ghil-unquote-exp
01967b69
AW
37
38 <ghil-unquote-splicing> make-ghil-unquote-splicing ghil-unquote-splicing?
bdaffda2 39 ghil-unquote-env ghil-unquote-loc ghil-unquote-exp
cb4cca12 40
01967b69 41 <ghil-ref> make-ghil-ref ghil-ref?
bdaffda2 42 ghil-ref-env ghil-ref-loc ghil-ref-var
01967b69
AW
43
44 <ghil-set> make-ghil-set ghil-set?
bdaffda2 45 ghil-set-env ghil-set-loc ghil-set-var ghil-set-val
01967b69
AW
46
47 <ghil-define> make-ghil-define ghil-define?
bdaffda2 48 ghil-define-env ghil-define-loc ghil-define-var ghil-define-val
cb4cca12 49
01967b69 50 <ghil-if> make-ghil-if ghil-if?
bdaffda2 51 ghil-if-env ghil-if-loc ghil-if-test ghil-if-then ghil-if-else
01967b69
AW
52
53 <ghil-and> make-ghil-and ghil-and?
bdaffda2 54 ghil-and-env ghil-and-loc ghil-and-exps
01967b69
AW
55
56 <ghil-or> make-ghil-or ghil-or?
bdaffda2 57 ghil-or-env ghil-or-loc ghil-or-exps
01967b69
AW
58
59 <ghil-begin> make-ghil-begin ghil-begin?
bdaffda2 60 ghil-begin-env ghil-begin-loc ghil-begin-exps
01967b69
AW
61
62 <ghil-bind> make-ghil-bind ghil-bind?
bdaffda2 63 ghil-bind-env ghil-bind-loc ghil-bind-vars ghil-bind-vals ghil-bind-body
01967b69
AW
64
65 <ghil-lambda> make-ghil-lambda ghil-lambda?
bdaffda2 66 ghil-lambda-env ghil-lambda-loc ghil-lambda-vars ghil-lambda-rest ghil-lambda-body
01967b69
AW
67
68 <ghil-inline> make-ghil-inline ghil-inline?
bdaffda2 69 ghil-inline-env ghil-inline-loc ghil-inline-inline ghil-inline-args
01967b69
AW
70
71 <ghil-call> make-ghil-call ghil-call?
bdaffda2 72 ghil-call-env ghil-call-loc ghil-call-proc ghil-call-args
aa0a011b 73
01967b69
AW
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
aa0a011b 77
01967b69
AW
78 <ghil-mod> make-ghil-mod ghil-mod?
79 ghil-mod-module ghil-mod-table ghil-mod-imports
aa0a011b 80
01967b69 81 <ghil-env> make-ghil-env ghil-env?
77046be3
AW
82 ghil-env-mod ghil-env-parent ghil-env-table ghil-env-variables
83
22bcbe8c 84 ghil-env-add! ghil-lookup ghil-define
859f6390 85 ghil-env-toplevel?
77046be3 86 call-with-ghil-environment call-with-ghil-bindings))
17e90c5e
KN
87
88\f
89;;;
90;;; Parse tree
91;;;
92
ac99cb0c
KN
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
17e90c5e
KN
115\f
116;;;
117;;; Variables
118;;;
119
ac99cb0c 120(define-record (<ghil-var> env name kind (type #f) (value #f) (index #f)))
cb4cca12 121
17e90c5e
KN
122\f
123;;;
124;;; Modules
125;;;
126
ac99cb0c 127(define-record (<ghil-mod> module (table '()) (imports '())))
17e90c5e
KN
128
129\f
130;;;
131;;; Environments
132;;;
133
ac99cb0c 134(define-record (<ghil-env> mod parent (table '()) (variables '())))
17e90c5e 135
849cefac 136(define %make-ghil-env make-ghil-env)
77046be3 137(define (make-ghil-env e)
849cefac
AW
138 (record-case e
139 ((<ghil-mod>) (%make-ghil-env :mod e :parent e))
f540e327 140 ((<ghil-env> mod) (%make-ghil-env :mod mod :parent e))))
17e90c5e 141
66292535 142(define (ghil-env-toplevel? e)
859f6390 143 (eq? (ghil-env-mod e) (ghil-env-parent e)))
66292535 144
ac99cb0c 145(define (ghil-env-ref env sym)
61dc81d9
AW
146 (assq-ref (ghil-env-table env) sym))
147
148(define-macro (push! item loc)
149 `(set! ,loc (cons ,item ,loc)))
150(define-macro (apush! k v loc)
151 `(set! ,loc (acons ,k ,v ,loc)))
152(define-macro (apopq! k loc)
cd702346 153 `(set! ,loc (assq-remove! ,loc ,k)))
17e90c5e 154
77046be3 155(define (ghil-env-add! env var)
61dc81d9
AW
156 (apush! (ghil-var-name var) var (ghil-env-table env))
157 (push! var (ghil-env-variables env)))
17e90c5e 158
ac99cb0c 159(define (ghil-env-remove! env var)
61dc81d9 160 (apopq! (ghil-var-name var) (ghil-env-table env)))
17e90c5e 161
ac99cb0c
KN
162\f
163;;;
164;;; Public interface
165;;;
166
cd702346 167(define (fix-ghil-mod! mod for-sym)
1b8abe55
AW
168 ;;; So, these warnings happen for all instances of define-module.
169 ;;; Rather than fixing the problem, I'm going to suppress the common
170 ;;; warnings.
171 (if (not (eq? for-sym 'process-define-module))
172 (warn "during lookup of" for-sym ":"
173 (ghil-mod-module mod) "!= current" (current-module)))
cd702346 174 (if (not (null? (ghil-mod-table mod)))
1b8abe55
AW
175 (warn "throwing away old variable table"
176 (ghil-mod-module) (ghil-mod-table mod)))
cd702346
AW
177 (set! (ghil-mod-module mod) (current-module))
178 (set! (ghil-mod-table mod) '())
179 (set! (ghil-mod-imports mod) '()))
180
61dc81d9 181;; looking up a var has side effects?
77046be3 182(define (ghil-lookup env sym)
17e90c5e 183 (or (ghil-env-ref env sym)
61dc81d9
AW
184 (let loop ((e (ghil-env-parent env)))
185 (record-case e
186 ((<ghil-mod> module table imports)
cd702346
AW
187 (cond ((not (eq? module (current-module)))
188 ;; FIXME: the primitive-eval in eval-case and/or macro
189 ;; expansion can have side effects on the compilation
190 ;; environment, for example changing the current
191 ;; module. We probably need to add a special case in
192 ;; compilation to handle define-module.
193 (fix-ghil-mod! e sym)
194 (loop e))
195 ((assq-ref table sym)) ;; when does this hit?
5e1cead4 196 (else
6297d229
AW
197 ;; although we could bind the variable here, in
198 ;; practice further toplevel definitions in this
199 ;; compilation unit could change how we would resolve
200 ;; this binding, so punt and memoize the lookup at
201 ;; runtime always.
202 (let ((var (make-ghil-var (make-ghil-env e) sym 'module)))
9cc649b8
AW
203 (apush! sym var table)
204 var))))
61dc81d9
AW
205 ((<ghil-env> mod parent table variables)
206 (let ((found (assq-ref table sym)))
207 (if found
208 (begin (set! (ghil-var-kind found) 'external) found)
209 (loop parent))))))))
17e90c5e 210
cd9d95d7 211(define (ghil-define mod sym)
cd702346
AW
212 (if (not (eq? (ghil-mod-module mod) (current-module)))
213 (fix-ghil-mod! mod sym))
cd9d95d7 214 (or (assq-ref (ghil-mod-table mod) sym)
6167de4f 215 (let ((var (make-ghil-var (make-ghil-env mod) sym 'module)))
cd9d95d7
AW
216 (apush! sym var (ghil-mod-table mod))
217 var)))
218
77046be3 219(define (call-with-ghil-environment e syms func)
cb4cca12
KN
220 (let* ((e (make-ghil-env e))
221 (vars (map (lambda (s)
222 (let ((v (make-ghil-var e s 'argument)))
223 (ghil-env-add! e v) v))
224 syms)))
225 (func e vars)))
226
77046be3 227(define (call-with-ghil-bindings e syms func)
cb4cca12
KN
228 (let* ((vars (map (lambda (s)
229 (let ((v (make-ghil-var e s 'local)))
230 (ghil-env-add! e v) v))
231 syms))
232 (ret (func vars)))
233 (for-each (lambda (v) (ghil-env-remove! e v)) vars)
234 ret))
235
17e90c5e
KN
236\f
237;;;
238;;; Parser
239;;;
240
ac99cb0c
KN
241;;; (define-public (parse-ghil x e)
242;;; (parse `(@lambda () ,x) (make-ghil-mod e)))
243;;;
244;;; (define (parse x e)
245;;; (cond ((pair? x) (parse-pair x e))
246;;; ((symbol? x)
247;;; (let ((str (symbol->string x)))
248;;; (case (string-ref str 0)
249;;; ((#\@) (error "Invalid use of IL primitive" x))
250;;; ((#\:) (let ((sym (string->symbol (substring str 1))))
251;;; (<ghil-quote> (symbol->keyword sym))))
252;;; (else (<ghil-ref> e (ghil-lookup e x))))))
253;;; (else (<ghil-quote> x))))
254;;;
255;;; (define (map-parse x e)
256;;; (map (lambda (x) (parse x e)) x))
257;;;
258;;; (define (parse-pair x e)
259;;; (let ((head (car x)) (tail (cdr x)))
260;;; (if (and (symbol? head) (eq? (string-ref (symbol->string head) 0) #\@))
261;;; (if (ghil-primitive-macro? head)
262;;; (parse (apply (ghil-macro-expander head) tail) e)
263;;; (parse-primitive head tail e))
264;;; (<ghil-call> e (parse head e) (map-parse tail e)))))
265;;;
266;;; (define (parse-primitive prim args e)
267;;; (case prim
268;;; ;; (@ IDENTIFIER)
269;;; ((@)
270;;; (match args
271;;; (()
272;;; (<ghil-ref> e (make-ghil-var '@ '@ 'module)))
273;;; ((identifier)
274;;; (receive (module name) (identifier-split identifier)
275;;; (<ghil-ref> e (make-ghil-var module name 'module))))))
276;;;
277;;; ;; (@@ OP ARGS...)
278;;; ((@@)
279;;; (match args
280;;; ((op . args)
281;;; (<ghil-inline> op (map-parse args e)))))
282;;;
283;;; ;; (@void)
284;;; ((@void)
285;;; (match args
286;;; (() (<ghil-void>))))
287;;;
288;;; ;; (@quote OBJ)
289;;; ((@quote)
290;;; (match args
291;;; ((obj)
292;;; (<ghil-quote> obj))))
293;;;
294;;; ;; (@define NAME VAL)
295;;; ((@define)
296;;; (match args
297;;; ((name val)
298;;; (let ((v (ghil-lookup e name)))
299;;; (<ghil-set> e v (parse val e))))))
300;;;
301;;; ;; (@set! NAME VAL)
302;;; ((@set!)
303;;; (match args
304;;; ((name val)
305;;; (let ((v (ghil-lookup e name)))
306;;; (<ghil-set> e v (parse val e))))))
307;;;
308;;; ;; (@if TEST THEN [ELSE])
309;;; ((@if)
310;;; (match args
311;;; ((test then)
312;;; (<ghil-if> (parse test e) (parse then e) (<ghil-void>)))
313;;; ((test then else)
314;;; (<ghil-if> (parse test e) (parse then e) (parse else e)))))
315;;;
316;;; ;; (@begin BODY...)
317;;; ((@begin)
318;;; (parse-body args e))
319;;;
320;;; ;; (@let ((SYM INIT)...) BODY...)
321;;; ((@let)
322;;; (match args
323;;; ((((sym init) ...) body ...)
324;;; (let* ((vals (map-parse init e))
325;;; (vars (map (lambda (s)
326;;; (let ((v (make-ghil-var e s 'local)))
327;;; (ghil-env-add! e v) v))
328;;; sym))
329;;; (body (parse-body body e)))
330;;; (for-each (lambda (v) (ghil-env-remove! e v)) vars)
331;;; (<ghil-bind> e vars vals body)))))
332;;;
333;;; ;; (@letrec ((SYM INIT)...) BODY...)
334;;; ((@letrec)
335;;; (match args
336;;; ((((sym init) ...) body ...)
337;;; (let* ((vars (map (lambda (s)
338;;; (let ((v (make-ghil-var e s 'local)))
339;;; (ghil-env-add! e v) v))
340;;; sym))
341;;; (vals (map-parse init e))
342;;; (body (parse-body body e)))
343;;; (for-each (lambda (v) (ghil-env-remove! e v)) vars)
344;;; (<ghil-bind> e vars vals body)))))
345;;;
346;;; ;; (@lambda FORMALS BODY...)
347;;; ((@lambda)
348;;; (match args
349;;; ((formals . body)
350;;; (receive (syms rest) (parse-formals formals)
351;;; (let* ((e (make-ghil-env e))
352;;; (vars (map (lambda (s)
353;;; (let ((v (make-ghil-var e s 'argument)))
354;;; (ghil-env-add! e v) v))
355;;; syms)))
356;;; (<ghil-lambda> e vars rest (parse-body body e)))))))
357;;;
358;;; ;; (@eval-case CLAUSE...)
359;;; ((@eval-case)
360;;; (let loop ((clauses args))
361;;; (cond ((null? clauses) (<ghil-void>))
362;;; ((or (eq? (caar clauses) '@else)
363;;; (and (memq 'load-toplevel (caar clauses))
364;;; (ghil-env-toplevel? e)))
365;;; (parse-body (cdar clauses) e))
366;;; (else
367;;; (loop (cdr clauses))))))
368;;;
369;;; (else (error "Unknown primitive:" prim))))
370;;;
371;;; (define (parse-body x e)
372;;; (<ghil-begin> (map-parse x e)))
373;;;
374;;; (define (parse-formals formals)
375;;; (cond
376;;; ;; (@lambda x ...)
377;;; ((symbol? formals) (values (list formals) #t))
378;;; ;; (@lambda (x y z) ...)
379;;; ((list? formals) (values formals #f))
380;;; ;; (@lambda (x y . z) ...)
381;;; ((pair? formals)
382;;; (let loop ((l formals) (v '()))
383;;; (if (pair? l)
384;;; (loop (cdr l) (cons (car l) v))
385;;; (values (reverse! (cons l v)) #t))))
386;;; (else (error "Invalid formals:" formals))))
387;;;
388;;; (define (identifier-split identifier)
389;;; (let ((m (string-match "::([^:]*)$" (symbol->string identifier))))
390;;; (if m
391;;; (values (string->symbol (match:prefix m))
392;;; (string->symbol (match:substring m 1)))
393;;; (values #f identifier))))