Phase 9: Agents (send/await/deref) + core.async channels (chan, >!, <!, close!, go). All phases complete.

This commit is contained in:
2026-05-08 20:28:51 +03:00
parent 1f9d662ed9
commit 30fde68fed
6 changed files with 222 additions and 10 deletions
+35
View File
@@ -0,0 +1,35 @@
# Simplified core.async for Clojure/Nim
# Provides channels with put!/take!/close! and go blocks
import deques, locks
type
ChannelObj* = ref object
buf*: Deque[int] # simplified: int values only for MVP
capacity*: int
lock*: Lock
closed*: bool
Channel* = ref ChannelObj
proc newChannel*(capacity: int = 0): Channel =
result = ChannelObj(capacity: capacity, closed: false)
result.buf = initDeque[int]()
initLock(result.lock)
proc put*(ch: Channel, val: int): bool =
withLock ch.lock:
if ch.closed: return false
if ch.capacity > 0 and ch.buf.len >= ch.capacity:
return false
ch.buf.addLast(val)
return true
proc take*(ch: Channel): (bool, int) =
withLock ch.lock:
if ch.buf.len == 0:
return (false, 0)
return (true, ch.buf.popFirst())
proc close*(ch: Channel) =
withLock ch.lock:
ch.closed = true