masak / alma

ALgoloid with MAcros -- a language with Algol-family syntax where macros take center stage
Artistic License 2.0
137 stars 14 forks source link

The quasi in the example name.alma doesn't need to take a closure #571

Open masak opened 2 years ago

masak commented 2 years ago
macro name(expr) {
    if expr ~~ Q.Postfix.Property {
        expr = expr.property;
    }
    assertType(expr, Q.Identifier);
    return quasi { expr.name };
}

my info = {
    foo: "Bond",
    bar: {
        baz: "James Bond"
    },
};

say(name(info));           # info
say(name(info.foo));       # foo
say(name(info.bar.baz));   # baz

I was re-watching a talk I made in 2018 and I saw this code, and I realized how extravagant return quasi { expr.name } really is. I mean, it works, but at the cost of inserting a property lookup into the macro-expanded code, keeping the expr AST node alive until runtime, just to do that lookup.

Suggest replacing the final return with this:

my name = expr.name;
return quasi { name };