mirror of
https://github.com/bloeys/cogo.git
synced 2025-12-29 08:58:19 +00:00
Stuff
This commit is contained in:
@ -77,12 +77,11 @@ func processDeclNode(c *astutil.Cursor) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only operate on func called 'test'
|
if !funcDeclCallsCogo(funcDecl) {
|
||||||
if funcDecl.Name.Name != "test" {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, stmt := range funcDecl.Body.List {
|
for i, stmt := range funcDecl.Body.List {
|
||||||
|
|
||||||
// Find functions calls in the style of 'cogo.ABC123()'
|
// Find functions calls in the style of 'cogo.ABC123()'
|
||||||
exprStmt, ok := stmt.(*ast.ExprStmt)
|
exprStmt, ok := stmt.(*ast.ExprStmt)
|
||||||
@ -107,7 +106,32 @@ func processDeclNode(c *astutil.Cursor) bool {
|
|||||||
fmt.Printf("Found: %+v\n", pkgFuncCallExpr)
|
fmt.Printf("Found: %+v\n", pkgFuncCallExpr)
|
||||||
|
|
||||||
// Now that we found a call to cogo decide what to do
|
// Now that we found a call to cogo decide what to do
|
||||||
if pkgFuncCallExpr.Sel.Name == "Yield" {
|
if pkgFuncCallExpr.Sel.Name == "Begin" {
|
||||||
|
|
||||||
|
beginStmt := &ast.SwitchStmt{
|
||||||
|
Tag: ast.NewIdent("state"),
|
||||||
|
Body: &ast.BlockStmt{
|
||||||
|
List: []ast.Stmt{
|
||||||
|
&ast.CaseClause{
|
||||||
|
List: nil,
|
||||||
|
Body: []ast.Stmt{
|
||||||
|
&ast.ExprStmt{
|
||||||
|
X: &ast.CallExpr{
|
||||||
|
Fun: &ast.Ident{
|
||||||
|
Name: "Wow",
|
||||||
|
},
|
||||||
|
Args: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
funcDecl.Body.List[i] = beginStmt
|
||||||
|
|
||||||
|
} else if pkgFuncCallExpr.Sel.Name == "Yield" {
|
||||||
|
|
||||||
exprStmt.X = &ast.CallExpr{
|
exprStmt.X = &ast.CallExpr{
|
||||||
Fun: &ast.Ident{
|
Fun: &ast.Ident{
|
||||||
@ -120,3 +144,49 @@ func processDeclNode(c *astutil.Cursor) bool {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func funcDeclCallsCogo(fd *ast.FuncDecl) bool {
|
||||||
|
|
||||||
|
if fd.Body == nil || len(fd.Body.List) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, stmt := range fd.Body.List {
|
||||||
|
|
||||||
|
// Find functions calls in the style of 'cogo.ABC123()'
|
||||||
|
exprStmt, ok := stmt.(*ast.ExprStmt)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
callExpr, ok := exprStmt.X.(*ast.CallExpr)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
pkgFuncCallExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
pkgIdent, ok := pkgFuncCallExpr.X.(*ast.Ident)
|
||||||
|
return ok && pkgIdent.Name == "cogo"
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func filter[T any](arr []T, where func(x T) bool) []T {
|
||||||
|
|
||||||
|
out := []T{}
|
||||||
|
for i := 0; i < len(arr); i++ {
|
||||||
|
|
||||||
|
if !where(arr[i]) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
out = append(out, arr[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|||||||
45
main.go
45
main.go
@ -24,32 +24,53 @@ func Wow() {
|
|||||||
println("wow")
|
println("wow")
|
||||||
}
|
}
|
||||||
|
|
||||||
func test() {
|
// func test() {
|
||||||
|
|
||||||
|
// cogo.Begin()
|
||||||
|
|
||||||
|
// println("hi")
|
||||||
|
// println("this is from state_0")
|
||||||
|
// cogo.Yield()
|
||||||
|
// state = 1
|
||||||
|
|
||||||
|
// if 1 > 2 {
|
||||||
|
// println("gg")
|
||||||
|
// }
|
||||||
|
|
||||||
|
// println("Bye")
|
||||||
|
// println("this is from state_1")
|
||||||
|
// cogo.Yield()
|
||||||
|
// state = 2
|
||||||
|
|
||||||
|
// cogo.End()
|
||||||
|
// }
|
||||||
|
|
||||||
|
func test2() {
|
||||||
|
|
||||||
cogo.Begin()
|
cogo.Begin()
|
||||||
|
|
||||||
println("hi")
|
println("Hey")
|
||||||
println("this is from state_0")
|
|
||||||
cogo.Yield()
|
cogo.Yield()
|
||||||
state = 1
|
|
||||||
|
|
||||||
if 1 > 2 {
|
println("How you?")
|
||||||
println("gg")
|
cogo.Yield()
|
||||||
}
|
|
||||||
|
|
||||||
println("Bye")
|
println("Bye")
|
||||||
println("this is from state_1")
|
|
||||||
cogo.Yield()
|
cogo.Yield()
|
||||||
state = 2
|
|
||||||
|
|
||||||
cogo.End()
|
cogo.End()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
test()
|
// test()
|
||||||
test()
|
// test()
|
||||||
test()
|
// test()
|
||||||
|
|
||||||
|
test2()
|
||||||
|
test2()
|
||||||
|
test2()
|
||||||
|
test2()
|
||||||
|
|
||||||
println("Final state:", state)
|
println("Final state:", state)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user