Created
November 3, 2012 15:47
-
-
Save DanielCollins/4007700 to your computer and use it in GitHub Desktop.
This file contains 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 addr 0) | |
(define (get-next-addr) | |
addr) | |
(define (emit inst) | |
(display addr) | |
(display ": ") | |
(display inst) | |
(newline) | |
(set! addr (+ addr 1)) | |
(- addr 1)) | |
(define (sum list) | |
(define (loop in accumulator) | |
(if (null? in) | |
accumulator | |
(loop (cdr in) (+ accumulator (car in))))) | |
(loop list 0)) | |
(define (compile-tail-pair exp) | |
(define (do-arg arg) | |
(compile-arg-form arg (+ (get-next-addr) (length-compile-arg-form arg)))) | |
(define proc (car exp)) | |
(define args (cdr exp)) | |
(if (null? args) | |
(emit `(JMP ,(car exp))) | |
(let ((entry-point (do-arg (car args)))) | |
(map do-arg (cdr args)) | |
(emit `(JMP ,(car exp))) | |
entry-point))) | |
(define (length-compile-tail-pair exp) | |
(define args (cdr exp)) | |
(if (null? args) | |
1 | |
(+ 1 (sum (map length-compile-arg-form args))))) | |
(define (compile-arg-pair exp return-address) | |
(let ((entry-point (emit `(PSH ,return-address)))) | |
(compile-tail-pair exp) | |
entry-point)) | |
(define (length-compile-arg-pair exp) | |
(+ (length-compile-tail-pair exp) 1)) | |
(define (compile-arg-form exp return-address) | |
(if (pair? exp) | |
(compile-arg-pair exp return-address) | |
(compile-atom exp))) | |
(define (length-compile-arg-form exp) | |
(if (pair? exp) | |
(length-compile-arg-pair exp) | |
(length-compile-atom exp))) | |
(define (compile-atom exp) | |
(emit `(PSH ,exp))) | |
(define (length-compile-atom exp) | |
1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment