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

196 lines
4.7 KiB
Nim
Raw Normal View History

import
std/typetraits, preserves, std/tables, std/tables
type
2021-09-24 19:25:47 +00:00
CRec*[E] {.preservesRecord: "rec".} = ref object
`label`*: Preserve[E]
2021-11-03 18:21:52 +00:00
`arity`*: int
2021-09-24 19:25:47 +00:00
PCompound*[E] {.preservesRecord: "compound".} = ref object
`ctor`*: ConstructorSpec[E]
`members`*: PCompoundMembers[E]
2021-09-24 19:25:47 +00:00
ConstructorSpecKind* {.pure.} = enum
`CRec`, `CArr`, `CDict`
`ConstructorSpec`*[E] {.preservesOr.} = ref object
case orKind*: ConstructorSpecKind
of ConstructorSpecKind.`CRec`:
`crec`*: CRec[E]
of ConstructorSpecKind.`CArr`:
`carr`*: CArr
of ConstructorSpecKind.`CDict`:
`cdict`*: CDict
PAnd*[E] {.preservesRecord: "and".} = ref object
`patterns`*: seq[Pattern[E]]
Rewrite*[E] {.preservesRecord: "rewrite".} = ref object
`pattern`*: Pattern[E]
`template`*: Template[E]
TCompoundMembers*[E] = Table[Preserve[E], Template[E]]
TRef* {.preservesRecord: "ref".} = object
2021-11-03 18:21:52 +00:00
`binding`*: int
2021-09-24 19:25:47 +00:00
PBind*[E] {.preservesRecord: "bind".} = ref object
`pattern`*: Pattern[E]
Lit*[E] {.preservesRecord: "lit".} = ref object
`value`*: Preserve[E]
TCompound*[E] {.preservesRecord: "compound".} = ref object
`ctor`*: ConstructorSpec[E]
`members`*: TCompoundMembers[E]
`PAtom`* {.preservesOr.} = enum
`Boolean`, `Float`, `Double`, `SignedInteger`, `String`, `ByteString`,
`Symbol`
Attenuation*[E] = seq[Caveat[E]]
PDiscard* {.preservesRecord: "_".} = object
TemplateKind* {.pure.} = enum
`TAttenuate`, `TRef`, `Lit`, `TCompound`
`Template`*[E] {.preservesOr.} = ref object
case orKind*: TemplateKind
of TemplateKind.`TAttenuate`:
`tattenuate`*: TAttenuate[E]
of TemplateKind.`TRef`:
`tref`*: TRef
of TemplateKind.`Lit`:
`lit`*: Lit[E]
of TemplateKind.`TCompound`:
`tcompound`*: TCompound[E]
CaveatKind* {.pure.} = enum
`Rewrite`, `Alts`
2021-09-24 19:25:47 +00:00
`Caveat`*[E] {.preservesOr.} = ref object
case orKind*: CaveatKind
of CaveatKind.`Rewrite`:
`rewrite`*: Rewrite[E]
of CaveatKind.`Alts`:
`alts`*: Alts[E]
2021-09-24 19:25:47 +00:00
CArr* {.preservesRecord: "arr".} = object
2021-11-03 18:21:52 +00:00
`arity`*: int
2021-09-24 19:25:47 +00:00
PCompoundMembers*[E] = Table[Preserve[E], Pattern[E]]
PNot*[E] {.preservesRecord: "not".} = ref object
`pattern`*: Pattern[E]
2021-09-24 19:25:47 +00:00
SturdyRef*[E] {.preservesRecord: "ref".} = ref object
`oid`*: Preserve[E]
`caveatChain`*: seq[Attenuation[E]]
`sig`*: seq[byte]
2021-09-24 19:25:47 +00:00
WireRefKind* {.pure.} = enum
`mine`, `yours`
2021-09-24 19:25:47 +00:00
WireRefMine* {.preservesTuple.} = object
`data`* {.preservesLiteral: "0".}: bool
`oid`*: Oid
2021-09-24 19:25:47 +00:00
WireRefYours*[E] {.preservesTuple.} = ref object
`data`* {.preservesLiteral: "1".}: bool
`oid`*: Oid
`attenuation`* {.preservesTupleTail.}: seq[Caveat[E]]
2021-09-24 19:25:47 +00:00
`WireRef`*[E] {.preservesOr.} = ref object
case orKind*: WireRefKind
of WireRefKind.`mine`:
`mine`*: WireRefMine
2021-09-24 19:25:47 +00:00
of WireRefKind.`yours`:
`yours`*: WireRefYours[E]
2021-09-24 19:25:47 +00:00
TAttenuate*[E] {.preservesRecord: "attenuate".} = ref object
`template`*: Template[E]
`attenuation`*: Attenuation[E]
2021-11-03 18:21:52 +00:00
Oid* = int
2021-09-24 19:25:47 +00:00
Alts*[E] {.preservesRecord: "or".} = ref object
`alternatives`*: seq[Rewrite[E]]
2021-09-24 19:25:47 +00:00
CDict* {.preservesRecord: "dict".} = object
PatternKind* {.pure.} = enum
2021-09-24 19:25:47 +00:00
`PDiscard`, `PAtom`, `PEmbedded`, `PBind`, `PAnd`, `PNot`, `Lit`,
`PCompound`
`Pattern`*[E] {.preservesOr.} = ref object
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`:
`pbind`*: PBind[E]
2021-09-24 19:25:47 +00:00
of PatternKind.`PAnd`:
`pand`*: PAnd[E]
2021-09-24 19:25:47 +00:00
of PatternKind.`PNot`:
`pnot`*: PNot[E]
of PatternKind.`Lit`:
`lit`*: Lit[E]
2021-09-24 19:25:47 +00:00
of PatternKind.`PCompound`:
`pcompound`*: PCompound[E]
2021-09-24 19:25:47 +00:00
proc `$`*[E](x: CRec[E] | PCompound[E] | ConstructorSpec[E] | PAnd[E] |
Rewrite[E] |
TCompoundMembers[E] |
PBind[E] |
2021-09-24 19:25:47 +00:00
Lit[E] |
TCompound[E] |
Attenuation[E] |
Template[E] |
2021-09-24 19:25:47 +00:00
Caveat[E] |
PCompoundMembers[E] |
PNot[E] |
SturdyRef[E] |
WireRef[E] |
TAttenuate[E] |
2021-09-24 19:25:47 +00:00
Alts[E] |
Pattern[E]): string =
`$`(toPreserve(x, E))
2021-09-24 19:25:47 +00:00
proc encode*[E](x: CRec[E] | PCompound[E] | ConstructorSpec[E] | PAnd[E] |
Rewrite[E] |
TCompoundMembers[E] |
PBind[E] |
2021-09-24 19:25:47 +00:00
Lit[E] |
TCompound[E] |
Attenuation[E] |
Template[E] |
2021-09-24 19:25:47 +00:00
Caveat[E] |
PCompoundMembers[E] |
PNot[E] |
SturdyRef[E] |
WireRef[E] |
TAttenuate[E] |
2021-09-24 19:25:47 +00:00
Alts[E] |
Pattern[E]): seq[byte] =
encode(toPreserve(x, E))
2021-09-24 19:25:47 +00:00
proc `$`*(x: TRef | PDiscard | CArr | Oid | CDict): string =
`$`(toPreserve(x))
proc encode*(x: TRef | PDiscard | CArr | Oid | CDict): seq[byte] =
encode(toPreserve(x))