My macro’s primary role is to create a dictionary mapping symbols to Nodes.
Each Node has an associated action (passed in with the curly braces). The
actions themselves are Exprs, which need to be encapsulated as functions.

that’s not something a macro can do (directly). and it's also why a macro
is different from a function.

specifically, a function’s purpose is to change one object into another
thing. on the other hand, a macro is for transforming code into more code.
so the return value from the macro isn’t a new object, but the code that
describes how to to make that new object. so for the example above, rather
than directly assigning expr.args[2] into rule.action, you would return the
equivalent expression (quote; rule.action = $(expr.args[2]); end) that do
that operation at runtime

On Sun, Mar 22, 2015 at 11:01 PM Abe Schneider [email protected]
<http://mailto:[email protected]> wrote:

Sorry, when I said AST, I meant the code that gets executed. A macro, at
> least the way I think about it, is inserting the resulting Expr into the
> main AST.
>
> My macro's primary role is to create a dictionary mapping symbols to
> `Node`s. Each Node has an associated action (passed in with the curly
> braces). The actions themselves are `Expr`s, which need to be encapsulated
> as functions.
>
> I was hoping initially that the `Expr` for the action could be converted
> inline with the creation of the `Node`, as it would save the need of a lot
> of book keeping. Thus, the: `rule.action =
> insert_action_expr(expr.args[2])`.
>
> Another solution might to do two passes, where in the second pass the
> rule's `Expr`s are converted. Unfortunately, that seems less elegant than
> being able to convert an `Expr` to a function.
>
>
> On Sunday, March 22, 2015 at 9:39:30 AM UTC-4, Toivo Henningsson wrote:
>>
>> Maybe there is a confusion of terms here? An Expr is the way to represent
>> an AST in Julia (some very simple ASTs are instead represented by
>> ints/floats/strings/symbols). So when you talk about inserting an Expr into
>> an AST it seems to me that all it means is to make an Expr where some
>> subtree is given by another Expr.
>>
>> Macros are really only a way to save typing. Consider a few cases: what
>> is the input AST, and what is the output AST that you want to generate in
>> each case. From there, you should be able to figure out how to write the
>> code that does the transformation.
>
> ​

Reply via email to