fix: Channel_RecvInt init + add channel example to README
- Initialize result to 0 in Channel_RecvInt/RecvFloat64 to handle closed channel - Add producer/consumer channel example in examples/channel.bux - Update README.md with Channels syntax preview
This commit is contained in:
@@ -124,6 +124,45 @@ func Main() -> int {
|
||||
}
|
||||
```
|
||||
|
||||
### Channels (Producer/Consumer)
|
||||
```bux
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
import Std::Task::{Task_Join, TaskHandle};
|
||||
import Std::Channel::{Channel, Channel_New, Channel_SendInt, Channel_RecvInt, Channel_Close};
|
||||
|
||||
func Producer(chPtr: *Channel<int>) {
|
||||
var i: int = 1;
|
||||
while i <= 5 {
|
||||
Channel_SendInt(chPtr, i * 10);
|
||||
i = i + 1;
|
||||
}
|
||||
Channel_Close<int>(chPtr);
|
||||
}
|
||||
|
||||
func Consumer(chPtr: *Channel<int>) {
|
||||
var total: int = 0;
|
||||
while true {
|
||||
let val: int = Channel_RecvInt(chPtr);
|
||||
if val == 0 { break; }
|
||||
total = total + val;
|
||||
PrintInt(val);
|
||||
PrintLine("");
|
||||
}
|
||||
PrintLine("Total:");
|
||||
PrintInt(total);
|
||||
PrintLine("");
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
let ch: Channel<int> = Channel_New<int>(3);
|
||||
let p: *void = spawn Producer(&ch);
|
||||
let c: *void = spawn Consumer(&ch);
|
||||
Task_Join(TaskHandle { handle: p });
|
||||
Task_Join(TaskHandle { handle: c });
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
### Hash Map
|
||||
```bux
|
||||
import Std::Map::{Map, Map_New, Map_Set, Map_Get};
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
import Std::Task::{Task_Join, TaskHandle};
|
||||
import Std::Channel::{Channel, Channel_New, Channel_SendInt, Channel_RecvInt, Channel_Close};
|
||||
|
||||
func Producer(chPtr: *Channel<int>) {
|
||||
var i: int = 1;
|
||||
while i <= 5 {
|
||||
Channel_SendInt(chPtr, i * 10);
|
||||
i = i + 1;
|
||||
}
|
||||
Channel_Close<int>(chPtr);
|
||||
}
|
||||
|
||||
func Consumer(chPtr: *Channel<int>) {
|
||||
var total: int = 0;
|
||||
while true {
|
||||
let val: int = Channel_RecvInt(chPtr);
|
||||
if val == 0 {
|
||||
break;
|
||||
}
|
||||
total = total + val;
|
||||
PrintInt(val);
|
||||
PrintLine("");
|
||||
}
|
||||
PrintLine("Total:");
|
||||
PrintInt(total);
|
||||
PrintLine("");
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
let ch: Channel<int> = Channel_New<int>(3);
|
||||
let p: *void = spawn Producer(&ch);
|
||||
let c: *void = spawn Consumer(&ch);
|
||||
Task_Join(TaskHandle { handle: p });
|
||||
Task_Join(TaskHandle { handle: c });
|
||||
return 0;
|
||||
}
|
||||
+23
-2
@@ -15,12 +15,12 @@ func Channel_New<T>(capacity: int64) -> Channel<T> {
|
||||
}
|
||||
|
||||
func Channel_Send<T>(ch: *Channel<T>, value: T) {
|
||||
bux_channel_send(ch.handle, &value);
|
||||
bux_channel_send(ch.handle, (&value) as *void);
|
||||
}
|
||||
|
||||
func Channel_Recv<T>(ch: *Channel<T>) -> T {
|
||||
var result: T;
|
||||
bux_channel_recv(ch.handle, &result);
|
||||
bux_channel_recv(ch.handle, (&result) as *void);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -32,4 +32,25 @@ func Channel_Free<T>(ch: *Channel<T>) {
|
||||
bux_channel_free(ch.handle);
|
||||
}
|
||||
|
||||
/* Non-generic wrappers for common types (avoid monomorphization issues) */
|
||||
func Channel_SendInt(ch: *Channel<int>, value: int) {
|
||||
bux_channel_send(ch.handle, (&value) as *void);
|
||||
}
|
||||
|
||||
func Channel_RecvInt(ch: *Channel<int>) -> int {
|
||||
var result: int = 0;
|
||||
bux_channel_recv(ch.handle, (&result) as *void);
|
||||
return result;
|
||||
}
|
||||
|
||||
func Channel_SendFloat64(ch: *Channel<float64>, value: float64) {
|
||||
bux_channel_send(ch.handle, (&value) as *void);
|
||||
}
|
||||
|
||||
func Channel_RecvFloat64(ch: *Channel<float64>) -> float64 {
|
||||
var result: float64 = 0.0;
|
||||
bux_channel_recv(ch.handle, (&result) as *void);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user