yaegi
yaegi copied to clipboard
Evaluate expressions within the context of a package
Proposal
Given code such as
package main
import "github.com/traefik/yaegi/interp"
const src = `package foo
func Bar(s string) string { return s + "-Foo" }`
func main() {
i := interp.New(interp.Options{})
_, err := i.Eval(src)
if err != nil {
panic(err)
}
}
It should be possible to evaluate the next expression in the context of the source or package previously evaluated.
eg
v, _ := i.Eval("Bar()") // instead of v, _ := i.Eval("foo.Bar()")
It should also be possible to to evaluate an entire folder/package.
Background
I'm creating a doctests cli and language server (https://github.com/apitoolkit/doctests) Which allows users run arbitrary expressions within their comments.
Eg
It works as expected, but only if the user includes the package name when referencing the function they intend to execute.
As a stretch question, is it possible to load an entire package with the EvalFile() function? Since most times a file would be referencing code in other files.
Workarounds
None that i'm aware of.