ex-scopes/lib/csys/csys.ex

45 lines
999 B
Elixir

defmodule Scopes.CSys do
alias Scopes.Core.Actor
def neuron(scope) do
Actor.create(fn msg -> process(msg, scope) end)
end
def synapse(rcvr, op) do
fn msg -> Actor.send(rcvr, op.(msg)) end
end
# predefined neuron processors
def process(msg, scope) do
proc(scope).(msg, scope)
end
def echo(msg, scope) do
send(env(scope), msg)
end
def forward(msg, scope) do
Enum.reduce(syns(scope), false, fn s, _acc -> s.(msg) end)
end
def std_proc({:parent}, scope) do
syn = synapse(self(), &Function.identity/1)
new = neuron({[], [&std_proc/2], [syn], env(scope)})
# send(env(scope), {:created, new})
echo({:created, new}, scope)
end
def std_proc(msg, scope) do
unless forward(msg, scope), do: echo(msg, scope)
end
# helper functions
def proc(scope), do: hd(code(scope))
def state(scope), do: elem(scope, 0)
def code(scope), do: elem(scope, 1)
def syns(scope), do: elem(scope, 2)
def env(scope), do: elem(scope, 3)
end