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

98 lines
2.2 KiB
Nim

import
preserves, std/tables
type
AnyAtomKind* {.pure.} = enum
`bool`, `float`, `double`, `int`, `string`, `bytes`, `symbol`, `embedded`
AnyAtomBool* = bool
AnyAtomFloat* = float32
AnyAtomDouble* = float64
AnyAtomInt* = BiggestInt
AnyAtomString* = string
AnyAtomBytes* = seq[byte]
AnyAtomSymbol* = Symbol
AnyAtomEmbedded* = Value
`AnyAtom`* {.preservesOr.} = 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
DLit* {.preservesRecord: "lit".} = object
`value`*: AnyAtom
DBind* {.preservesRecord: "bind".} = object
`pattern`*: Pattern
DDiscard* {.preservesRecord: "_".} = object
DCompoundKind* {.pure.} = enum
`rec`, `arr`, `dict`
DCompoundRec* {.preservesRecord: "rec".} = object
`label`*: Value
`fields`*: seq[Pattern]
DCompoundArr* {.preservesRecord: "arr".} = object
`items`*: seq[Pattern]
DCompoundDict* {.preservesRecord: "dict".} = object
`entries`*: Table[Value, Pattern]
`DCompound`* {.preservesOr.} = object
case orKind*: DCompoundKind
of DCompoundKind.`rec`:
`rec`*: DCompoundRec
of DCompoundKind.`arr`:
`arr`*: DCompoundArr
of DCompoundKind.`dict`:
`dict`*: DCompoundDict
PatternKind* {.pure.} = enum
`DDiscard`, `DBind`, `DLit`, `DCompound`
`Pattern`* {.acyclic, preservesOr.} = ref object
case orKind*: PatternKind
of PatternKind.`DDiscard`:
`ddiscard`*: DDiscard
of PatternKind.`DBind`:
`dbind`*: DBind
of PatternKind.`DLit`:
`dlit`*: DLit
of PatternKind.`DCompound`:
`dcompound`*: DCompound
proc `$`*(x: AnyAtom | DLit | DBind | DDiscard | DCompound | Pattern): string =
`$`(toPreserves(x))
proc encode*(x: AnyAtom | DLit | DBind | DDiscard | DCompound | Pattern): seq[
byte] =
encode(toPreserves(x))