2021-10-06 09:30:38 +00:00
|
|
|
|
|
|
|
import
|
2022-03-07 23:53:05 +00:00
|
|
|
std/typetraits, preserves, std/tables
|
2021-10-06 09:30:38 +00:00
|
|
|
|
|
|
|
type
|
2022-03-07 23:53:05 +00:00
|
|
|
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-10-06 09:30:38 +00:00
|
|
|
|
2022-03-07 23:53:05 +00:00
|
|
|
|
2021-09-24 19:25:47 +00:00
|
|
|
DLit*[E] {.preservesRecord: "lit".} = ref object
|
2022-03-07 23:53:05 +00:00
|
|
|
`value`*: AnyAtom[E]
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2021-09-24 19:25:47 +00:00
|
|
|
DBind*[E] {.preservesRecord: "bind".} = ref object
|
2021-10-06 09:30:38 +00:00
|
|
|
`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
|
2021-10-06 09:30:38 +00:00
|
|
|
`rec`, `arr`, `dict`
|
2022-03-07 23:53:05 +00:00
|
|
|
DCompoundRec*[E] {.preservesRecord: "rec".} = ref object
|
|
|
|
`label`*: Preserve[E]
|
|
|
|
`fields`*: seq[Pattern[E]]
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2022-03-07 23:53:05 +00:00
|
|
|
DCompoundArr*[E] {.preservesRecord: "arr".} = ref object
|
|
|
|
`items`*: seq[Pattern[E]]
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2022-03-07 23:53:05 +00:00
|
|
|
DCompoundDict*[E] {.preservesRecord: "dict".} = ref object
|
|
|
|
`entries`*: Table[Preserve[E], Pattern[E]]
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2021-09-24 19:25:47 +00:00
|
|
|
`DCompound`*[E] {.preservesOr.} = ref object
|
|
|
|
case orKind*: DCompoundKind
|
|
|
|
of DCompoundKind.`rec`:
|
|
|
|
`rec`*: DCompoundRec[E]
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2021-09-24 19:25:47 +00:00
|
|
|
of DCompoundKind.`arr`:
|
|
|
|
`arr`*: DCompoundArr[E]
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2021-09-24 19:25:47 +00:00
|
|
|
of DCompoundKind.`dict`:
|
|
|
|
`dict`*: DCompoundDict[E]
|
2021-10-06 09:30:38 +00:00
|
|
|
|
|
|
|
|
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-10-06 09:30:38 +00:00
|
|
|
|
2021-09-24 19:25:47 +00:00
|
|
|
of PatternKind.`DBind`:
|
|
|
|
`dbind`*: DBind[E]
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2021-09-24 19:25:47 +00:00
|
|
|
of PatternKind.`DLit`:
|
|
|
|
`dlit`*: DLit[E]
|
|
|
|
|
|
|
|
of PatternKind.`DCompound`:
|
|
|
|
`dcompound`*: DCompound[E]
|
|
|
|
|
|
|
|
|
2022-03-07 23:53:05 +00:00
|
|
|
proc `$`*[E](x: AnyAtom[E] | DLit[E] | DBind[E] | DCompound[E] | Pattern[E]): string =
|
2021-10-06 09:30:38 +00:00
|
|
|
`$`(toPreserve(x, E))
|
|
|
|
|
2022-03-07 23:53:05 +00:00
|
|
|
proc encode*[E](x: AnyAtom[E] | DLit[E] | DBind[E] | DCompound[E] | Pattern[E]): seq[
|
2021-09-24 19:25:47 +00:00
|
|
|
byte] =
|
2021-10-06 09:30:38 +00:00
|
|
|
encode(toPreserve(x, E))
|
2021-09-24 19:25:47 +00:00
|
|
|
|
2022-03-07 23:53:05 +00:00
|
|
|
proc `$`*(x: DDiscard): string =
|
2021-09-24 19:25:47 +00:00
|
|
|
`$`(toPreserve(x))
|
|
|
|
|
2022-03-07 23:53:05 +00:00
|
|
|
proc encode*(x: DDiscard): seq[byte] =
|
2021-09-24 19:25:47 +00:00
|
|
|
encode(toPreserve(x))
|