1
votes

I have some s-expression (let ((whatever (foo (bar 4) (bar 5)))) ...) (as a result of (read "(whatever (foo (bar 4) (bar 5)))"). Now I would like to transform it with a custom macro that expands for example foo into the macro expression.

What is the mechanism to apply macros to given S-Expressions? Note that I do not want this substitution to happen at compiletime of the .scm file but but after reading in the s expression.

Furthermore, I don't want to use eval, since these S-Expressions are not Scheme code. I would not want to expand a let statement in that given s-expression, only foo.

Basically I thought that Scheme would contain a sufficiently flexible Tree-Transformation Framework that would enable me to use specific macros for manipulating any kind of S-expressions at any time. In the meantime however, I learned that Scheme's macros are not first class objects and cannot be used this way, as also implied by Sylwester.

1
What you want to do here isn't exactly clear. Are you trying to re-invent quasi-quote and unquote? Walk us through with some more code. - WorBlux
You want to expand macros, at read time, from data that you get during runtime? That sounds a tad impossible - Daniel Gratzer

1 Answers

2
votes

I presume you want to expand data and end up with data. If that is not the case, please update your question with more information.

You'll need to run your own macro-implementation. Let's say you create macros with make-macro and expand them with expand-macros such that only expands you user defined macros.

Then the solution would be: (let ((whatever (expand-macros (read)))) ...)

There are lots of macro-implementation you can look at. Alexpander is one based on on hygienic syntax-rules while a classical unygenic defmacro + expander would be very easy. However, it would impossible to make if completely safe. Imagine your foo:

(let ((foo +))
  (foo 5 6))

This should override you definition of foo with + in the let. To fix this your expander needs to know all language constructs of Scheme that you are going to use and be able to understand that it's bindings can become shadowed. All in all you'll almost need it to be an interpreter for Scheme to be able to see what to expand or not. It's not so easy, but it's doable.