Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tdakkota/astbuilders
Go AST utility package
https://github.com/tdakkota/astbuilders
ast builders codegen codegeneration go golang
Last synced: 15 days ago
JSON representation
Go AST utility package
- Host: GitHub
- URL: https://github.com/tdakkota/astbuilders
- Owner: tdakkota
- License: mit
- Created: 2020-08-09T02:40:24.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-10-06T19:16:37.000Z (about 1 year ago)
- Last Synced: 2024-10-18T21:17:18.436Z (20 days ago)
- Topics: ast, builders, codegen, codegeneration, go, golang
- Language: Go
- Homepage:
- Size: 109 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# astbuilders
[![Go](https://github.com/tdakkota/astbuilders/workflows/Go/badge.svg)](https://github.com/tdakkota/astbuilders/actions)
[![Documentation](https://godoc.org/github.com/tdakkota/astbuilders?status.svg)](https://pkg.go.dev/github.com/tdakkota/astbuilders)
[![codecov](https://codecov.io/gh/tdakkota/astbuilders/branch/master/graph/badge.svg)](https://codecov.io/gh/tdakkota/astbuilders)
[![license](https://img.shields.io/github/license/tdakkota/astbuilders.svg)](https://github.com/tdakkota/astbuilders/blob/master/LICENSE)Go AST utility package
## Install
```
go get github.com/tdakkota/astbuilders
```## Examples
### Creating a function
```go
package mainimport (
"go/printer"
"go/token"
"os""github.com/tdakkota/astbuilders"
)func main() {
node := builders.NewFunctionBuilder("main").
Body(func(s builders.StatementBuilder) builders.StatementBuilder {
return s.Expr(builders.CallPackage("fmt", "Println", builders.StringLit("Hello, world!")))
}).
CompleteAsDecl()printer.Fprint(os.Stdout, token.NewFileSet(), node)
}
```
prints
```go
func main() {
fmt.Println("Hello, world!")
}
```### `if err != nil`
```go
package mainimport (
"go/ast"
"go/printer"
"go/token"
"os"
"github.com/tdakkota/astbuilders"
)func main() {
errIdent := ast.NewIdent("err")
nilIdent := ast.NewIdent("nil")
cond := builders.NotEq(errIdent, nilIdent)s := builders.NewStatementBuilder()
s = s.If(nil, cond, func(body builders.StatementBuilder) builders.StatementBuilder {
return body.Return(errIdent)
})stmts := s.Complete()
node := stmts[0]
printer.Fprint(os.Stdout, token.NewFileSet(), node)
}
```
prints
```go
if err != nil {
return err
}
```### Reverse from SliceTricks
```go
package mainimport (
"go/ast"
"go/printer"
"go/token"
"os"
"github.com/tdakkota/astbuilders"
)func main() {
s := builders.NewStatementBuilder()a := ast.NewIdent("a")
left, right := ast.NewIdent("left"), ast.NewIdent("right")
one := builders.IntegerLit(1)// left, right := 0, len(a)-1
init := builders.Define(left, right)(builders.IntegerLit(0), builders.Sub(builders.Len(a), one))
// left < right
cond := builders.Less(left, right)
// left, right = left+1, right-1
post := builders.Assign(left, right)(token.ASSIGN)(builders.Add(left, one), builders.Sub(right, one))// a[left]
indexLeft := builders.Index(a, left)
// a[right]
indexRight := builders.Index(a, right)// for $init; $cond; $post {
// for left, right := 0, len(a)-1; left < right; left, right = left+1, right-1 {
s = s.For(init, cond, post, func(loop builders.StatementBuilder) builders.StatementBuilder {
// a[left], a[right] = a[right], a[left]
loop = loop.AddStmts(builders.Swap(indexLeft, indexRight))
return loop
})stmts := s.Complete()
node := stmts[0]
printer.Fprint(os.Stdout, token.NewFileSet(), node)
}
```prints
```go
for left, right := 0, len(a)-1; left < right; left, right = left+1, right-1 {
a[left], a[right] = a[right], a[left]
}
```