snow-deprecated icon indicating copy to clipboard operation
snow-deprecated copied to clipboard

Expose parser to runtime

Open judofyr opened this issue 16 years ago • 4 comments
trafficstars

This would be pretty nifty:

ast: AST.parse("a: 1 + 1")
// Do funky stuff with ast, like replace 1 with 2.
ast()
a = 4

This will allow us write code analysys and other stuff like that in Snow. Even better if we could turn a closure to an AST-object, but that might be more difficult...

judofyr avatar Jun 28 '09 15:06 judofyr

There's no technical reason why we shouldn't be able to simply save the AST subtree for a function during compilation, so it could be accessed later.

It could also be interesting to expose the Assemblers, so one could write inline assembly in Snow code, without any overhead at all.

simonask avatar Jun 28 '09 15:06 simonask

If we had a class similar to Cocoa's NSAttributedString, we could also return the parsed string along with the associated AST tokens for each. I can imagine many uses for this, the most immediate a 50 LOC pure Snow syntax highlighter.

Is this feasible?

Mikoangelo avatar Jun 28 '09 15:06 Mikoangelo

This reminds me of a good old Ruby problem, namely that of serializing closures, and being able to create AST-nodes from Snow code would accomplish just that as well. I'll look into this, but I can't see why it shouldn't be possible.

Very good idea :)

ghost avatar Jun 30 '09 08:06 ghost

Serializing closures this way is not necessarily a good idea.

Closures, by nature, are context-dependent. Namely, they depend on the context in which they are defined, and can reference parent closures. So while it's technically possible to serialize a closure this way, a lot of complex decisions need to be made.

One simple early-out solution is to mark "discrete" closures (in the sense that they don't access any variables outside their own scope) in the codegen, and give a warning when serializing any non-discrete closure.

simonask avatar Jun 30 '09 14:06 simonask