Skip to content

Instantly share code, notes, and snippets.

@carld
Created July 15, 2017 00:21
Show Gist options
  • Save carld/4e0a51385bd7180226a9f9e59b4e7687 to your computer and use it in GitHub Desktop.
Save carld/4e0a51385bd7180226a9f9e59b4e7687 to your computer and use it in GitHub Desktop.
The compiler from my blog article
(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