| 1 |
;;; Copyright (c) 2006, 2007 Abdulaziz Ghuloum and Kent Dybvig |
| 2 |
;;; |
| 3 |
;;; Permission is hereby granted, free of charge, to any person obtaining a |
| 4 |
;;; copy of this software and associated documentation files (the "Software"), |
| 5 |
;;; to deal in the Software without restriction, including without limitation |
| 6 |
;;; the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 7 |
;;; and/or sell copies of the Software, and to permit persons to whom the |
| 8 |
;;; Software is furnished to do so, subject to the following conditions: |
| 9 |
;;; |
| 10 |
;;; The above copyright notice and this permission notice shall be included in |
| 11 |
;;; all copies or substantial portions of the Software. |
| 12 |
;;; |
| 13 |
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 16 |
;;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 |
;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 |
;;; DEALINGS IN THE SOFTWARE. |
| 20 |
|
| 21 |
(library (psyntax builders) |
| 22 |
(export build-lexical-assignment build-global-reference |
| 23 |
build-application build-conditional build-lexical-reference |
| 24 |
build-global-assignment build-global-definition build-lambda |
| 25 |
build-case-lambda build-let build-primref build-foreign-call |
| 26 |
build-data build-sequence build-void build-letrec build-letrec* |
| 27 |
build-global-define build-library-letrec*) |
| 28 |
(import (rnrs) (psyntax compat) (psyntax config)) |
| 29 |
|
| 30 |
(define (build-global-define x) |
| 31 |
(if-wants-global-defines |
| 32 |
`(define ,x '#f) |
| 33 |
(build-void))) |
| 34 |
(define build-application |
| 35 |
(lambda (ae fun-exp arg-exps) |
| 36 |
(if ae |
| 37 |
`(annotated-call ,ae ,fun-exp . ,arg-exps) |
| 38 |
(cons fun-exp arg-exps)))) |
| 39 |
(define-syntax build-conditional |
| 40 |
(syntax-rules () |
| 41 |
((_ ae test-exp then-exp else-exp) |
| 42 |
`(if ,test-exp ,then-exp ,else-exp)))) |
| 43 |
(define-syntax build-lexical-reference |
| 44 |
(syntax-rules () |
| 45 |
((_ ae var) var))) |
| 46 |
(define-syntax build-lexical-assignment |
| 47 |
(syntax-rules () |
| 48 |
((_ ae var exp) `(set! ,var ,exp)))) |
| 49 |
(define-syntax build-global-reference |
| 50 |
(syntax-rules () |
| 51 |
((_ ae var) var))) |
| 52 |
(define-syntax build-global-assignment |
| 53 |
(syntax-rules () |
| 54 |
((_ ae var exp) `(set! ,var ,exp)))) |
| 55 |
(define-syntax build-global-definition |
| 56 |
(syntax-rules () |
| 57 |
((_ ae var exp) (build-global-assignment ae var exp)))) |
| 58 |
(define build-lambda |
| 59 |
(lambda (ae vars exp) |
| 60 |
(if-wants-case-lambda |
| 61 |
(build-case-lambda ae (list vars) (list exp)) |
| 62 |
`(lambda ,vars ,exp)))) |
| 63 |
(define build-case-lambda |
| 64 |
(if-wants-case-lambda |
| 65 |
(lambda (ae vars* exp*) |
| 66 |
(if ae |
| 67 |
`(annotated-case-lambda ,ae . ,(map list vars* exp*)) |
| 68 |
`(case-lambda . ,(map list vars* exp*)))) |
| 69 |
(lambda (ae vars* exp*) |
| 70 |
(define (build-error ae) |
| 71 |
(build-application ae |
| 72 |
(build-primref ae 'error) |
| 73 |
(list (build-data ae 'apply) |
| 74 |
(build-data ae "invalid arg count")))) |
| 75 |
(define (build-pred ae n vars) |
| 76 |
(let-values (((count pred) |
| 77 |
(let f ((vars vars) (count 0)) |
| 78 |
(cond |
| 79 |
((pair? vars) (f (cdr vars) (+ count 1))) |
| 80 |
((null? vars) (values count '=)) |
| 81 |
(else (values count '>=)))))) |
| 82 |
(build-application ae (build-primref ae pred) |
| 83 |
(list (build-lexical-reference ae n) |
| 84 |
(build-data ae count))))) |
| 85 |
(define (build-apply ae g vars exp) |
| 86 |
(build-application ae (build-primref ae 'apply) |
| 87 |
(list (build-lambda ae vars exp) |
| 88 |
(build-lexical-reference ae g)))) |
| 89 |
(define (expand-case-lambda ae vars exp*) |
| 90 |
(let ((g (gensym)) (n (gensym))) |
| 91 |
`(lambda ,g |
| 92 |
,(build-let ae |
| 93 |
(list n) (list (build-application ae |
| 94 |
(build-primref ae 'length) |
| 95 |
(list (build-lexical-reference ae g)))) |
| 96 |
(let f ((vars* vars*) (exp* exp*)) |
| 97 |
(if (null? vars*) |
| 98 |
(build-error ae) |
| 99 |
(build-conditional ae |
| 100 |
(build-pred ae n (car vars*)) |
| 101 |
(build-apply ae g (car vars*) (car exp*)) |
| 102 |
(f (cdr vars*) (cdr exp*))))))))) |
| 103 |
(if (= (length exp*) 1) |
| 104 |
(build-lambda ae (car vars*) (car exp*)) |
| 105 |
(expand-case-lambda ae vars* exp*))))) |
| 106 |
(define build-let |
| 107 |
(lambda (ae lhs* rhs* body) |
| 108 |
(build-application ae (build-lambda ae lhs* body) rhs*))) |
| 109 |
(define-syntax build-primref |
| 110 |
(syntax-rules () |
| 111 |
((_ ae name) (build-primref ae 1 name)) |
| 112 |
((_ ae level name) `(primitive ,name)))) |
| 113 |
(define-syntax build-foreign-call |
| 114 |
(syntax-rules () |
| 115 |
((_ ae name arg*) `(foreign-call ,name . ,arg*)))) |
| 116 |
(define-syntax build-data |
| 117 |
(syntax-rules () |
| 118 |
((_ ae exp) `',exp))) |
| 119 |
(define build-sequence |
| 120 |
(lambda (ae exps) |
| 121 |
(let loop ((exps exps)) |
| 122 |
(if (null? (cdr exps)) |
| 123 |
(car exps) |
| 124 |
(if (equal? (car exps) (build-void)) |
| 125 |
(loop (cdr exps)) |
| 126 |
`(begin ,@exps)))))) |
| 127 |
(define build-void |
| 128 |
(lambda () '((primitive void)))) |
| 129 |
(define build-letrec |
| 130 |
(lambda (ae vars val-exps body-exp) |
| 131 |
(if (null? vars) body-exp `(letrec ,(map list vars val-exps) ,body-exp)))) |
| 132 |
(define build-letrec* |
| 133 |
(lambda (ae vars val-exps body-exp) |
| 134 |
(cond |
| 135 |
((null? vars) body-exp) |
| 136 |
(else |
| 137 |
(if-wants-letrec* |
| 138 |
`(letrec* ,(map list vars val-exps) ,body-exp) |
| 139 |
(build-let ae vars (map (lambda (x) (build-data ae #f)) vars) |
| 140 |
(build-sequence ae |
| 141 |
(append |
| 142 |
(map (lambda (lhs rhs) |
| 143 |
(build-lexical-assignment ae lhs rhs)) |
| 144 |
vars val-exps) |
| 145 |
(list body-exp))))))))) |
| 146 |
(define build-library-letrec* |
| 147 |
(lambda (ae top? vars locs val-exps body-exp) |
| 148 |
(if-wants-library-letrec* |
| 149 |
`(library-letrec* ,(map list vars locs val-exps) ,body-exp) |
| 150 |
(build-letrec* ae vars val-exps |
| 151 |
(if top? |
| 152 |
body-exp |
| 153 |
(build-sequence ae |
| 154 |
(cons body-exp |
| 155 |
(map (lambda (var loc) |
| 156 |
(build-global-assignment ae loc var)) |
| 157 |
vars locs)))))))) |
| 158 |
|
| 159 |
|
| 160 |
) |
| 161 |
|
| 162 |
|