diff --git a/inliner/main.go b/inliner/main.go index f9a5274..374a43b 100755 --- a/inliner/main.go +++ b/inliner/main.go @@ -179,6 +179,7 @@ func (p *processor) genCogoFuncsNodeProcessor(c *astutil.Cursor) bool { for i, stmt := range funcDecl.Body.List { var cogoFuncSelExpr *ast.SelectorExpr + var blockStmt *ast.BlockStmt ifStmt, ifStmtOk := stmt.(*ast.IfStmt) if ifStmtOk { @@ -188,7 +189,15 @@ func (p *processor) genCogoFuncsNodeProcessor(c *astutil.Cursor) bool { continue } - subStateNums := p.genCogoIfStmt(ifStmt, coroutineParamName, len(switchStmt.Body.List)) + blockStmt = ifStmt.Body + + } else if bStmt, blockStmtOk := stmt.(*ast.BlockStmt); blockStmtOk { + blockStmt = bStmt + } + + if blockStmt != nil { + + subStateNums := p.genCogoBlockStmt(blockStmt, coroutineParamName, len(switchStmt.Body.List)) for _, subStateNum := range subStateNums { subSwitchStmt.Body.List = append(subSwitchStmt.Body.List, @@ -356,9 +365,9 @@ func (p *processor) genCogoFuncsNodeProcessor(c *astutil.Cursor) bool { return true } -func (p *processor) genCogoIfStmt(ifStmt *ast.IfStmt, coroutineParamName string, currCase int) (subStateNums []int32) { +func (p *processor) genCogoBlockStmt(blockStmt *ast.BlockStmt, coroutineParamName string, currCase int) (subStateNums []int32) { - for i, stmt := range ifStmt.Body.List { + for i, stmt := range blockStmt.List { selExpr, selExprArgs := tryGetSelExprFromStmt(stmt, coroutineParamName, "Yield") if selExpr == nil { @@ -369,7 +378,7 @@ func (p *processor) genCogoIfStmt(ifStmt *ast.IfStmt, coroutineParamName string, // subStateNum >= 1000_000 newSubStateNum := rand.Int31() + 1000_000 subStateNums = append(subStateNums, newSubStateNum) - ifStmt.Body.List[i] = &ast.BlockStmt{ + blockStmt.List[i] = &ast.BlockStmt{ List: []ast.Stmt{ &ast.AssignStmt{ Lhs: []ast.Expr{ast.NewIdent(coroutineParamName + ".State")}, diff --git a/main.cogo.go b/main.cogo.go index c282fc8..d9eba7d 100755 --- a/main.cogo.go +++ b/main.cogo.go @@ -19,19 +19,32 @@ func test_cogo(c *cogo.Coroutine[int, int]) (out int) { default: case 1299498081: goto cogo_1299498081 + case 2020727887: + goto cogo_2020727887 } - if c.In > 1 { - println("\nTick 1.5") + { + println("\nInside block 1") { c.State = 1 c.SubState = 1299498081 - return c.In + return -20 } } cogo_1299498081: ; + if c.In > 1 { + println("\nInside if 1") + { + c.State = 1 + c.SubState = 2020727887 + return c.In + } + } + cogo_2020727887: + ; + println("\nTick 2") c.State++ c.SubState = -1 diff --git a/main.go b/main.go index a781b75..0ce9c85 100755 --- a/main.go +++ b/main.go @@ -19,8 +19,13 @@ func test(c *cogo.Coroutine[int, int]) (out int) { println("\nTick 1") c.Yield(1) + { + println("\nInside block 1") + c.Yield(-20) + } + if c.In > 1 { - println("\nTick 1.5") + println("\nInside if 1") c.Yield(c.In) }