Add megaType procs

This commit is contained in:
Emery Hemingway 2021-09-08 11:20:48 +02:00
parent c407179540
commit fc53b47ec6
1 changed files with 36 additions and 2 deletions

View File

@ -349,14 +349,15 @@ proc generateNimFile*(scm: Schema; path: string) =
knownTypes: TypeTable
typeSection = newNode nkTypeSection
procs: seq[PNode]
megaType: PNode
if scm.embeddedType == "":
typeSection.add nn(nkTypeDef,
ident"EmbeddedType",
exportIdent"EmbeddedType",
newEmpty(),
ident"void")
else:
typeSection.add nn(nkTypeDef,
ident"EmbeddedType",
exportIdent"EmbeddedType",
newEmpty(),
scm.embeddedType.ident)
typeSection.add nn(nkTypeDef,
@ -371,6 +372,11 @@ proc generateNimFile*(scm: Schema; path: string) =
else:
var name = name
name[0] = name[0].toUpperAscii
if megaType.isNil:
megaType = ident(name)
else:
megaType = nn(nkInfix,
ident"|", megaType, ident(name))
let t = nimTypeOf(scm, knownTypes, def, name)
t.comment = "``" & $def & "``"
case def.kind
@ -417,6 +423,34 @@ proc generateNimFile*(scm: Schema; path: string) =
ident"std/typetraits",
ident"preserves")
collectRefImports(imports, scm)
procs.add nn(nkProcDef,
"$".ident.accQuote.toExport,
newEmpty(),
newEmpty(),
nn(nkFormalParams,
ident"string",
nn(nkIdentDefs,
ident"x",
megaType,
newEmpty())),
newEmpty(),
newEmpty(),
nn(nkStmtList,
nn(nkCall, ident"$", nn(nkCall, ident"toPreserve", ident"x", ident"EmbeddedType"))))
procs.add nn(nkProcDef,
"encode".ident.accQuote.toExport,
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"EmbeddedType"))))
var module = newNode(nkStmtList).add(
imports,
typeSection