mirror of
https://github.com/bloeys/cogo.git
synced 2025-12-29 08:58:19 +00:00
Moving
This commit is contained in:
@ -23,3 +23,7 @@ func (c *Coroutine[InT, OutT]) Tick() (out OutT, done bool) {
|
|||||||
|
|
||||||
func (c *Coroutine[InT, OutT]) Yield(out OutT) {
|
func (c *Coroutine[InT, OutT]) Yield(out OutT) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HasGen() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
@ -11,8 +11,6 @@ import (
|
|||||||
"golang.org/x/tools/go/packages"
|
"golang.org/x/tools/go/packages"
|
||||||
)
|
)
|
||||||
|
|
||||||
const cogoSwitchLbl = "cogoSwitchLbl"
|
|
||||||
|
|
||||||
func printDebugInfo() {
|
func printDebugInfo() {
|
||||||
|
|
||||||
fmt.Printf("Running inliner on '%s'\n", os.Getenv("GOFILE"))
|
fmt.Printf("Running inliner on '%s'\n", os.Getenv("GOFILE"))
|
||||||
@ -55,24 +53,43 @@ func processPkg(pkg *packages.Package) {
|
|||||||
|
|
||||||
p := processor{
|
p := processor{
|
||||||
fset: pkg.Fset,
|
fset: pkg.Fset,
|
||||||
|
funcDeclsToWrite: []*ast.FuncDecl{},
|
||||||
}
|
}
|
||||||
for i, synFile := range pkg.Syntax {
|
for i, synFile := range pkg.Syntax {
|
||||||
|
|
||||||
pkg.Syntax[i] = astutil.Apply(synFile, p.processDeclNode, nil).(*ast.File)
|
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")
|
for _, v := range p.funcDeclsToWrite {
|
||||||
// if err != nil {
|
root.Decls = append(root.Decls, &ast.FuncDecl{
|
||||||
// panic(err.Error())
|
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 {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
p.funcDeclsToWrite = p.funcDeclsToWrite[:0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type processor struct {
|
type processor struct {
|
||||||
fset *token.FileSet
|
fset *token.FileSet
|
||||||
|
funcDeclsToWrite []*ast.FuncDecl
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *processor) processDeclNode(c *astutil.Cursor) bool {
|
func (p *processor) processDeclNode(c *astutil.Cursor) bool {
|
||||||
@ -105,13 +122,7 @@ func (p *processor) processDeclNode(c *astutil.Cursor) bool {
|
|||||||
|
|
||||||
var cogoFuncSelExpr *ast.SelectorExpr
|
var cogoFuncSelExpr *ast.SelectorExpr
|
||||||
|
|
||||||
// ifStmt, ifStmtOk := stmt.(*ast.IfStmt)
|
// Find functions calls in the style of 'xyz.ABC123()'
|
||||||
// if ifStmtOk {
|
|
||||||
// handleNestedCogo(ifStmt.Body)
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Find functions calls in the style of 'cogo.ABC123()'
|
|
||||||
exprStmt, exprStmtOk := stmt.(*ast.ExprStmt)
|
exprStmt, exprStmtOk := stmt.(*ast.ExprStmt)
|
||||||
if !exprStmtOk {
|
if !exprStmtOk {
|
||||||
continue
|
continue
|
||||||
@ -193,9 +204,49 @@ func (p *processor) processDeclNode(c *astutil.Cursor) bool {
|
|||||||
funcDecl.Body.List = append(funcDecl.Body.List,
|
funcDecl.Body.List = append(funcDecl.Body.List,
|
||||||
switchStmt,
|
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
|
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 {
|
func funcDeclCallsCoroutineBegin(fd *ast.FuncDecl, coroutineParamName string) bool {
|
||||||
|
|
||||||
if fd.Body == nil || len(fd.Body.List) == 0 {
|
if fd.Body == nil || len(fd.Body.List) == 0 {
|
||||||
|
|||||||
24
main.go
24
main.go
@ -9,10 +9,6 @@ import (
|
|||||||
"github.com/bloeys/cogo/cogo"
|
"github.com/bloeys/cogo/cogo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Wow() {
|
|
||||||
println("wow")
|
|
||||||
}
|
|
||||||
|
|
||||||
func test(c *cogo.Coroutine[int, int]) (out int) {
|
func test(c *cogo.Coroutine[int, int]) (out int) {
|
||||||
|
|
||||||
c.Begin()
|
c.Begin()
|
||||||
@ -34,22 +30,6 @@ func test(c *cogo.Coroutine[int, int]) (out int) {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// func test2() {
|
|
||||||
|
|
||||||
// // cogo.Begin()
|
|
||||||
|
|
||||||
// println("Hey")
|
|
||||||
// cogo.Yield()
|
|
||||||
|
|
||||||
// println("How you?")
|
|
||||||
// cogo.Yield()
|
|
||||||
|
|
||||||
// println("Bye")
|
|
||||||
// cogo.Yield()
|
|
||||||
|
|
||||||
// cogo.End()
|
|
||||||
// }
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
c := &cogo.Coroutine[int, int]{
|
c := &cogo.Coroutine[int, int]{
|
||||||
@ -61,10 +41,6 @@ func main() {
|
|||||||
println(out)
|
println(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
// test2()
|
|
||||||
// test2()
|
|
||||||
// test2()
|
|
||||||
// test2()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func FileLine() int {
|
func FileLine() int {
|
||||||
|
|||||||
Reference in New Issue
Block a user