syndicate_utils/src/syndump.nim

70 lines
1.9 KiB
Nim
Raw Permalink Normal View History

2023-08-24 08:20:33 +00:00
# SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/[os, tables]
2023-12-25 23:11:54 +00:00
import preserves, syndicate, syndicate/[durings, relays]
2023-08-24 08:20:33 +00:00
2023-12-25 23:11:54 +00:00
proc parsePattern(pr: Value): Pattern =
2023-08-24 08:20:33 +00:00
let
2023-12-25 23:11:54 +00:00
dropSigil = initRecord("lit", "_".toSymbol)
grabSigil = initRecord("lit", "?".toSymbol)
var pr = grab(pr).toPreserves
apply(pr) do (pr: var Value):
2023-08-24 08:20:33 +00:00
if pr == dropSigil:
2023-12-25 23:11:54 +00:00
pr = initRecord("_")
2023-08-24 08:20:33 +00:00
elif pr == grabSigil:
2023-12-25 23:11:54 +00:00
pr = initRecord("bind", initRecord("_"))
doAssert result.fromPreserves(pr)
2023-08-24 08:20:33 +00:00
2023-11-30 07:50:16 +00:00
proc inputPatterns: seq[Pattern] =
2023-08-24 08:20:33 +00:00
var args = commandLineParams()
2023-11-30 07:50:16 +00:00
result.setLen(args.len)
for i, input in args:
2023-12-25 23:11:54 +00:00
try: result[i] = input.parsePreserves.parsePattern
2023-08-25 19:11:24 +00:00
except ValueError:
quit "failed to parse Preserves argument"
2023-08-24 08:20:33 +00:00
type DumpEntity {.final.} = ref object of Entity
2023-12-25 23:11:54 +00:00
assertions: Table[Handle, seq[Value]]
2023-08-24 08:20:33 +00:00
2023-12-25 23:11:54 +00:00
proc toLine(values: seq[Value]; prefix: char): string =
2023-08-24 08:20:33 +00:00
result = newStringOfCap(1024)
let sep = getEnv("FS", " ")
result.add(prefix)
for v in values:
add(result, sep)
add(result, $v)
add(result, '\n')
2024-04-30 11:56:48 +00:00
method publish(dump: DumpEntity; turn: Turn; ass: AssertionRef; h: Handle) =
2023-08-24 08:20:33 +00:00
var values = ass.value.sequence
stdout.write(values.toLine('+'))
stdout.flushFile()
dump.assertions[h] = values
2024-04-30 11:56:48 +00:00
method retract(dump: DumpEntity; turn: Turn; h: Handle) =
2023-12-25 23:11:54 +00:00
var values: seq[Value]
2023-08-24 08:20:33 +00:00
if dump.assertions.pop(h, values):
stdout.write(values.toLine('-'))
stdout.flushFile()
2024-04-30 11:56:48 +00:00
method message*(dump: DumpEntity; turn: Turn; ass: AssertionRef) =
2023-08-24 08:20:33 +00:00
stdout.write(ass.value.sequence.toLine('!'))
stdout.flushFile()
proc exitProc() {.noconv.} =
stdout.write('\n')
quit()
proc main =
let
2023-11-30 07:50:16 +00:00
patterns = inputPatterns()
2023-08-24 08:20:33 +00:00
entity = DumpEntity()
2024-04-30 11:56:48 +00:00
runActor("syndex_card") do (turn: Turn):
resolveEnvironment(turn) do (turn: Turn; ds: Cap):
2023-11-30 07:50:16 +00:00
for pat in patterns:
2023-10-21 18:03:11 +00:00
discard observe(turn, ds, pat, entity)
2023-08-24 08:20:33 +00:00
setControlCHook(exitProc)
main()