Hello,

I am looking for an advice on how to write a macro that is aware of the information extracted from syntax objects from another macro that is called "inside" the first one. For instance, let it be the (this) macro that detects if its argument is an integer or float, and let it be the (print) macro that should emit the according printing command -- at compile time.

Here is a stub (not working) to show what I am trying to achieve:

#lang racket/base

(require racket/format
         (for-syntax syntax/parse)
         (for-syntax racket/base))

(define-syntax (print stx)
  (syntax-parse stx
    (((~literal print) expr)
     (if #t ;; need some condition here that knows whether expr is an integer or a float
         (syntax/loc stx
           (displayln (~r expr #:notation 'positional #:precision '(= 2))))
         (syntax/loc stx
           (printf "~a\n" expr))))))

(define-syntax (this stx)
  (syntax-parse stx
    (((~literal this) (expr ...))
     (syntax/loc stx (expr ...))
     )
    (((~literal this) datum)
     (if (exact? (syntax->datum #'datum))
       (printf "we know at compile time that is is an integer\n")
       (printf "we know at compile time that is is a float\n"))
     (syntax/loc stx datum))))


(print (this (this (this 123))))
(print (this (this (this 456.0))))


How do I achieve that (print) knows which number is in (this) down there?
I thought about (syntax-property), but it does not work, either because I am misapplying it, or because of the way the syntax expander works.

I also tried to (expand-syntax) to get the syntax expander work bottom-up, but eventually decided that it can not work too, or I do not understand how it should work.

The other option that I see is "manual" processing of the whole syntax tree after the (syntax-parse) did its job. But it seems a bit extra for the task. Is it the only way?

Best regards,

Dmitry


--
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to