2021-10-28 17:43:20 +00:00
|
|
|
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
|
|
|
|
# SPDX-License-Identifier: Unlicense
|
|
|
|
|
2022-03-11 05:43:54 +00:00
|
|
|
import std/[tables, typetraits]
|
2022-06-10 15:46:57 +00:00
|
|
|
from std/sequtils import toSeq
|
2021-10-28 17:43:20 +00:00
|
|
|
|
|
|
|
import preserves
|
2021-11-02 11:58:51 +00:00
|
|
|
import ./protocols/dataspacePatterns
|
2021-10-28 17:43:20 +00:00
|
|
|
from ./actors import Ref
|
|
|
|
|
2022-03-11 05:43:54 +00:00
|
|
|
export dataspacePatterns.`$`, PatternKind, DCompoundKind
|
2021-10-29 12:06:00 +00:00
|
|
|
|
2021-10-28 17:43:20 +00:00
|
|
|
type
|
2022-03-11 05:43:54 +00:00
|
|
|
AnyAtom* = dataspacePatterns.AnyAtom[Ref]
|
2022-04-24 01:01:30 +00:00
|
|
|
DBind* = dataspacePatterns.DBind[Ref]
|
|
|
|
DCompound* = dataspacePatterns.DCompound[Ref]
|
|
|
|
DCompoundArr* = dataspacePatterns.DCompoundArr[Ref]
|
|
|
|
DCompoundDict* = dataspacePatterns.DCompoundDict[Ref]
|
|
|
|
DCompoundRec* = dataspacePatterns.DCompoundRec[Ref]
|
|
|
|
DLit* = dataspacePatterns.DLit[Ref]
|
2021-10-28 17:43:20 +00:00
|
|
|
Pattern* = dataspacePatterns.Pattern[Ref]
|
|
|
|
|
2021-10-29 12:06:00 +00:00
|
|
|
proc `?`*(d: DBind): Pattern =
|
|
|
|
Pattern(orKind: PatternKind.DBind, dbind: d)
|
|
|
|
|
|
|
|
proc `?`*(d: DLit): Pattern =
|
|
|
|
Pattern(orKind: PatternKind.DLit, dlit: d)
|
|
|
|
|
|
|
|
proc `?`*(d: DCompound): Pattern =
|
|
|
|
Pattern(orKind: PatternKind.DCompound, dcompound: d)
|
|
|
|
|
2022-06-10 15:46:57 +00:00
|
|
|
proc `?`*(d: DCompoundRec): Pattern =
|
|
|
|
?DCompound(orKind: DCompoundKind.rec, rec: d)
|
|
|
|
|
2022-03-07 23:53:05 +00:00
|
|
|
proc `?`*(x: bool): Pattern =
|
|
|
|
?DLit(value: AnyAtom(orKind: AnyAtomKind.`bool`, bool: x))
|
|
|
|
|
|
|
|
proc `?`*(x: float32): Pattern =
|
|
|
|
?DLit(value: AnyAtom(orKind: AnyAtomKind.`float`, float: x))
|
|
|
|
|
|
|
|
proc `?`*(x: float64): Pattern =
|
|
|
|
?DLit(value: AnyAtom(orKind: AnyAtomKind.`double`, double: x))
|
|
|
|
|
|
|
|
proc `?`*(x: int): Pattern =
|
|
|
|
?DLit(value: AnyAtom(orKind: AnyAtomKind.`int`, int: x))
|
|
|
|
|
2021-11-02 11:58:51 +00:00
|
|
|
proc `?`*(s: string): Pattern =
|
2022-03-07 23:53:05 +00:00
|
|
|
?DLit(value: AnyAtom(orKind: AnyAtomKind.`string`, string: s))
|
|
|
|
|
|
|
|
proc `?`*(x: seq[byte]): Pattern =
|
|
|
|
?DLit(value: AnyAtom(orKind: AnyAtomKind.`bytes`, bytes: x))
|
|
|
|
|
|
|
|
proc `?`*(x: Symbol): Pattern =
|
|
|
|
?DLit(value: AnyAtom(orKind: AnyAtomKind.`symbol`, symbol: x))
|
2021-11-02 11:58:51 +00:00
|
|
|
|
2021-12-11 18:36:31 +00:00
|
|
|
proc drop*(): Pattern = Pattern(orKind: PatternKind.DDiscard)
|
|
|
|
proc grab*(): Pattern = ?DBind(pattern: drop())
|
2021-11-02 11:58:51 +00:00
|
|
|
|
2021-12-11 18:36:31 +00:00
|
|
|
proc `?_`*(): Pattern = drop()
|
|
|
|
proc `?*`*(): Pattern = grab()
|
2021-10-29 12:06:00 +00:00
|
|
|
|
2022-06-09 01:19:12 +00:00
|
|
|
proc `?`*[A, B](table: TableRef[A,B]): Pattern =
|
|
|
|
raiseAssert "not implemented"
|
|
|
|
|
2022-06-10 15:46:57 +00:00
|
|
|
proc recordPattern*(label: Preserve[Ref], fields: varargs[Pattern]): Pattern =
|
|
|
|
?DCompoundRec(label: label, fields: fields.toSeq)
|
|
|
|
|
2022-03-11 05:43:54 +00:00
|
|
|
type
|
|
|
|
Value = Preserve[Ref]
|
|
|
|
Path = seq[Value]
|
|
|
|
Analysis* = tuple
|
|
|
|
constPaths: seq[Path]
|
|
|
|
constValues: seq[Value]
|
|
|
|
capturePaths: seq[Path]
|
|
|
|
|
|
|
|
func walk(result: var Analysis; path: var Path; p: Pattern)
|
|
|
|
|
|
|
|
func walk(result: var Analysis; path: var Path; key: int|Value; pat: Pattern) =
|
|
|
|
path.add(key.toPreserve(Ref))
|
|
|
|
walk(result, path, pat)
|
|
|
|
discard path.pop
|
|
|
|
|
|
|
|
func walk(result: var Analysis; path: var Path; p: Pattern) =
|
|
|
|
case p.orKind
|
|
|
|
of PatternKind.DCompound:
|
|
|
|
case p.dcompound.orKind
|
|
|
|
of DCompoundKind.rec:
|
|
|
|
for k, e in p.dcompound.rec.fields: walk(result, path, k, e)
|
|
|
|
of DCompoundKind.arr:
|
|
|
|
for k, e in p.dcompound.arr.items: walk(result, path, k, e)
|
|
|
|
of DCompoundKind.dict:
|
|
|
|
for k, e in p.dcompound.dict.entries: walk(result, path, k, e)
|
|
|
|
of PatternKind.DBind:
|
|
|
|
result.capturePaths.add(path)
|
|
|
|
walk(result, path, p.dbind.pattern)
|
|
|
|
of PatternKind.DDiscard: discard
|
|
|
|
of PatternKind.DLit:
|
|
|
|
result.constPaths.add(path)
|
|
|
|
result.constValues.add(p.dlit.value.toPreserve(Ref))
|
|
|
|
|
|
|
|
func analyse*(p: Pattern): Analysis =
|
|
|
|
var path: Path
|
|
|
|
walk(result, path, p)
|