2021-10-06 09:30:38 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
std/typetraits, preserves, std/tables, std/tables, std/tables
|
|
|
|
|
|
|
|
type
|
2021-09-24 19:25:47 +00:00
|
|
|
CRec*[E] {.preservesRecord: "rec".} = ref object
|
|
|
|
`label`*: Preserve[E]
|
|
|
|
`arity`*: BiggestInt
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2021-09-24 19:25:47 +00:00
|
|
|
DLit*[E] {.preservesRecord: "lit".} = ref object
|
|
|
|
`value`*: Preserve[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
|
|
|
|
|
|
|
|
CArr* {.preservesRecord: "arr".} = object
|
|
|
|
`arity`*: BiggestInt
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2021-09-24 19:25:47 +00:00
|
|
|
DCompoundKind* {.pure.} = enum
|
2021-10-06 09:30:38 +00:00
|
|
|
`rec`, `arr`, `dict`
|
2021-09-24 19:25:47 +00:00
|
|
|
DCompoundRec*[E] {.preservesRecord: "compound".} = ref object
|
2021-10-06 09:30:38 +00:00
|
|
|
`ctor`*: CRec[E]
|
2021-09-24 19:25:47 +00:00
|
|
|
`members`*: Table[BiggestInt, Pattern[E]]
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2021-09-24 19:25:47 +00:00
|
|
|
DCompoundArr*[E] {.preservesRecord: "compound".} = ref object
|
2021-10-06 09:30:38 +00:00
|
|
|
`ctor`*: CArr
|
2021-09-24 19:25:47 +00:00
|
|
|
`members`*: Table[BiggestInt, Pattern[E]]
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2021-09-24 19:25:47 +00:00
|
|
|
DCompoundDict*[E] {.preservesRecord: "compound".} = ref object
|
2021-10-06 09:30:38 +00:00
|
|
|
`ctor`*: CDict
|
2021-09-24 19:25:47 +00:00
|
|
|
`members`*: 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
|
|
|
CDict* {.preservesRecord: "dict".} = object
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
|
|
|
|
proc `$`*[E](x: CRec[E] | DLit[E] | DBind[E] | DCompound[E] | Pattern[E]): string =
|
2021-10-06 09:30:38 +00:00
|
|
|
`$`(toPreserve(x, E))
|
|
|
|
|
2021-09-24 19:25:47 +00:00
|
|
|
proc encode*[E](x: CRec[E] | DLit[E] | DBind[E] | DCompound[E] | Pattern[E]): seq[
|
|
|
|
byte] =
|
2021-10-06 09:30:38 +00:00
|
|
|
encode(toPreserve(x, E))
|
2021-09-24 19:25:47 +00:00
|
|
|
|
|
|
|
proc `$`*(x: DDiscard | CArr | CDict): string =
|
|
|
|
`$`(toPreserve(x))
|
|
|
|
|
|
|
|
proc encode*(x: DDiscard | CArr | CDict): seq[byte] =
|
|
|
|
encode(toPreserve(x))
|