masak / bel

An interpreter for Bel, Paul Graham's Lisp language
GNU General Public License v3.0
27 stars 1 forks source link

Get rid of the Perl-based bquote expander #388

Closed masak closed 3 years ago

masak commented 3 years ago

I don't think I attempted this already. Right now, the bquote expander is only used as part of generating the globals. We could easily do the same with the built-in Bel bquote expander, and save ourselves some Perl code.

masak commented 3 years ago

We could easily do the same with the built-in Bel bquote expander, and save ourselves some Perl code.

I tried this, and I'm no longer sure it's possible, as stated.

bquote is a macro. The way the Bel specification seems to intend it to be used is at evaluation time, as macros usually are. Currently we don't do that in the generated globals — we get rid of them at "compile time", during the generation itself. This, it should be said, speeds things up considerably.

But replacing the Perl code with which we do this with (a wrapper which walks the entire expression and calls) the bquote global introduces one important and unintended consequence: when Bel evaluates a macro invocation, it does the macro expansion, and then evaluates the result. We really don't want that last bit.

Maybe if Bel had a macroexpand-1 function, that could help in this case...

...Wait a minute...

> (bquote (list 'lit 'clo scope ',parms ',(car body)))
Error: ('unboundb parms)
> bquote
(lit mac (lit clo nil (e) ((list (quote lit) (quote clo) scope (quote ((sub change))) (quote (if change sub (list (quote quote) e)))) (bqex e nil))))
> (3 bquote)
(lit clo nil (e) ((list (quote lit) (quote clo) scope (quote ((sub change))) (quote (if change sub (list (quote quote) e)))) (bqex e nil)))
> (def macroexpand-1 (m e) ((3 m) e))
> (macroexpand-1 bquote (list 'lit 'clo scope ',parms ',(car body)))
(cons (quote lit) (cons (quote clo) (cons nil (cons parms (cons (car body) nil)))))

Well! That was easy. Also probably the first tricky thing I've solved in Bel while talking aloud to myself in a GitHub issue.

masak commented 3 years ago

Hah, but the result still had macros nested inside it, notably spa. Ok, giving up on this way of doing things.

Closing.