WIP - macros

This commit is contained in:
Justin Ethier 2015-08-06 22:03:54 -04:00
parent cbb969221a
commit 9c447a384a
2 changed files with 47 additions and 27 deletions

View file

@ -4,6 +4,8 @@
; TODO: really need export-all for these cyclone libs!!
(export
define-syntax?
macro?
macro-expand
)
(begin
;; Macro section
@ -11,6 +13,23 @@
(define (define-syntax? exp)
(tagged-list? 'define-syntax exp))
(define (macro? exp defined-macros) (assoc (car exp) defined-macros))
(define (macro-expand exp defined-macros)
(let ((macro (assoc (car exp) defined-macros)))
;; assumes ER macro
(if macro
((cdr macro)
;exp
; could be a raw lambda, if that is the case try quoting it
(if (procedure? (cdr macro))
exp
(list 'quote exp))
(lambda (sym) ;; TODO: not good enough, need to actually rename, and keep same results if
sym) ;; the same symbol is renamed more than once
(lambda (sym-a sym-b) ;; TODO: the compare function from exrename.
(eq? sym-a sym-b))) ;; this may need to be more sophisticated
exp))) ;; TODO: error instead??
; TODO: get macro name, transformer
; TODO: base off of syntactic closures instead of ER macros??
; TODO: let-syntax forms

View file

@ -1,30 +1,31 @@
(import (scheme eval))
(eval '((lambda (expr rename compare) (cond ((null? (cdr expr))) ((null? (cddr expr)) (cadr expr)) (else (list (rename (quote if)) (cadr expr) (cons (rename (quote and)) (cddr expr)) #f)))) (test 1 2 3) (lambda (x) x) '()))
;(eval '((lambda (x) x) 1))
;(import (scheme base)
; (scheme write))
;(import (scheme eval) (scheme write))
;
;;(define-syntax test
;; (er-macro-transformer
;; (lambda (expr rename compare)
;; `((lambda ()
;; (write "testing")
;; (write (quote ,(cdr expr))))))))
;;
;; WTF is the macro unable to be evaluated when the same code works as part of *defined-macros*???
;;
;(define-syntax test
;(eval '((lambda (expr rename compare) (cond ((null? (cdr expr)) #t) ((null? (cddr expr)) (cadr expr)) (else (list (rename (quote if)) (cadr expr) (cons (rename (quote and)) (cddr expr)) #f)))) '(test 1 2 3) (lambda (x) x) '()))
;;;(eval '((lambda (x) x) 1))
;
;
(import (scheme base)
(scheme write))
;(define-syntax test
; (er-macro-transformer
; (lambda (expr rename compare)
; (cond ((null? (cdr expr)))
; ((null? (cddr expr)) (cadr expr))
; (else (list (rename 'if) (cadr expr)
; (cons (rename 'and) (cddr expr))
; #f))))))
; (lambda (expr rename compare)
; `((lambda ()
; (write "testing")
; (write (quote ,(cdr expr))))))))
;
;(test 1 2 3)
;;(test 'done)
;'done
; WTF is the macro unable to be evaluated when the same code works as part of *defined-macros*???
;
(define-syntax test
(er-macro-transformer
(lambda (expr rename compare)
(cond ((null? (cdr expr)) #t)
; (cond ((null? (cdr expr)))
((null? (cddr expr)) (cadr expr))
(else (list (rename 'if) (cadr expr)
(cons (rename 'and) (cddr expr))
#f))))))
(test 1 2 3)
;(test 'done)
'done