Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interp: add a function to compile AST (instead of source) #1251

Closed
firelizzard18 opened this issue Sep 14, 2021 · 1 comment · Fixed by #1261
Closed

interp: add a function to compile AST (instead of source) #1251

firelizzard18 opened this issue Sep 14, 2021 · 1 comment · Fixed by #1261
Labels
area/api change in API enhancement New feature or request
Milestone

Comments

@firelizzard18
Copy link
Contributor

Proposal

func (interp *Interpreter) CompileAST(ast.Node) (*Program, error)

This would accept Go AST provided by the user. Currently, the two supported pathways, compile a file and compile a function body, are equivalent to passing in an *ast.File or an *ast.BlockStmt, respectively. The new function could explicitly support only a File or BlockStmt input, though the existing (*Interpreter).ast may handle arbitrary (valid) ASTs.

I wrote a relatively small patch for this. Would you accept this as a PR?

Background

I'm playing around with writing a Go-/Yaegi-based shell. I want to support string interpolation and other features foreign to Go. This would require manipulation of the AST. While I could generate source, I would much prefer to directly pass the modified AST to Yaegi.

Workarounds

Parsing the source, modifying the AST, writing it back out to a buffer, and handing that buffer to Yaegi, which would parse the source again.

@mvertes mvertes added area/api change in API enhancement New feature or request labels Sep 21, 2021
@mvertes
Copy link
Member

mvertes commented Sep 21, 2021

Thanks for this proposal. It is ok to me, as the standard lib AST format is stable and already in use by many tools / libraries in the Go ecosystem. The patch looks good too.

traefiker pushed a commit that referenced this issue Sep 23, 2021
Adds CompileAST, which can be used to compile Go AST directly. This
allows users to delegate parsing of source to their own code instead of
relying on the interpreter.

CLoses #1251
@traefiker traefiker added this to the v0.11.x milestone Sep 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api change in API enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants