From 9c447a384a5dc6f3de7cb314ca16f9c784defd45 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Thu, 6 Aug 2015 22:03:54 -0400 Subject: [PATCH] WIP - macros --- scheme/cyclone/macros.sld | 19 ++++++++++++++ test2.scm | 55 ++++++++++++++++++++------------------- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/scheme/cyclone/macros.sld b/scheme/cyclone/macros.sld index 4a2fcf95..a1dc40d6 100644 --- a/scheme/cyclone/macros.sld +++ b/scheme/cyclone/macros.sld @@ -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 diff --git a/test2.scm b/test2.scm index d066bf1c..04cf1b28 100644 --- a/test2.scm +++ b/test2.scm @@ -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