parens
parens copied to clipboard
Implement Builtin Expander (Macro System)
- [ ] Implement
BuiltinExpanderthat expandsSeqbased macro-invocation forms.
Need to figure out how a symbol/value would be identified as a macro (Clojure uses a ^:macro tag)
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.
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,
- Expander would resolve
my-macrosymbol to get a value of typeMacro. (If it's not of that type, skip expansion) - Create bindings for parameters and the corresponding invocation arguments in the Env. (symbol collision needs to be handled. See gensym).
- Then evaluate the
m.Bodyin currentEnv. - Return the result as the new form.
Ah gotcha!
Ok this makes sense. I’ll have a think.
@spy16 I'm going to start working on your proposed implementation. Just a heads up so that we don't duplicate any work 🙂.
@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 😞
No worries. Let me take a stab at this. 🙂
I will try to do this over this weekend. sorry for the slow progress here. have been extremely busy.
No worries, and thanks for the update! 🙂