preserves_schema_nim: use field0,1,..N for anonymous fields

This commit is contained in:
Emery Hemingway 2023-05-03 16:02:48 +01:00
parent 67b470078c
commit cd2dde6d71
2 changed files with 8 additions and 8 deletions

View File

@ -1,6 +1,6 @@
# Package
version = "20230410"
version = "20230503"
author = "Emery Hemingway"
description = "data model and serialization format"
license = "Unlicense"

View File

@ -415,12 +415,12 @@ proc identDef(scm: Schema; l: PNode; ts: TypeSpec): PNode =
proc label(pat: Pattern): string =
raiseAssert "need to derive record label for " & $pat
proc label(na: NamedPattern): string =
proc label(na: NamedPattern; index: int): string =
case na.orKind
of NamedPatternKind.named:
string na.named.name
of NamedPatternKind.anonymous:
"data" # TODO
"field" & $index
proc idStr(sp: SimplePattern): string =
if sp.orKind == SimplepatternKind.lit:
@ -523,9 +523,9 @@ proc addField(recList: PNode; loc: Location; known: var TypeTable; sp: SimplePat
proc addFields(recList: PNode; loc: Location; known: var TypeTable; cp: CompoundPattern; parentName: string): PNode {.discardable.} =
let scm = loc.schema
template addField(np: NamedPattern) =
template addField(np: NamedPattern; index: int) =
let
label = np.label
label = label(np, index)
id = label.toFieldIdent
pattern = np.pattern
if pattern.isRef or pattern.isSimple:
@ -542,9 +542,9 @@ proc addFields(recList: PNode; loc: Location; known: var TypeTable; cp: Compound
# recList.add identDef(scm, ident(label), nimTypeOf(loc, known, cp, ""))
raiseassert "unexpected record of fields " #& $cp.rec
of CompoundPatternKind.tuple:
for np in cp.tuple.patterns: addField(np)
for i, np in cp.tuple.patterns: addField(np, i)
of CompoundPatternKind.tuplePrefix:
for np in cp.tuplePrefix.fixed: addField(np)
for i, np in cp.tuplePrefix.fixed: addField(np, i)
let variableType = nimTypeOf(loc, known, cp.tuplePrefix.variable)
recList.add identDef(
scm,
@ -559,7 +559,7 @@ proc addFields(recList: PNode; loc: Location; known: var TypeTable; cp: Compound
proc addFields(recList: PNode; loc: Location; known: var TypeTable; pat: Pattern; parentName: string): PNode {.discardable.} =
case pat.orKind
of PatternKind.SimplePattern:
addField(recList, loc, known, pat.simplePattern, "data")
addField(recList, loc, known, pat.simplePattern, "field0")
of PatternKind.CompoundPattern:
discard addFields(recList, loc, known, pat.compoundPattern, parentName)
reclist