Created
July 15, 2017 00:21
-
-
Save carld/4e0a51385bd7180226a9f9e59b4e7687 to your computer and use it in GitHub Desktop.
The compiler from my blog article
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 emit printf) | |
(define (compile exp) | |
(cond | |
[(fixnum? exp) (emit "push ~a~%" exp)] | |
[(eq? '+ (car exp)) (begin | |
(compile (cadr exp)) | |
(compile (caddr exp)) | |
(emit "pop eax~%") | |
(emit "pop ebx~%") | |
(emit "add eax, ebx~%") | |
(emit "push eax~%"))])) | |
(define (compile-top exp) | |
(emit "global start~%") | |
(emit "start:~%") | |
(compile exp) | |
(emit "sub esp, 4~%") ; Mach-O special | |
(emit "mov eax, 0x1~%") | |
(emit "int 0x80~%")) | |
;(compile-top '(+ 1 2)) | |
(compile-top '(+ (+ 21 21) (+ 64 64))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment