This commit is contained in:
bloeys
2022-10-27 04:49:52 +04:00
parent be2a6f72ba
commit 5b1ed9ed71
3 changed files with 75 additions and 44 deletions

View File

@ -23,3 +23,7 @@ func (c *Coroutine[InT, OutT]) Tick() (out OutT, done bool) {
func (c *Coroutine[InT, OutT]) Yield(out OutT) {
}
func HasGen() bool {
return true
}

View File

@ -11,8 +11,6 @@ import (
"golang.org/x/tools/go/packages"
)
const cogoSwitchLbl = "cogoSwitchLbl"
func printDebugInfo() {
fmt.Printf("Running inliner on '%s'\n", os.Getenv("GOFILE"))
@ -55,24 +53,43 @@ func processPkg(pkg *packages.Package) {
p := processor{
fset: pkg.Fset,
funcDeclsToWrite: []*ast.FuncDecl{},
}
for i, synFile := range pkg.Syntax {
pkg.Syntax[i] = astutil.Apply(synFile, p.processDeclNode, nil).(*ast.File)
if len(p.funcDeclsToWrite) > 0 {
root := &ast.File{
Name: synFile.Name,
Imports: synFile.Imports,
Decls: []ast.Decl{},
}
// f, err := os.Create("main_gen.go")
// if err != nil {
// panic(err.Error())
// }
for _, v := range p.funcDeclsToWrite {
root.Decls = append(root.Decls, &ast.FuncDecl{
Name: ast.NewIdent(v.Name.Name + "_cogo"),
Type: v.Type,
Body: v.Body,
})
}
err := format.Node(os.Stdout, pkg.Fset, pkg.Syntax[0])
// imports.Process()
err := format.Node(os.Stdout, pkg.Fset, root)
if err != nil {
panic(err.Error())
}
p.funcDeclsToWrite = p.funcDeclsToWrite[:0]
}
}
}
type processor struct {
fset *token.FileSet
funcDeclsToWrite []*ast.FuncDecl
}
func (p *processor) processDeclNode(c *astutil.Cursor) bool {
@ -105,13 +122,7 @@ func (p *processor) processDeclNode(c *astutil.Cursor) bool {
var cogoFuncSelExpr *ast.SelectorExpr
// ifStmt, ifStmtOk := stmt.(*ast.IfStmt)
// if ifStmtOk {
// handleNestedCogo(ifStmt.Body)
// continue
// }
// Find functions calls in the style of 'cogo.ABC123()'
// Find functions calls in the style of 'xyz.ABC123()'
exprStmt, exprStmtOk := stmt.(*ast.ExprStmt)
if !exprStmtOk {
continue
@ -193,9 +204,49 @@ func (p *processor) processDeclNode(c *astutil.Cursor) bool {
funcDecl.Body.List = append(funcDecl.Body.List,
switchStmt,
)
originalList := funcDecl.Body.List
funcDecl.Body.List = make([]ast.Stmt, 0, len(funcDecl.Body.List)+1)
funcDecl.Body.List = append(funcDecl.Body.List,
&ast.IfStmt{
Cond: createStmtFromSelFuncCall("cogo", "HasGen").(*ast.ExprStmt).X,
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.ReturnStmt{
Results: []ast.Expr{&ast.CallExpr{
Fun: ast.NewIdent(funcDecl.Name.Name + "_cogo"),
}},
},
},
},
},
)
funcDecl.Body.List = append(funcDecl.Body.List, originalList...)
p.funcDeclsToWrite = append(p.funcDeclsToWrite, funcDecl)
return true
}
func createStmtFromFuncCall(funcName string) ast.Stmt {
return &ast.ExprStmt{
X: &ast.CallExpr{
Fun: ast.NewIdent(funcName),
},
}
}
func createStmtFromSelFuncCall(lhs, rhs string) ast.Stmt {
return &ast.ExprStmt{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent(lhs),
Sel: ast.NewIdent(rhs),
},
},
}
}
func funcDeclCallsCoroutineBegin(fd *ast.FuncDecl, coroutineParamName string) bool {
if fd.Body == nil || len(fd.Body.List) == 0 {

24
main.go
View File

@ -9,10 +9,6 @@ import (
"github.com/bloeys/cogo/cogo"
)
func Wow() {
println("wow")
}
func test(c *cogo.Coroutine[int, int]) (out int) {
c.Begin()
@ -34,22 +30,6 @@ func test(c *cogo.Coroutine[int, int]) (out int) {
return out
}
// func test2() {
// // cogo.Begin()
// println("Hey")
// cogo.Yield()
// println("How you?")
// cogo.Yield()
// println("Bye")
// cogo.Yield()
// cogo.End()
// }
func main() {
c := &cogo.Coroutine[int, int]{
@ -61,10 +41,6 @@ func main() {
println(out)
}
// test2()
// test2()
// test2()
// test2()
}
func FileLine() int {