|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 | "io" |
6 | 7 | "os" |
@@ -39,12 +40,45 @@ func loadIR(path string, stdin io.Reader) (*state.IR[string, string, any], error |
39 | 40 | // target, for example), so the panic is recovered and returned as an error |
40 | 41 | // rather than crashing the tool. |
41 | 42 | func quench(ir *state.IR[string, string, any]) (m *state.Machine[string, string, any], err error) { |
| 43 | + return quenchWith(ir, stubRegistry(ir)) |
| 44 | +} |
| 45 | + |
| 46 | +// quenchWith binds an IR against a caller-supplied registry and assembles a |
| 47 | +// *Machine. Like quench, it recovers the panic Quench raises on a structural |
| 48 | +// defect and returns it as an error. simulate uses this to bind a registry whose |
| 49 | +// guards return seeded verdicts rather than the always-false stubs. |
| 50 | +func quenchWith(ir *state.IR[string, string, any], reg *state.Registry[any]) (m *state.Machine[string, string, any], err error) { |
42 | 51 | defer func() { |
43 | 52 | if r := recover(); r != nil { |
44 | 53 | m = nil |
45 | 54 | err = fmt.Errorf("quench: %v", r) |
46 | 55 | } |
47 | 56 | }() |
48 | | - reg := stubRegistry(ir) |
49 | 57 | return ir.Provide(reg).Quench(), nil |
50 | 58 | } |
| 59 | + |
| 60 | +// simulateRegistry walks an IR to enumerate every referenced behavior name, then |
| 61 | +// registers behaviors suitable for a structural simulation. Guards return the |
| 62 | +// seeded verdict from verdicts (defaulting to false for an unseeded guard), so a |
| 63 | +// caller can drive a machine down a chosen path without real implementations. |
| 64 | +// Actions, reducers, and services remain total no-ops, matching stubRegistry. |
| 65 | +func simulateRegistry(ir *state.IR[string, string, any], verdicts map[string]bool) *state.Registry[any] { |
| 66 | + var b behaviorNames |
| 67 | + for i := range ir.States { |
| 68 | + b.walkState(&ir.States[i]) |
| 69 | + } |
| 70 | + reg := state.NewRegistry[any]() |
| 71 | + for name := range b.guards { |
| 72 | + reg.Guard(name, func(state.GuardCtx[any]) bool { return verdicts[name] }) |
| 73 | + } |
| 74 | + for name := range b.actions { |
| 75 | + reg.Action(name, func(state.ActionCtx[any]) (state.Effect, error) { return nil, nil }) |
| 76 | + } |
| 77 | + for name := range b.reducers { |
| 78 | + reg.Reducer(name, func(in state.AssignCtx[any]) any { return in.Entity }) |
| 79 | + } |
| 80 | + for name := range b.services { |
| 81 | + reg.Service(name, func(context.Context, state.ServiceCtx[any]) (any, error) { return nil, nil }) |
| 82 | + } |
| 83 | + return reg |
| 84 | +} |
0 commit comments