csys: helper functions for accessing scope elements

This commit is contained in:
Helmut Merz 2026-04-22 07:06:52 +02:00
parent 60912e0655
commit 386f6e1790

View file

@ -12,24 +12,34 @@ defmodule Scopes.CSys do
# predefined neuron processors # predefined neuron processors
def process(msg, scope) do def process(msg, scope) do
hd(elem(scope, 1)).(msg, scope) proc(scope).(msg, scope)
end end
def echo(msg, {_state, _code, _syns, env}) do def echo(msg, scope) do
send(env, msg) send(env(scope), msg)
end end
def forward(msg, {_state, _code, syns, _env}) do def forward(msg, scope) do
Enum.reduce(syns, false, fn s, _acc -> s.(msg); true end) Enum.reduce(syns(scope), false, fn s, _acc -> s.(msg) end)
end end
def std_proc({:parent}, {_state, _code, _syns, env}) do def std_proc({:parent}, scope) do
syn = synapse(self(), &Function.identity/1) syn = synapse(self(), &Function.identity/1)
new = neuron({[], [&std_proc/2], [syn], env}) new = neuron({[], [&std_proc/2], [syn], env(scope)})
send(env, {:created, new}) # send(env(scope), {:created, new})
echo({:created, new}, scope)
end end
def std_proc(msg, scope) do def std_proc(msg, scope) do
unless forward(msg, scope), do: echo(msg, scope) unless forward(msg, scope), do: echo(msg, scope)
end 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 end