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

98 lines
2.2 KiB
Nim
Raw Normal View History

import
2023-05-18 10:20:44 +00:00
preserves, std/tables
type
AnyAtomKind* {.pure.} = enum
`bool`, `float`, `double`, `int`, `string`, `bytes`, `symbol`, `embedded`
AnyAtomBool* = bool
AnyAtomFloat* = float32
AnyAtomDouble* = float64
2022-07-08 12:49:24 +00:00
AnyAtomInt* = BiggestInt
AnyAtomString* = string
AnyAtomBytes* = seq[byte]
AnyAtomSymbol* = Symbol
2023-12-31 17:15:06 +00:00
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`:
2023-12-31 17:15:06 +00:00
`embedded`*: AnyAtomEmbedded
2023-12-31 17:15:06 +00:00
DLit* {.preservesRecord: "lit".} = object
`value`*: AnyAtom
2023-12-31 17:15:06 +00:00
DBind* {.preservesRecord: "bind".} = object
`pattern`*: Pattern
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`
2023-12-31 17:15:06 +00:00
DCompoundRec* {.preservesRecord: "rec".} = object
`label`*: Value
`fields`*: seq[Pattern]
2023-12-31 17:15:06 +00:00
DCompoundArr* {.preservesRecord: "arr".} = object
`items`*: seq[Pattern]
2023-12-31 17:15:06 +00:00
DCompoundDict* {.preservesRecord: "dict".} = object
`entries`*: Table[Value, Pattern]
2023-12-31 17:15:06 +00:00
`DCompound`* {.preservesOr.} = object
2021-09-24 19:25:47 +00:00
case orKind*: DCompoundKind
of DCompoundKind.`rec`:
2023-12-31 17:15:06 +00:00
`rec`*: DCompoundRec
2021-09-24 19:25:47 +00:00
of DCompoundKind.`arr`:
2023-12-31 17:15:06 +00:00
`arr`*: DCompoundArr
2021-09-24 19:25:47 +00:00
of DCompoundKind.`dict`:
2023-12-31 17:15:06 +00:00
`dict`*: DCompoundDict
2021-09-24 19:25:47 +00:00
PatternKind* {.pure.} = enum
`DDiscard`, `DBind`, `DLit`, `DCompound`
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.`DDiscard`:
`ddiscard`*: DDiscard
2021-09-24 19:25:47 +00:00
of PatternKind.`DBind`:
2023-12-31 17:15:06 +00:00
`dbind`*: DBind
2021-09-24 19:25:47 +00:00
of PatternKind.`DLit`:
2023-12-31 17:15:06 +00:00
`dlit`*: DLit
2021-09-24 19:25:47 +00:00
of PatternKind.`DCompound`:
2023-12-31 17:15:06 +00:00
`dcompound`*: DCompound
2021-09-24 19:25:47 +00:00
2023-12-31 17:15:06 +00:00
proc `$`*(x: AnyAtom | DLit | DBind | DDiscard | DCompound | Pattern): string =
`$`(toPreserves(x))
2023-12-31 17:15:06 +00:00
proc encode*(x: AnyAtom | DLit | DBind | DDiscard | DCompound | Pattern): seq[
byte] =
encode(toPreserves(x))