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:
2025-11-23 19:30:37 +00:00
parent 837e254ad6
commit 3fab35d7cb
10 changed files with 813 additions and 16 deletions
+82
View File
@@ -0,0 +1,82 @@
async int fetch_data(int id) {
printf("Fetching data for ID: %d\n", id);
int i = 0;
int sum = 0;
while (i < 1000) {
sum = sum + i;
i = i + 1;
}
printf("Data fetched for ID %d: value = %d\n", id, sum);
return sum;
}
async int process_data(int data) {
printf("Processing data: %d\n", data);
int result = data * 2;
printf("Processed result: %d\n", result);
return result;
}
async int save_result(int result) {
printf("Saving result: %d\n", result);
printf("Result saved successfully\n");
return 1;
}
int main() {
printf("=== Python-like Async/Await Demo ===\n\n");
printf("Example 1: Sequential async operations\n");
int coro1 = fetch_data(1);
int data = await(coro1);
printf("Received data: %d\n\n", data);
printf("Example 2: Chained async operations\n");
int fetch_coro = fetch_data(2);
int fetched = await(fetch_coro);
int process_coro = process_data(fetched);
int processed = await(process_coro);
int save_coro = save_result(processed);
int saved = await(save_coro);
printf("Pipeline complete, status: %d\n\n", saved);
printf("Example 3: Parallel execution with gather\n");
int task1 = fetch_data(10);
int task2 = fetch_data(20);
int task3 = fetch_data(30);
printf("Started 3 parallel tasks\n");
gather(task1, task2, task3);
printf("All parallel tasks completed\n\n");
printf("Example 4: Mixed parallel and sequential\n");
int parallel1 = fetch_data(100);
int parallel2 = fetch_data(200);
gather(parallel1, parallel2);
printf("Parallel fetch completed\n");
int sequential = process_data(500);
int seq_result = await(sequential);
printf("Sequential processing result: %d\n\n", seq_result);
printf("Example 5: Multiple gather batches\n");
printf("Batch 1: Fetching\n");
int batch1_a = fetch_data(1);
int batch1_b = fetch_data(2);
int batch1_c = fetch_data(3);
gather(batch1_a, batch1_b, batch1_c);
printf("Batch 2: Processing\n");
int batch2_a = process_data(100);
int batch2_b = process_data(200);
gather(batch2_a, batch2_b);
printf("All batches completed\n\n");
printf("=== Demo Complete ===\n");
printf("This demonstrates Python-like async/await with:\n");
printf("- async function definitions\n");
printf("- await() for waiting on coroutines\n");
printf("- gather() for parallel execution\n");
printf("- Thread-safe execution\n");
return 0;
}