mirror of
https://github.com/bloeys/cogo.git
synced 2025-12-29 08:58:19 +00:00
More stuff
This commit is contained in:
@ -23,6 +23,3 @@ 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 (c *Coroutine[InT, OutT]) End() {
|
|
||||||
}
|
|
||||||
|
|||||||
@ -165,14 +165,13 @@ func (p *processor) processDeclNode(c *astutil.Cursor) bool {
|
|||||||
|
|
||||||
lastCaseEndBodyListIndex = i
|
lastCaseEndBodyListIndex = i
|
||||||
|
|
||||||
} else if cogoFuncSelExpr.Sel.Name == "End" {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Add everything from the last begin/yield until this yield into a case
|
// Add everything after the last yield in a separate case
|
||||||
stmtsSinceLastCogo := funcDecl.Body.List[lastCaseEndBodyListIndex+1 : i]
|
stmtsToEndOfFunc := funcDecl.Body.List[lastCaseEndBodyListIndex+1:]
|
||||||
stmtsAfterEnd := funcDecl.Body.List[i+1:]
|
|
||||||
|
|
||||||
caseStmts := make([]ast.Stmt, 0, len(stmtsSinceLastCogo)+len(stmtsAfterEnd)+1)
|
caseStmts := make([]ast.Stmt, 0, len(stmtsToEndOfFunc)+1)
|
||||||
caseStmts = append(caseStmts, stmtsSinceLastCogo...)
|
|
||||||
caseStmts = append(caseStmts,
|
caseStmts = append(caseStmts,
|
||||||
&ast.AssignStmt{
|
&ast.AssignStmt{
|
||||||
Lhs: []ast.Expr{ast.NewIdent(coroutineParamName + ".State")},
|
Lhs: []ast.Expr{ast.NewIdent(coroutineParamName + ".State")},
|
||||||
@ -180,7 +179,7 @@ func (p *processor) processDeclNode(c *astutil.Cursor) bool {
|
|||||||
Rhs: []ast.Expr{ast.NewIdent("-1")},
|
Rhs: []ast.Expr{ast.NewIdent("-1")},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
caseStmts = append(caseStmts, stmtsAfterEnd...)
|
caseStmts = append(caseStmts, stmtsToEndOfFunc...)
|
||||||
|
|
||||||
switchStmt.Body.List = append(switchStmt.Body.List,
|
switchStmt.Body.List = append(switchStmt.Body.List,
|
||||||
getCaseWithStmts(
|
getCaseWithStmts(
|
||||||
@ -189,16 +188,9 @@ func (p *processor) processDeclNode(c *astutil.Cursor) bool {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
lastCaseEndBodyListIndex = i
|
// Apply changes
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
funcDecl.Body.List = funcDecl.Body.List[:beginBodyListIndex]
|
funcDecl.Body.List = funcDecl.Body.List[:beginBodyListIndex]
|
||||||
funcDecl.Body.List = append(funcDecl.Body.List,
|
funcDecl.Body.List = append(funcDecl.Body.List,
|
||||||
// &ast.LabeledStmt{
|
|
||||||
// Label: ast.NewIdent(cogoSwitchLbl),
|
|
||||||
// Stmt: switchStmt,
|
|
||||||
// },
|
|
||||||
switchStmt,
|
switchStmt,
|
||||||
)
|
)
|
||||||
return true
|
return true
|
||||||
|
|||||||
22
main.go
22
main.go
@ -30,28 +30,6 @@ func test(c *cogo.Coroutine[int, int]) (out int) {
|
|||||||
c.Yield(4)
|
c.Yield(4)
|
||||||
|
|
||||||
println("Tick before end")
|
println("Tick before end")
|
||||||
c.End()
|
|
||||||
|
|
||||||
// switch c.State {
|
|
||||||
// case 0:
|
|
||||||
// println("Tick 0")
|
|
||||||
// c.State++
|
|
||||||
// return 1, false
|
|
||||||
// case 1:
|
|
||||||
// println("Tick 1")
|
|
||||||
// c.State++
|
|
||||||
// return 2, false
|
|
||||||
// case 2:
|
|
||||||
// println("Tick 2")
|
|
||||||
// c.State++
|
|
||||||
// return 3, false
|
|
||||||
// case 3:
|
|
||||||
// println("Tick 3")
|
|
||||||
// c.State++
|
|
||||||
// return 4, false
|
|
||||||
// default:
|
|
||||||
// return out, true
|
|
||||||
// }
|
|
||||||
|
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user