feat: add async/await coroutine support with include system and file I/O to RC language
Add Python-like async/await functionality to the RC interpreter, enabling non-blocking coroutine execution via pthreads. Introduce `async` keyword for function declarations, `await()` for retrieving results, and `gather()` for parallel execution. Extend the tokenizer, parser, interpreter, and native functions with coroutine lifecycle management (init, alloc, thread execution, result retrieval). Also add include system with relative paths, nested includes up to 32 levels, and automatic circular include prevention. Document new features in TUTORIAL.md with examples (async_await_demo.rc) and tests (async_await_test.rc). Update types.h with Coroutine struct, MAX_COROUTINES constant, and new token types (Async, Await).
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
async int compute_square(int n) {
|
||||
printf("Computing square of %d\n", n);
|
||||
int result = n * n;
|
||||
return result;
|
||||
}
|
||||
|
||||
async int compute_sum(int a, int b) {
|
||||
printf("Computing sum of %d and %d\n", a, b);
|
||||
int result = a + b;
|
||||
return result;
|
||||
}
|
||||
|
||||
async int slow_computation(int n) {
|
||||
printf("Starting slow computation for %d\n", n);
|
||||
int total = 0;
|
||||
int i = 0;
|
||||
while (i < n) {
|
||||
total = total + i;
|
||||
i = i + 1;
|
||||
}
|
||||
printf("Finished slow computation for %d: result = %d\n", n, total);
|
||||
return total;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== Python-like Async/Await Tests ===\n\n");
|
||||
|
||||
printf("Test 1: Simple async function call and await\n");
|
||||
int coro1 = compute_square(5);
|
||||
printf("Coroutine created: %d\n", coro1);
|
||||
int result1 = await(coro1);
|
||||
printf("Result: 5 squared = %d\n", result1);
|
||||
printf("PASS: Simple async/await works\n\n");
|
||||
|
||||
printf("Test 2: Multiple async calls with await\n");
|
||||
int coro2 = compute_sum(10, 20);
|
||||
int coro3 = compute_sum(5, 15);
|
||||
printf("Two coroutines created\n");
|
||||
int result2 = await(coro2);
|
||||
int result3 = await(coro3);
|
||||
printf("Result 2: 10 + 20 = %d\n", result2);
|
||||
printf("Result 3: 5 + 15 = %d\n", result3);
|
||||
printf("PASS: Multiple async calls work\n\n");
|
||||
|
||||
printf("Test 3: Parallel execution with gather\n");
|
||||
int coro4 = slow_computation(100);
|
||||
int coro5 = slow_computation(200);
|
||||
int coro6 = slow_computation(150);
|
||||
printf("Three slow computations started in parallel\n");
|
||||
gather(coro4, coro5, coro6);
|
||||
printf("All computations completed via gather\n");
|
||||
printf("PASS: Gather function works\n\n");
|
||||
|
||||
printf("Test 4: Chaining async calls\n");
|
||||
int step1 = compute_square(3);
|
||||
int val1 = await(step1);
|
||||
printf("Step 1 complete: %d\n", val1);
|
||||
|
||||
int step2 = compute_sum(val1, 5);
|
||||
int val2 = await(step2);
|
||||
printf("Step 2 complete: %d\n", val2);
|
||||
printf("PASS: Chaining async calls works\n\n");
|
||||
|
||||
printf("Test 5: Multiple gather calls\n");
|
||||
int batch1_a = compute_square(2);
|
||||
int batch1_b = compute_square(3);
|
||||
printf("First batch started\n");
|
||||
gather(batch1_a, batch1_b);
|
||||
printf("First batch complete\n");
|
||||
|
||||
int batch2_a = compute_sum(10, 10);
|
||||
int batch2_b = compute_sum(20, 20);
|
||||
printf("Second batch started\n");
|
||||
gather(batch2_a, batch2_b);
|
||||
printf("Second batch complete\n");
|
||||
printf("PASS: Multiple gather calls work\n\n");
|
||||
|
||||
printf("=== All Async/Await Tests Completed Successfully ===\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user