Schema: make all Preserves embeddable

This commit is contained in:
Emery Hemingway 2021-10-17 14:50:27 +02:00
parent 171502f1d2
commit 5250707f0e
3 changed files with 183 additions and 185 deletions

View File

@ -13,6 +13,20 @@ import compiler/[ast, idents, renderer, lineinfos]
import ../preserves, ./schema, ./parse import ../preserves, ./schema, ./parse
type type
Bundle = schema.Bundle[void]
Modules = schema.Modules[void]
Schema = schema.Schema[void]
Definitions = schema.Definitions[void]
Definition = schema.Definition[void]
Pattern = schema.Pattern[void]
SimplePattern = schema.SimplePattern[void]
CompoundPattern = schema.CompoundPattern[void]
DictionaryEntries = schema.DictionaryEntries[void]
NamedAlternative = schema.NamedAlternative[void]
NamedSimplePattern = schema.NamedSimplePattern[void]
NamedPattern = schema.NamedPattern[void]
Binding = schema.Binding[void]
TypeSpec = tuple[node: PNode, embeddable: bool] TypeSpec = tuple[node: PNode, embeddable: bool]
TypeTable = OrderedTable[string, PNode] TypeTable = OrderedTable[string, PNode]
@ -53,7 +67,7 @@ proc pattern(np: NamedSimplePattern): SimplePattern =
np.anonymous np.anonymous
proc ident(sp: SimplePattern): PNode = proc ident(sp: SimplePattern): PNode =
raiseAssert "need ident from " & $sp raiseAssert "need ident from " #& $sp
proc ident(cp: CompoundPattern; fallback: string): PNode = proc ident(cp: CompoundPattern; fallback: string): PNode =
case cp.orKind case cp.orKind
@ -113,11 +127,8 @@ proc deref(scm: Schema; r: Ref): Definition =
assert r.module == @[] assert r.module == @[]
scm.data.definitions[r.name] scm.data.definitions[r.name]
proc isEmbeddable(scm: Schema): bool =
scm.data.embeddedType.orKind == EmbeddedtypenameKind.`Ref`
proc preserveIdent(scm: Schema): Pnode = proc preserveIdent(scm: Schema): Pnode =
nn(nkBracketExpr, ident"Preserve", ident(if scm.isEmbeddable: "E" else: "void")) nn(nkBracketExpr, ident"Preserve", ident("E"))
proc embeddedIdent(scm: Schema): PNode = proc embeddedIdent(scm: Schema): PNode =
case scm.data.embeddedType.orKind case scm.data.embeddedType.orKind
@ -131,27 +142,25 @@ proc isEmbeddable(scm: Schema; pat: Pattern; seen: RefSet): bool {.gcsafe.}
proc isEmbeddable(scm: Schema; def: Definition; seen: RefSet): bool {.gcsafe.} proc isEmbeddable(scm: Schema; def: Definition; seen: RefSet): bool {.gcsafe.}
proc isEmbeddable(scm: Schema; sp: SimplePattern; seen: RefSet): bool = proc isEmbeddable(scm: Schema; sp: SimplePattern; seen: RefSet): bool =
if not scm.isEmbeddable: false case sp.orKind
else: of SimplepatternKind.`atom`, SimplepatternKind.`lit`: false
case sp.orKind of SimplepatternKind.`any`: true
of SimplepatternKind.`atom`, SimplepatternKind.`lit`: false of SimplepatternKind.`embedded`: true
of SimplepatternKind.`any`: true of SimplepatternKind.`seqof`:
of SimplepatternKind.`embedded`: true isEmbeddable(scm, sp.seqof.pattern, seen)
of SimplepatternKind.`seqof`: of SimplepatternKind.`setof`:
isEmbeddable(scm, sp.seqof.pattern, seen) isEmbeddable(scm, sp.setof.pattern, seen)
of SimplepatternKind.`setof`: of SimplepatternKind.`dictof`:
isEmbeddable(scm, sp.setof.pattern, seen) isEmbeddable(scm, sp.dictof.key, seen) or
of SimplepatternKind.`dictof`: isEmbeddable(scm, sp.dictof.value, seen)
isEmbeddable(scm, sp.dictof.key, seen) or of SimplepatternKind.`Ref`:
isEmbeddable(scm, sp.dictof.value, seen) if sp.ref.module != @[]: true
of SimplepatternKind.`Ref`: else:
if sp.ref.module != @[]: true if sp.ref in seen: false
else: else:
if sp.ref in seen: false var seen = seen
else: seen.incl sp.ref
var seen = seen isEmbeddable(scm, deref(scm, sp.ref), seen)
seen.incl sp.ref
isEmbeddable(scm, deref(scm, sp.ref), seen)
proc isEmbeddable(scm: Schema; np: NamedSimplePattern; seen: RefSet): bool = proc isEmbeddable(scm: Schema; np: NamedSimplePattern; seen: RefSet): bool =
case np.orKind case np.orKind
@ -161,22 +170,20 @@ proc isEmbeddable(scm: Schema; np: NamedSimplePattern; seen: RefSet): bool =
isEmbeddable(scm, np.anonymous, seen) isEmbeddable(scm, np.anonymous, seen)
proc isEmbeddable(scm: Schema; cp: CompoundPattern; seen: RefSet): bool = proc isEmbeddable(scm: Schema; cp: CompoundPattern; seen: RefSet): bool =
if not scm.isEmbeddable: false case cp.orKind
else: of CompoundPatternKind.`rec`:
case cp.orKind isEmbeddable(scm, cp.rec.label.pattern, seen) or
of CompoundPatternKind.`rec`: isEmbeddable(scm, cp.rec.fields.pattern, seen)
isEmbeddable(scm, cp.rec.label.pattern, seen) or of CompoundPatternKind.`tuple`:
isEmbeddable(scm, cp.rec.fields.pattern, seen) any(cp.tuple.patterns) do (np: NamedPattern) -> bool:
of CompoundPatternKind.`tuple`: isEmbeddable(scm, np.pattern, seen)
any(cp.tuple.patterns) do (np: NamedPattern) -> bool: of CompoundPatternKind.`tupleprefix`:
isEmbeddable(scm, np.pattern, seen) proc pred(np: NamedPattern): bool =
of CompoundPatternKind.`tupleprefix`: isEmbeddable(scm, np.pattern, seen)
proc pred(np: NamedPattern): bool = isEmbeddable(scm, cp.tupleprefix.variable, seen) or
isEmbeddable(scm, np.pattern, seen) any(cp.tupleprefix.fixed, pred)
isEmbeddable(scm, cp.tupleprefix.variable, seen) or of CompoundPatternKind.`dict`:
any(cp.tupleprefix.fixed, pred) true # the key type is `Preserve`
of CompoundPatternKind.`dict`:
true # the key type is `Preserve`
proc isEmbeddable(scm: Schema; pat: Pattern; seen: RefSet): bool = proc isEmbeddable(scm: Schema; pat: Pattern; seen: RefSet): bool =
case pat.orKind case pat.orKind
@ -185,26 +192,26 @@ proc isEmbeddable(scm: Schema; pat: Pattern; seen: RefSet): bool =
of PatternKind.`CompoundPattern`: of PatternKind.`CompoundPattern`:
isEmbeddable(scm, pat.compoundPattern, seen) isEmbeddable(scm, pat.compoundPattern, seen)
proc isEmbeddable(scm: Schema; def: Definition; seen: RefSet): bool = proc isEmbeddable(scm: Schema; orDef: DefinitionOr; seen: RefSet): bool =
if not scm.isEmbeddable: false proc isEmbeddable(na: NamedAlternative): bool =
else: isEmbeddable(scm, na.pattern, seen)
case def.orKind isEmbeddable(orDef.data.pattern0) or
of DefinitionKind.`or`: isEmbeddable(orDef.data.pattern1) or
proc isEmbeddable(na: NamedAlternative): bool = any(orDef.data.patternN, isEmbeddable)
isEmbeddable(scm, na.pattern, seen)
isEmbeddable(def.or.data.pattern0) or
isEmbeddable(def.or.data.pattern1) or
any(def.or.data.patternN, isEmbeddable)
of DefinitionKind.`and`:
proc isEmbeddable(np: NamedPattern): bool =
isEmbeddable(scm, np.pattern, seen)
isEmbeddable(def.and.data.pattern0) or
isEmbeddable(def.and.data.pattern1) or
any(def.and.data.patternN, isEmbeddable)
of DefinitionKind.`Pattern`:
isEmbeddable(scm, def.pattern, seen)
proc isEmbeddable(scm: Schema; p: Definition|Pattern|SimplePattern): bool = proc isEmbeddable(scm: Schema; def: Definition; seen: RefSet): bool =
case def.orKind
of DefinitionKind.`or`: isEmbeddable(scm, def.or, seen)
of DefinitionKind.`and`:
proc isEmbeddable(np: NamedPattern): bool =
isEmbeddable(scm, np.pattern, seen)
isEmbeddable(def.and.data.pattern0) or
isEmbeddable(def.and.data.pattern1) or
any(def.and.data.patternN, isEmbeddable)
of DefinitionKind.`Pattern`:
isEmbeddable(scm, def.pattern, seen)
proc isEmbeddable(scm: Schema; p: Definition|DefinitionOr|Pattern|CompoundPattern|SimplePattern): bool =
var seen: RefSet var seen: RefSet
isEmbeddable(scm, p, seen) isEmbeddable(scm, p, seen)
@ -273,8 +280,6 @@ proc typeIdent(scm: Schema; sp: SimplePattern): TypeSpec =
case sp.orKind case sp.orKind
of SimplepatternKind.`atom`: of SimplepatternKind.`atom`:
result = (typeIdent(sp.atom.atomKind), false) result = (typeIdent(sp.atom.atomKind), false)
of SimplepatternKind.`embedded`:
result = (scm.embeddedIdent, scm.isEmbeddable)
of SimplepatternKind.`seqof`: of SimplepatternKind.`seqof`:
result = typeIdent(scm, sp.seqof.pattern) result = typeIdent(scm, sp.seqof.pattern)
result.node = nn(nkBracketExpr, ident"seq", result.node) result.node = nn(nkBracketExpr, ident"seq", result.node)
@ -291,7 +296,7 @@ proc typeIdent(scm: Schema; sp: SimplePattern): TypeSpec =
result = (ident(sp.ref), isEmbeddable(scm, sp)) result = (ident(sp.ref), isEmbeddable(scm, sp))
result.node = parameterize(result) result.node = parameterize(result)
else: else:
result = (preserveIdent(scm), isEmbeddable(scm)) result = (preserveIdent(scm), true)
proc typeIdent(scm: Schema; pat: Pattern): TypeSpec = proc typeIdent(scm: Schema; pat: Pattern): TypeSpec =
case pat.orKind case pat.orKind
@ -325,22 +330,9 @@ proc toFieldIdent(scm: Schema, label: string; pat: Pattern): PNode =
proc newEmpty(): PNode = newNode(nkEmpty) proc newEmpty(): PNode = newNode(nkEmpty)
#[
proc literal(scm: Schema; sn: SchemaNode): Value =
case sn.orKind
of snkLiteral: result = sn.value
of snkRef:
if sn.refPath.len == 1:
result = literal(scm, scm.definitions[sn.refPath[0]])
else:
raiseAssert("not convertable to a literal: " & $sn)
else:
raiseAssert("not convertable to a literal: " & $sn)
]#
proc embeddingParams(embeddable: bool): PNode = proc embeddingParams(embeddable: bool): PNode =
if embeddable: if embeddable:
nn(nkGenericParams, nn(nkIdentDefs, ident"E", newEmpty(), ident"void")) nn(nkGenericParams, nn(nkIdentDefs, ident"E", newEmpty(), newEmpty()))
else: else:
newEmpty() newEmpty()
@ -456,7 +448,7 @@ proc addFields(recList: PNode; scm: Schema; known: var TypeTable; cp: CompoundPa
case cp.orKind case cp.orKind
of CompoundPatternKind.rec: of CompoundPatternKind.rec:
# recList.add identDef(ident(label), nimTypeOf(scm, known, cp, "")) # recList.add identDef(ident(label), nimTypeOf(scm, known, cp, ""))
raiseassert "unexpected record of fields " & $cp.rec raiseassert "unexpected record of fields " #& $cp.rec
of CompoundPatternKind.tuple: of CompoundPatternKind.tuple:
for np in cp.tuple.patterns: for np in cp.tuple.patterns:
let let
@ -470,14 +462,14 @@ proc addFields(recList: PNode; scm: Schema; known: var TypeTable; cp: CompoundPa
typeName = parentName & capitalizeAscii(label) typeName = parentName & capitalizeAscii(label)
fieldSpec = nimTypeOf(scm, known, pat, label) fieldSpec = nimTypeOf(scm, known, pat, label)
known[typeName] = typeDef(scm, typeName, pat, fieldSpec.node) known[typeName] = typeDef(scm, typeName, pat, fieldSpec.node)
recList.add identDef(id, ident(typeName), fieldSpec.embeddable) recList.add identDef(id, ident(typeName), isEmbeddable(scm, pat))
else: raiseAssert "not adding fields for " & $cp else: raiseAssert "not adding fields for " #& $cp
reclist reclist
proc addFields(recList: PNode; scm: Schema; known: var TypeTable; pat: Pattern; parentName: string): PNode {.discardable.} = proc addFields(recList: PNode; scm: Schema; known: var TypeTable; pat: Pattern; parentName: string): PNode {.discardable.} =
case pat.orKind case pat.orKind
of PatternKind.SimplePattern: of PatternKind.SimplePattern:
raiseAssert "addFields called with SimplePattern " & $pat.simplePattern raiseAssert "addFields called with SimplePattern " #& $pat.simplePattern
# addField(recList, scm, known, pat.simplePattern, "data") # addField(recList, scm, known, pat.simplePattern, "data")
of PatternKind.CompoundPattern: of PatternKind.CompoundPattern:
discard addFields(recList, scm, known, pat.compoundPattern, parentName) discard addFields(recList, scm, known, pat.compoundPattern, parentName)
@ -531,6 +523,8 @@ proc nimTypeOf(scm: Schema; known: var TypeTable; cp: CompoundPattern; name: str
of CompoundPatternKind.`dict`: of CompoundPatternKind.`dict`:
result.node = nn(nkObjectTy, newEmpty(), newEmpty(), result.node = nn(nkObjectTy, newEmpty(), newEmpty(),
nn(nkRecList).addFields(scm, known, cp.dict.entries, name)) nn(nkRecList).addFields(scm, known, cp.dict.entries, name))
if result.node.kind == nkObjectTy and isEmbeddable(scm, cp):
result.node = nn(nkRefTy, result.node)
proc nimTypeOf(scm: Schema; known: var TypeTable; pat: Pattern; name: string): TypeSpec = proc nimTypeOf(scm: Schema; known: var TypeTable; pat: Pattern; name: string): TypeSpec =
case pat.orKind case pat.orKind
@ -583,6 +577,8 @@ proc nimTypeOf(scm: Schema; known: var TypeTable; orDef: DefinitionOr; name: str
known[memberTypeName] = known[memberTypeName] =
typeDef(scm, memberTypeName, na.pattern, ty.node) typeDef(scm, memberTypeName, na.pattern, ty.node)
orEmbed result, memberType orEmbed result, memberType
memberType.node = parameterize(
memberType.node, isEmbeddable(scm, na.pattern))
branchRecList.add nn(nkIdentDefs, branchRecList.add nn(nkIdentDefs,
toFieldIdent(scm, na.variantLabel.normalize, na.pattern), toFieldIdent(scm, na.variantLabel.normalize, na.pattern),
memberType.node, newEmpty()) memberType.node, newEmpty())
@ -593,10 +589,12 @@ proc nimTypeOf(scm: Schema; known: var TypeTable; orDef: DefinitionOr; name: str
addCase(orDef.data.pattern0) addCase(orDef.data.pattern0)
addCase(orDef.data.pattern1) addCase(orDef.data.pattern1)
for na in orDef.data.patternN: addCase(na) for na in orDef.data.patternN: addCase(na)
result.node = nn(nkRefTy, nn(nkObjectTy, result.node = nn(nkObjectTy,
newEmpty(), newEmpty(),
newEmpty(), newEmpty(),
nn(nkRecList, recCase))) nn(nkRecList, recCase))
if result.node.kind == nkObjectTy and isEmbeddable(scm, orDef):
result.node = nn(nkRefTy, result.node)
proc nimTypeOf(scm: Schema; known: var TypeTable; def: Definition; name: string): TypeSpec = proc nimTypeOf(scm: Schema; known: var TypeTable; def: Definition; name: string): TypeSpec =
case def.orKind case def.orKind
@ -721,15 +719,13 @@ proc renderNimModule*(scm: Schema): string =
ident"std/typetraits", ident"std/typetraits",
ident"preserves") ident"preserves")
collectRefImports(imports, scm) collectRefImports(imports, scm)
let dollarGenericParams = let genericParams =
if isEmbeddable(scm): nn(nkGenericParams, nn(nkIdentDefs, ident"E", newEmpty(), newEmpty()))
nn(nkGenericParams, nn(nkIdentDefs, ident"E", newEmpty(), newEmpty()))
else: newEmpty()
if not embeddableType.isNil: if not embeddableType.isNil:
procs.add nn(nkProcDef, procs.add nn(nkProcDef,
"$".toFieldIdent, "$".toFieldIdent,
newEmpty(), newEmpty(),
dollarGenericParams, genericParams,
nn(nkFormalParams, nn(nkFormalParams,
ident"string", ident"string",
nn(nkIdentDefs, nn(nkIdentDefs,
@ -744,7 +740,7 @@ proc renderNimModule*(scm: Schema): string =
procs.add nn(nkProcDef, procs.add nn(nkProcDef,
"encode".ident.toExport, "encode".ident.toExport,
newEmpty(), newEmpty(),
nn(nkGenericParams, nn(nkIdentDefs, ident"E", newEmpty(), newEmpty())), genericParams,
nn(nkFormalParams, nn(nkFormalParams,
nn(nkBracketExpr, ident"seq", ident"byte"), nn(nkBracketExpr, ident"seq", ident"byte"),
nn(nkIdentDefs, nn(nkIdentDefs,

View File

@ -7,44 +7,44 @@ type
`name`* {.preservesSymbol.}: string `name`* {.preservesSymbol.}: string
ModulePath* = seq[string] ModulePath* = seq[string]
Bundle* {.preservesRecord: "bundle".} = object Bundle*[E] {.preservesRecord: "bundle".} = ref object
`modules`*: Modules `modules`*: Modules[E]
CompoundPatternKind* {.pure.} = enum CompoundPatternKind* {.pure.} = enum
`rec`, `tuple`, `tuplePrefix`, `dict` `rec`, `tuple`, `tuplePrefix`, `dict`
CompoundPatternRec* {.preservesRecord: "rec".} = object CompoundPatternRec*[E] {.preservesRecord: "rec".} = ref object
`label`*: NamedPattern `label`*: NamedPattern[E]
`fields`*: NamedPattern `fields`*: NamedPattern[E]
CompoundPatternTuple* {.preservesRecord: "tuple".} = object CompoundPatternTuple*[E] {.preservesRecord: "tuple".} = ref object
`patterns`*: seq[NamedPattern] `patterns`*: seq[NamedPattern[E]]
CompoundPatternTuplePrefix* {.preservesRecord: "tuplePrefix".} = object CompoundPatternTuplePrefix*[E] {.preservesRecord: "tuplePrefix".} = ref object
`fixed`*: seq[NamedPattern] `fixed`*: seq[NamedPattern[E]]
`variable`*: NamedSimplePattern `variable`*: NamedSimplePattern[E]
CompoundPatternDict* {.preservesRecord: "dict".} = object CompoundPatternDict*[E] {.preservesRecord: "dict".} = ref object
`entries`*: DictionaryEntries `entries`*: DictionaryEntries[E]
`CompoundPattern`* {.preservesOr.} = ref object `CompoundPattern`*[E] {.preservesOr.} = ref object
case orKind*: CompoundPatternKind case orKind*: CompoundPatternKind
of CompoundPatternKind.`rec`: of CompoundPatternKind.`rec`:
`rec`*: CompoundPatternRec `rec`*: CompoundPatternRec[E]
of CompoundPatternKind.`tuple`: of CompoundPatternKind.`tuple`:
`tuple`*: CompoundPatternTuple `tuple`*: CompoundPatternTuple[E]
of CompoundPatternKind.`tuplePrefix`: of CompoundPatternKind.`tuplePrefix`:
`tupleprefix`*: CompoundPatternTuplePrefix `tupleprefix`*: CompoundPatternTuplePrefix[E]
of CompoundPatternKind.`dict`: of CompoundPatternKind.`dict`:
`dict`*: CompoundPatternDict `dict`*: CompoundPatternDict[E]
Modules* = Table[ModulePath, Schema] Modules*[E] = Table[ModulePath, Schema[E]]
EmbeddedTypeNameKind* {.pure.} = enum EmbeddedTypeNameKind* {.pure.} = enum
`Ref`, `false` `Ref`, `false`
`EmbeddedTypeName`* {.preservesOr.} = ref object `EmbeddedTypeName`* {.preservesOr.} = object
case orKind*: EmbeddedTypeNameKind case orKind*: EmbeddedTypeNameKind
of EmbeddedTypeNameKind.`Ref`: of EmbeddedTypeNameKind.`Ref`:
`ref`*: Ref `ref`*: Ref
@ -56,17 +56,17 @@ type
`AtomKind`* {.preservesOr.} = enum `AtomKind`* {.preservesOr.} = enum
`Boolean`, `Float`, `Double`, `SignedInteger`, `String`, `ByteString`, `Boolean`, `Float`, `Double`, `SignedInteger`, `String`, `ByteString`,
`Symbol` `Symbol`
Definitions* = Table[string, Definition] Definitions*[E] = Table[string, Definition[E]]
DictionaryEntries* = Table[Preserve[void], NamedSimplePattern] DictionaryEntries*[E] = Table[Preserve[E], NamedSimplePattern[E]]
NamedPatternKind* {.pure.} = enum NamedPatternKind* {.pure.} = enum
`named`, `anonymous` `named`, `anonymous`
`NamedPattern`* {.preservesOr.} = ref object `NamedPattern`*[E] {.preservesOr.} = ref object
case orKind*: NamedPatternKind case orKind*: NamedPatternKind
of NamedPatternKind.`named`: of NamedPatternKind.`named`:
`named`*: Binding `named`*: Binding[E]
of NamedPatternKind.`anonymous`: of NamedPatternKind.`anonymous`:
`anonymous`*: Pattern `anonymous`*: Pattern[E]
SimplePatternKind* {.pure.} = enum SimplePatternKind* {.pure.} = enum
@ -74,23 +74,23 @@ type
SimplePatternAtom* {.preservesRecord: "atom".} = object SimplePatternAtom* {.preservesRecord: "atom".} = object
`atomKind`*: AtomKind `atomKind`*: AtomKind
SimplePatternEmbedded* {.preservesRecord: "embedded".} = object SimplePatternEmbedded*[E] {.preservesRecord: "embedded".} = ref object
`interface`*: SimplePattern `interface`*: SimplePattern[E]
SimplePatternLit* {.preservesRecord: "lit".} = object SimplePatternLit*[E] {.preservesRecord: "lit".} = ref object
`value`*: Preserve[void] `value`*: Preserve[E]
SimplePatternSeqof* {.preservesRecord: "seqof".} = object SimplePatternSeqof*[E] {.preservesRecord: "seqof".} = ref object
`pattern`*: SimplePattern `pattern`*: SimplePattern[E]
SimplePatternSetof* {.preservesRecord: "setof".} = object SimplePatternSetof*[E] {.preservesRecord: "setof".} = ref object
`pattern`*: SimplePattern `pattern`*: SimplePattern[E]
SimplePatternDictof* {.preservesRecord: "dictof".} = object SimplePatternDictof*[E] {.preservesRecord: "dictof".} = ref object
`key`*: SimplePattern `key`*: SimplePattern[E]
`value`*: SimplePattern `value`*: SimplePattern[E]
`SimplePattern`* {.preservesOr.} = ref object `SimplePattern`*[E] {.preservesOr.} = ref object
case orKind*: SimplePatternKind case orKind*: SimplePatternKind
of SimplePatternKind.`any`: of SimplePatternKind.`any`:
`any`* {.preservesLiteral: "any".}: bool `any`* {.preservesLiteral: "any".}: bool
@ -99,19 +99,19 @@ type
`atom`*: SimplePatternAtom `atom`*: SimplePatternAtom
of SimplePatternKind.`embedded`: of SimplePatternKind.`embedded`:
`embedded`*: SimplePatternEmbedded `embedded`*: SimplePatternEmbedded[E]
of SimplePatternKind.`lit`: of SimplePatternKind.`lit`:
`lit`*: SimplePatternLit `lit`*: SimplePatternLit[E]
of SimplePatternKind.`seqof`: of SimplePatternKind.`seqof`:
`seqof`*: SimplePatternSeqof `seqof`*: SimplePatternSeqof[E]
of SimplePatternKind.`setof`: of SimplePatternKind.`setof`:
`setof`*: SimplePatternSetof `setof`*: SimplePatternSetof[E]
of SimplePatternKind.`dictof`: of SimplePatternKind.`dictof`:
`dictof`*: SimplePatternDictof `dictof`*: SimplePatternDictof[E]
of SimplePatternKind.`Ref`: of SimplePatternKind.`Ref`:
`ref`*: Ref `ref`*: Ref
@ -119,96 +119,98 @@ type
NamedSimplePatternKind* {.pure.} = enum NamedSimplePatternKind* {.pure.} = enum
`named`, `anonymous` `named`, `anonymous`
`NamedSimplePattern`* {.preservesOr.} = ref object `NamedSimplePattern`*[E] {.preservesOr.} = ref object
case orKind*: NamedSimplePatternKind case orKind*: NamedSimplePatternKind
of NamedSimplePatternKind.`named`: of NamedSimplePatternKind.`named`:
`named`*: Binding `named`*: Binding[E]
of NamedSimplePatternKind.`anonymous`: of NamedSimplePatternKind.`anonymous`:
`anonymous`*: SimplePattern `anonymous`*: SimplePattern[E]
DefinitionKind* {.pure.} = enum DefinitionKind* {.pure.} = enum
`or`, `and`, `Pattern` `or`, `and`, `Pattern`
DefinitionOrData* {.preservesTuple.} = object DefinitionOrData*[E] {.preservesTuple.} = ref object
`pattern0`*: NamedAlternative `pattern0`*: NamedAlternative[E]
`pattern1`*: NamedAlternative `pattern1`*: NamedAlternative[E]
`patternN`* {.preservesTupleTail.}: seq[NamedAlternative] `patternN`* {.preservesTupleTail.}: seq[NamedAlternative[E]]
DefinitionOr* {.preservesRecord: "or".} = object DefinitionOr*[E] {.preservesRecord: "or".} = ref object
`data`*: DefinitionOrData `data`*: DefinitionOrData[E]
DefinitionAndData* {.preservesTuple.} = object DefinitionAndData*[E] {.preservesTuple.} = ref object
`pattern0`*: NamedPattern `pattern0`*: NamedPattern[E]
`pattern1`*: NamedPattern `pattern1`*: NamedPattern[E]
`patternN`* {.preservesTupleTail.}: seq[NamedPattern] `patternN`* {.preservesTupleTail.}: seq[NamedPattern[E]]
DefinitionAnd* {.preservesRecord: "and".} = object DefinitionAnd*[E] {.preservesRecord: "and".} = ref object
`data`*: DefinitionAndData `data`*: DefinitionAndData[E]
`Definition`* {.preservesOr.} = ref object `Definition`*[E] {.preservesOr.} = ref object
case orKind*: DefinitionKind case orKind*: DefinitionKind
of DefinitionKind.`or`: of DefinitionKind.`or`:
`or`*: DefinitionOr `or`*: DefinitionOr[E]
of DefinitionKind.`and`: of DefinitionKind.`and`:
`and`*: DefinitionAnd `and`*: DefinitionAnd[E]
of DefinitionKind.`Pattern`: of DefinitionKind.`Pattern`:
`pattern`*: Pattern `pattern`*: Pattern[E]
NamedAlternative* {.preservesTuple.} = object NamedAlternative*[E] {.preservesTuple.} = ref object
`variantLabel`*: string `variantLabel`*: string
`pattern`*: Pattern `pattern`*: Pattern[E]
SchemaData* {.preservesDictionary.} = object SchemaData*[E] {.preservesDictionary.} = ref object
`embeddedType`*: EmbeddedTypeName `embeddedType`*: EmbeddedTypeName
`version`* {.preservesLiteral: "1".}: bool `version`* {.preservesLiteral: "1".}: bool
`definitions`*: Definitions `definitions`*: Definitions[E]
Schema* {.preservesRecord: "schema".} = object Schema*[E] {.preservesRecord: "schema".} = ref object
`data`*: SchemaData `data`*: SchemaData[E]
PatternKind* {.pure.} = enum PatternKind* {.pure.} = enum
`SimplePattern`, `CompoundPattern` `SimplePattern`, `CompoundPattern`
`Pattern`* {.preservesOr.} = ref object `Pattern`*[E] {.preservesOr.} = ref object
case orKind*: PatternKind case orKind*: PatternKind
of PatternKind.`SimplePattern`: of PatternKind.`SimplePattern`:
`simplepattern`*: SimplePattern `simplepattern`*: SimplePattern[E]
of PatternKind.`CompoundPattern`: of PatternKind.`CompoundPattern`:
`compoundpattern`*: CompoundPattern `compoundpattern`*: CompoundPattern[E]
Binding* {.preservesRecord: "named".} = object Binding*[E] {.preservesRecord: "named".} = ref object
`name`* {.preservesSymbol.}: string `name`* {.preservesSymbol.}: string
`pattern`*: SimplePattern `pattern`*: SimplePattern[E]
proc `$`*(x: Ref | ModulePath | Bundle | CompoundPattern | Modules | proc `$`*[E](x: Bundle[E] | CompoundPattern[E] | Modules[E] | Definitions[E] |
EmbeddedTypeName | DictionaryEntries[E] |
Definitions | NamedPattern[E] |
DictionaryEntries | SimplePattern[E] |
NamedPattern | NamedSimplePattern[E] |
SimplePattern | Definition[E] |
NamedSimplePattern | NamedAlternative[E] |
Definition | Schema[E] |
NamedAlternative | Pattern[E] |
Schema | Binding[E]): string =
Pattern |
Binding): string =
`$`(toPreserve(x)) `$`(toPreserve(x))
proc encode*(x: Ref | ModulePath | Bundle | CompoundPattern | Modules | proc encode*[E](x: Bundle[E] | CompoundPattern[E] | Modules[E] | Definitions[E] |
EmbeddedTypeName | DictionaryEntries[E] |
Definitions | NamedPattern[E] |
DictionaryEntries | SimplePattern[E] |
NamedPattern | NamedSimplePattern[E] |
SimplePattern | Definition[E] |
NamedSimplePattern | NamedAlternative[E] |
Definition | Schema[E] |
NamedAlternative | Pattern[E] |
Schema | Binding[E]): seq[byte] =
Pattern | encode(toPreserve(x, E))
Binding): seq[byte] =
proc `$`*(x: Ref | ModulePath | EmbeddedTypeName): string =
`$`(toPreserve(x))
proc encode*(x: Ref | ModulePath | EmbeddedTypeName): seq[byte] =
encode(toPreserve(x)) encode(toPreserve(x))

View File

@ -14,7 +14,7 @@ suite "schema":
else: else:
var var
b = decodePreserves readFile(binPath) b = decodePreserves readFile(binPath)
scm = preserveTo(b, Schema) scm = preserveTo(b, Schema[void])
check scm.isSome check scm.isSome
if scm.isSome: if scm.isSome:
var a = toPreserve(get scm) var a = toPreserve(get scm)