|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (language tree-il demux-lambda) |
|
#:use-module (srfi srfi-1) |
|
#:use-module (ice-9 match) |
|
#:use-module (language tree-il) |
|
#:export (demux-lambda)) |
|
|
|
(define (make-binding name sym val) (vector name sym val)) |
|
|
|
(define (demux-clause func-name clause) |
|
(match clause |
|
(#f (values '() clause)) |
|
(($ <lambda-case> src req opt rest kw inits gensyms body alternate) |
|
(call-with-values (lambda () (demux-clause func-name alternate)) |
|
(lambda (bindings alternate) |
|
(define simple-req |
|
(append req (or opt '()) (if rest (list rest) '()) |
|
(match kw |
|
((aok? (kw name sym) ...) name) |
|
(#f '())))) |
|
(define simple-clause |
|
(make-lambda-case src simple-req '() #f #f '() gensyms body #f)) |
|
(define simple-func (make-lambda src '() simple-clause)) |
|
(define simple-sym (gensym "demuxed")) |
|
(define simple-binding |
|
(make-binding func-name simple-sym simple-func)) |
|
|
|
(define renamed-syms |
|
(map (lambda (_) (gensym "demux")) gensyms)) |
|
(define rename-sym |
|
(let ((renamed (map cons gensyms renamed-syms))) |
|
(lambda (sym) (or (assq-ref renamed sym) sym)))) |
|
(define renamed-kw |
|
(match kw |
|
((aok? (kw name sym) ...) |
|
(cons aok? (map list kw name (map rename-sym sym)))) |
|
(#f #f))) |
|
(define renamed-inits |
|
(map (lambda (init) |
|
(post-order |
|
(lambda (exp) |
|
(match exp |
|
(($ <lexical-ref> src name sym) |
|
(make-lexical-ref src name (rename-sym sym))) |
|
(($ <lexical-set> src name sym exp) |
|
(make-lexical-set src name (rename-sym sym) exp)) |
|
(_ exp))) |
|
init)) |
|
inits)) |
|
(define dispatch-call |
|
(make-call src (make-lexical-ref src func-name simple-sym) |
|
(map (lambda (name sym) |
|
(make-lexical-ref src name sym)) |
|
simple-req renamed-syms))) |
|
(define dispatch-clause |
|
(make-lambda-case src req opt rest renamed-kw renamed-inits |
|
renamed-syms dispatch-call alternate)) |
|
|
|
(values (cons simple-binding bindings) |
|
dispatch-clause)))))) |
|
|
|
(define (demux-lambda exp) |
|
(define (complex-lambda? val) |
|
(match val |
|
(($ <lambda> src meta |
|
($ <lambda-case> src req opt rest kw inits gensyms body alternate)) |
|
(or (pair? opt) rest (pair? kw) alternate)) |
|
(_ #f))) |
|
|
|
(define (demux-binding name gensym val) |
|
(if (complex-lambda? val) |
|
(match val |
|
(($ <lambda> src meta clause) |
|
(call-with-values (lambda () (demux-clause name clause)) |
|
(lambda (extra-bindings clause) |
|
(let ((val (make-lambda src meta clause))) |
|
(append extra-bindings |
|
(list (make-binding name gensym val)))))))) |
|
(list (make-binding name gensym val)))) |
|
|
|
(define (demux-lexically-bound-complex-lambdas exp) |
|
(match exp |
|
(($ <letrec> src in-order? names gensyms vals body) |
|
(match (append-map demux-binding names gensyms vals) |
|
((#(name gensym val) ...) |
|
(make-letrec src in-order? name gensym val body)))) |
|
|
|
(($ <let> src names gensyms vals body) |
|
(if (or-map lambda? vals) |
|
(demux-lexically-bound-complex-lambdas |
|
(make-letrec src #f names gensyms vals body)) |
|
exp)) |
|
|
|
(_ exp))) |
|
|
|
(post-order demux-lexically-bound-complex-lambdas exp)) |
|
|