user2162871
user2162871

Reputation: 409

racket expanding a macro which is in quoted list form

I have a macro

(define-syntax-rule (with tag body)
    (string-append (format "<span class=\"~s\">" 'tag)
                    body
                    (format "</span>")))

> (display (with p "some text"))
<span class="p">some text</span>

which is the desired result

How do I "macro-expand" and then evaluate the list '(with p "some text")? This is an example what is returned when I read a file of these forms with the intention of expanding them all.

eval works, but I know that's not the way to go.

I know there are a number of html-templating solutions available - but the target here is not quite HTML - don't ask! :)

Thanks in advance

Upvotes: 0

Views: 228

Answers (1)

Martin Půda
Martin Půda

Reputation: 7568

Racket doesn't have any built-in function like this, but it's simple to add it:

(define (macroexpand form)
  (syntax->datum
     (expand-to-top-form
      form)))

Example:

> (macroexpand '(with p "some text"))
'(string-append (format "<span class=\"~s\">" 'p) "some text" (format "</span>"))

Upvotes: 2

Related Questions