2021-10-06 09:30:38 +00:00
|
|
|
|
|
|
|
import
|
2023-05-18 10:20:44 +00:00
|
|
|
preserves, std/tables
|
2021-10-06 09:30:38 +00:00
|
|
|
|
|
|
|
type
|
2022-03-07 23:53:05 +00:00
|
|
|
AnyAtomKind* {.pure.} = enum
|
2024-02-08 15:25:31 +00:00
|
|
|
`bool`, `double`, `int`, `string`, `bytes`, `symbol`, `embedded`
|
2023-12-31 17:15:06 +00:00
|
|
|
`AnyAtom`* {.preservesOr.} = object
|
2022-03-07 23:53:05 +00:00
|
|
|
case orKind*: AnyAtomKind
|
|
|
|
of AnyAtomKind.`bool`:
|
2024-01-07 22:11:59 +00:00
|
|
|
`bool`*: bool
|
2022-03-07 23:53:05 +00:00
|
|
|
|
|
|
|
of AnyAtomKind.`double`:
|
2024-02-08 15:25:31 +00:00
|
|
|
`double`*: float
|
2022-03-07 23:53:05 +00:00
|
|
|
|
|
|
|
of AnyAtomKind.`int`:
|
2024-01-07 22:11:59 +00:00
|
|
|
`int`*: BiggestInt
|
2022-03-07 23:53:05 +00:00
|
|
|
|
|
|
|
of AnyAtomKind.`string`:
|
2024-01-07 22:11:59 +00:00
|
|
|
`string`*: string
|
2022-03-07 23:53:05 +00:00
|
|
|
|
|
|
|
of AnyAtomKind.`bytes`:
|
2024-01-07 22:11:59 +00:00
|
|
|
`bytes`*: seq[byte]
|
2022-03-07 23:53:05 +00:00
|
|
|
|
|
|
|
of AnyAtomKind.`symbol`:
|
2024-01-07 22:11:59 +00:00
|
|
|
`symbol`*: Symbol
|
2022-03-07 23:53:05 +00:00
|
|
|
|
|
|
|
of AnyAtomKind.`embedded`:
|
2024-01-07 22:11:59 +00:00
|
|
|
`embedded`* {.preservesEmbedded.}: EmbeddedRef
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2022-03-07 23:53:05 +00:00
|
|
|
|
2024-04-17 14:39:28 +00:00
|
|
|
GroupTypeKind* {.pure.} = enum
|
2021-10-06 09:30:38 +00:00
|
|
|
`rec`, `arr`, `dict`
|
2024-04-17 14:39:28 +00:00
|
|
|
GroupTypeRec* {.preservesRecord: "rec".} = object
|
2023-12-31 17:15:06 +00:00
|
|
|
`label`*: Value
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2024-04-17 14:39:28 +00:00
|
|
|
GroupTypeArr* {.preservesRecord: "arr".} = object
|
|
|
|
|
|
|
|
GroupTypeDict* {.preservesRecord: "dict".} = object
|
|
|
|
|
|
|
|
`GroupType`* {.preservesOr.} = object
|
|
|
|
case orKind*: GroupTypeKind
|
|
|
|
of GroupTypeKind.`rec`:
|
|
|
|
`rec`*: GroupTypeRec
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2024-04-17 14:39:28 +00:00
|
|
|
of GroupTypeKind.`arr`:
|
|
|
|
`arr`*: GroupTypeArr
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2024-04-17 14:39:28 +00:00
|
|
|
of GroupTypeKind.`dict`:
|
|
|
|
`dict`*: GroupTypeDict
|
2021-10-06 09:30:38 +00:00
|
|
|
|
|
|
|
|
2021-09-24 19:25:47 +00:00
|
|
|
PatternKind* {.pure.} = enum
|
2024-04-17 14:39:28 +00:00
|
|
|
`discard`, `bind`, `lit`, `group`
|
|
|
|
PatternDiscard* {.preservesRecord: "_".} = object
|
|
|
|
|
|
|
|
PatternBind* {.preservesRecord: "bind".} = object
|
|
|
|
`pattern`*: Pattern
|
|
|
|
|
|
|
|
PatternLit* {.preservesRecord: "lit".} = object
|
|
|
|
`value`*: AnyAtom
|
|
|
|
|
|
|
|
PatternGroup* {.preservesRecord: "group".} = object
|
|
|
|
`type`*: GroupType
|
|
|
|
`entries`*: Table[Value, Pattern]
|
|
|
|
|
2023-12-31 17:15:06 +00:00
|
|
|
`Pattern`* {.acyclic, preservesOr.} = ref object
|
2021-09-24 19:25:47 +00:00
|
|
|
case orKind*: PatternKind
|
2024-04-17 14:39:28 +00:00
|
|
|
of PatternKind.`discard`:
|
|
|
|
`discard`*: PatternDiscard
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2024-04-17 14:39:28 +00:00
|
|
|
of PatternKind.`bind`:
|
|
|
|
`bind`* {.preservesEmbedded.}: PatternBind
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2024-04-17 14:39:28 +00:00
|
|
|
of PatternKind.`lit`:
|
|
|
|
`lit`* {.preservesEmbedded.}: PatternLit
|
2021-09-24 19:25:47 +00:00
|
|
|
|
2024-04-17 14:39:28 +00:00
|
|
|
of PatternKind.`group`:
|
|
|
|
`group`* {.preservesEmbedded.}: PatternGroup
|
2021-09-24 19:25:47 +00:00
|
|
|
|
|
|
|
|
2024-04-17 14:39:28 +00:00
|
|
|
proc `$`*(x: AnyAtom | GroupType | Pattern): string =
|
2023-12-31 17:15:06 +00:00
|
|
|
`$`(toPreserves(x))
|
2021-10-06 09:30:38 +00:00
|
|
|
|
2024-04-17 14:39:28 +00:00
|
|
|
proc encode*(x: AnyAtom | GroupType | Pattern): seq[byte] =
|
2023-12-31 17:15:06 +00:00
|
|
|
encode(toPreserves(x))
|