neco: init
This commit is contained in:
34
c/neco/_demo/generators/_c/generators.c
Normal file
34
c/neco/_demo/generators/_c/generators.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "../../../_neco/neco.h"
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void coroutine(int argc, void *argv[]) {
|
||||
// Yield each int to the caller, one at a time.
|
||||
for (int i = 0; i < 10; i++) {
|
||||
neco_gen_yield(&i);
|
||||
}
|
||||
}
|
||||
|
||||
static int __neco_main() {
|
||||
|
||||
// Create a new generator coroutine that is used to send ints.
|
||||
neco_gen *gen;
|
||||
neco_gen_start(&gen, sizeof(int), coroutine, 0);
|
||||
|
||||
// Iterate over each int until the generator is closed.
|
||||
int i;
|
||||
while (neco_gen_next(gen, &i) != NECO_CLOSED) {
|
||||
printf("%d\n", i);
|
||||
}
|
||||
|
||||
// This coroutine no longer needs the generator.
|
||||
neco_gen_release(gen);
|
||||
return 0;
|
||||
}
|
||||
static void _neco_main() { __neco_exit_prog(__neco_main()); }
|
||||
void run_main() {
|
||||
neco_env_setpaniconerror(true);
|
||||
neco_env_setcanceltype(NECO_CANCEL_ASYNC);
|
||||
int ret = neco_start(_neco_main, 0);
|
||||
fprintf(stderr, "neco_start: %s (code %d)\n", neco_strerror(ret), ret);
|
||||
};
|
||||
19
c/neco/_demo/generators/generators.go
Normal file
19
c/neco/_demo/generators/generators.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "unsafe"
|
||||
|
||||
_ "github.com/goplus/llgo/c"
|
||||
)
|
||||
|
||||
const (
|
||||
LLGoFiles = "_c/generators.c; ../../_neco/neco.c"
|
||||
LLGoPackage = "link"
|
||||
)
|
||||
|
||||
func main() {
|
||||
run_main()
|
||||
}
|
||||
|
||||
//go:linkname run_main C.run_main
|
||||
func run_main() {}
|
||||
70
c/neco/_demo/gogenerators/g2.go
Normal file
70
c/neco/_demo/gogenerators/g2.go
Normal file
@@ -0,0 +1,70 @@
|
||||
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)
|
||||
neco.Neco_env_setpaniconerror(true)
|
||||
neco.Neco_env_setcanceltype(neco.NECO_CANCEL_ASYNC)
|
||||
ret := neco.Neco_start(main2, 0)
|
||||
c.Fprintf(c.Stderr, c.Str("neco_start: %s (code %d)\n"), neco.Neco_strerror(int(ret)), ret)
|
||||
}
|
||||
|
||||
func main2(argc c.Int, argv ...any) {
|
||||
// c.Printf(c.Str("main2"))
|
||||
// c.Fflush(c.Stdout)
|
||||
neco.Neco_exit_prog(main3())
|
||||
}
|
||||
|
||||
func main3() c.Int {
|
||||
|
||||
// c.Printf(c.Str("main3"))
|
||||
// c.Fflush(c.Stdout)
|
||||
|
||||
// Create a new generator coroutine that is used to send ints.
|
||||
gen := new(neco.Neco_gen)
|
||||
neco.Neco_gen_start(&gen, unsafe.Sizeof(int(0)), coroutine, 0)
|
||||
|
||||
// Iterate over each int until the generator is closed.
|
||||
var i c.Int
|
||||
for {
|
||||
ret := neco.Neco_gen_next(gen, c.Pointer(&i))
|
||||
|
||||
// c.Printf(c.Str("gen [%d, %d] "), ret, c.Int(neco.NECO_CLOSED))
|
||||
// c.Fflush(c.Stdout)
|
||||
|
||||
if ret != c.Int(neco.NECO_CLOSED) {
|
||||
c.Printf(c.Str("%d\n"), i)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// This coroutine no longer needs the generator.
|
||||
neco.Neco_gen_release(gen)
|
||||
|
||||
// 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++ {
|
||||
neco.Neco_gen_yield(c.Pointer(&i))
|
||||
}
|
||||
// c.Printf(c.Str("coroutine end"))
|
||||
// c.Fflush(c.Stdout)
|
||||
}
|
||||
Reference in New Issue
Block a user