From 171502f1d2ad0a23b907aa5162a57f78a26657aa Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Sun, 17 Oct 2021 17:24:36 +0200 Subject: [PATCH] Schemas: seperate procs for unembeddables --- src/preserves/preserves_schema_nim.nim | 104 ++++++++++++++++--------- src/preserves/schema.nim | 4 +- 2 files changed, 71 insertions(+), 37 deletions(-) diff --git a/src/preserves/preserves_schema_nim.nim b/src/preserves/preserves_schema_nim.nim index f6ccf25..9dcb8b2 100644 --- a/src/preserves/preserves_schema_nim.nim +++ b/src/preserves/preserves_schema_nim.nim @@ -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 diff --git a/src/preserves/schema.nim b/src/preserves/schema.nim index 8325a00..dbc50f2 100644 --- a/src/preserves/schema.nim +++ b/src/preserves/schema.nim @@ -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))