Matthias,

Thank you for the reference. It turns out that I should have looked into
local-expand rather than expand-syntax.

Out of the two options (inserting type info into syntax or keeping
it in the syntax-property) I chose the latter, because I like
thinking of them as properties rather than syntax.


Stephen, thank you. I actually wrote a similar program myself
before I received yours. It is similar. I put it here for
the record. One difference is that it uses syntax-track-origin
to handle nested (this).




#lang racket/base

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

(define-syntax (print stx)
  (syntax-parse stx
    (((~literal print) expr)
     (with-syntax ((expanded (local-expand #'expr 'expression '())))
       (case (syntax-property #'expanded 'number-type)
         ((float)
          (syntax/loc stx
(displayln (~r expanded #:notation 'positional #:precision '(= 2)))))
         ((integer)
          (syntax/loc stx
            (printf "~a\n" expanded))))))))

(define-syntax (this stx)
  (syntax-parse stx
    (((~literal this) (expr ...))
     (with-syntax ((expanded (local-expand #'(expr ...) 'expression '())))
(syntax-track-origin (syntax/loc stx expanded) (car (syntax-e #'(expr ...))) #'this)))
    (((~literal this) datum)
     (if (exact? (syntax->datum #'datum))
         (begin
           (printf "we know at compile time that it is an integer\n")
           (syntax-property (syntax/loc stx datum) 'number-type 'integer))
         (begin
           (printf "we know at compile time that it is a float\n")
(syntax-property (syntax/loc stx datum) 'number-type 'float))))))

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


Regards,

Dmitry






On 18.04.2018 16:39, Matthias Felleisen wrote:

You want to look at Stephen's Turnstile, a DSL for making typed DSLs and 
macros. Like all type systems, this is exactly what it does.

Here is the link to the paper:

  https://www2.ccs.neu.edu/racket/pubs/#popl17-ckg

It obscures what you need, which is a combination of local-expand, syntax-parse 
and its various hooks. The rough idea is to locally expand a given phrase and 
to expand in such a way that macros return two pieces of information: the 
expanded syntax and the additional property. You can try to encode this with

        #'(begin expanded-code property)

or just use a syntax-property, whatever fits best. When the local-expand 
returns, take apart the macro and re-do the same thing.

Details in the implementation.

You may also wish to look at the implementation of Alexis’ Hackett.






On Apr 18, 2018, at 8:40 AM, Dmitry Pavlov <dpav...@iaaras.ru> wrote:

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.


--
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