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

87 lines
2.0 KiB
Nim
Raw Normal View History

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