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