spy16 / parens

Parens is a highly flexible and embeddable LISP toolkit. :computer:
33 stars 3 forks source link

Implement Builtin Expander (Macro System) #15

Open spy16 opened 4 years ago

spy16 commented 4 years ago

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

lthibault commented 4 years ago

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.

spy16 commented 4 years ago

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.
lthibault commented 4 years ago

Ah gotcha!

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

lthibault commented 3 years ago

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

lthibault commented 3 years ago

@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 😞

spy16 commented 3 years ago

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

spy16 commented 3 years ago

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

lthibault commented 3 years ago

No worries, and thanks for the update! 🙂