syndicate-nim/src/syndicate/protocols/dataspacePatterns.nim

104 lines
2.5 KiB
Nim
Raw Normal View History

import
std/typetraits, preserves, std/tables
type
AnyAtomKind* {.pure.} = enum
`bool`, `float`, `double`, `int`, `string`, `bytes`, `symbol`, `embedded`
AnyAtomBool* = bool
AnyAtomFloat* = float32
AnyAtomDouble* = float64
AnyAtomInt* = int
AnyAtomString* = string
AnyAtomBytes* = seq[byte]
AnyAtomSymbol* = Symbol
AnyAtomEmbedded*[E] = Preserve[E]
`AnyAtom`*[E] {.preservesOr.} = ref object
case orKind*: AnyAtomKind
of AnyAtomKind.`bool`:
`bool`*: AnyAtomBool
of AnyAtomKind.`float`:
`float`*: AnyAtomFloat
of AnyAtomKind.`double`:
`double`*: AnyAtomDouble
of AnyAtomKind.`int`:
`int`*: AnyAtomInt
of AnyAtomKind.`string`:
`string`*: AnyAtomString
of AnyAtomKind.`bytes`:
`bytes`*: AnyAtomBytes
of AnyAtomKind.`symbol`:
`symbol`*: AnyAtomSymbol
of AnyAtomKind.`embedded`:
`embedded`*: AnyAtomEmbedded[E]
2021-09-24 19:25:47 +00:00
DLit*[E] {.preservesRecord: "lit".} = ref object
`value`*: AnyAtom[E]
2021-09-24 19:25:47 +00:00
DBind*[E] {.preservesRecord: "bind".} = ref object
`pattern`*: Pattern[E]
2021-09-24 19:25:47 +00:00
DDiscard* {.preservesRecord: "_".} = object
2021-11-03 18:21:52 +00:00
2021-09-24 19:25:47 +00:00
DCompoundKind* {.pure.} = enum
`rec`, `arr`, `dict`
DCompoundRec*[E] {.preservesRecord: "rec".} = ref object
`label`*: Preserve[E]
`fields`*: seq[Pattern[E]]
DCompoundArr*[E] {.preservesRecord: "arr".} = ref object
`items`*: seq[Pattern[E]]
DCompoundDict*[E] {.preservesRecord: "dict".} = ref object
`entries`*: Table[Preserve[E], Pattern[E]]
2021-09-24 19:25:47 +00:00
`DCompound`*[E] {.preservesOr.} = ref object
case orKind*: DCompoundKind
of DCompoundKind.`rec`:
`rec`*: DCompoundRec[E]
2021-09-24 19:25:47 +00:00
of DCompoundKind.`arr`:
`arr`*: DCompoundArr[E]
2021-09-24 19:25:47 +00:00
of DCompoundKind.`dict`:
`dict`*: DCompoundDict[E]
2021-09-24 19:25:47 +00:00
PatternKind* {.pure.} = enum
`DDiscard`, `DBind`, `DLit`, `DCompound`
`Pattern`*[E] {.preservesOr.} = ref object
case orKind*: PatternKind
of PatternKind.`DDiscard`:
`ddiscard`*: DDiscard
2021-09-24 19:25:47 +00:00
of PatternKind.`DBind`:
`dbind`*: DBind[E]
2021-09-24 19:25:47 +00:00
of PatternKind.`DLit`:
`dlit`*: DLit[E]
of PatternKind.`DCompound`:
`dcompound`*: DCompound[E]
proc `$`*[E](x: AnyAtom[E] | DLit[E] | DBind[E] | DCompound[E] | Pattern[E]): string =
`$`(toPreserve(x, E))
proc encode*[E](x: AnyAtom[E] | DLit[E] | DBind[E] | DCompound[E] | Pattern[E]): seq[
2021-09-24 19:25:47 +00:00
byte] =
encode(toPreserve(x, E))
2021-09-24 19:25:47 +00:00
proc `$`*(x: DDiscard): string =
2021-09-24 19:25:47 +00:00
`$`(toPreserve(x))
proc encode*(x: DDiscard): seq[byte] =
2021-09-24 19:25:47 +00:00
encode(toPreserve(x))