src/remove/run.bend

ezx@1.9.0.0 · 51 lines · 0 definitions

1# remove/run: the interpreter for `ez remove`. It reads the directory it runs
2# in, ez.toml, `.gitignore` and the `.bend` files git lists into a World, then
3# loops: it asks the planner (remove/relock.bend) what the lock after the
4# remove still needs, answers each question with the lock's interpreter
5# (`Run.answer.all`), and adds the answers to the World, until nothing is
6# left to ask. Then it runs the plan with the lock's effect runner
7# (`Run.exec.plan`), which exits 1 with the reason when the plan refuses. It
8# decides nothing; that it reads and executes faithfully is EZ-TRUST-2.
9import Base
10import snap@1.3.0.0/main.bend as R
11import ezx@1.9.0.0/src/io/file.bend as F
12import ezx@1.9.0.0/src/lock/run.bend as Run
13import ezx@1.9.0.0/src/lock/world.bend as W
14import ezx@1.9.0.0/src/remove/plan.bend as RP
15import ezx@1.9.0.0/src/remove/relock.bend as RR
16
17# the World with more of the lock's answers in it
18def more(world: RR.World, rs: List<&2, W.Reply>) -> RR.World:
19  RR.World{r, here, listing, locks} = world
20  RR.World{r, here, listing, List.append(&2, W.Reply, locks, rs)}
21
22# the planner's step: its questions answered and the loop gone round again,
23# or its plan run
24def loop.step(st: RR.Next, +lib: String, world: RR.World, go: RR.World -> IO(Unit)) -> IO(Unit):
25  match st:
26    case RR.AskLock{asks}:
27      do IO<Unit>:
28        rs : List<&2, W.Reply> <- Run.answer.all(lib, asks)
29        go(more(world, rs))
30    case RR.Planned{pl}:
31      Run.exec.plan(lib, pl)
32
33# the loop, under fuel. Every round adds an answer, since the lock never asks
34# what the World answers already, so the fuel (`Run.rounds`) is only a guard.
35def loop(fuel: Nat, +lib: String, +world: RR.World) -> IO(Unit):
36  match fuel:
37    case 0n:
38      IO.die(Unit, 1, "ez: ez remove kept asking questions")
39    case 1n+f:
40      loop.step(RR.next(world), lib, world, w => loop(f, lib, w))
41
42# `ez remove <name>`
43def run(+name: String) -> IO(Unit):
44  do IO<Unit>:
45    lib : String <- Run.bend.lib()
46    +pwd : String <- R.exec(["pwd"])
47    led : Maybe<&2, String> <- F.read(RP.toml())
48    ig : Maybe<&2, String> <- F.read(RP.ignore.file())
49    ls : W.Listing <- Run.read.listing()
50    loop(Run.rounds(), lib, RR.World{RP.World{name, led, F.text_of(ig)},
51      String.trim(R.text(pwd)), ls, []})