Generate procs for constants

This commit is contained in:
Emery Hemingway 2021-09-06 18:33:20 +02:00
parent 58a9f5dde4
commit 41675a2daf
1 changed files with 24 additions and 13 deletions

View File

@ -240,21 +240,35 @@ proc nimTypeOf(scm: Schema; known: var TypeTable; sn: SchemaNode; name = ""): PN
result = nkCommentStmt.newNode result = nkCommentStmt.newNode
result.comment.add("Missing type generator for " & $sn.kind & " " & $sn) result.comment.add("Missing type generator for " & $sn.kind & " " & $sn)
proc toConst(name: string; def: SchemaNode): Pnode = proc exportIdent(id: string): PNode = nn(nkPostFix, ident"*", ident(id))
proc generateConstProcs(result: var seq[PNode]; name: string; def: SchemaNode) =
case def.kind case def.kind
of snkLiteral: of snkLiteral:
result = nkConstDef.newNode.add( var stmts = nn(nkStmtList)
name.ident.accQuote, newEmpty())
case def.value.kind case def.value.kind
of pkSignedInteger: of pkSignedInteger:
discard result.add newIntNode(nkIntLit, def.value.int) discard stmts.add newIntNode(nkIntLit, def.value.int)
of pkSymbol: of pkSymbol:
discard result.add nn(nkCall, discard stmts.add nn(nkCall,
ident"symbol", ident"symbol",
PNode(kind: nkStrLit, strVal: def.value.symbol), PNode(kind: nkStrLit,
ident"EmbeddedType") strVal: def.value.symbol),
ident"EmbeddedType")
else: else:
raiseAssert("cannot convert " & $def & " to a Nim literal") raiseAssert("conversion of " & $def & " to a Nim literal is not implemented")
var procId = name
procId[0] = procId[0].toLowerAscii
let constProc= nn(nkProcDef,
exportIdent(procId),
newEmpty(),
newEmpty(),
nn(nkFormalParams, ident"Preserve"),
newEmpty(),
newEmpty(),
stmts)
constProc.comment = $def
result.add constProc
else: discard else: discard
proc toNimLit(sn: SchemaNode): PNode = proc toNimLit(sn: SchemaNode): PNode =
@ -336,7 +350,6 @@ proc preserveTypeOf(scm: Schema; known: var TypeTable; sn: SchemaNode; name = ""
result = nimTypeOf(scm, known, sn, name) result = nimTypeOf(scm, known, sn, name)
proc generateProcs(result: var seq[PNode]; name: string; sn: SchemaNode) = proc generateProcs(result: var seq[PNode]; name: string; sn: SchemaNode) =
proc exportIdent(id: string): PNode = nn(nkPostFix, ident"*", ident(id))
case sn.kind case sn.kind
of snkRecord: of snkRecord:
var var
@ -409,7 +422,6 @@ proc generateNimFile*(scm: Schema; path: string) =
var var
knownTypes: TypeTable knownTypes: TypeTable
typeSection = newNode nkTypeSection typeSection = newNode nkTypeSection
constSection = newNode nkLetSection
procs: seq[PNode] procs: seq[PNode]
if scm.embeddedType == "": if scm.embeddedType == "":
typeSection.add nn(nkTypeDef, typeSection.add nn(nkTypeDef,
@ -429,7 +441,7 @@ proc generateNimFile*(scm: Schema; path: string) =
ident"EmbeddedType")) ident"EmbeddedType"))
for name, def in scm.definitions.pairs: for name, def in scm.definitions.pairs:
if isConst(scm, def): if isConst(scm, def):
constSection.add toConst(name, def) generateConstProcs(procs, name, def)
else: else:
var name = name var name = name
name[0] = name[0].toUpperAscii name[0] = name[0].toUpperAscii
@ -481,8 +493,7 @@ proc generateNimFile*(scm: Schema; path: string) =
collectRefImports(imports, scm) collectRefImports(imports, scm)
var module = newNode(nkStmtList).add( var module = newNode(nkStmtList).add(
imports, imports,
typeSection, typeSection
constSection
).add(procs) ).add(procs)
writeFile(path, renderTree(module, {renderNone, renderIr})) writeFile(path, renderTree(module, {renderNone, renderIr}))