Expanding Macros

34 views
Skip to first unread message

Thomas Del Vecchio

unread,
Jul 9, 2020, 9:30:38 PM7/9/20
to Racket Users
I'm trying to work through expanding a macro to pass the result into another macro. I've attached and pasted the spec below that is my toy example. I've tried using expand, but it gives me "(C (#%app (#%top . B) (quote 1) (quote 2)))" instead of "(C (in 1 2))". I've also tried many other things, but none seem to do what I expect/want. Any help?

#lang racket
(require (for-syntax syntax/parse))
 
; I want A to take a (B x y) expand it to be passed to C.
; (A (B 1 2))
; -> (C (in 1 2))
(define-syntax (A stx)
  (syntax-case stx (A)
    [(A b)
     (with-syntax ([expanded-b #'b]) ; <- what goes here?
     #'(C expanded-b))]))
 
(define-syntax (B stx)
  (syntax-case stx (B)
    [(B x y)
     #'(in x y)]))
 
(define-syntax (C stx)
  (syntax-case stx (C in)
    [(C (in x y))
     #'(printf "~a ~a ~n" x y)]))
 
(A (B 1 2)) ; expect to print "1 2 ~n"

Thanks!
testing-macros.rkt

William J. Bowman

unread,
Jul 9, 2020, 9:44:08 PM7/9/20
to Thomas Del Vecchio, Racket Users
You might want `local-expand`. It can still give you something that is expanded
further than you want, unless you know exactly which identifier you want to stop
at, which you can specify in the stop list.
https://docs.racket-lang.org/reference/stxtrans.html#%28def._%28%28quote._~23~25kernel%29._local-expand%29%29

--
William J. Bowman
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/dd6d4e30-9fd9-4e29-9d36-3a4b0935fc4ao%40googlegroups.com.


Thomas Del Vecchio

unread,
Jul 9, 2020, 9:52:43 PM7/9/20
to Racket Users
Thanks so much! Using 

(with-syntax ([expanded-b (datum->syntax #'b (local-expand #'b 'expression #f))])

worked out great!
> To unsubscribe from this group and stop receiving emails from it, send an email to racket...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages