-
-
Save adragomir/78448cf0c9662f987f644e23cb9da12a 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