Create a gist now

Instantly share code, notes, and snippets.

traditional lisp macros
;; outlet code for implementing traditional macro expansion
;; macros
(define (expand form)
((variable? form) form)
((literal? form) form)
((macro? (car form))
(expand ((macro-function (car form)) form)))
((eq? (car form) 'quote)
((eq? (car form) 'lambda)
`(lambda ,(car (cdr form))
,@(map expand (cdr (cdr form)))))
(else (map expand form))))
(define _macros_ {})
(define (macro-function name)
(ref _macros_ (symbol->string name)))
(define (install-macro name func)
(put! _macros_ (symbol->string name) func))
(define (macro? name)
(not (eq? (ref _macros_ (symbol->string name))
;; compiler
(define (read src)
(reader grammar src '[begin])))
(install-macro 'define (lambda (form)
`(define* ,(car (cdr form))
,@(cdr (cdr form)))))
(let ((src (fs.readFileSync "example.ol" "utf-8")))
(pretty (expand (read src))))
;; (define (foo x y z)
;; (+ x y z))
;; expand to:
;; (define* (foo x y z)
;; (+ x y z))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment