class_name RollPipes extends RefCounted ## Pipe snapshots and card inputs for the Roller: what an entry's pipes do ## (pipe_plan()), the values they pass, and binding a card's inputs for the ## subtree it runs. ## The snapshot of an entry's pipes, taken once after the entry's effects ran ## and its text expanded: one item per pipe, in pipe order, ## {"to": table id, "run": bool, "count": int, "with": {name: value}} ("with" ## only on a pipe that runs and passes input values; a value whose expression ## fails is left out, so the target's default applies, with an error line). ## - A pipe with a condition ("when") runs only if it's true. An error ## counts as false and adds an error line to `log`. ## - An "otherwise" pipe runs only if none of the entry's conditions was true. ## - Any other pipe always runs. ## Counts are worked out only for pipes that run (0 for the others); an ## expression count's error gives 0 and an error line. `why` gets the trace ## facts: {"type": "when", "to", "expr", "values": {name: value}, "result": ## bool, "error"?} per conditional pipe and {"type": "otherwise", "to", ## "result"} per otherwise pipe. static func pipe_plan(e: TableEntry, ctx: RollContext, log: Array = [], why: Array = []) -> Array: var plan: Array = [] var any_true := false for p in e.pipes: var w := e.when_for(p) var run := true if w != "": var r := Expr.evaluate(w, ctx) var fact := {"type": "when", "to": p, "expr": w, "values": Expr.values_read(w, ctx), "result": false} if r["error"] != "": log.append({"text": "Thread condition %s: %s" % [w, r["error"]], "error": true}) run = false fact["error"] = str(r["error"]) else: run = Expr.truthy(r["value"]) fact["result"] = run why.append(fact) any_true = any_true or run plan.append({"to": p, "run": run, "count": 0, "otherwise": w == "" and e.is_otherwise(p)}) for item: Dictionary in plan: if item["otherwise"]: item["run"] = not any_true why.append({"type": "otherwise", "to": item["to"], "result": item["run"]}) item.erase("otherwise") if item["run"]: item["count"] = e.count_value(str(item["to"]), ctx, log) if not e.pipe_with.is_empty() and e.pipe_with.has(item["to"]): item["with"] = with_values(e, str(item["to"]), ctx, log) return plan ## The input values a pipe passes, worked out now: {name: value}. A value ## whose expression fails is left out (the target's default applies) and adds ## an error line to `log`. static func with_values(e: TableEntry, target: String, ctx: RollContext, log: Array = []) -> Dictionary: var out := {} var w: Variant = e.pipe_with.get(target) if not (w is Dictionary): return out for nm: String in w: var src := str(w[nm]) var r := Expr.evaluate(src, ctx) if r["error"] != "": log.append({"text": "Thread input %s = %s: %s" % [nm, src, r["error"]], "error": true}) else: out[nm] = Expr.normalize(r["value"]) return out # ---------- Card inputs ---------- ## Binds a card's inputs as chain values for the subtree about to run: the ## pipe's "with" values (f["with"]), and each declared input's default for the ## rest. Pushes a Roller.OP_RESTORE frame first, so the previous values come ## back when the subtree ends. The trace facts and error lines go on the node ## via Roller._attach(). static func bind_inputs(t: RollTable, f: Dictionary, ctx: RollContext, stack: Array) -> void: var given: Dictionary = f.get("with", {}) if t.inputs.is_empty() and given.is_empty(): return var values := {} var facts: Array = [] var log: Array = [] for inp: Dictionary in t.inputs: var nm := str(inp.get("name", "")) if nm == "" or values.has(nm): continue if given.has(nm): values[nm] = given[nm] facts.append({"type": "input", "name": nm, "value": given[nm], "from": "pipe"}) continue var def := str(inp.get("default", "")).strip_edges() var v: Variant = 0 if def != "": var r := Expr.evaluate(def, ctx) if r["error"] != "": log.append({"text": "Input %s default %s: %s" % [nm, def, r["error"]], "error": true}) else: v = Expr.normalize(r["value"]) values[nm] = v facts.append({"type": "input", "name": nm, "value": v, "from": "default"}) for nm: String in given: if not values.has(nm): values[nm] = given[nm] facts.append({"type": "input", "name": nm, "value": given[nm], "from": "pipe"}) var saved := {} for nm: String in values: saved[nm] = [ctx.chain_vars.has(nm), ctx.chain_vars.get(nm)] ctx.chain_vars[nm] = values[nm] stack.append({"op": Roller.OP_RESTORE, "saved": saved}) f["inputs"] = values f["pre_why"] = facts f["pre_log"] = log ## Puts back the chain values a card's inputs hid. static func restore(f: Dictionary, ctx: RollContext) -> void: var saved: Dictionary = f["saved"] for nm: String in saved: var pair: Array = saved[nm] if pair[0]: ctx.chain_vars[nm] = pair[1] else: ctx.chain_vars.erase(nm)