diff --git a/src/syndicate.nim b/src/syndicate.nim index 9b104d5..95809ec 100644 --- a/src/syndicate.nim +++ b/src/syndicate.nim @@ -34,6 +34,69 @@ export Assertion, Facet, Handle, Ref, Symbol, Turn, TurnAction, `$`, `?`, bootDataspace, connectStdio, connectUnix, drop, facet, grab, message, 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 = ## Construct a `Pattern` from type `T`. runnableExamples: diff --git a/src/syndicate/patterns.nim b/src/syndicate/patterns.nim index c2557bd..9b2b948 100644 --- a/src/syndicate/patterns.nim +++ b/src/syndicate/patterns.nim @@ -62,65 +62,6 @@ proc `?*`*(): Pattern = grab() proc `?`*[A, B](table: TableRef[A,B]): Pattern = 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 = ?DCompoundRec(label: label, fields: fields.toSeq)