yaegi icon indicating copy to clipboard operation
yaegi copied to clipboard

Evaluate expressions within the context of a package

Open tonyalaribe opened this issue 2 years ago • 0 comments

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 image

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.

tonyalaribe avatar Jun 28 '22 09:06 tonyalaribe