alexmojaki/sorcery

[Question] Is it possible/easy to implement something like this using sorcery?

pwwang opened this issue · 4 comments

# turn
lambd(X.a + X.b)
# into
lambda X: X.a + X.b

Suppose X.a + X.b is some valid syntax (proxies). When it is turned into a lambda function, it becomes a valid function body and X is replaced by the argument.

This could be implemented inside lambd, and it can return the lambda function.

Sure, you could get the source like in that recent varname issue, construct the lambda in a string, then eval it.

Is it possible to fetch the AST node and put/eval it in the context as a function body? I mean instead of eval the source code but the AST node. Worried about that source code could be arbitrary.

Yes, after all you get the AST node first and get the source code from that. You can eval the node but the node contains the exact same code as the source, it's just as 'dangerous'.

This is implemented with executing.
Just in case if you are curious:

https://github.com/pwwang/pipedy