dalekbaldwin / expansion-handlers Goto Github PK
View Code? Open in Web Editor NEWA proof-of-concept sketch of a code-walker-free condition system for macroexpansions.
A proof-of-concept sketch of a code-walker-free condition system for macroexpansions.
I wrote this a little while ago: https://news.ycombinator.com/item?id=9280737 and I thought there had to be some other way to allow two-way information flow between nodes in the syntax tree without using a code walker.
This is basically an extension of a trick I've used a few times before: you can put any data in a symbol-macrolet
or macrolet
binding, not just code that you actually intend to expand somewhere. So a handler can set up a context by stashing some information in a macro binding, and other macros further down the tree can inspect that binding and "signal" a condition by destructively modifying the data stored there. This doesn't halt the current expansion; the inner macros need to return some code that is at least valid enough not to trigger a genuine compile-time error.
An expansion-time analogue of handler-case
can attempt to expand its body inside an flet
binding. Once the region of code where the handler is active is fully expanded, the outer macro that established the handler inspects the environment again to see what was signalled, and either expands into a call to the function defined in flet
if everything was okay, or invokes the actual Lisp-level condition-handling code to attempt a new expansion with whatever additional changes are needed to fix the problem further down the tree. If a condition was signalled within that expansion, no references to the function defined by that expansion are ever inserted in the code, so a compiler should be able to just strip out all the overhead for everything except for the final, completed expansion.
I think you could implement analogues of all the major features of the CL condition system this way, or you could create a condition system with different semantics. I'm not sure how much of the standard CL condition system is useful in this context. You probably don't want to invoke interactive restarts at compile-time, and expansion-handler-bind
wouldn't really accomplish anything you couldn't already do with a simple macrolet
. expansion-catch
wouldn't allow for a notion of expanding code differently depending on whether an expansion-throw
occurred, so it couldn't really react to specific conditions occurring further down the code tree. In runtime code, it can be perfectly okay for the program to keep chugging along even if a sub-computation couldn't finish because of an exceptional situation. When you're writing a set of macros that make up a DSL, that's never acceptable. You need to generate working, reliable code based only on foreseeable, non-erroneous conditions. There may be a way to use this trick to enable a more general notion of compile-time continuations, but I'm not sure how useful that would be without seeing more potential examples of DSLs that could benefit from such a system.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.