Schemas: seperate procs for unembeddables

This commit is contained in:
Emery Hemingway 2021-10-17 17:24:36 +02:00
parent 54c28c90ce
commit 171502f1d2
2 changed files with 71 additions and 37 deletions

View File

@ -692,11 +692,14 @@ proc collectRefImports(imports: PNode; scm: Schema) =
proc renderNimModule*(scm: Schema): string =
## Construct and render a Nim module from a `Schema`.
proc mergeType(x: var PNode; y: PNode) =
if x.isNil: x = y
else: x = nn(nkInfix, ident"|", x, y)
var
typeDefs: TypeTable
typeSection = newNode nkTypeSection
procs: seq[PNode]
megaType: PNode
unembeddableType, embeddableType: PNode
for name, def in scm.data.definitions.pairs:
if isLiteral(scm, def):
generateConstProcs(procs, scm, name, def)
@ -705,11 +708,10 @@ proc renderNimModule*(scm: Schema): string =
name[0] = name[0].toUpperAscii
var defIdent = parameterize(ident(name), isEmbeddable(scm, def))
if not isSymbolEnum(scm, def):
if megaType.isNil:
megaType = defIdent
if isEmbeddable(scm, def):
mergeType(embeddableType, defIdent)
else:
megaType = nn(nkInfix,
ident"|", megaType, defIdent)
mergeType(unembeddableType, defIdent)
let typeSpec = nimTypeOf(scm, typeDefs, def, name)
typeDefs[name] = typeDef(scm, name, def, typeSpec.node)
generateProcs(procs, scm, name, def)
@ -723,36 +725,68 @@ proc renderNimModule*(scm: Schema): string =
if isEmbeddable(scm):
nn(nkGenericParams, nn(nkIdentDefs, ident"E", newEmpty(), newEmpty()))
else: newEmpty()
procs.add nn(nkProcDef,
"$".toFieldIdent,
newEmpty(),
dollarGenericParams,
nn(nkFormalParams,
ident"string",
nn(nkIdentDefs,
ident"x",
megaType,
newEmpty())),
newEmpty(),
newEmpty(),
nn(nkStmtList,
nn(nkCall, ident"$",
nn(nkCall, ident"toPreserve", ident"x"))))
procs.add nn(nkProcDef,
"encode".ident.toExport,
newEmpty(),
nn(nkGenericParams, nn(nkIdentDefs, ident"E", newEmpty(), newEmpty())),
nn(nkFormalParams,
nn(nkBracketExpr, ident"seq", ident"byte"),
nn(nkIdentDefs,
ident"x",
megaType,
newEmpty())),
newEmpty(),
newEmpty(),
nn(nkStmtList,
nn(nkCall, ident"encode", nn(nkCall,
ident"toPreserve", ident"x", ident"E"))))
if not embeddableType.isNil:
procs.add nn(nkProcDef,
"$".toFieldIdent,
newEmpty(),
dollarGenericParams,
nn(nkFormalParams,
ident"string",
nn(nkIdentDefs,
ident"x",
embeddableType,
newEmpty())),
newEmpty(),
newEmpty(),
nn(nkStmtList,
nn(nkCall, ident"$",
nn(nkCall, ident"toPreserve", ident"x"))))
procs.add nn(nkProcDef,
"encode".ident.toExport,
newEmpty(),
nn(nkGenericParams, nn(nkIdentDefs, ident"E", newEmpty(), newEmpty())),
nn(nkFormalParams,
nn(nkBracketExpr, ident"seq", ident"byte"),
nn(nkIdentDefs,
ident"x",
embeddableType,
newEmpty())),
newEmpty(),
newEmpty(),
nn(nkStmtList,
nn(nkCall, ident"encode", nn(nkCall,
ident"toPreserve", ident"x", ident"E"))))
if not unembeddableType.isNil:
procs.add nn(nkProcDef,
"$".toFieldIdent,
newEmpty(),
newEmpty(),
nn(nkFormalParams,
ident"string",
nn(nkIdentDefs,
ident"x",
unembeddableType,
newEmpty())),
newEmpty(),
newEmpty(),
nn(nkStmtList,
nn(nkCall, ident"$",
nn(nkCall, ident"toPreserve", ident"x"))))
procs.add nn(nkProcDef,
"encode".ident.toExport,
newEmpty(),
newEmpty(),
nn(nkFormalParams,
nn(nkBracketExpr, ident"seq", ident"byte"),
nn(nkIdentDefs,
ident"x",
unembeddableType,
newEmpty())),
newEmpty(),
newEmpty(),
nn(nkStmtList,
nn(nkCall, ident"encode", nn(nkCall,
ident"toPreserve", ident"x"))))
var module = newNode(nkStmtList).add(
imports,
typeSection

View File

@ -199,7 +199,7 @@ proc `$`*(x: Ref | ModulePath | Bundle | CompoundPattern | Modules |
Binding): string =
`$`(toPreserve(x))
proc encode*[E](x: Ref | ModulePath | Bundle | CompoundPattern | Modules |
proc encode*(x: Ref | ModulePath | Bundle | CompoundPattern | Modules |
EmbeddedTypeName |
Definitions |
DictionaryEntries |
@ -211,4 +211,4 @@ proc encode*[E](x: Ref | ModulePath | Bundle | CompoundPattern | Modules |
Schema |
Pattern |
Binding): seq[byte] =
encode(toPreserve(x, E))
encode(toPreserve(x))