Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] c/neco: init and demo #525

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions c/neco/_demo/generators/_c/generators.c
Original file line number Diff line number Diff line change
@@ -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 changes: 19 additions & 0 deletions c/neco/_demo/generators/generators.go
Original file line number Diff line number Diff line change
@@ -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() {}
kindy marked this conversation as resolved.
Show resolved Hide resolved
70 changes: 70 additions & 0 deletions c/neco/_demo/gogenerators/g2.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading