lmfit / asteval

minimalistic evaluator of python expression using ast module
https://lmfit.github.io/asteval
MIT License
183 stars 41 forks source link

Supporting lambda expressions #90

Closed fusiongyro closed 1 year ago

fusiongyro commented 3 years ago

Hi Matthew,

I need something like asteval for a project I'm working on, but I need lambda nodes in particular to work. For my use case, it would be appropriate for the interpretation of a lambda to be simply the AST node itself. Interpreting the call of a lambda appears to be the tricky part, since it looks like you don't have the code currently to handle it. It looks like you kind of let Python take care of that for you, since you'd have actual Python functions in the symtable to call, and Python can do that lining up itself. To really interpret a call to a lambda I think would require taking the arguments, generate a new symtable, evaluating the body, and reverting the symtable. I have done a very trivial demo of this and would be happy to flesh it out and send you a PR if you're interested. Or, if you already know that this concept doesn't work for some reason I haven't thought of, that would be good for me to know before I get too far down the road!

Thanks!

newville commented 3 years ago

@fusiongyro I think that Lambda would "just" create a function with the one line of code in it, run that function, keep the result, and don't save that function definition.

So I would thinkn that modeling a on_lambda() on on_functiondef() is probably the way to go -- just keep the created Procedure out of the main symbol table. That is, an asteval function built with Procedure already has a local namespace for function args that it manages.

fusiongyro commented 3 years ago

Ran into a famous hitch but I sent you #91 to have a peek at it.