shrub/pkg/arvo/age/spider.hoon

463 lines
11 KiB
Plaintext
Raw Normal View History

2019-09-26 22:00:11 +03:00
/- spider
2019-11-10 10:27:07 +03:00
/+ libthread=thread, default-agent, verb
2019-09-26 07:12:58 +03:00
=, thread=thread:libthread
|%
2019-11-10 10:27:07 +03:00
+$ card card:agent:mall
+$ imp-thread imp:spider
+$ pid @udpid
+$ iid iid:spider
+$ imp (list pid)
+$ imput [=imp =cage]
+$ imp-form _*eval-form:eval:(thread ,vase)
+$ trie
$~ [*imp-form ~]
[=imp-form kid=(map pid trie)]
::
+$ state
2019-11-10 10:27:07 +03:00
$: started=(map imp vase)
running=trie
iid=(map iid imp)
count=pid
==
::
+$ clean-slate
$: started=(map imp vase)
running=(list imp)
iid=(map iid imp)
count=pid
==
::
+$ start-args
2019-11-12 08:36:32 +03:00
[parent=(unit iid) use=(unit iid) file=term =vase]
2019-11-10 10:27:07 +03:00
--
::
:: Trie operations
::
|%
++ get-imp
|= [=trie =imp]
^- (unit =imp-form)
?~ imp
`imp-form.trie
=/ son (~(get by kid.trie) i.imp)
?~ son
~
$(trie u.son, imp t.imp)
::
++ get-imp-children
|= [=trie =imp]
^- (list ^imp)
?~ imp
(turn (tap-imp trie) head)
=/ son (~(get by kid.trie) i.imp)
?~ son
~
$(trie u.son, imp t.imp)
::
::
++ has-imp
|= [=trie =imp]
!=(~ (get-imp trie imp))
::
++ put-imp
|= [=trie =imp =imp-form]
^+ trie
?~ imp
trie(imp-form imp-form)
=/ son (~(gut by kid.trie) i.imp [*^imp-form ~])
%= trie
kid
%+ ~(put by kid.trie) i.imp
$(trie son, imp t.imp)
==
::
++ del-imp
|= [=trie =imp]
^+ trie
?~ imp
trie
|-
?~ t.imp
trie(kid (~(del by kid.trie) i.imp))
=/ son (~(get by kid.trie) i.imp)
?~ son
trie
%= trie
kid
%+ ~(put by kid.trie) i.imp
$(trie u.son, imp t.imp)
==
2019-11-10 10:27:07 +03:00
::
++ tap-imp
=| =imp
|= =trie
^- (list [=^imp =imp-form])
%+ welp
?~ imp
~
[imp imp-form.trie]~
=/ kids ~(tap by kid.trie)
|- ^- (list [=^imp =imp-form])
?~ kids
~
=/ next-1 ^$(imp [p.i.kids imp], trie q.i.kids)
=/ next-2 $(kids t.kids)
(welp next-1 next-2)
2019-09-26 07:12:58 +03:00
--
2019-11-10 10:27:07 +03:00
::
2019-09-26 07:12:58 +03:00
^- agent:mall
2019-11-07 09:19:32 +03:00
=| =state
=<
2019-11-10 10:27:07 +03:00
%+ verb &
2019-09-26 07:12:58 +03:00
|_ =bowl:mall
2019-11-10 10:27:07 +03:00
+* this .
2019-11-07 09:19:32 +03:00
spider-core +>
2019-11-10 10:27:07 +03:00
sc ~(. spider-core bowl)
def ~(. (default-agent this %|) bowl)
2019-09-26 07:12:58 +03:00
::
2019-11-07 09:19:32 +03:00
++ on-init on-init:def
2019-11-10 10:27:07 +03:00
++ on-save clean-state:sc
++ on-load
|= old-state=vase
2019-11-10 10:27:07 +03:00
=+ !<(=clean-slate old-state)
=. count.state count.clean-slate
=. iid.state iid.clean-slate
|- ^- (quip card _this)
2019-11-10 10:27:07 +03:00
?~ running.clean-slate
`this
2019-11-10 10:27:07 +03:00
=^ cards-1 state
(handle-stop-imp:sc (imp-to-iid i.running.clean-slate) |)
=^ cards-2 this
$(running.clean-slate t.running.clean-slate)
[(weld cards-1 cards-2) this]
::
2019-11-07 09:19:32 +03:00
++ on-poke
2019-09-26 07:12:58 +03:00
|= [=mark =vase]
^- (quip card _this)
=^ cards state
2019-11-07 09:19:32 +03:00
?+ mark (on-poke:def mark vase)
%spider-imput (on-poke-imput:sc !<(imput vase))
2019-11-10 10:27:07 +03:00
%spider-start (handle-start-imp:sc !<(start-args vase))
%spider-stop (handle-stop-imp:sc !<([iid ?] vase))
2019-09-26 07:12:58 +03:00
==
[cards this]
::
2019-11-07 09:19:32 +03:00
++ on-watch
2019-10-11 01:30:24 +03:00
|= =path
^- (quip card _this)
=^ cards state
2019-11-07 09:19:32 +03:00
?+ path (on-watch:def path)
2019-11-12 08:36:32 +03:00
[%next-iid ~] on-watch-next-iid
[%imp @ *] (on-watch:sc t.path)
[%imp-result @ ~] (on-watch-result:sc i.t.path)
2019-10-11 01:30:24 +03:00
==
[cards this]
::
2019-11-07 09:19:32 +03:00
++ on-leave on-leave:def
++ on-peek
2019-09-29 07:44:31 +03:00
|= =path
^- (unit (unit cage))
2019-11-10 10:27:07 +03:00
?+ path (on-peek:def path)
[%x %tree ~]
``noun+!>((turn (tap-imp running.state) head))
::
[%x %started @ ~]
``noun+!>((has-imp running.state (~(got by iid.state) i.t.t.path)))
::
[%x %saxo @ ~]
``noun+!>((~(got by iid.state) i.t.t.path))
2019-09-29 07:44:31 +03:00
==
::
2019-11-07 09:19:32 +03:00
++ on-agent
|= [=wire =sign:agent:mall]
2019-09-29 07:44:31 +03:00
^- (quip card _this)
=^ cards state
?+ wire !!
[%imp @ *] (on-agent:sc i.t.wire t.t.wire sign)
2019-09-29 07:44:31 +03:00
==
[cards this]
::
2019-11-07 09:19:32 +03:00
++ on-arvo
2019-09-26 07:12:58 +03:00
|= [=wire =sign-arvo]
^- (quip card _this)
=^ cards state
2019-11-07 09:19:32 +03:00
?+ wire (on-arvo:def wire sign-arvo)
2019-09-26 22:00:11 +03:00
[%imp @ *] (handle-sign:sc i.t.wire t.t.wire sign-arvo)
2019-09-29 07:44:31 +03:00
[%find @ ~] (handle-find:sc i.t.wire sign-arvo)
2019-09-26 22:00:11 +03:00
[%build @ ~] (handle-build:sc i.t.wire sign-arvo)
2019-09-26 07:12:58 +03:00
==
[cards this]
2019-09-29 07:44:31 +03:00
::
2019-11-07 09:19:32 +03:00
++ on-fail on-fail:def
2019-09-26 07:12:58 +03:00
--
::
2019-11-07 09:19:32 +03:00
|_ =bowl:mall
++ on-poke-imput
2019-09-26 07:12:58 +03:00
|= imput
2019-11-10 10:27:07 +03:00
(take-input imp ~ %poke cage)
2019-09-26 07:12:58 +03:00
::
2019-11-07 09:19:32 +03:00
++ on-watch
2019-11-10 10:27:07 +03:00
|= [=iid =path]
(take-input (~(got by iid.state) iid) ~ %watch path)
2019-10-11 01:30:24 +03:00
::
++ on-watch-result
2019-11-10 10:27:07 +03:00
|= =iid
^- (quip card ^state)
2019-11-12 08:36:32 +03:00
?> (lth (slav %ud iid) count.state) :: (~(has by started.state) (~(got by iid.state) iid))
`state
::
2019-11-12 08:36:32 +03:00
++ on-watch-next-iid
^- (quip card ^state)
:_ state(count +(count.state))
:~ [%give %fact ~ %iid !>((scot %ud count.state))]
[%give %kick ~ ~]
==
::
2019-09-26 07:12:58 +03:00
++ handle-sign
2019-11-10 10:27:07 +03:00
|= [=iid =wire =sign-arvo]
=/ imp (~(get by iid.state) iid)
?~ imp
2019-11-12 08:36:32 +03:00
%- (slog leaf+"spider got sign for non-existent {<iid>}" ~)
2019-11-10 10:27:07 +03:00
`state
(take-input u.imp ~ %sign wire sign-arvo)
2019-09-26 07:12:58 +03:00
::
2019-11-07 09:19:32 +03:00
++ on-agent
2019-11-10 10:27:07 +03:00
|= [=iid =wire =sign:agent:mall]
=/ imp (~(get by iid.state) iid)
?~ imp
2019-11-12 08:36:32 +03:00
%- (slog leaf+"spider got agent for non-existent {<iid>}" ~)
2019-11-10 10:27:07 +03:00
`state
(take-input u.imp ~ %agent wire sign)
2019-09-29 07:44:31 +03:00
::
2019-09-26 07:12:58 +03:00
++ handle-start-imp
2019-11-12 08:36:32 +03:00
|= [parent-iid=(unit iid) use=(unit iid) file=term =vase]
2019-09-26 07:12:58 +03:00
^- (quip card ^state)
2019-11-10 10:27:07 +03:00
=/ parent-imp=imp
?~ parent-iid
/
(~(got by iid.state) u.parent-iid)
2019-11-12 08:36:32 +03:00
=^ new-iid count.state
?~ use
[(scot %ud count.state) +(count.state)]
[u.use count.state]
=/ =imp (snoc parent-imp (slav %ud new-iid))
2019-11-10 10:27:07 +03:00
::
?: (has-imp running.state imp)
~| [%already-started imp]
2019-09-26 07:12:58 +03:00
!!
2019-11-10 10:27:07 +03:00
?: (~(has by started.state) imp)
~| [%already-starting imp]
!!
2019-11-12 08:36:32 +03:00
::
2019-11-10 10:27:07 +03:00
=: started.state (~(put by started.state) imp vase)
iid.state (~(put by iid.state) new-iid imp)
==
2019-09-26 22:00:11 +03:00
=/ =card
2019-11-10 10:27:07 +03:00
=/ =schematic:ford [%path [our.bowl %home] %imp file]
[%pass /find/[new-iid] %arvo %f %build live=%.n schematic]
2019-09-29 07:44:31 +03:00
[[card ~] state]
::
++ handle-find
2019-11-10 10:27:07 +03:00
|= [=iid =sign-arvo]
2019-09-29 07:44:31 +03:00
^- (quip card ^state)
2019-11-10 10:27:07 +03:00
=/ =imp (~(got by iid.state) iid)
?> (~(has by started.state) imp)
2019-09-29 07:44:31 +03:00
?> ?=([%f %made *] sign-arvo)
?: ?=(%incomplete -.result.sign-arvo)
2019-11-10 10:27:07 +03:00
%- (slog leaf+"{<imp>} find incomplete" tang.result.sign-arvo)
2019-09-29 07:44:31 +03:00
`state
=/ =build-result:ford build-result.result.sign-arvo
?: ?=(%error -.build-result)
2019-11-10 10:27:07 +03:00
%- (slog leaf+"{<imp>} find error" message.build-result)
2019-09-29 07:44:31 +03:00
`state
?. ?=([%path *] +.build-result)
2019-11-10 10:27:07 +03:00
%- (slog leaf+"{<imp>} find strange" ~)
2019-09-29 07:44:31 +03:00
`state
=/ =card
=/ =schematic:ford [%core rail.build-result]
2019-11-10 10:27:07 +03:00
[%pass /build/[iid] %arvo %f %build live=%.y schematic]
2019-09-26 22:00:11 +03:00
[[card ~] state]
::
++ handle-build
2019-11-10 10:27:07 +03:00
|= [=iid =sign-arvo]
2019-09-26 22:00:11 +03:00
^- (quip card ^state)
2019-11-10 10:27:07 +03:00
=/ =imp (~(got by iid.state) iid)
?> (~(has by started.state) imp)
2019-09-26 22:00:11 +03:00
?> ?=([%f %made *] sign-arvo)
?: ?=(%incomplete -.result.sign-arvo)
2019-11-10 10:27:07 +03:00
%- (slog leaf+"{<imp>} build incomplete" tang.result.sign-arvo)
2019-09-26 22:00:11 +03:00
`state
=/ =build-result:ford build-result.result.sign-arvo
?: ?=(%error -.build-result)
2019-11-10 10:27:07 +03:00
%- (slog leaf+"{<imp>} build error" message.build-result)
2019-09-26 22:00:11 +03:00
`state
=/ =cage (result-to-cage:ford build-result)
?. ?=(%noun p.cage)
2019-11-10 10:27:07 +03:00
%- (slog leaf+"{<imp>} build not noun, is {<p.cage>}" ~)
2019-09-26 22:00:11 +03:00
`state
2019-11-10 10:27:07 +03:00
=/ maybe-imp (mule |.(!<(imp-thread q.cage)))
2019-09-26 22:00:11 +03:00
?: ?=(%| -.maybe-imp)
2019-11-10 10:27:07 +03:00
%- (slog leaf+"{<imp>} not valid imp" p.maybe-imp)
2019-09-26 22:00:11 +03:00
`state
2019-11-10 10:27:07 +03:00
(start-imp imp p.maybe-imp)
2019-09-26 22:00:11 +03:00
::
++ start-imp
2019-11-10 10:27:07 +03:00
|= [=imp =imp-thread]
2019-09-26 22:00:11 +03:00
^- (quip card ^state)
2019-11-10 10:27:07 +03:00
=/ =vase (~(got by started.state) imp)
=^ cards-1 state
2019-11-10 10:27:07 +03:00
?. (has-imp running.state imp)
`state
2019-11-10 10:27:07 +03:00
(imp-fail imp %updated ~)
=/ m (thread ,^vase)
2019-11-10 10:27:07 +03:00
=/ =bowl:spider (convert-bowl imp bowl)
=/ =eval-form:eval:m
(from-form:eval:m (imp-thread bowl vase))
=. running.state (put-imp running.state imp eval-form)
=^ cards-2 state
2019-11-10 10:27:07 +03:00
(take-input imp ~)
[(weld cards-1 cards-2) state]
2019-09-26 07:12:58 +03:00
::
2019-09-29 07:44:31 +03:00
++ handle-stop-imp
2019-11-10 10:27:07 +03:00
|= [=iid nice=?]
2019-09-29 07:44:31 +03:00
^- (quip card ^state)
2019-11-10 10:27:07 +03:00
=/ =imp (~(got by iid.state) iid)
~? !(has-imp running.state imp)
[%not-started imp]
?: nice
2019-11-10 10:27:07 +03:00
(imp-done imp *vase)
(imp-fail imp %cancelled ~)
2019-09-29 07:44:31 +03:00
::
2019-09-26 07:12:58 +03:00
++ take-input
2019-11-10 10:27:07 +03:00
|= [=imp input=(unit input:thread)]
2019-09-26 07:12:58 +03:00
^- (quip card ^state)
=/ m (thread ,vase)
2019-11-10 10:27:07 +03:00
?. (has-imp running.state imp)
%- (slog leaf+"spider got input for non-existent {<imp>} 2" ~)
`state
2019-09-26 07:12:58 +03:00
=/ =eval-form:eval:m
2019-11-10 10:27:07 +03:00
imp-form:(need (get-imp running.state imp))
2019-09-26 07:12:58 +03:00
=| cards=(list card)
|- ^- (quip card ^state)
=^ r=[cards=(list card) =eval-result:eval:m] eval-form
=/ out
%- mule |.
2019-11-10 10:27:07 +03:00
(take:eval:m eval-form (convert-bowl imp bowl) input)
2019-09-26 07:12:58 +03:00
?- -.out
%& p.out
%| [[~ [%fail %crash p.out]] eval-form]
==
2019-11-10 10:27:07 +03:00
=. running.state (put-imp running.state imp eval-form)
=/ =iid (imp-to-iid imp)
2019-09-26 07:12:58 +03:00
=. cards.r
%+ turn cards.r
|= =card
^- ^card
?+ card card
2019-11-10 10:27:07 +03:00
[%pass * *] [%pass [%imp iid p.card] q.card]
2019-11-07 09:19:32 +03:00
[%give %fact *]
2019-09-26 07:12:58 +03:00
?~ path.p.card
card
2019-11-10 10:27:07 +03:00
card(path.p `[%imp iid u.path.p.card])
2019-09-26 07:12:58 +03:00
::
2019-11-07 09:19:32 +03:00
[%give %kick *]
2019-09-26 07:12:58 +03:00
?~ path.p.card
card
2019-11-10 10:27:07 +03:00
card(path.p `[%imp iid u.path.p.card])
2019-09-26 07:12:58 +03:00
==
=. cards (weld cards cards.r)
=^ final-cards=(list card) state
?- -.eval-result.r
%next `state
2019-11-10 10:27:07 +03:00
%fail (imp-fail imp err.eval-result.r)
%done (imp-done imp value.eval-result.r)
2019-09-26 07:12:58 +03:00
==
[(weld cards final-cards) state]
::
++ imp-fail
2019-11-10 10:27:07 +03:00
|= [=imp =term =tang]
2019-09-26 07:12:58 +03:00
^- (quip card ^state)
2019-11-10 10:27:07 +03:00
%- (slog leaf+"thread {<imp>} failed" leaf+<term> tang)
=/ =iid (imp-to-iid imp)
=/ fail-cards=(list card)
2019-11-10 10:27:07 +03:00
:~ [%give %fact `/imp-result/[iid] %imp-fail !>([term tang])]
[%give %kick `/imp-result/[iid] ~]
==
2019-11-10 10:27:07 +03:00
=^ cards state (imp-clean imp)
[(weld fail-cards cards) state]
2019-09-26 07:12:58 +03:00
::
++ imp-done
2019-11-10 10:27:07 +03:00
|= [=imp =vase]
2019-09-26 07:12:58 +03:00
^- (quip card ^state)
2019-11-12 08:36:32 +03:00
:: %- (slog leaf+"thread {<imp>} finished" (sell vase) ~)
2019-11-10 10:27:07 +03:00
=/ =iid (imp-to-iid imp)
=/ done-cards=(list card)
2019-11-10 10:27:07 +03:00
:~ [%give %fact `/imp-result/[iid] %imp-done vase]
[%give %kick `/imp-result/[iid] ~]
==
2019-11-10 10:27:07 +03:00
=^ cards state (imp-clean imp)
[(weld done-cards cards) state]
::
++ imp-clean
2019-11-10 10:27:07 +03:00
|= =imp
^- (quip card ^state)
2019-11-10 10:27:07 +03:00
=/ children=(list ^imp)
[imp (get-imp-children running.state imp)]
|- ^- (quip card ^state)
?~ children
`state
=^ cards-children state $(children t.children)
=^ cards-our state
=/ =^imp i.children
=/ =iid (imp-to-iid imp)
=: started.state (~(del by started.state) imp)
running.state (del-imp running.state imp)
iid.state (~(del by iid.state) iid)
==
2019-11-10 10:27:07 +03:00
:_ state
:- [%pass /build/[iid] %arvo %f %kill ~]
%+ murn ~(tap by wex.bowl)
|= [[=wire =ship =term] [acked=? =path]]
^- (unit card)
?. ?& ?=([%imp @ *] wire)
=(iid i.t.wire)
==
~
`[%pass wire %agent [ship term] %leave ~]
[(welp cards-children cards-our) state]
::
++ convert-bowl
|= [=imp =bowl:mall]
^- bowl:spider
:* our.bowl
src.bowl
(imp-to-iid imp)
(imp-to-parent imp)
wex.bowl
sup.bowl
eny.bowl
now.bowl
byk.bowl
==
::
++ imp-to-iid
|= =imp
^- iid
=/ fimp (flop imp)
?> ?=([@ *] fimp)
(scot %ud i.fimp)
::
++ imp-to-parent
|= =imp
^- (unit iid)
=/ fimp (flop imp)
?> ?=([@ *] fimp)
?~ t.fimp
~
2019-11-10 10:27:07 +03:00
`(scot %ud i.t.fimp)
::
++ clean-state
!> ^- clean-slate
state(running (turn (tap-imp running.state) head))
2019-09-26 07:12:58 +03:00
--