defmodule Scopes.CSys do alias Scopes.Core.Actor alias Scopes.CSys.Environ 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 notify(msg, scope) do Environ.notify(env(scope), msg) end def forward(msg, scope) do Enum.reduce(syns(scope), false, fn s, _acc -> s.(msg); true end) end def std_proc({:parent}, scope) do env = env(scope) syn = synapse(self(), &Function.identity/1) new = neuron({[], Environ.get_prog(env, :basic), [syn], env}) notify({:created, new}, scope) end def std_proc(msg, scope) do unless forward(msg, scope), do: notify(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