3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
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)
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.
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.
22 (define-module (language scheme inline)
23 #:use-module (system base syntax)
24 #:use-module (language ghil)
25 #:use-module (srfi srfi-16)
26 #:export (*inline-table* define-inline try-inline try-inline-with-env))
28 (define *inline-table* '())
30 (define-macro (define-inline sym . clauses)
31 (define (inline-args args)
32 (let lp ((in args) (out '()))
33 (cond ((null? in) `(list ,@(reverse out)))
34 ((symbol? in) `(cons* ,@(reverse out) ,in))
37 (cons `(or (try-inline ,(caar in) ,(inline-args (cdar in)))
38 (error "what" ',(car in)))
41 ;; assume it's locally bound
42 (lp (cdr in) (cons (car in) out)))
44 (lp (cdr in) (cons `(make-ghil-quote #f #f ,(car in)) out)))
46 (error "what what" (car in))))))
47 (define (consequent exp)
50 `(make-ghil-inline #f #f ',(car exp) ,(inline-args (cdr exp))))
52 ;; assume locally bound
55 `(make-ghil-quote #f #f ,exp))
56 (else (error "bad consequent yall" exp))))
57 `(set! (@ (language scheme inline) *inline-table*)
58 (assq-set! (@ (language scheme inline) *inline-table*)
60 (let ((make-ghil-inline (@ (language ghil) make-ghil-inline))
61 (make-ghil-quote (@ (language ghil) make-ghil-quote))
62 (try-inline (@ (language scheme inline) try-inline)))
64 ,@(let lp ((in clauses) (out '()))
66 (reverse (cons '(else #f) out))
69 ,(consequent (cadr in))) out)))))))))
71 (define (try-inline head-value args)
72 (and=> (assq-ref *inline-table* head-value)
73 (lambda (proc) (apply proc args))))
76 (define (try-inline-with-env env loc exp)
77 (let ((sym (car exp)))
80 ((<ghil-toplevel-env> table)
81 (let ((mod (current-module)))
82 (and (not (assoc-ref table (cons (module-name mod) sym)))
83 (module-bound? mod sym)
84 (try-inline (module-ref mod sym) (cdr exp)))))
85 ((<ghil-env> parent table variables)
86 (and (not (assq-ref table sym))
89 (define-inline eq? (x y)
92 (define-inline eqv? (x y)
95 (define-inline equal? (x y)
98 (define-inline = (x y)
101 (define-inline < (x y)
104 (define-inline > (x y)
107 (define-inline <= (x y)
110 (define-inline >= (x y)
113 (define-inline zero? (x)
120 (x y . rest) (add x (+ y . rest)))
126 (x y . rest) (mul x (* y . rest)))
131 (x y . rest) (sub x (+ y . rest)))
139 (x y . rest) (div x (* y . rest)))
141 (define-inline quotient (x y)
144 (define-inline remainder (x y)
147 (define-inline modulo (x y)
150 (define-inline not (x)
153 (define-inline pair? (x)
156 (define-inline cons (x y)
159 (define-inline car (x) (car x))
160 (define-inline cdr (x) (cdr x))
162 (define-inline set-car! (x y) (set-car! x y))
163 (define-inline set-cdr! (x y) (set-cdr! x y))
165 (define-inline caar (x) (car (car x)))
166 (define-inline cadr (x) (car (cdr x)))
167 (define-inline cdar (x) (cdr (car x)))
168 (define-inline cddr (x) (cdr (cdr x)))
169 (define-inline caaar (x) (car (car (car x))))
170 (define-inline caadr (x) (car (car (cdr x))))
171 (define-inline cadar (x) (car (cdr (car x))))
172 (define-inline caddr (x) (car (cdr (cdr x))))
173 (define-inline cdaar (x) (cdr (car (car x))))
174 (define-inline cdadr (x) (cdr (car (cdr x))))
175 (define-inline cddar (x) (cdr (cdr (car x))))
176 (define-inline cdddr (x) (cdr (cdr (cdr x))))
177 (define-inline caaaar (x) (car (car (car (car x)))))
178 (define-inline caaadr (x) (car (car (car (cdr x)))))
179 (define-inline caadar (x) (car (car (cdr (car x)))))
180 (define-inline caaddr (x) (car (car (cdr (cdr x)))))
181 (define-inline cadaar (x) (car (cdr (car (car x)))))
182 (define-inline cadadr (x) (car (cdr (car (cdr x)))))
183 (define-inline caddar (x) (car (cdr (cdr (car x)))))
184 (define-inline cadddr (x) (car (cdr (cdr (cdr x)))))
185 (define-inline cdaaar (x) (cdr (car (car (car x)))))
186 (define-inline cdaadr (x) (cdr (car (car (cdr x)))))
187 (define-inline cdadar (x) (cdr (car (cdr (car x)))))
188 (define-inline cdaddr (x) (cdr (car (cdr (cdr x)))))
189 (define-inline cddaar (x) (cdr (cdr (car (car x)))))
190 (define-inline cddadr (x) (cdr (cdr (car (cdr x)))))
191 (define-inline cdddar (x) (cdr (cdr (cdr (car x)))))
192 (define-inline cddddr (x) (cdr (cdr (cdr (cdr x)))))
194 (define-inline null? (x)
197 (define-inline list? (x)
203 (x y . rest) (cons x (cons* y . rest)))
206 (x y z) (cons (cons x y) z))