syndicate-nim/src/syndicate/protocols/sturdy.nim

253 lines
5.8 KiB
Nim
Raw Normal View History

import
2024-01-06 11:59:38 +00:00
preserves, std/tables, std/options
type
PCompoundKind* {.pure.} = enum
`rec`, `arr`, `dict`
2023-12-31 17:15:06 +00:00
PCompoundRec* {.preservesRecord: "rec".} = object
`label`*: Value
`fields`*: seq[Pattern]
2021-09-24 19:25:47 +00:00
2023-12-31 17:15:06 +00:00
PCompoundArr* {.preservesRecord: "arr".} = object
`items`*: seq[Pattern]
2023-12-31 17:15:06 +00:00
PCompoundDict* {.preservesRecord: "dict".} = object
`entries`*: Table[Value, Pattern]
2021-09-24 19:25:47 +00:00
2023-12-31 17:15:06 +00:00
`PCompound`* {.preservesOr.} = object
case orKind*: PCompoundKind
of PCompoundKind.`rec`:
2023-12-31 17:15:06 +00:00
`rec`*: PCompoundRec
of PCompoundKind.`arr`:
2023-12-31 17:15:06 +00:00
`arr`*: PCompoundArr
of PCompoundKind.`dict`:
2023-12-31 17:15:06 +00:00
`dict`*: PCompoundDict
2021-09-24 19:25:47 +00:00
2022-12-08 08:15:01 +00:00
2023-12-31 17:15:06 +00:00
Reject* {.preservesRecord: "reject".} = object
`pattern`*: Pattern
2023-05-18 10:20:44 +00:00
CaveatsFieldKind* {.pure.} = enum
`present`, `invalid`, `absent`
2023-12-31 17:15:06 +00:00
CaveatsFieldPresent* {.preservesDictionary.} = object
`caveats`*: seq[Caveat]
2023-05-18 10:20:44 +00:00
2023-12-31 17:15:06 +00:00
CaveatsFieldInvalid* {.preservesDictionary.} = object
`caveats`*: Value
2023-05-18 10:20:44 +00:00
CaveatsFieldAbsent* {.preservesDictionary.} = object
2023-12-31 17:15:06 +00:00
`CaveatsField`* {.preservesOr.} = object
2023-05-18 10:20:44 +00:00
case orKind*: CaveatsFieldKind
of CaveatsFieldKind.`present`:
2023-12-31 17:15:06 +00:00
`present`*: CaveatsFieldPresent
2023-05-18 10:20:44 +00:00
of CaveatsFieldKind.`invalid`:
2023-12-31 17:15:06 +00:00
`invalid`*: CaveatsFieldInvalid
2023-05-18 10:20:44 +00:00
of CaveatsFieldKind.`absent`:
`absent`*: CaveatsFieldAbsent
2023-12-31 17:15:06 +00:00
SturdyDescriptionDetail* {.preservesDictionary.} = object
2023-05-18 10:20:44 +00:00
`key`*: seq[byte]
2023-12-31 17:15:06 +00:00
`oid`*: Value
2023-05-18 10:20:44 +00:00
2023-12-31 17:15:06 +00:00
PAnd* {.preservesRecord: "and".} = object
`patterns`*: seq[Pattern]
2021-09-24 19:25:47 +00:00
2023-12-31 17:15:06 +00:00
SturdyStepDetail* = Parameters
Rewrite* {.preservesRecord: "rewrite".} = object
`pattern`*: Pattern
`template`*: Template
2021-09-24 19:25:47 +00:00
2024-01-06 11:59:38 +00:00
ParametersCaveats* = Option[Value]
ParametersOid* = Value
ParametersSig* = seq[byte]
`Parameters`* {.preservesDictionary.} = object
`caveats`*: Option[Value]
`oid`*: Value
`sig`*: seq[byte]
2021-09-24 19:25:47 +00:00
TRef* {.preservesRecord: "ref".} = object
2022-07-08 12:49:24 +00:00
`binding`*: BiggestInt
2021-09-24 19:25:47 +00:00
2023-12-31 17:15:06 +00:00
PBind* {.preservesRecord: "bind".} = object
`pattern`*: Pattern
2021-09-24 19:25:47 +00:00
2023-12-31 17:15:06 +00:00
Lit* {.preservesRecord: "lit".} = object
`value`*: Value
2021-09-24 19:25:47 +00:00
TCompoundKind* {.pure.} = enum
`rec`, `arr`, `dict`
2023-12-31 17:15:06 +00:00
TCompoundRec* {.preservesRecord: "rec".} = object
`label`*: Value
`fields`*: seq[Template]
2023-12-31 17:15:06 +00:00
TCompoundArr* {.preservesRecord: "arr".} = object
`items`*: seq[Template]
2023-12-31 17:15:06 +00:00
TCompoundDict* {.preservesRecord: "dict".} = object
`entries`*: Table[Value, Template]
2023-12-31 17:15:06 +00:00
`TCompound`* {.preservesOr.} = object
case orKind*: TCompoundKind
of TCompoundKind.`rec`:
2023-12-31 17:15:06 +00:00
`rec`*: TCompoundRec
of TCompoundKind.`arr`:
2023-12-31 17:15:06 +00:00
`arr`*: TCompoundArr
2021-09-24 19:25:47 +00:00
of TCompoundKind.`dict`:
2023-12-31 17:15:06 +00:00
`dict`*: TCompoundDict
2022-12-08 08:15:01 +00:00
2023-12-31 17:15:06 +00:00
SturdyPathStepDetail* = Parameters
`PAtom`* {.preservesOr, pure.} = enum
2024-02-08 15:25:31 +00:00
`Boolean`, `Double`, `SignedInteger`, `String`, `ByteString`, `Symbol`
2021-09-24 19:25:47 +00:00
PDiscard* {.preservesRecord: "_".} = object
2022-12-08 08:15:01 +00:00
2021-09-24 19:25:47 +00:00
TemplateKind* {.pure.} = enum
`TAttenuate`, `TRef`, `Lit`, `TCompound`
2023-12-31 17:15:06 +00:00
`Template`* {.acyclic, preservesOr.} = ref object
2021-09-24 19:25:47 +00:00
case orKind*: TemplateKind
of TemplateKind.`TAttenuate`:
2023-12-31 17:15:06 +00:00
`tattenuate`*: TAttenuate
2021-09-24 19:25:47 +00:00
of TemplateKind.`TRef`:
`tref`*: TRef
of TemplateKind.`Lit`:
2023-12-31 17:15:06 +00:00
`lit`*: Lit
2021-09-24 19:25:47 +00:00
of TemplateKind.`TCompound`:
2023-12-31 17:15:06 +00:00
`tcompound`*: TCompound
2022-12-08 08:15:01 +00:00
CaveatKind* {.pure.} = enum
2023-05-18 10:20:44 +00:00
`Rewrite`, `Alts`, `Reject`, `unknown`
2023-12-31 17:15:06 +00:00
`Caveat`* {.preservesOr.} = object
2021-09-24 19:25:47 +00:00
case orKind*: CaveatKind
of CaveatKind.`Rewrite`:
2023-12-31 17:15:06 +00:00
`rewrite`*: Rewrite
of CaveatKind.`Alts`:
2023-12-31 17:15:06 +00:00
`alts`*: Alts
2021-09-24 19:25:47 +00:00
2023-05-18 10:20:44 +00:00
of CaveatKind.`Reject`:
2023-12-31 17:15:06 +00:00
`reject`*: Reject
2023-05-18 10:20:44 +00:00
of CaveatKind.`unknown`:
2024-01-07 22:11:59 +00:00
`unknown`*: Value
2023-05-18 10:20:44 +00:00
2022-12-08 08:15:01 +00:00
2023-12-31 17:15:06 +00:00
PNot* {.preservesRecord: "not".} = object
`pattern`*: Pattern
2023-12-31 17:15:06 +00:00
SturdyRef* {.preservesRecord: "ref".} = object
`parameters`*: Parameters
2021-09-24 19:25:47 +00:00
WireRefKind* {.pure.} = enum
`mine`, `yours`
2021-09-24 19:25:47 +00:00
WireRefMine* {.preservesTuple.} = object
2023-05-06 19:22:21 +00:00
`field0`* {.preservesLiteral: "0".}: tuple[]
2021-09-24 19:25:47 +00:00
`oid`*: Oid
2023-12-31 17:15:06 +00:00
WireRefYours* {.preservesTuple.} = object
2023-05-06 19:22:21 +00:00
`field0`* {.preservesLiteral: "1".}: tuple[]
2021-09-24 19:25:47 +00:00
`oid`*: Oid
2023-12-31 17:15:06 +00:00
`attenuation`* {.preservesTupleTail.}: seq[Caveat]
2023-12-31 17:15:06 +00:00
`WireRef`* {.preservesOr.} = object
2021-09-24 19:25:47 +00:00
case orKind*: WireRefKind
of WireRefKind.`mine`:
`mine`*: WireRefMine
2021-09-24 19:25:47 +00:00
of WireRefKind.`yours`:
2023-12-31 17:15:06 +00:00
`yours`*: WireRefYours
2022-07-08 12:49:24 +00:00
2023-12-31 17:15:06 +00:00
TAttenuate* {.preservesRecord: "attenuate".} = object
`template`*: Template
`attenuation`*: seq[Caveat]
2022-07-08 12:49:24 +00:00
Oid* = BiggestInt
2023-12-31 17:15:06 +00:00
Alts* {.preservesRecord: "or".} = object
`alternatives`*: seq[Rewrite]
PatternKind* {.pure.} = enum
2021-09-24 19:25:47 +00:00
`PDiscard`, `PAtom`, `PEmbedded`, `PBind`, `PAnd`, `PNot`, `Lit`,
`PCompound`
2023-12-31 17:15:06 +00:00
`Pattern`* {.acyclic, preservesOr.} = ref object
2021-09-24 19:25:47 +00:00
case orKind*: PatternKind
of PatternKind.`PDiscard`:
`pdiscard`*: PDiscard
2021-09-24 19:25:47 +00:00
of PatternKind.`PAtom`:
`patom`*: PAtom
2021-09-24 19:25:47 +00:00
of PatternKind.`PEmbedded`:
`pembedded`* {.preservesLiteral: "Embedded".}: bool
2021-09-24 19:25:47 +00:00
of PatternKind.`PBind`:
2023-12-31 17:15:06 +00:00
`pbind`*: PBind
2021-09-24 19:25:47 +00:00
of PatternKind.`PAnd`:
2023-12-31 17:15:06 +00:00
`pand`*: PAnd
2021-09-24 19:25:47 +00:00
of PatternKind.`PNot`:
2023-12-31 17:15:06 +00:00
`pnot`*: PNot
of PatternKind.`Lit`:
2023-12-31 17:15:06 +00:00
`lit`*: Lit
2021-09-24 19:25:47 +00:00
of PatternKind.`PCompound`:
2023-12-31 17:15:06 +00:00
`pcompound`*: PCompound
2022-12-08 08:15:01 +00:00
2023-12-31 17:15:06 +00:00
proc `$`*(x: PCompound | Reject | CaveatsField | SturdyDescriptionDetail | PAnd |
SturdyStepDetail |
Rewrite |
Parameters |
TRef |
PBind |
Lit |
TCompound |
SturdyPathStepDetail |
PDiscard |
Template |
Caveat |
PNot |
SturdyRef |
WireRef |
TAttenuate |
Oid |
Alts |
Pattern): string =
`$`(toPreserves(x))
proc encode*(x: PCompound | Reject | CaveatsField | SturdyDescriptionDetail |
PAnd |
SturdyStepDetail |
Rewrite |
Parameters |
TRef |
PBind |
Lit |
TCompound |
SturdyPathStepDetail |
PDiscard |
Template |
Caveat |
PNot |
SturdyRef |
WireRef |
TAttenuate |
Oid |
Alts |
Pattern): seq[byte] =
encode(toPreserves(x))