Skip to content

Instantly share code, notes, and snippets.

@adragomir
Forked from carld/c.scm
Created November 18, 2020 21:57
Show Gist options
  • Save adragomir/78448cf0c9662f987f644e23cb9da12a to your computer and use it in GitHub Desktop.
Save adragomir/78448cf0c9662f987f644e23cb9da12a 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