preserves_schema_nim: remove type parameterization

This commit is contained in:
Emery Hemingway 2023-12-28 18:23:14 +02:00
parent 10fc78172e
commit a5cc0a431d
1 changed files with 25 additions and 63 deletions

View File

@ -6,9 +6,13 @@
# the schema module must be regenerated!
# nim c --path:../../../nim --path:.. -r ./preserves_schema_nim ../../../preserves/schema/schema.bin
# This module formally parameterized types wherever embedded values were found.
# This is no longer the case but some of that code is lingering around.
import std/[hashes, strutils, sets, tables]
# Cannot use std/macros, must uss compiler because we are generating code at run-time.
# Cannot use std/macros, must use compiler modules because
# we are generating code at run-time.
import compiler/[ast, idents, renderer, lineinfos]
import ../preserves, ./schema
@ -127,27 +131,6 @@ proc hasEmbeddedType(scm: Schema): bool =
of EmbeddedtypenameKind.false: false
of EmbeddedtypenameKind.Ref: true
proc embeddedIdentString(scm: Schema): string =
case scm.field0.embeddedType.orKind
of EmbeddedtypenameKind.false:
"E"
of EmbeddedtypenameKind.Ref:
doAssert $scm.field0.embeddedType.ref.name != ""
$scm.field0.embeddedType.ref.name
proc embeddedIdent(scm: Schema): PNode =
ident(embeddedIdentString(scm))
proc preserveIdent(scm: Schema): Pnode = ident"Value"
proc parameterize(scm: Schema; node: PNode; embeddable: bool): PNode =
if embeddable and node.kind notin {nkBracketExpr}:
nkBracketExpr.newTree(node, scm.embeddedIdent)
else: node
proc parameterize(scm: Schema; spec: TypeSpec): PNode =
parameterize(scm, spec.node, spec.isEmbedded)
proc hash(r: Ref): Hash = r.toPreserves.hash
type RefSet = HashSet[Ref]
@ -159,10 +142,9 @@ proc attrs(loc: Location; n: NamedAlternative|NamedPattern; seen: RefSet): Attri
proc attrs(loc: Location; sp: SimplePattern; seen: RefSet): Attributes =
case sp.orKind
of SimplepatternKind.atom, SimplepatternKind.lit: {}
of SimplepatternKind.any, SimplepatternKind.embedded:
if loc.schema.hasEmbeddedType: {embedded}
else: {}
of SimplepatternKind.atom, SimplepatternKind.lit, SimplepatternKind.any: {}
of SimplepatternKind.embedded:
attrs(loc, sp.embedded.interface, seen)
of SimplepatternKind.seqof:
attrs(loc, sp.seqof.pattern, seen)
of SimplepatternKind.setof:
@ -282,9 +264,9 @@ proc isSimple(pat: Pattern): bool =
proc isLiteral(loc: Location; na: NamedAlternative): bool = isLiteral(loc, na.pattern)
proc isSymbolEnum(loc: Location; orDef: DefinitionOr): bool =
result = isLiteral(loc, orDef.field0.pattern0) and isLiteral(loc, orDef.field0.pattern1)
for na in orDef.field0.patternN:
proc isSymbolEnum(loc: Location; def: DefinitionOr): bool =
result = isLiteral(loc, def.field0.pattern0) and isLiteral(loc, def.field0.pattern1)
for na in def.field0.patternN:
if not result: break
result = isLiteral(loc, na)
@ -339,7 +321,6 @@ proc typeIdent(atom: AtomKind): PNode =
of AtomKind.Symbol: ident"Symbol"
proc typeIdent(loc: Location; sp: SimplePattern): TypeSpec =
let scm = loc.schema
case sp.orKind
of SimplepatternKind.atom:
result = TypeSpec(node: typeIdent(sp.atom.atomKind))
@ -361,16 +342,11 @@ proc typeIdent(loc: Location; sp: SimplePattern): TypeSpec =
result.attrs = key.attrs + val.attrs
of SimplepatternKind.Ref:
result = TypeSpec(node: ident(sp.ref), attrs: attrs(loc, sp))
result.node = parameterize(scm, result)
of SimplepatternKind.embedded:
case scm.field0.embeddedType.orKind
of EmbeddedtypenameKind.false:
result = typeIdent(loc, sp.embedded.interface)
of EmbeddedtypenameKind.Ref:
result = TypeSpec(node: scm.embeddedIdent())
result = typeIdent(loc, sp.embedded.interface)
incl(result.attrs, embedded)
of SimplepatternKind.any, SimplepatternKind.lit:
result = TypeSpec(node: preserveIdent(scm))
result = TypeSpec(node: ident"Value")
proc typeIdent(loc: Location; pat: Pattern): TypeSpec =
case pat.orKind
@ -414,18 +390,8 @@ proc toFieldIdent(loc: Location, label: string; pat: Pattern): PNode =
proc newEmpty(): PNode = newNode(nkEmpty)
proc embeddingParams(scm: Schema; embeddable: bool): PNode =
if embeddable:
nkGenericParams.newTree(nkIdentDefs.newTree(embeddedIdent(scm), newEmpty(), newEmpty()))
else:
newEmpty()
proc identDef(scm: Schema; a, b: PNode; embeddable: bool): PNode =
if embeddable and scm.hasEmbeddedType and b.kind notin {nkBracketExpr, nkTupleTy} and
(b.kind != nkIdent or b.ident.s != scm.embeddedIdentString):
nkIdentDefs.newTree(a, nkBracketExpr.newTree(b, embeddedIdent(scm)), newEmpty())
else:
nkIdentDefs.newTree(a, b, newEmpty())
nkIdentDefs.newTree(a, b, newEmpty())
proc identDef(scm: Schema; l: PNode; ts: TypeSpec): PNode =
identDef(scm, l, ts.node, ts.isEmbedded)
@ -463,8 +429,7 @@ proc idStr(np: NamedPattern): string =
proc typeDef(loc: Location; name: string; pat: Pattern; ty: PNode): PNode =
let
scm = loc.schema
embedParams = embeddingParams(scm, isEmbedded(loc, pat))
embedParams = newEmpty()
id = name.ident.toExport
case pat.orKind
of PatternKind.CompoundPattern:
@ -503,13 +468,13 @@ proc typeDef(loc: Location; name: string; def: Definition; ty: PNode): PNode =
nkPragmaExpr.newTree(
name.ident.accQuote.toExport,
pragma),
embeddingParams(loc.schema, isEmbedded(loc, def)),
newEmpty(),
ty)
of DefinitionKind.and:
nkTypeDef.newTree(
name.ident.toExport,
embeddingParams(loc.schema, isEmbedded(loc, def)),
preserveIdent(loc.schema))
newEmpty(),
ident"Value")
of DefinitionKind.Pattern:
typeDef(loc, name, def.pattern, ty)
@ -569,7 +534,7 @@ proc addFields(recList: PNode; loc: Location; known: var TypeTable; cp: Compound
nkPragmaExpr.newTree(
ident(cp.tuplePrefix.variable, parentName).accQuote.toExport,
nkPragma.newTree(ident"preservesTupleTail")),
parameterize(scm, variableType),
variableType.node,
variableType.isEmbedded)
else: raiseAssert "not adding fields for " #& $cp
reclist
@ -608,7 +573,7 @@ proc nimTypeOf(loc: Location; known: var TypeTable; rec: CompoundPatternRec; nam
newEmpty(), newEmpty(),
newNode(nkRecList).addFields(loc, known, rec.fields.pattern, name))
else:
result.node = preserveIdent(loc.schema)
result.node = ident"Value"
proc nimTypeOf(loc: Location; known: var TypeTable; cp: CompoundPattern; name: string): TypeSpec =
case cp.orKind
@ -679,8 +644,6 @@ proc nimTypeOf(loc: Location; known: var TypeTable; orDef: DefinitionOr; name: s
known[memberPath] =
typeDef(loc, memberTypeName, na.pattern, ty.node)
addAttrs(result, memberType)
memberType.node = parameterize(
scm, memberType.node, isEmbedded(loc, na.pattern))
branchRecList.add nkIdentDefs.newTree(
toFieldIdent(loc, na.variantLabel.normalize, na.pattern),
memberType.node, newEmpty())
@ -704,7 +667,7 @@ proc nimTypeOf(loc: Location; known: var TypeTable; def: Definition; name: strin
of DefinitionKind.or:
nimTypeOf(loc, known, def.or, name)
of DefinitionKind.and:
TypeSpec(node: preserveIdent(loc.schema))
TypeSpec(node: ident"Value")
of DefinitionKind.Pattern:
nimTypeOf(loc, known, def.pattern, name)
@ -792,7 +755,7 @@ proc renderNimBundle*(bundle: Bundle): Table[string, string] =
else:
var name = string name
name[0] = name[0].toUpperAscii
var defIdent = parameterize(scm, ident(name), isEmbedded(loc, def))
var defIdent = ident(name)
if not isSymbolEnum(loc, def) and not isAny(loc, def):
if isEmbedded(loc, def):
mergeType(embeddableType, defIdent)
@ -814,8 +777,7 @@ proc renderNimBundle*(bundle: Bundle): Table[string, string] =
for module in importSet:
add(imports, ident(module))
if not embeddableType.isNil:
let genericParams =
nkGenericParams.newTree(nkIdentDefs.newTree(embeddedIdent(scm), newEmpty(), newEmpty()))
let genericParams = newEmpty()
procs.add nkProcDef.newTree(
"$".toFieldIdent,
newEmpty(),
@ -830,7 +792,7 @@ proc renderNimBundle*(bundle: Bundle): Table[string, string] =
newEmpty(),
nkStmtList.newTree(
nkCall.newTree(ident"$",
nkCall.newTree(ident"toPreserves", ident"x", embeddedIdent(scm)))))
nkCall.newTree(ident"toPreserves", ident"x"))))
procs.add nkProcDef.newTree(
"encode".ident.toExport,
newEmpty(),
@ -845,7 +807,7 @@ proc renderNimBundle*(bundle: Bundle): Table[string, string] =
newEmpty(),
nkStmtList.newTree(
nkCall.newTree(ident"encode",
nkCall.newTree(ident"toPreserves", ident"x", embeddedIdent(scm)))))
nkCall.newTree(ident"toPreserves", ident"x"))))
if not unembeddableType.isNil:
procs.add nkProcDef.newTree(
"$".toFieldIdent,