|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (language cps) |
|
#:use-module (ice-9 match) |
|
#:use-module (srfi srfi-9) |
|
#:use-module (srfi srfi-9 gnu) |
|
#:use-module (srfi srfi-11) |
|
#:export ( |
|
$arity |
|
make-$arity |
|
|
|
|
|
$kreceive $kargs $kfun $ktail $kclause |
|
|
|
|
|
$continue $branch $switch $prompt $throw |
|
|
|
|
|
$const $prim $fun $rec $const-fun $code |
|
$call $callk $calli $primcall $values |
|
|
|
|
|
build-cont build-term build-exp |
|
rewrite-cont rewrite-term rewrite-exp |
|
|
|
|
|
parse-cps unparse-cps)) |
|
|
|
|
|
(define-syntax define-record-type* |
|
(lambda (x) |
|
(define (id-append ctx . syms) |
|
(datum->syntax ctx (apply symbol-append (map syntax->datum syms)))) |
|
(syntax-case x () |
|
((_ name field ...) |
|
(and (identifier? #'name) (and-map identifier? #'(field ...))) |
|
(with-syntax ((cons (id-append #'name #'make- #'name)) |
|
(pred (id-append #'name #'name #'?)) |
|
((getter ...) (map (lambda (f) |
|
(id-append f #'name #'- f)) |
|
#'(field ...)))) |
|
#'(define-record-type name |
|
(cons field ...) |
|
pred |
|
(field getter) |
|
...)))))) |
|
|
|
(define-syntax-rule (define-cps-type name field ...) |
|
(begin |
|
(define-record-type* name field ...) |
|
(set-record-type-printer! name print-cps))) |
|
|
|
(define (print-cps exp port) |
|
(format port "#<cps ~S>" (unparse-cps exp))) |
|
|
|
|
|
(define-record-type* $arity req opt rest kw allow-other-keys?) |
|
|
|
|
|
(define-cps-type $kreceive arity kbody) |
|
(define-cps-type $kargs names syms term) |
|
(define-cps-type $kfun src meta self ktail kentry) |
|
(define-cps-type $ktail) |
|
(define-cps-type $kclause arity kbody kalternate) |
|
|
|
|
|
(define-cps-type $continue k src exp) |
|
(define-cps-type $branch kf kt src op param args) |
|
(define-cps-type $switch kf kt* src arg) |
|
(define-cps-type $prompt k kh src escape? tag) |
|
(define-cps-type $throw src op param args) |
|
|
|
|
|
(define-cps-type $const val) |
|
(define-cps-type $prim name) |
|
(define-cps-type $fun body) |
|
(define-cps-type $rec names syms funs) |
|
(define-cps-type $const-fun label) |
|
(define-cps-type $code label) |
|
(define-cps-type $call proc args) |
|
(define-cps-type $callk k proc args) |
|
(define-cps-type $calli args callee) |
|
(define-cps-type $primcall name param args) |
|
(define-cps-type $values args) |
|
|
|
(define-syntax build-arity |
|
(syntax-rules (unquote) |
|
((_ (unquote exp)) exp) |
|
((_ (req opt rest kw allow-other-keys?)) |
|
(make-$arity req opt rest kw allow-other-keys?)))) |
|
|
|
(define-syntax build-cont |
|
(syntax-rules (unquote $kreceive $kargs $kfun $ktail $kclause) |
|
((_ (unquote exp)) |
|
exp) |
|
((_ ($kreceive req rest kargs)) |
|
(make-$kreceive (make-$arity req '() rest '() #f) kargs)) |
|
((_ ($kargs (name ...) (unquote syms) body)) |
|
(make-$kargs (list name ...) syms (build-term body))) |
|
((_ ($kargs (name ...) (sym ...) body)) |
|
(make-$kargs (list name ...) (list sym ...) (build-term body))) |
|
((_ ($kargs names syms body)) |
|
(make-$kargs names syms (build-term body))) |
|
((_ ($kfun src meta self ktail kentry)) |
|
(make-$kfun src meta self ktail kentry)) |
|
((_ ($ktail)) |
|
(make-$ktail)) |
|
((_ ($kclause arity kbody kalternate)) |
|
(make-$kclause (build-arity arity) kbody kalternate)))) |
|
|
|
(define-syntax build-term |
|
(syntax-rules (unquote $continue $branch $switch $prompt $throw) |
|
((_ (unquote exp)) |
|
exp) |
|
((_ ($continue k src exp)) |
|
(make-$continue k src (build-exp exp))) |
|
((_ ($branch kf kt src op param (unquote args))) |
|
(make-$branch kf kt src op param args)) |
|
((_ ($branch kf kt src op param (arg ...))) |
|
(make-$branch kf kt src op param (list arg ...))) |
|
((_ ($branch kf kt src op param args)) |
|
(make-$branch kf kt src op param args)) |
|
((_ ($switch kf kt* src arg)) |
|
(make-$switch kf kt* src arg)) |
|
((_ ($prompt k kh src escape? tag)) |
|
(make-$prompt k kh src escape? tag)) |
|
((_ ($throw src op param (unquote args))) |
|
(make-$throw src op param args)) |
|
((_ ($throw src op param (arg ...))) |
|
(make-$throw src op param (list arg ...))) |
|
((_ ($throw src op param args)) |
|
(make-$throw src op param args)))) |
|
|
|
(define-syntax build-exp |
|
(syntax-rules (unquote |
|
$const $prim $fun $rec $const-fun $code |
|
$call $callk $calli $primcall $values) |
|
((_ (unquote exp)) exp) |
|
((_ ($const val)) (make-$const val)) |
|
((_ ($prim name)) (make-$prim name)) |
|
((_ ($fun kentry)) (make-$fun kentry)) |
|
((_ ($rec names gensyms funs)) (make-$rec names gensyms funs)) |
|
((_ ($const-fun k)) (make-$const-fun k)) |
|
((_ ($code k)) (make-$code k)) |
|
((_ ($call proc (unquote args))) (make-$call proc args)) |
|
((_ ($call proc (arg ...))) (make-$call proc (list arg ...))) |
|
((_ ($call proc args)) (make-$call proc args)) |
|
((_ ($callk k proc (unquote args))) (make-$callk k proc args)) |
|
((_ ($callk k proc (arg ...))) (make-$callk k proc (list arg ...))) |
|
((_ ($callk k proc args)) (make-$callk k proc args)) |
|
((_ ($calli (unquote args) callee)) (make-$calli args callee)) |
|
((_ ($calli (arg ...) callee)) (make-$calli (list arg ...) callee)) |
|
((_ ($calli args callee)) (make-$calli args callee)) |
|
((_ ($primcall name param (unquote args))) (make-$primcall name param args)) |
|
((_ ($primcall name param (arg ...))) (make-$primcall name param (list arg ...))) |
|
((_ ($primcall name param args)) (make-$primcall name param args)) |
|
((_ ($values (unquote args))) (make-$values args)) |
|
((_ ($values (arg ...))) (make-$values (list arg ...))) |
|
((_ ($values args)) (make-$values args)))) |
|
|
|
(define-syntax-rule (rewrite-cont x (pat cont) ...) |
|
(match x |
|
(pat (build-cont cont)) ...)) |
|
(define-syntax-rule (rewrite-term x (pat term) ...) |
|
(match x |
|
(pat (build-term term)) ...)) |
|
(define-syntax-rule (rewrite-exp x (pat body) ...) |
|
(match x |
|
(pat (build-exp body)) ...)) |
|
|
|
(define (parse-cps exp) |
|
(define (src exp) |
|
(let ((props (source-properties exp))) |
|
(and (pair? props) props))) |
|
(match exp |
|
|
|
(('kreceive req rest k) |
|
(build-cont ($kreceive req rest k))) |
|
(('kargs names syms body) |
|
(build-cont ($kargs names syms ,(parse-cps body)))) |
|
(('kfun meta self ktail kentry) |
|
(build-cont ($kfun (src exp) meta self ktail kentry))) |
|
(('ktail) |
|
(build-cont ($ktail))) |
|
(('kclause (req opt rest kw allow-other-keys?) kbody) |
|
(build-cont ($kclause (req opt rest kw allow-other-keys?) kbody #f))) |
|
(('kclause (req opt rest kw allow-other-keys?) kbody kalt) |
|
(build-cont ($kclause (req opt rest kw allow-other-keys?) kbody kalt))) |
|
|
|
|
|
(('continue k exp) |
|
(build-term ($continue k (src exp) ,(parse-cps exp)))) |
|
(('branch kf kt op param arg ...) |
|
(build-term ($branch kf kt (src exp) op param arg))) |
|
(('switch kf (kt* ...) arg) |
|
(build-term ($switch kf kt* (src exp) arg))) |
|
(('prompt k kh escape? tag) |
|
(build-term ($prompt k kh (src exp) escape? tag))) |
|
(('throw op param arg ...) |
|
(build-term ($throw (src exp) op param arg))) |
|
|
|
|
|
(('unspecified) |
|
(build-exp ($const *unspecified*))) |
|
(('const exp) |
|
(build-exp ($const exp))) |
|
(('prim name) |
|
(build-exp ($prim name))) |
|
(('fun kbody) |
|
(build-exp ($fun kbody))) |
|
(('const-fun k) |
|
(build-exp ($const-fun k))) |
|
(('code k) |
|
(build-exp ($code k))) |
|
(('rec (name sym fun) ...) |
|
(build-exp ($rec name sym (map parse-cps fun)))) |
|
(('call proc arg ...) |
|
(build-exp ($call proc arg))) |
|
(('callk k proc arg ...) |
|
(build-exp ($callk k proc arg))) |
|
(('calli arg ... callee) |
|
(build-exp ($calli arg callee))) |
|
(('primcall name param arg ...) |
|
(build-exp ($primcall name param arg))) |
|
(('values arg ...) |
|
(build-exp ($values arg))) |
|
(_ |
|
(error "unexpected cps" exp)))) |
|
|
|
(define (unparse-cps exp) |
|
(match exp |
|
|
|
(($ $kreceive ($ $arity req () rest () #f) k) |
|
`(kreceive ,req ,rest ,k)) |
|
(($ $kargs names syms body) |
|
`(kargs ,names ,syms ,(unparse-cps body))) |
|
(($ $kfun src meta self ktail kentry) |
|
`(kfun ,meta ,self ,ktail ,kentry)) |
|
(($ $ktail) |
|
`(ktail)) |
|
(($ $kclause ($ $arity req opt rest kw allow-other-keys?) kbody kalternate) |
|
`(kclause (,req ,opt ,rest ,kw ,allow-other-keys?) ,kbody |
|
. ,(if kalternate (list kalternate) '()))) |
|
|
|
|
|
(($ $continue k src exp) |
|
`(continue ,k ,(unparse-cps exp))) |
|
(($ $branch kf kt src op param args) |
|
`(branch ,kf ,kt ,op ,param ,@args)) |
|
(($ $switch kf kt* src arg) |
|
`(switch ,kf ,kt* ,arg)) |
|
(($ $prompt k kh src escape? tag) |
|
`(prompt ,k ,kh ,escape? ,tag)) |
|
(($ $throw src op param args) |
|
`(throw ,op ,param ,@args)) |
|
|
|
|
|
(($ $const val) |
|
(if (unspecified? val) |
|
'(unspecified) |
|
`(const ,val))) |
|
(($ $prim name) |
|
`(prim ,name)) |
|
(($ $fun kbody) |
|
`(fun ,kbody)) |
|
(($ $const-fun k) |
|
`(const-fun ,k)) |
|
(($ $code k) |
|
`(code ,k)) |
|
(($ $rec names syms funs) |
|
`(rec ,@(map (lambda (name sym fun) |
|
(list name sym (unparse-cps fun))) |
|
names syms funs))) |
|
(($ $call proc args) |
|
`(call ,proc ,@args)) |
|
(($ $callk k proc args) |
|
`(callk ,k ,proc ,@args)) |
|
(($ $calli args callee) |
|
`(callk ,@args ,callee)) |
|
(($ $primcall name param args) |
|
`(primcall ,name ,param ,@args)) |
|
(($ $values args) |
|
`(values ,@args)) |
|
(_ |
|
(error "unexpected cps" exp)))) |
|
|