preserves_schema_nim: use Table for And types
This commit is contained in:
parent
43498a4b94
commit
c01e587e5b
|
@ -192,31 +192,26 @@ proc attrs(loc: Location; pat: Pattern; seen: RefSet): Attributes =
|
||||||
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; def: DefinitionOr|DefinitionAnd; seen: RefSet): Attributes =
|
||||||
result = attrs(loc, orDef.field0.pattern0, seen) + attrs(loc, orDef.field0.pattern1, seen)
|
result = attrs(loc, def.field0.pattern0, seen) + attrs(loc, def.field0.pattern1, seen)
|
||||||
for p in orDef.field0.patternN:
|
for p in def.field0.patternN:
|
||||||
result = result + attrs(loc, p, seen)
|
result = result + attrs(loc, p, seen)
|
||||||
|
|
||||||
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, seen)
|
||||||
result =
|
|
||||||
attrs(loc, def.and.field0.pattern0, seen) +
|
|
||||||
attrs(loc, def.and.field0.pattern1, seen)
|
|
||||||
for p in def.and.field0.patternN:
|
|
||||||
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|DefinitionAnd|Pattern|CompoundPattern|SimplePattern): Attributes =
|
||||||
var seen: RefSet
|
var seen: RefSet
|
||||||
attrs(loc, p, seen)
|
attrs(loc, p, seen)
|
||||||
|
|
||||||
proc isEmbedded(loc: Location; p: Definition|DefinitionOr|Pattern|CompoundPattern): bool =
|
proc isEmbedded(loc: Location; p: Definition|DefinitionOr|DefinitionAnd|Pattern|CompoundPattern|SimplePattern): bool =
|
||||||
embedded in attrs(loc, p)
|
embedded in attrs(loc, p)
|
||||||
|
|
||||||
proc isRecursive(loc: Location; p: Definition|DefinitionOr|Pattern|CompoundPattern): bool =
|
proc isRecursive(loc: Location; p: Definition|DefinitionOr|DefinitionAnd|Pattern|CompoundPattern): bool =
|
||||||
recursive in attrs(loc, p)
|
recursive in attrs(loc, p)
|
||||||
|
|
||||||
proc isLiteral(loc: Location; def: Definition): bool {.gcsafe.}
|
proc isLiteral(loc: Location; def: Definition): bool {.gcsafe.}
|
||||||
|
@ -288,6 +283,35 @@ proc isSymbolEnum(loc: Location; sp: SimplePattern): bool =
|
||||||
result = isSymbolEnum(loc, def)
|
result = isSymbolEnum(loc, def)
|
||||||
else: discard
|
else: discard
|
||||||
|
|
||||||
|
proc isDictionary(loc: Location; def: Definition): bool {.gcsafe.}
|
||||||
|
|
||||||
|
proc isDictionary(loc: Location; pat: Pattern): bool =
|
||||||
|
case pat.orKind
|
||||||
|
of PatternKind.SimplePattern:
|
||||||
|
case pat.simplePattern.orKind
|
||||||
|
of SimplePatternKind.Ref:
|
||||||
|
var (loc, def) = deref(loc, pat.simplePattern.ref)
|
||||||
|
result = isDictionary(loc, def)
|
||||||
|
of SimplePatternKind.dictof:
|
||||||
|
result = true
|
||||||
|
else: discard
|
||||||
|
of PatternKind.CompoundPattern:
|
||||||
|
case pat.compoundpattern.orKind
|
||||||
|
of CompoundPatternKind.dict:
|
||||||
|
result = true
|
||||||
|
else: discard
|
||||||
|
|
||||||
|
proc isDictionary(loc: Location; defAnd: DefinitionAnd): bool =
|
||||||
|
result = isDictionary(loc, defAnd.field0.pattern0.pattern)
|
||||||
|
|
||||||
|
proc isDictionary(loc: Location; def: Definition): bool =
|
||||||
|
case def.orKind
|
||||||
|
of DefinitionKind.Pattern:
|
||||||
|
result = isDictionary(loc, def.pattern)
|
||||||
|
of DefinitionKind.and:
|
||||||
|
result = isDictionary(loc, def.and)
|
||||||
|
else: discard
|
||||||
|
|
||||||
proc isAny(loc: Location; def: Definition): bool =
|
proc isAny(loc: Location; def: Definition): bool =
|
||||||
case def.orKind
|
case def.orKind
|
||||||
of DefinitionKind.Pattern:
|
of DefinitionKind.Pattern:
|
||||||
|
@ -305,9 +329,6 @@ proc isAny(loc: Location; def: Definition): bool =
|
||||||
of CompoundPatternKind.rec:
|
of CompoundPatternKind.rec:
|
||||||
result = not isLiteral(loc, def.pattern.compoundpattern.rec.label)
|
result = not isLiteral(loc, def.pattern.compoundpattern.rec.label)
|
||||||
else: discard
|
else: discard
|
||||||
of DefinitionKind.and:
|
|
||||||
# not actually "any" but it will be a Preserve[E] type
|
|
||||||
result = true
|
|
||||||
else: discard
|
else: discard
|
||||||
|
|
||||||
proc typeIdent(atom: AtomKind): PNode =
|
proc typeIdent(atom: AtomKind): PNode =
|
||||||
|
@ -482,10 +503,13 @@ proc typeDef(loc: Location; name: string; def: Definition; ty: PNode): PNode =
|
||||||
newEmpty(),
|
newEmpty(),
|
||||||
ty)
|
ty)
|
||||||
of DefinitionKind.and:
|
of DefinitionKind.and:
|
||||||
|
let pragma = newEmpty()
|
||||||
nkTypeDef.newTree(
|
nkTypeDef.newTree(
|
||||||
name.ident.toExport,
|
nkPragmaExpr.newTree(
|
||||||
|
name.ident.accQuote.toExport,
|
||||||
|
pragma),
|
||||||
newEmpty(),
|
newEmpty(),
|
||||||
ident"Value")
|
ty)
|
||||||
of DefinitionKind.Pattern:
|
of DefinitionKind.Pattern:
|
||||||
typeDef(loc, name, def.pattern, ty)
|
typeDef(loc, name, def.pattern, ty)
|
||||||
|
|
||||||
|
@ -577,7 +601,9 @@ proc addFields(recList: PNode; loc: Location; known: var TypeTable; parentName:
|
||||||
nkPragma.newTree(ident"preservesTupleTail")),
|
nkPragma.newTree(ident"preservesTupleTail")),
|
||||||
variableType.node,
|
variableType.node,
|
||||||
variableType.isEmbedded)
|
variableType.isEmbedded)
|
||||||
else: raiseAssert "not adding fields for " #& $cp
|
of CompoundPatternKind.dict:
|
||||||
|
for nameVal, nsp in cp.dict.entries:
|
||||||
|
recList.addField(loc, known, $nameVal, nsp)
|
||||||
reclist
|
reclist
|
||||||
|
|
||||||
proc addFields(recList: PNode; loc: Location; known: var TypeTable; name: string; pat: SimplePattern): PNode {.discardable.} =
|
proc addFields(recList: PNode; loc: Location; known: var TypeTable; name: string; pat: SimplePattern): PNode {.discardable.} =
|
||||||
|
@ -705,6 +731,12 @@ proc nimTypeOf(loc: Location; known: var TypeTable; name: string; orDef: Definit
|
||||||
if result.node.kind == nkObjectTy and (recursive in attrs(loc, orDef)):
|
if result.node.kind == nkObjectTy and (recursive in attrs(loc, orDef)):
|
||||||
result.node = nkRefTy.newTree(result.node)
|
result.node = nkRefTy.newTree(result.node)
|
||||||
|
|
||||||
|
proc nimTypeOf(loc: Location; known: var TypeTable; name: string; def: DefinitionAnd): TypeSpec =
|
||||||
|
if isDictionary(loc, def):
|
||||||
|
result.node = nkBracketExpr.newTree(ident"Table", ident"Symbol", ident"Value")
|
||||||
|
else:
|
||||||
|
result.node = ident"Value"
|
||||||
|
|
||||||
proc nimTypeOf(loc: Location; known: var TypeTable; name: string; def: Definition): TypeSpec =
|
proc nimTypeOf(loc: Location; known: var TypeTable; name: string; def: Definition): TypeSpec =
|
||||||
case def.orKind
|
case def.orKind
|
||||||
of DefinitionKind.or:
|
of DefinitionKind.or:
|
||||||
|
@ -762,6 +794,8 @@ proc collectRefImports(imports: var StringSet; loc: Location; def: Definition) =
|
||||||
for na in def.or.field0.patternN:
|
for na in def.or.field0.patternN:
|
||||||
collectRefImports(imports, loc, na.pattern)
|
collectRefImports(imports, loc, na.pattern)
|
||||||
of DefinitionKind.and:
|
of DefinitionKind.and:
|
||||||
|
if isDictionary(loc, def):
|
||||||
|
incl(imports, "std/tables")
|
||||||
collectRefImports(imports, loc, def.and.field0.pattern0.pattern)
|
collectRefImports(imports, loc, def.and.field0.pattern0.pattern)
|
||||||
collectRefImports(imports, loc, def.and.field0.pattern1.pattern)
|
collectRefImports(imports, loc, def.and.field0.pattern1.pattern)
|
||||||
for np in def.and.field0.patternN:
|
for np in def.and.field0.patternN:
|
||||||
|
|
Loading…
Reference in New Issue