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

View File

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