Created
September 11, 2017 21:16
-
-
Save lazear/259c7f6eccdf488e7bfe46442bbab4e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (transform f) | |
"Transform a lambda function into a STACKREF form" | |
(define (lambda-args f) (cadr f)) | |
(define (lambda-body f) (caddr f)) | |
(define (deep-map func list) | |
"Recursively map function onto sublists" | |
(map | |
(lambda (x) | |
(if (pair? x) | |
(deep-map func x) | |
(func x))) list)) | |
(define (swap sym refs) | |
(let ((r (assoc sym refs))) | |
(if r | |
(cdr r) | |
sym))) | |
(let ((args '()) | |
(body '())) | |
(let loop ((a (lambda-args f)) (x 0)) | |
(if (not (null? a)) | |
(begin | |
(set! args (cons (cons (car a) `(STACKREF ,x)) args)) | |
(loop (cdr a) (+ 1 x))))) | |
(deep-map (lambda (x) (swap x args)) (lambda-body f)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment