google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.26k stars 204 forks source link

support `eval()` like python? #543

Closed wetlittle closed 2 months ago

wetlittle commented 2 months ago

like this ---

>>> eval("1+2")
3
adonovan commented 2 months ago

Starlark intentionally does not come with eval because it tends to invite metaprogramming uses that make programs very hard to read and reason about. If the purpose of this issue is to add eval as a standard built-in, you will need to propose a language change at https://github.com/bazelbuild/starlark. I plan to close this issue.

That said, it shouldn't be hard to implement the eval function as a built-in outside the interpreter. You will need to enumerate the the set of local and module-level variables that are defined at the call to eval, which can be done using the new API added in https://github.com/google/starlark-go/issues/538.