Support single nested blocks

This commit is contained in:
bloeys
2022-10-28 01:11:58 +04:00
parent e0cc0c3de4
commit d8748b6168
3 changed files with 35 additions and 8 deletions

View File

@ -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")},

View File

@ -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

View File

@ -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)
}