Hello,
I am having some issues with the subtleties of the low-level define-macro system. The problem seems to be that the identifiers created by syntax-rules are not equivalent to the identifiers created by define-macro. I would really appreciate some help on this.
Here is a small example:
(define-syntax multi-let (syntax-rules () ((multi-let (vars ...) body ...) (let* [(vars '()) ...] ;; Declare the variables ($set vars) ... ;; Set them to "initial" body ...))))
(define-macro ($set var) `(set! ,var "initial")) ;; Set the variable to "initial"
And here is what happens when I try to use it:
(multi-let (x y z) x)
*** ERROR IN #<procedure #2> -- Unbound variable: x
Thank you very much! For my use case, I am unable to stick completely to using define-syntax thus have to deal with how to intermix these two systems properly. -Patrick
Afficher les réponses par date
On Sep 27, 2012 6:35 PM, "Patrick Li" patrickli.2001@gmail.com wrote:
Thank you very much! For my use case, I am unable to stick completely to
using define-syntax thus have to deal with how to intermix these two systems properly.
Don't try to mix the two systems in Gambit. That way lies madness.
If you need to break hygiene, Gambit includes a syntax-case implementation you can use with define-syntax.
Here is an example from
http://c2.com/cgi/wiki?DefineSyntax
;; Common-lisp-style define-macro, implemented on top of syntax-case. ;; ;; Note that this is a macro that generates another macro (with the ;; appropriate name). ;; ;; This is the weirdest one. It... ;; * strips all syntax information out of the incoming s-exp, ;; leaving raw symbols. ;; * runs the result through the transformer function ;; * injects that result back into the original scope ;; * returns the final result for processing ;; ;; Note that because we're generating the *entire* result through ;; DATUM->SYNTAX-OBJECT, we don't need to quote or expand anything ;; with SYNTAX. (define-syntax define-macro (lambda (stx) (syntax-case stx () ((_ (macro . args) . body) (syntax (define-macro macro (lambda args . body)))) ((_ macro transformer) (syntax (define-syntax (macro stx2) (let ((v (syntax-object->datum stx2))) (datum->syntax-object stx2 (apply transformer (cdr v))))))))))
Maybe you could use this to redefine Gambit's define-macro.
HTH.
Brad
Black Hole provides this with an pretty-much-as-well-as-can-be-done quality.
2012/9/28 Patrick Li patrickli.2001@gmail.com
Hello,
I am having some issues with the subtleties of the low-level define-macro system. The problem seems to be that the identifiers created by syntax-rules are not equivalent to the identifiers created by define-macro. I would really appreciate some help on this.
Here is a small example:
(define-syntax multi-let (syntax-rules () ((multi-let (vars ...) body ...) (let* [(vars '()) ...] ;; Declare the variables ($set vars) ... ;; Set them to "initial" body ...))))
(define-macro ($set var) `(set! ,var "initial")) ;; Set the variable to "initial"
And here is what happens when I try to use it:
(multi-let (x y z) x)
*** ERROR IN #<procedure #2> -- Unbound variable: x
Thank you very much! For my use case, I am unable to stick completely to using define-syntax thus have to deal with how to intermix these two systems properly. -Patrick
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list