parens icon indicating copy to clipboard operation
parens copied to clipboard

Implement Builtin Expander (Macro System)

Open spy16 opened this issue 5 years ago • 8 comments

  • [ ] Implement BuiltinExpander that expands Seq based macro-invocation forms.

Need to figure out how a symbol/value would be identified as a macro (Clojure uses a ^:macro tag)

spy16 avatar Sep 09 '20 08:09 spy16

Perhaps we could use an interface to "tag" values as macros?

type Macro interface{
    Macro()
}

We would then wrap a Seq in something like this:

type macro struct{ Seq }

func (macro) Macro() {}

Then we can just type-assert against the Macro type to determine whether macro expansion is required.

lthibault avatar Sep 21 '20 12:09 lthibault

No, we need to tag the value of the operator to indicate macro or not. We don't need to tag the entire Seq form.

For example, in the invocation form (my-macro arg1 arg2), expander need to be able to figure out that my-macro is a macro and then do expansion.

We could introduce a Macro type:

type Macro struct {
    Name string    // macro name.
    Args   []string // argument names.
    Body  Any
}

In above example,

  1. Expander would resolve my-macro symbol to get a value of type Macro. (If it's not of that type, skip expansion)
  2. Create bindings for parameters and the corresponding invocation arguments in the Env. (symbol collision needs to be handled. See gensym).
  3. Then evaluate the m.Body in current Env.
  4. Return the result as the new form.

spy16 avatar Sep 21 '20 13:09 spy16

Ah gotcha!

Ok this makes sense. I’ll have a think.

lthibault avatar Sep 21 '20 17:09 lthibault

@spy16 I'm going to start working on your proposed implementation. Just a heads up so that we don't duplicate any work 🙂.

lthibault avatar Sep 28 '20 22:09 lthibault

@spy16 Ok, so as much as I hate to admit it, I think I've bitten off more than I can chew! I think you probably have a much more complete picture of how macro expansion fits into our current architecture, whereas every attempt I've made so far ends up requiring major changes in other parts of the codebase ... a sure sign that I'm Doing It Wrong! 😅

I'm unassigning myself from this issue, and letting you take over. My latest attempt can be found in the feature/macro branch. If you have a moment to look, I'd welcome your feedback about what I'm doing incorrectly.

Sorry about this 😞

lthibault avatar Sep 29 '20 19:09 lthibault

No worries. Let me take a stab at this. 🙂

spy16 avatar Sep 30 '20 04:09 spy16

I will try to do this over this weekend. sorry for the slow progress here. have been extremely busy.

spy16 avatar Oct 09 '20 11:10 spy16

No worries, and thanks for the update! 🙂

lthibault avatar Oct 09 '20 14:10 lthibault