Skip to content

Instantly share code, notes, and snippets.

@lazear
Created September 11, 2017 21:16
Show Gist options
  • Save lazear/259c7f6eccdf488e7bfe46442bbab4e5 to your computer and use it in GitHub Desktop.
Save lazear/259c7f6eccdf488e7bfe46442bbab4e5 to your computer and use it in GitHub Desktop.
(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