2024-07-15 21:52:45 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
|
|
"github.com/goplus/llgo/c"
|
|
|
|
|
"github.com/goplus/llgo/c/neco"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
// c.Printf(c.Str("main"))
|
|
|
|
|
run_main()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func run_main() {
|
|
|
|
|
// c.Printf(c.Str("run_main"))
|
|
|
|
|
// c.Fflush(c.Stdout)
|
2024-07-17 22:41:35 +08:00
|
|
|
neco.EnvSetpaniconerror(true)
|
|
|
|
|
neco.EnvSetcanceltype(neco.CANCEL_ASYNC)
|
|
|
|
|
ret := neco.Start(main2, 0)
|
|
|
|
|
c.Fprintf(c.Stderr, c.Str("neco_start: %s (code %d)\n"), neco.Strerror(ret), ret)
|
2024-07-15 21:52:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main2(argc c.Int, argv ...any) {
|
|
|
|
|
// c.Printf(c.Str("main2"))
|
|
|
|
|
// c.Fflush(c.Stdout)
|
2024-07-17 22:41:35 +08:00
|
|
|
neco.ExitProg(main3())
|
2024-07-15 21:52:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main3() c.Int {
|
|
|
|
|
|
|
|
|
|
// c.Printf(c.Str("main3"))
|
|
|
|
|
// c.Fflush(c.Stdout)
|
|
|
|
|
|
|
|
|
|
// Create a new generator coroutine that is used to send ints.
|
2024-07-17 22:41:35 +08:00
|
|
|
gen := new(neco.Gen)
|
|
|
|
|
neco.GenStart(&gen, unsafe.Sizeof(int(0)), coroutine, 0)
|
2024-07-15 21:52:45 +08:00
|
|
|
|
|
|
|
|
// Iterate over each int until the generator is closed.
|
|
|
|
|
var i c.Int
|
|
|
|
|
for {
|
2024-07-17 22:41:35 +08:00
|
|
|
ret := neco.GenNext(gen, c.Pointer(&i))
|
2024-07-15 21:52:45 +08:00
|
|
|
|
|
|
|
|
// c.Printf(c.Str("gen [%d, %d] "), ret, c.Int(neco.NECO_CLOSED))
|
|
|
|
|
// c.Fflush(c.Stdout)
|
|
|
|
|
|
2024-07-17 22:41:35 +08:00
|
|
|
if ret != c.Int(neco.CLOSED) {
|
2024-07-15 21:52:45 +08:00
|
|
|
c.Printf(c.Str("%d\n"), i)
|
|
|
|
|
} else {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This coroutine no longer needs the generator.
|
2024-07-17 22:41:35 +08:00
|
|
|
neco.GenRelease(gen)
|
2024-07-15 21:52:45 +08:00
|
|
|
|
|
|
|
|
// c.Printf(c.Str("main3 end"))
|
|
|
|
|
// c.Fflush(c.Stdout)
|
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func coroutine(argc c.Int, argv ...any) {
|
|
|
|
|
// Yield each int to the caller, one at a time.
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2024-07-17 22:41:35 +08:00
|
|
|
neco.GenYield(c.Pointer(&i))
|
2024-07-15 21:52:45 +08:00
|
|
|
}
|
|
|
|
|
// c.Printf(c.Str("coroutine end"))
|
|
|
|
|
// c.Fflush(c.Stdout)
|
|
|
|
|
}
|