mirror of
https://github.com/bloeys/cogo.git
synced 2025-12-29 08:58:19 +00:00
Implement YieldNone+ comments
This commit is contained in:
10
cogo/cogo.go
10
cogo/cogo.go
@ -48,14 +48,24 @@ func (c *Coroutine[InT, OutT]) Tick() (done bool) {
|
|||||||
return c.State == -1
|
return c.State == -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Yield yields and sets the Out variable to the passed variable
|
||||||
func (c *Coroutine[InT, OutT]) Yield(out OutT) {
|
func (c *Coroutine[InT, OutT]) Yield(out OutT) {
|
||||||
panic(fmt.Sprintf("Yield got called at runtime, which means the code generator was not run, you used cogo incorrectly, or cogo has a bug. Yield should NOT get called at runtime. coroutine: %+v;;; yield value: %+v;;;", c, out))
|
panic(fmt.Sprintf("Yield got called at runtime, which means the code generator was not run, you used cogo incorrectly, or cogo has a bug. Yield should NOT get called at runtime. coroutine: %+v;;; yield value: %+v;;;", c, out))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// YieldTo gives control to a Yielder object and immediately executes one Tick on it.
|
||||||
|
// Future 'Tick' calls on the original coroutine will run 'Tick' on passed Yielder.
|
||||||
|
//
|
||||||
|
// The original coroutine will only resume execution once this yielder reports that its done
|
||||||
func (c *Coroutine[InT, OutT]) YieldTo(y Yielder) {
|
func (c *Coroutine[InT, OutT]) YieldTo(y Yielder) {
|
||||||
panic(fmt.Sprintf("YieldTo got called at runtime, which means the code generator was not run, you used cogo incorrectly, or cogo has a bug. Yield should NOT get called at runtime. coroutine: %+v;;; yielder value: %+v;;;", c, y))
|
panic(fmt.Sprintf("YieldTo got called at runtime, which means the code generator was not run, you used cogo incorrectly, or cogo has a bug. Yield should NOT get called at runtime. coroutine: %+v;;; yielder value: %+v;;;", c, y))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// YieldNone yields without updating the Out variable
|
||||||
|
func (c *Coroutine[InT, OutT]) YieldNone() {
|
||||||
|
panic(fmt.Sprintf("YieldNone got called at runtime, which means the code generator was not run, you used cogo incorrectly, or cogo has a bug. Yield should NOT get called at runtime. coroutine: %+v;;;", c))
|
||||||
|
}
|
||||||
|
|
||||||
func HasGen() bool {
|
func HasGen() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
11
demo.cogo.go
11
demo.cogo.go
@ -84,8 +84,19 @@ func test2_cogo(c *cogo.Coroutine[int, int]) {
|
|||||||
switch c.SubState {
|
switch c.SubState {
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
println("test2222 before yield none")
|
||||||
|
c.State++
|
||||||
|
c.SubState = -1
|
||||||
|
return
|
||||||
|
case 3:
|
||||||
|
switch c.SubState {
|
||||||
|
default:
|
||||||
|
}
|
||||||
c.State = -1
|
c.State = -1
|
||||||
c.SubState = -1
|
c.SubState = -1
|
||||||
|
|
||||||
|
println("test2222 after yield none")
|
||||||
default:
|
default:
|
||||||
c.State = -1
|
c.State = -1
|
||||||
c.SubState = -1
|
c.SubState = -1
|
||||||
|
|||||||
4
demo.go
4
demo.go
@ -54,4 +54,8 @@ func test2(c *cogo.Coroutine[int, int]) {
|
|||||||
|
|
||||||
println("test2222 yield:", 2)
|
println("test2222 yield:", 2)
|
||||||
c.Yield(2)
|
c.Yield(2)
|
||||||
|
|
||||||
|
println("test2222 before yield none")
|
||||||
|
c.YieldNone()
|
||||||
|
println("test2222 after yield none")
|
||||||
}
|
}
|
||||||
|
|||||||
39
main.go
39
main.go
@ -371,6 +371,45 @@ func (p *processor) genCogoFuncsNodeProcessor(c *astutil.Cursor) bool {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
lastCaseEndBodyListIndex = i
|
||||||
|
} else if cogoFuncSelExpr.Sel.Name == "YieldNone" {
|
||||||
|
|
||||||
|
// Add everything from the last begin/yield until this yield into a case
|
||||||
|
stmtsSinceLastCogo := funcDecl.Body.List[lastCaseEndBodyListIndex+1 : i]
|
||||||
|
|
||||||
|
caseStmts := make([]ast.Stmt, 0, len(stmtsSinceLastCogo)+5)
|
||||||
|
|
||||||
|
caseStmts = append(caseStmts, subSwitchStmt)
|
||||||
|
subSwitchStmt = &ast.SwitchStmt{
|
||||||
|
Tag: ast.NewIdent(coroutineParamName + ".SubState"),
|
||||||
|
Body: &ast.BlockStmt{
|
||||||
|
List: []ast.Stmt{
|
||||||
|
getCaseWithStmts(nil, []ast.Stmt{}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
caseStmts = append(caseStmts, stmtsSinceLastCogo...)
|
||||||
|
caseStmts = append(caseStmts,
|
||||||
|
&ast.IncDecStmt{
|
||||||
|
Tok: token.INC,
|
||||||
|
X: ast.NewIdent(coroutineParamName + ".State"),
|
||||||
|
},
|
||||||
|
&ast.AssignStmt{
|
||||||
|
Lhs: []ast.Expr{ast.NewIdent(coroutineParamName + ".SubState")},
|
||||||
|
Tok: token.ASSIGN,
|
||||||
|
Rhs: []ast.Expr{ast.NewIdent("-1")},
|
||||||
|
},
|
||||||
|
&ast.ReturnStmt{},
|
||||||
|
)
|
||||||
|
|
||||||
|
switchStmt.Body.List = append(switchStmt.Body.List,
|
||||||
|
getCaseWithStmts(
|
||||||
|
[]ast.Expr{ast.NewIdent(fmt.Sprint(len(switchStmt.Body.List)))},
|
||||||
|
caseStmts,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
lastCaseEndBodyListIndex = i
|
lastCaseEndBodyListIndex = i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user