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!! ; TODO: really need export-all for these cyclone libs!!
(export (export
define-syntax? define-syntax?
macro?
macro-expand
) )
(begin (begin
;; Macro section ;; Macro section
@ -11,6 +13,23 @@
(define (define-syntax? exp) (define (define-syntax? exp)
(tagged-list? '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: get macro name, transformer
; TODO: base off of syntactic closures instead of ER macros?? ; TODO: base off of syntactic closures instead of ER macros??
; TODO: let-syntax forms ; TODO: let-syntax forms

View file

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