Fw: [plt-scheme] Struggling with macro...
Hi,
Your original question was
;(machine hello ; (variables (x 1)
; (y 2)
; (z 3)))
;
;to
;(let ([mc (make-mc () () () () ())])
; (add-variable-to-mc x 1)
; (add-variable-to-mc z 3)
; mc)
(BTW, the name 'hello' is not used and I guess that (add-variable-to-mc x 1)
must be (add-variable-to-mc mc x 1))
For this I would write:
(define-syntax machine
(syntax-rules (variables)
((machine name (variables (id v)) ...)
(let ((mc (make-mc () () () () ())))
(add-variable-to-mc mc id v) ...
mc))))
I probably dont grasp your problem. Can you be more specific?
Jos Koot
> ---- Original Message -----
> From: "Paulo J. Matos" <pocm at soton.ac.uk>
> To: "Jos K oot" <jos.koot at telefonica.net>
> Cc: <plt-scheme at list.cs.brown.edu>
> Sent: Wednesday, May 23, 2007 4:08 PM
> Subject: Re: [plt-scheme] Struggling with macro...
>
>
>> On 5/23/07, Jos K oot <jos.koot at telefonica.net> wrote:
>>> Hi
>>> The ellipsis in the inner macro, it is handled by the outer macro. In
>>> order
>>> to push the ellipsis into the inner macro replace it by (... ...) which
>>> is a
>>> kind of quoting an ellipsis within a pattern or template.
>>
>> Yes, I've read about it... so I guess the following should work even
>> though is pretty cryptic:
>> (define-syntax (machine stx)
>> (syntax-case stx ()
>> [(_ mname sexps ...)
>> (identifier? #'mname) ; fender
>> #`(let ([mc (make-mc () () () () ())])
>> #,(with-syntax ([variables (syntax-case #'(sexps ...)
>> (variables)
>> (((variables (name value) (...
>> ...)) (... ...))
>> (andmap identifier? #'(name (...
>> ...)))
>> ((add-variable-to-mc #'mc
>> #'name #'value) (... ...))))])
>> #'variables
>> #'mc))]))
>>
>> Because if (... ...) transforms to ... in the inner macro, the inner
>> macro is:
>> [variables (syntax-case #'(sexps ...) (variables)
>> (((variables (name value) ...)
>> ...)
>> (andmap identifier? #'(name ...))
>> ((add-variable-to-mc #'mc
>> #'name #'value) ...)))]
>>
>> It would probably be easier to dump with-syntax and just define a
>> separate macro for this but I can't for a very simple reason (to which
>> I don't know a workaround), I need to access the mc variable from the
>> macro, so the macro needs to be declared after mc is defined and in
>> the same environment, due to hygiene.
>>
>> Any ideas?
>>
>>> Jos Koot
>>>
>>> ----- Original Message -----
>>> From: "Paulo J. Matos" <pocm at soton.ac.uk>
>>> To: <plt-scheme at list.cs.brown.edu>
>>> Sent: Wednesday, May 23, 2007 2:03 PM
>>> Subject: [plt-scheme] Struggling with macro...
>>>
>>>
>>> > Hi all,
>>> >
>>> > Even though from time to time I can write a macro, unfortunately I
>>> > feel they are not yet clear enough for me.
>>> >
>>> > I'm doing a macro to recognise the following:
>>> > (machine <name>
>>> > (variables (<varname> <value>)
>>> > (<varname> <value>)
>>> > (<varname> <value>)
>>> > etc.)
>>> > ... (other syntax))
>>> >
>>> > I've done some simple structure to keep the information of machine
>>> > macro called mc and function to save information to structure.
>>> > So, macro is something like this:
>>> > (require-for-syntax "mc-utils.scm")
>>> >
>>> > (define-syntax (machine stx)
>>> > (syntax-case stx ()
>>> > [(_ mname sexps ...)
>>> > (identifier? #'mname) ; fender
>>> > #'(let ([mc (make-mc () () () () ())])
>>> > #,(with-syntax ([variables (syntax-case #'(sexps ...)
>>> > (variables)
>>> > ((variables (name value) ...)
>>> > (andmap identifier? #'(name
>>> > ...))
>>> > ((add-variable-to-mc #'name
>>> > #'value) ...)))])
>>> > #'variables
>>> > #'mc))]))
>>> >
>>> > This gives me: syntax: no pattern variables before ellipses in
>>> > template
>>> > in: ...
>>> > in the second ellipsis of with-syntax.
>>> > I want to expand:
>>> > (machine hello
>>> > (variables (x 1)
>>> > (y 2)
>>> > (z 3)))
>>> >
>>> > to
>>> > (let ([mc (make-mc () () () () ())])
>>> > (add-variable-to-mc x 1)
>>> > (add-variable-to-mc y 2)
>>> > (add-variable-to-mc z 3)
>>> > mc)
>>> >
>>> > It seems that no matter what I change I get some kind of wierd error.
>>> > Can you please point me out in the right direction? (sorry for
>>> > presenting such ugly and probably full or error macro).
>>> >
>>> > Cheers,
>>> > --
>>> > Paulo Jorge Matos - pocm at soton.ac.uk
>>> > http://www.personal.soton.ac.uk/pocm
>>> > PhD Student @ ECS
>>> > University of Southampton, UK
>>> > _________________________________________________
>>> > For list-related administrative tasks:
>>> > http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>> >
>>>
>>>
>>>
>>>
>>
>>
>> --
>> Paulo Jorge Matos - pocm at soton.ac.uk
>> http://www.personal.soton.ac.uk/pocm
>> PhD Student @ ECS
>> University of Southampton, UK
>>
>