Move literal pattern builder to syndicate module
This commit is contained in:
parent
7ca28768d4
commit
07cd833e3d
|
@ -34,6 +34,69 @@ export Assertion, Facet, Handle, Ref, Symbol, Turn, TurnAction, `$`, `?`,
|
||||||
bootDataspace, connectStdio, connectUnix, drop, facet, grab, message,
|
bootDataspace, connectStdio, connectUnix, drop, facet, grab, message,
|
||||||
newDataspace, publish, retract, replace, run, stop, unembed
|
newDataspace, publish, retract, replace, run, stop, unembed
|
||||||
|
|
||||||
|
proc `?`*[T](val: T): Pattern =
|
||||||
|
## Construct a `Pattern` from value of type `T`.
|
||||||
|
when T is Pattern: result = val
|
||||||
|
elif T is Ref:
|
||||||
|
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
||||||
|
value: AnyAtom(
|
||||||
|
orKind: AnyAtomKind.embedded,
|
||||||
|
embedded: embed(val))))
|
||||||
|
# elif T is Preserve[Ref]:
|
||||||
|
# result = grab()
|
||||||
|
# TODO
|
||||||
|
elif T is ptr | ref:
|
||||||
|
if system.`==`(val, nil): result = ?(Symbol "null")
|
||||||
|
else: result = ?(val[])
|
||||||
|
elif T is bool:
|
||||||
|
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
||||||
|
value: AnyAtom(
|
||||||
|
orKind: AnyAtomKind.bool,
|
||||||
|
bool: val)))
|
||||||
|
elif T is float32:
|
||||||
|
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
||||||
|
value: AnyAtom(
|
||||||
|
orKind: AnyAtomKind.float,
|
||||||
|
float: val)))
|
||||||
|
elif T is float64:
|
||||||
|
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
||||||
|
value: AnyAtom(
|
||||||
|
orKind: AnyAtomKind.double,
|
||||||
|
double: val)))
|
||||||
|
elif T is SomeInteger:
|
||||||
|
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
||||||
|
value: AnyAtom(
|
||||||
|
orKind: AnyAtomKind.int,
|
||||||
|
int: AnyAtomInt val)))
|
||||||
|
elif T is string:
|
||||||
|
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
||||||
|
value: AnyAtom(
|
||||||
|
orKind: AnyAtomKind.string,
|
||||||
|
string: val)))
|
||||||
|
elif T is seq[byte]:
|
||||||
|
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
||||||
|
value: AnyAtom(
|
||||||
|
orKind: AnyAtomKind.bytes,
|
||||||
|
bytes: val)))
|
||||||
|
elif T is enum or T is Symbol:
|
||||||
|
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
||||||
|
value: AnyAtom(
|
||||||
|
orKind: AnyAtomKind.symbol,
|
||||||
|
symbol: Symbol $val)))
|
||||||
|
elif T.hasPreservesRecordPragma:
|
||||||
|
var
|
||||||
|
label = T.recordLabel.tosymbol(Ref)
|
||||||
|
fields = newSeq[Pattern]()
|
||||||
|
for f in fields(val):
|
||||||
|
fields.add ?f
|
||||||
|
result = ?DCompound(
|
||||||
|
orKind: DCompoundKind.rec,
|
||||||
|
rec: DCompoundRec(
|
||||||
|
label: label, fields: fields))
|
||||||
|
else:
|
||||||
|
{.error: "cannot derive literal pattern from " & $T.}
|
||||||
|
|
||||||
|
|
||||||
proc `?`*(T: static typedesc): Pattern =
|
proc `?`*(T: static typedesc): Pattern =
|
||||||
## Construct a `Pattern` from type `T`.
|
## Construct a `Pattern` from type `T`.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
|
|
|
@ -62,65 +62,6 @@ proc `?*`*(): Pattern = grab()
|
||||||
proc `?`*[A, B](table: TableRef[A,B]): Pattern =
|
proc `?`*[A, B](table: TableRef[A,B]): Pattern =
|
||||||
raiseAssert "not implemented"
|
raiseAssert "not implemented"
|
||||||
|
|
||||||
proc `?`*[T](val: T): Pattern =
|
|
||||||
## Construct a `Pattern` from value of type `T`.
|
|
||||||
when T is Pattern: result = val
|
|
||||||
elif T is Ref:
|
|
||||||
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
|
||||||
value: AnyAtom(
|
|
||||||
orKind: AnyAtomKind.embedded,
|
|
||||||
embedded: embed(val))))
|
|
||||||
elif T is ptr | ref:
|
|
||||||
if system.`==`(val, nil): result = ?(Symbol "null")
|
|
||||||
else: result = ?(val[])
|
|
||||||
elif T is bool:
|
|
||||||
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
|
||||||
value: AnyAtom(
|
|
||||||
orKind: AnyAtomKind.bool,
|
|
||||||
bool: val)))
|
|
||||||
elif T is float32:
|
|
||||||
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
|
||||||
value: AnyAtom(
|
|
||||||
orKind: AnyAtomKind.float,
|
|
||||||
float: val)))
|
|
||||||
elif T is float64:
|
|
||||||
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
|
||||||
value: AnyAtom(
|
|
||||||
orKind: AnyAtomKind.double,
|
|
||||||
double: val)))
|
|
||||||
elif T is SomeInteger:
|
|
||||||
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
|
||||||
value: AnyAtom(
|
|
||||||
orKind: AnyAtomKind.int,
|
|
||||||
int: AnyAtomInt val)))
|
|
||||||
elif T is string:
|
|
||||||
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
|
||||||
value: AnyAtom(
|
|
||||||
orKind: AnyAtomKind.string,
|
|
||||||
string: val)))
|
|
||||||
elif T is seq[byte]:
|
|
||||||
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
|
||||||
value: AnyAtom(
|
|
||||||
orKind: AnyAtomKind.bytes,
|
|
||||||
bytes: val)))
|
|
||||||
elif T is enum or T is Symbol:
|
|
||||||
result = Pattern(orKind: PatternKind.DLit, dlit: DLit(
|
|
||||||
value: AnyAtom(
|
|
||||||
orKind: AnyAtomKind.symbol,
|
|
||||||
symbol: Symbol $val)))
|
|
||||||
elif T.hasPreservesRecordPragma:
|
|
||||||
var
|
|
||||||
label = T.recordLabel.tosymbol(Ref)
|
|
||||||
fields = newSeq[Pattern]()
|
|
||||||
for f in fields(val):
|
|
||||||
fields.add ?f
|
|
||||||
result = ?DCompound(
|
|
||||||
orKind: DCompoundKind.rec,
|
|
||||||
rec: DCompoundRec(
|
|
||||||
label: label, fields: fields))
|
|
||||||
else:
|
|
||||||
{.error: "cannot derive pattern from " & $T.}
|
|
||||||
|
|
||||||
proc recordPattern*(label: Preserve[Ref], fields: varargs[Pattern]): Pattern =
|
proc recordPattern*(label: Preserve[Ref], fields: varargs[Pattern]): Pattern =
|
||||||
?DCompoundRec(label: label, fields: fields.toSeq)
|
?DCompoundRec(label: label, fields: fields.toSeq)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue