preserves_schema_nim: code formatting

This commit is contained in:
Emery Hemingway 2023-03-01 11:03:22 -06:00
parent 3a6dfc0349
commit a130079162
1 changed files with 81 additions and 77 deletions

View File

@ -70,11 +70,11 @@ proc ident(sp: SimplePattern): PNode =
proc ident(cp: CompoundPattern; fallback: string): PNode = proc ident(cp: CompoundPattern; fallback: string): PNode =
case cp.orKind case cp.orKind
of CompoundPatternKind.`rec`: of CompoundPatternKind.rec:
ident($cp.rec.label) ident($cp.rec.label)
of CompoundPatternKind.`tuple`, of CompoundPatternKind.tuple,
CompoundPatternKind.`tuplePrefix`, CompoundPatternKind.tuplePrefix,
CompoundPatternKind.`dict`: CompoundPatternKind.dict:
ident(fallback) ident(fallback)
proc ident(pat: Pattern; fallback = string): PNode = proc ident(pat: Pattern; fallback = string): PNode =
@ -86,16 +86,16 @@ proc ident(pat: Pattern; fallback = string): PNode =
proc ident(np: NamedPattern; fallback: string): PNode = proc ident(np: NamedPattern; fallback: string): PNode =
case np.orKind case np.orKind
of NamedPatternKind.`named`: of NamedPatternKind.named:
ident(string np.named.name) ident(string np.named.name)
of NamedPatternKind.`anonymous`: of NamedPatternKind.anonymous:
ident(fallback) ident(fallback)
proc ident(np: NamedSimplePattern; fallback: string): PNode = proc ident(np: NamedSimplePattern; fallback: string): PNode =
case np.orKind case np.orKind
of NamedSimplePatternKind.`named`: of NamedSimplePatternKind.named:
ident(string np.named.name) ident(string np.named.name)
of NamedSimplePatternKind.`anonymous`: of NamedSimplePatternKind.anonymous:
ident(fallback) ident(fallback)
proc isPreserve(n: PNode): bool = proc isPreserve(n: PNode): bool =
@ -117,7 +117,7 @@ proc dotExtend(result: var PNode; label: string) =
else: result = nn(nkDotExpr, result, id) else: result = nn(nkDotExpr, result, id)
proc ident(`ref`: Ref): PNode = proc ident(`ref`: Ref): PNode =
for m in`ref`.module: dotExtend(result, string m) for m in `ref`.module: dotExtend(result, string m)
dotExtend(result, `ref`.name.string.capitalizeAscii) dotExtend(result, `ref`.name.string.capitalizeAscii)
proc deref(loc: Location; r: Ref): (Location, Definition) = proc deref(loc: Location; r: Ref): (Location, Definition) =
@ -130,14 +130,14 @@ proc deref(loc: Location; r: Ref): (Location, Definition) =
proc hasEmbeddedType(scm: Schema): bool = proc hasEmbeddedType(scm: Schema): bool =
case scm.data.embeddedType.orKind case scm.data.embeddedType.orKind
of EmbeddedtypenameKind.`false`: false of EmbeddedtypenameKind.false: false
of EmbeddedtypenameKind.`Ref`: true of EmbeddedtypenameKind.Ref: true
proc embeddedIdentString(scm: Schema): string = proc embeddedIdentString(scm: Schema): string =
case scm.data.embeddedType.orKind case scm.data.embeddedType.orKind
of EmbeddedtypenameKind.`false`: of EmbeddedtypenameKind.false:
raiseAssert "no embedded type for this module" raiseAssert "no embedded type for this module"
of EmbeddedtypenameKind.`Ref`: of EmbeddedtypenameKind.Ref:
doAssert $scm.data.embeddedType.ref.name != "" doAssert $scm.data.embeddedType.ref.name != ""
$scm.data.embeddedType.ref.name $scm.data.embeddedType.ref.name
@ -171,17 +171,17 @@ proc step(loc: Location; r: Ref): Location = (loc.bundle, r.module)
proc attrs(loc: Location; sp: SimplePattern; seen: RefSet): Attributes = proc attrs(loc: Location; sp: SimplePattern; seen: RefSet): Attributes =
case sp.orKind case sp.orKind
of SimplepatternKind.`atom`, SimplepatternKind.`lit`: {} of SimplepatternKind.atom, SimplepatternKind.lit: {}
of SimplepatternKind.`any`, SimplepatternKind.`embedded`: of SimplepatternKind.any, SimplepatternKind.embedded:
if loc.schema.hasEmbeddedType: {embedded} if loc.schema.hasEmbeddedType: {embedded}
else: {} else: {}
of SimplepatternKind.`seqof`: of SimplepatternKind.seqof:
attrs(loc, sp.seqof.pattern, seen) attrs(loc, sp.seqof.pattern, seen)
of SimplepatternKind.`setof`: of SimplepatternKind.setof:
attrs(loc, sp.setof.pattern, seen) attrs(loc, sp.setof.pattern, seen)
of SimplepatternKind.`dictof`: of SimplepatternKind.dictof:
attrs(loc, sp.dictof.key, seen) + attrs(loc, sp.dictof.value, seen) attrs(loc, sp.dictof.key, seen) + attrs(loc, sp.dictof.value, seen)
of SimplepatternKind.`Ref`: of SimplepatternKind.Ref:
if sp.ref in seen: {recursive} if sp.ref in seen: {recursive}
else: else:
var var
@ -192,32 +192,32 @@ proc attrs(loc: Location; sp: SimplePattern; seen: RefSet): Attributes =
proc attrs(loc: Location; np: NamedSimplePattern; seen: RefSet): Attributes = proc attrs(loc: Location; np: NamedSimplePattern; seen: RefSet): Attributes =
case np.orKind case np.orKind
of NamedSimplePatternKind.`named`: of NamedSimplePatternKind.named:
attrs(loc, np.named.pattern, seen) attrs(loc, np.named.pattern, seen)
of NamedSimplePatternKind.`anonymous`: of NamedSimplePatternKind.anonymous:
attrs(loc, np.anonymous, seen) attrs(loc, np.anonymous, seen)
proc attrs(loc: Location; cp: CompoundPattern; seen: RefSet): Attributes = proc attrs(loc: Location; cp: CompoundPattern; seen: RefSet): Attributes =
case cp.orKind case cp.orKind
of CompoundPatternKind.`rec`: of CompoundPatternKind.rec:
result = result =
attrs(loc, cp.rec.label.pattern, seen) + attrs(loc, cp.rec.label.pattern, seen) +
attrs(loc, cp.rec.fields.pattern, seen) attrs(loc, cp.rec.fields.pattern, seen)
of CompoundPatternKind.`tuple`: of CompoundPatternKind.tuple:
for np in cp.tuple.patterns: for np in cp.tuple.patterns:
result = result + attrs(loc, np.pattern, seen) result = result + attrs(loc, np.pattern, seen)
of CompoundPatternKind.`tupleprefix`: of CompoundPatternKind.tupleprefix:
result = attrs(loc, cp.tupleprefix.variable, seen) result = attrs(loc, cp.tupleprefix.variable, seen)
for p in cp.tupleprefix.fixed: for p in cp.tupleprefix.fixed:
result = result + attrs(loc, p, seen) result = result + attrs(loc, p, seen)
of CompoundPatternKind.`dict`: of CompoundPatternKind.dict:
discard discard
proc attrs(loc: Location; pat: Pattern; seen: RefSet): Attributes = proc attrs(loc: Location; pat: Pattern; seen: RefSet): Attributes =
case pat.orKind case pat.orKind
of PatternKind.`SimplePattern`: of PatternKind.SimplePattern:
attrs(loc, pat.simplePattern, seen) attrs(loc, pat.simplePattern, seen)
of PatternKind.`CompoundPattern`: of PatternKind.CompoundPattern:
attrs(loc, pat.compoundPattern, seen) attrs(loc, pat.compoundPattern, seen)
proc attrs(loc: Location; orDef: DefinitionOr; seen: RefSet): Attributes = proc attrs(loc: Location; orDef: DefinitionOr; seen: RefSet): Attributes =
@ -227,12 +227,14 @@ proc attrs(loc: Location; orDef: DefinitionOr; seen: RefSet): Attributes =
proc attrs(loc: Location; def: Definition; seen: RefSet): Attributes = proc attrs(loc: Location; def: Definition; seen: RefSet): Attributes =
case def.orKind case def.orKind
of DefinitionKind.`or`: result = attrs(loc, def.or, seen) of DefinitionKind.or: result = attrs(loc, def.or, seen)
of DefinitionKind.`and`: of DefinitionKind.and:
result = attrs(loc, def.and.data.pattern0, seen) + attrs(loc, def.and.data.pattern1, seen) result =
attrs(loc, def.and.data.pattern0, seen) +
attrs(loc, def.and.data.pattern1, seen)
for p in def.and.data.patternN: for p in def.and.data.patternN:
result = result + attrs(loc, p, seen) result = result + attrs(loc, p, seen)
of DefinitionKind.`Pattern`: of DefinitionKind.Pattern:
result = attrs(loc, def.pattern, seen) result = attrs(loc, def.pattern, seen)
proc attrs(loc: Location; p: Definition|DefinitionOr|Pattern|CompoundPattern|SimplePattern): Attributes = proc attrs(loc: Location; p: Definition|DefinitionOr|Pattern|CompoundPattern|SimplePattern): Attributes =
@ -271,7 +273,7 @@ proc isLiteral(loc: Location; def: Definition): bool =
result = isLiteral(loc, def.pattern) result = isLiteral(loc, def.pattern)
proc isRef(sp: SimplePattern): bool = proc isRef(sp: SimplePattern): bool =
sp.orKind == SimplePatternKind.`Ref` sp.orKind == SimplePatternKind.Ref
proc isRef(pat: Pattern): bool = proc isRef(pat: Pattern): bool =
pat.orKind == PatternKind.SimplePattern and pat.orKind == PatternKind.SimplePattern and
@ -292,16 +294,16 @@ proc isSymbolEnum(loc: Location; def: Definition): bool =
case def.orKind case def.orKind
of DefinitionKind.Pattern: of DefinitionKind.Pattern:
if def.pattern.orKind == PatternKind.SimplePattern and if def.pattern.orKind == PatternKind.SimplePattern and
def.pattern.simplePattern.orKind == SimplepatternKind.`Ref`: def.pattern.simplePattern.orKind == SimplepatternKind.Ref:
var (loc, def) = deref(loc, def.pattern.simplePattern.ref) var (loc, def) = deref(loc, def.pattern.simplePattern.ref)
result = isSymbolEnum(loc, def) result = isSymbolEnum(loc, def)
of DefinitionKind.or: of DefinitionKind.or:
result = isSymbolEnum(loc, def.or) result = isSymbolEnum(loc, def.or)
else: discard else: discard
proc isSymbolEnum(loc: Location; sp: SimplePattern): bool = proc isSymbolEnum(loc: Location; sp: SimplePattern): bool =
# HashSet # HashSet
if sp.orKind == SimplepatternKind.`Ref`: if sp.orKind == SimplepatternKind.Ref:
var (loc, def) = deref(loc, sp.ref) var (loc, def) = deref(loc, sp.ref)
result = isSymbolEnum(loc, def) result = isSymbolEnum(loc, def)
else: discard else: discard
@ -311,7 +313,7 @@ proc isAny(loc: Location; def: Definition): bool =
if def.pattern.orKind == PatternKind.SimplePattern: if def.pattern.orKind == PatternKind.SimplePattern:
case def.pattern.simplePattern.orKind case def.pattern.simplePattern.orKind
of SimplePatternKind.Ref: of SimplePatternKind.Ref:
var (loc, def) = deref(loc, def.pattern.simplePattern.`ref`) var (loc, def) = deref(loc, def.pattern.simplePattern.ref)
result = isAny(loc, def) result = isAny(loc, def)
of SimplePatternKind.any: of SimplePatternKind.any:
result = true result = true
@ -319,46 +321,46 @@ proc isAny(loc: Location; def: Definition): bool =
proc typeIdent(atom: AtomKind): PNode = proc typeIdent(atom: AtomKind): PNode =
case atom case atom
of AtomKind.`Boolean`: ident"bool" of AtomKind.Boolean: ident"bool"
of AtomKind.`Float`: ident"float32" of AtomKind.Float: ident"float32"
of AtomKind.`Double`: ident"float64" of AtomKind.Double: ident"float64"
of AtomKind.`Signedinteger`: ident"BiggestInt" of AtomKind.Signedinteger: ident"BiggestInt"
of AtomKind.`String`: ident"string" of AtomKind.String: ident"string"
of AtomKind.`Bytestring`: nn(nkBracketExpr, ident"seq", ident"byte") of AtomKind.Bytestring: nn(nkBracketExpr, ident"seq", ident"byte")
of AtomKind.`Symbol`: ident"Symbol" of AtomKind.Symbol: ident"Symbol"
proc typeIdent(loc: Location; sp: SimplePattern): TypeSpec = proc typeIdent(loc: Location; sp: SimplePattern): TypeSpec =
let scm = loc.schema let scm = loc.schema
case sp.orKind case sp.orKind
of SimplepatternKind.`atom`: of SimplepatternKind.atom:
result = TypeSpec(node: typeIdent(sp.atom.atomKind)) result = TypeSpec(node: typeIdent(sp.atom.atomKind))
of SimplepatternKind.`seqof`: of SimplepatternKind.seqof:
result = typeIdent(loc, sp.seqof.pattern) result = typeIdent(loc, sp.seqof.pattern)
result.node = nn(nkBracketExpr, ident"seq", result.node) result.node = nn(nkBracketExpr, ident"seq", result.node)
of SimplepatternKind.`setof`: of SimplepatternKind.setof:
result = typeIdent(loc, sp.setof.pattern) result = typeIdent(loc, sp.setof.pattern)
result.node = result.node =
if isSymbolEnum(loc, sp.setof.pattern): if isSymbolEnum(loc, sp.setof.pattern):
nn(nkBracketExpr, ident"set", result.node) nn(nkBracketExpr, ident"set", result.node)
else: else:
nn(nkBracketExpr, ident"HashSet", result.node) nn(nkBracketExpr, ident"HashSet", result.node)
of SimplepatternKind.`dictof`: of SimplepatternKind.dictof:
let let
key = typeIdent(loc, sp.dictof.key) key = typeIdent(loc, sp.dictof.key)
val = typeIdent(loc, sp.dictof.value) val = typeIdent(loc, sp.dictof.value)
result.node = nn(nkBracketExpr, ident"Table", key.node, val.node) result.node = nn(nkBracketExpr, ident"Table", key.node, val.node)
result.attrs = key.attrs + val.attrs result.attrs = key.attrs + val.attrs
of SimplepatternKind.`Ref`: of SimplepatternKind.Ref:
result = TypeSpec(node: ident(sp.ref), attrs: attrs(loc, sp)) result = TypeSpec(node: ident(sp.ref), attrs: attrs(loc, sp))
result.node = parameterize(scm, result) result.node = parameterize(scm, result)
of SimplepatternKind.`embedded`: of SimplepatternKind.embedded:
case scm.data.embeddedType.orKind case scm.data.embeddedType.orKind
of EmbeddedtypenameKind.`false`: of EmbeddedtypenameKind.false:
result = typeIdent(loc, sp.embedded.interface) result = typeIdent(loc, sp.embedded.interface)
of EmbeddedtypenameKind.`Ref`: of EmbeddedtypenameKind.Ref:
result = TypeSpec(node: scm.embeddedIdent()) result = TypeSpec(node: scm.embeddedIdent())
incl(result.attrs, embedded) incl(result.attrs, embedded)
of SimplepatternKind.`any`, SimplepatternKind.`lit`: of SimplepatternKind.any, SimplepatternKind.lit:
result = TypeSpec(node: preserveIdent(scm)) result = TypeSpec(node: preserveIdent(scm))
proc typeIdent(loc: Location; pat: Pattern): TypeSpec = proc typeIdent(loc: Location; pat: Pattern): TypeSpec =
@ -424,9 +426,9 @@ proc label(pat: Pattern): string =
proc label(na: NamedPattern): string = proc label(na: NamedPattern): string =
case na.orKind case na.orKind
of NamedPatternKind.`named`: of NamedPatternKind.named:
string na.named.name string na.named.name
of NamedPatternKind.`anonymous`: of NamedPatternKind.anonymous:
"data" # TODO "data" # TODO
proc idStr(sp: SimplePattern): string = proc idStr(sp: SimplePattern): string =
@ -437,7 +439,7 @@ proc idStr(sp: SimplePattern): string =
of pkSymbol: of pkSymbol:
result = string sp.lit.value.symbol result = string sp.lit.value.symbol
else: discard else: discard
doAssert(result != "", "no idStr for " & $sp) doAssert(result != "", "no idStr for " & $sp)
proc idStr(pat: Pattern): string = proc idStr(pat: Pattern): string =
doAssert(pat.orKind == PatternKind.SimplePattern) doAssert(pat.orKind == PatternKind.SimplePattern)
@ -445,9 +447,9 @@ proc idStr(pat: Pattern): string =
proc idStr(np: NamedPattern): string = proc idStr(np: NamedPattern): string =
case np.orKind case np.orKind
of NamedPatternKind.`named`: of NamedPatternKind.named:
string np.named.name string np.named.name
of NamedPatternKind.`anonymous`: of NamedPatternKind.anonymous:
np.anonymous.idStr np.anonymous.idStr
proc typeDef(loc: Location; name: string; pat: Pattern; ty: PNode): PNode = proc typeDef(loc: Location; name: string; pat: Pattern; ty: PNode): PNode =
@ -456,25 +458,27 @@ proc typeDef(loc: Location; name: string; pat: Pattern; ty: PNode): PNode =
embedParams = embeddingParams(scm, isEmbedded(loc, pat)) embedParams = embeddingParams(scm, isEmbedded(loc, pat))
id = name.ident.toExport id = name.ident.toExport
case pat.orKind case pat.orKind
of PatternKind.`CompoundPattern`: of PatternKind.CompoundPattern:
case pat.compoundPattern.orKind case pat.compoundPattern.orKind
of CompoundPatternKind.`rec`: of CompoundPatternKind.rec:
nn(nkTypeDef, nn(nkTypeDef,
nn(nkPragmaExpr, nn(nkPragmaExpr,
id, nn(nkPragma, id,
nn(nkExprColonExpr, nn(nkPragma,
ident"preservesRecord", nn(nkExprColonExpr,
PNode(kind: nkStrLit, strVal: pat.compoundPattern.rec.label.idStr)))), ident"preservesRecord",
PNode(kind: nkStrLit, strVal: pat.compoundPattern.rec.label.idStr)))),
embedParams, embedParams,
ty) ty)
of CompoundPatternKind.`tuple`, CompoundPatternKind.`tuplePrefix`: of CompoundPatternKind.tuple, CompoundPatternKind.tuplePrefix:
nn(nkTypeDef, nn(nkTypeDef,
nn(nkPragmaExpr, id, nn(nkPragma, ident"preservesTuple")), nn(nkPragmaExpr, id, nn(nkPragma, ident"preservesTuple")),
embedParams, embedParams,
ty) ty)
of CompoundPatternKind.`dict`: of CompoundPatternKind.dict:
nn(nkTypeDef, nn(nkTypeDef,
nn(nkPragmaExpr, id, nn(nkPragma, ident"preservesDictionary")), nn(nkPragmaExpr,
id, nn(nkPragma, ident"preservesDictionary")),
embedParams, embedParams,
ty) ty)
else: else:
@ -520,9 +524,9 @@ proc addField(recList: PNode; loc: Location; known: var TypeTable; sp: SimplePat
toStrLit(loc, sp)))) toStrLit(loc, sp))))
recList.add identDef(scm, id, TypeSpec(node: ident"bool")) recList.add identDef(scm, id, TypeSpec(node: ident"bool"))
elif sp.orKind == SimplePatternKind.embedded and not scm.hasEmbeddedType: elif sp.orKind == SimplePatternKind.embedded and not scm.hasEmbeddedType:
let id = nn(nkPragmaExpr, let id = nn(nkPragmaExpr,
id, nn(nkPragma, ident"preservesEmbedded")) id, nn(nkPragma, ident"preservesEmbedded"))
recList.add identDef(scm, id, nimTypeOf(loc, known, sp)) recList.add identDef(scm, id, nimTypeOf(loc, known, sp))
else: else:
recList.add identDef(scm, id, nimTypeOf(loc, known, sp)) recList.add identDef(scm, id, nimTypeOf(loc, known, sp))
@ -723,21 +727,21 @@ proc collectRefImports(loc: Location; imports: PNode; sp: SimplePattern) =
of SimplePatternKind.dictof: of SimplePatternKind.dictof:
imports.add ident"std/tables" imports.add ident"std/tables"
of SimplePatternKind.Ref: of SimplePatternKind.Ref:
if sp.`ref`.module != @[] and sp.`ref`.module != loc.schemaPath: if sp.ref.module != @[] and sp.ref.module != loc.schemaPath:
imports.add ident(string sp.ref.module[0]) imports.add ident(string sp.ref.module[0])
else: discard else: discard
proc collectRefImports(loc: Location; imports: PNode; cp: CompoundPattern) = proc collectRefImports(loc: Location; imports: PNode; cp: CompoundPattern) =
case cp.orKind case cp.orKind
of CompoundPatternKind.`rec`: of CompoundPatternKind.rec:
collectRefImports(loc, imports, cp.rec.label.pattern) collectRefImports(loc, imports, cp.rec.label.pattern)
collectRefImports(loc, imports, cp.rec.fields.pattern) collectRefImports(loc, imports, cp.rec.fields.pattern)
of CompoundPatternKind.`tuple`: of CompoundPatternKind.tuple:
for p in cp.tuple.patterns: collectRefImports(loc, imports, p.pattern) for p in cp.tuple.patterns: collectRefImports(loc, imports, p.pattern)
of CompoundPatternKind.`tupleprefix`: of CompoundPatternKind.tupleprefix:
for np in cp.tupleprefix.fixed: collectRefImports(loc, imports, np.pattern) for np in cp.tupleprefix.fixed: collectRefImports(loc, imports, np.pattern)
collectRefImports(loc, imports, cp.tupleprefix.variable.pattern) collectRefImports(loc, imports, cp.tupleprefix.variable.pattern)
of CompoundPatternKind.`dict`: of CompoundPatternKind.dict:
for nsp in cp.dict.entries.values: for nsp in cp.dict.entries.values:
collectRefImports(loc, imports, nsp.pattern) collectRefImports(loc, imports, nsp.pattern)
@ -750,12 +754,12 @@ proc collectRefImports(loc: Location; imports: PNode; pat: Pattern) =
proc collectRefImports(loc: Location; imports: PNode; def: Definition) = proc collectRefImports(loc: Location; imports: PNode; def: Definition) =
case def.orKind case def.orKind
of DefinitionKind.`or`: of DefinitionKind.or:
collectRefImports(loc, imports, def.or.data.pattern0.pattern) collectRefImports(loc, imports, def.or.data.pattern0.pattern)
collectRefImports(loc, imports, def.or.data.pattern1.pattern) collectRefImports(loc, imports, def.or.data.pattern1.pattern)
for na in def.or.data.patternN: for na in def.or.data.patternN:
collectRefImports(loc, imports, na.pattern) collectRefImports(loc, imports, na.pattern)
of DefinitionKind.`and`: of DefinitionKind.and:
collectRefImports(loc, imports, def.and.data.pattern0.pattern) collectRefImports(loc, imports, def.and.data.pattern0.pattern)
collectRefImports(loc, imports, def.and.data.pattern1.pattern) collectRefImports(loc, imports, def.and.data.pattern1.pattern)
for np in def.and.data.patternN: for np in def.and.data.patternN: