Here's a version of your program that performs as you specified:
http://pasterack.org/pastes/91460

But do take a look at the paper, docs [1], or codebase [2], which
turns this idea into complete languages.

[1]: http://docs.racket-lang.org/turnstile/index.html
[2]: https://github.com/stchang/macrotypes

On Wed, Apr 18, 2018 at 9:39 AM, Matthias Felleisen
<matth...@felleisen.org> 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.

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