much simpler implementation of constant tuple declarations
This commit is contained in:
parent
8e90ed0618
commit
3300c8a500
4 changed files with 33 additions and 55 deletions
|
|
@ -222,8 +222,7 @@ type
|
||||||
nkState, # give a label to a code section (for iterators)
|
nkState, # give a label to a code section (for iterators)
|
||||||
nkBreakState, # special break statement for easier code generation
|
nkBreakState, # special break statement for easier code generation
|
||||||
nkFuncDef, # a func
|
nkFuncDef, # a func
|
||||||
nkTupleConstr, # a tuple constructor
|
nkTupleConstr # a tuple constructor
|
||||||
nkConstTuple # a ``const (a, b) = expr`` construct
|
|
||||||
|
|
||||||
TNodeKinds* = set[TNodeKind]
|
TNodeKinds* = set[TNodeKind]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1764,43 +1764,6 @@ proc parseSection(p: var TParser, kind: TNodeKind,
|
||||||
else:
|
else:
|
||||||
parMessage(p, errIdentifierExpected, p.tok)
|
parMessage(p, errIdentifierExpected, p.tok)
|
||||||
|
|
||||||
proc parseConstTuple(p: var TParser): PNode =
|
|
||||||
result = newNodeP(nkConstTuple, p)
|
|
||||||
getTok(p) # skip '('
|
|
||||||
optInd(p, result)
|
|
||||||
while p.tok.tokType in {tkSymbol, tkAccent}:
|
|
||||||
addSon(result, identWithPragma(p))
|
|
||||||
if p.tok.tokType == tkColon:
|
|
||||||
getTok(p)
|
|
||||||
optInd(p, result)
|
|
||||||
addSon(result, parseTypeDesc(p))
|
|
||||||
if p.tok.tokType != tkComma: break
|
|
||||||
getTok(p)
|
|
||||||
|
|
||||||
addSon(result, p.emptyNode)
|
|
||||||
eat(p, tkParRi)
|
|
||||||
eat(p, tkEquals)
|
|
||||||
optInd(p, result)
|
|
||||||
addSon(result, parseExpr(p))
|
|
||||||
indAndComment(p, result)
|
|
||||||
|
|
||||||
proc parseConstant(p: var TParser): PNode =
|
|
||||||
#| constant = identWithPragma (colon typeDesc)? '=' optInd expr indAndComment
|
|
||||||
if p.tok.tokType == tkParLe: result = parseConstTuple(p)
|
|
||||||
else:
|
|
||||||
result = newNodeP(nkConstDef, p)
|
|
||||||
addSon(result, identWithPragma(p))
|
|
||||||
if p.tok.tokType == tkColon:
|
|
||||||
getTok(p)
|
|
||||||
optInd(p, result)
|
|
||||||
addSon(result, parseTypeDesc(p))
|
|
||||||
else:
|
|
||||||
addSon(result, p.emptyNode)
|
|
||||||
eat(p, tkEquals)
|
|
||||||
optInd(p, result)
|
|
||||||
addSon(result, parseExpr(p))
|
|
||||||
indAndComment(p, result)
|
|
||||||
|
|
||||||
proc parseEnum(p: var TParser): PNode =
|
proc parseEnum(p: var TParser): PNode =
|
||||||
#| enum = 'enum' optInd (symbol optInd ('=' optInd expr COMMENT?)? comma?)+
|
#| enum = 'enum' optInd (symbol optInd ('=' optInd expr COMMENT?)? comma?)+
|
||||||
result = newNodeP(nkEnumTy, p)
|
result = newNodeP(nkEnumTy, p)
|
||||||
|
|
@ -2057,6 +2020,23 @@ proc parseVariable(p: var TParser): PNode =
|
||||||
result[^1] = postExprBlocks(p, result[^1])
|
result[^1] = postExprBlocks(p, result[^1])
|
||||||
indAndComment(p, result)
|
indAndComment(p, result)
|
||||||
|
|
||||||
|
proc parseConstant(p: var TParser): PNode =
|
||||||
|
#| constant = (parseVarTuple / identWithPragma) (colon typeDesc)? '=' optInd expr indAndComment
|
||||||
|
if p.tok.tokType == tkParLe: result = parseVarTuple(p)
|
||||||
|
else:
|
||||||
|
result = newNodeP(nkConstDef, p)
|
||||||
|
addSon(result, identWithPragma(p))
|
||||||
|
if p.tok.tokType == tkColon:
|
||||||
|
getTok(p)
|
||||||
|
optInd(p, result)
|
||||||
|
addSon(result, parseTypeDesc(p))
|
||||||
|
else:
|
||||||
|
addSon(result, p.emptyNode)
|
||||||
|
eat(p, tkEquals)
|
||||||
|
optInd(p, result)
|
||||||
|
addSon(result, parseExpr(p))
|
||||||
|
indAndComment(p, result)
|
||||||
|
|
||||||
proc parseBind(p: var TParser, k: TNodeKind): PNode =
|
proc parseBind(p: var TParser, k: TNodeKind): PNode =
|
||||||
#| bindStmt = 'bind' optInd qualifiedIdent ^+ comma
|
#| bindStmt = 'bind' optInd qualifiedIdent ^+ comma
|
||||||
#| mixinStmt = 'mixin' optInd qualifiedIdent ^+ comma
|
#| mixinStmt = 'mixin' optInd qualifiedIdent ^+ comma
|
||||||
|
|
|
||||||
|
|
@ -542,7 +542,7 @@ proc semConst(c: PContext, n: PNode): PNode =
|
||||||
var a = n.sons[i]
|
var a = n.sons[i]
|
||||||
if c.config.cmd == cmdIdeTools: suggestStmt(c, a)
|
if c.config.cmd == cmdIdeTools: suggestStmt(c, a)
|
||||||
if a.kind == nkCommentStmt: continue
|
if a.kind == nkCommentStmt: continue
|
||||||
if a.kind notin {nkConstDef, nkConstTuple}: illFormedAst(a, c.config)
|
if a.kind notin {nkConstDef, nkVarTuple}: illFormedAst(a, c.config)
|
||||||
checkMinSonsLen(a, 3, c.config)
|
checkMinSonsLen(a, 3, c.config)
|
||||||
var length = sonsLen(a)
|
var length = sonsLen(a)
|
||||||
|
|
||||||
|
|
@ -567,16 +567,16 @@ proc semConst(c: PContext, n: PNode): PNode =
|
||||||
continue
|
continue
|
||||||
|
|
||||||
var b: PNode
|
var b: PNode
|
||||||
if a.kind == nkConstTuple:
|
if a.kind == nkVarTuple:
|
||||||
if typ.kind != tyTuple:
|
if typ.kind != tyTuple:
|
||||||
localError(c.config, a.info, errXExpected, "tuple")
|
localError(c.config, a.info, errXExpected, "tuple")
|
||||||
elif int(length/2) != sonsLen(typ):
|
elif int(length/2) != sonsLen(typ):
|
||||||
localError(c.config, a.info, errWrongNumberOfVariables)
|
localError(c.config, a.info, errWrongNumberOfVariables)
|
||||||
b = newNodeI(nkConstTuple, a.info)
|
b = newNodeI(nkVarTuple, a.info)
|
||||||
newSons(b, length)
|
newSons(b, length)
|
||||||
b.sons[length-2] = a.sons[length-2]
|
b.sons[length-2] = a.sons[length-2]
|
||||||
b.sons[length-1] = def
|
b.sons[length-1] = def
|
||||||
|
|
||||||
for j in countup(0, length-3):
|
for j in countup(0, length-3):
|
||||||
var v = semIdentDef(c, a.sons[j], skConst)
|
var v = semIdentDef(c, a.sons[j], skConst)
|
||||||
if sfGenSym notin v.flags: addInterfaceDecl(c, v)
|
if sfGenSym notin v.flags: addInterfaceDecl(c, v)
|
||||||
|
|
@ -584,14 +584,14 @@ proc semConst(c: PContext, n: PNode): PNode =
|
||||||
styleCheckDef(c.config, v)
|
styleCheckDef(c.config, v)
|
||||||
onDef(a[j].info, v)
|
onDef(a[j].info, v)
|
||||||
|
|
||||||
if a.kind != nkConstTuple:
|
if a.kind != nkVarTuple:
|
||||||
setVarType(c, v, typ)
|
setVarType(c, v, typ)
|
||||||
v.ast = def # no need to copy
|
v.ast = def # no need to copy
|
||||||
b = newNodeI(nkConstDef, a.info)
|
b = newNodeI(nkConstDef, a.info)
|
||||||
if importantComments(c.config): b.comment = a.comment
|
if importantComments(c.config): b.comment = a.comment
|
||||||
addSon(b, newSymNode(v))
|
addSon(b, newSymNode(v))
|
||||||
addSon(b, a.sons[1])
|
addSon(b, a.sons[1])
|
||||||
addSon(b, copyTree(def))
|
addSon(b, copyTree(def))
|
||||||
else:
|
else:
|
||||||
setVarType(c, v, typ.sons[j])
|
setVarType(c, v, typ.sons[j])
|
||||||
v.ast = def[j]
|
v.ast = def[j]
|
||||||
|
|
|
||||||
|
|
@ -81,8 +81,7 @@ type
|
||||||
nnkState,
|
nnkState,
|
||||||
nnkBreakState,
|
nnkBreakState,
|
||||||
nnkFuncDef,
|
nnkFuncDef,
|
||||||
nnkTupleConstr,
|
nnkTupleConstr
|
||||||
nnkConstTuple
|
|
||||||
|
|
||||||
NimNodeKinds* = set[NimNodeKind]
|
NimNodeKinds* = set[NimNodeKind]
|
||||||
NimTypeKind* = enum # some types are no longer used, see ast.nim
|
NimTypeKind* = enum # some types are no longer used, see ast.nim
|
||||||
|
|
@ -278,9 +277,9 @@ when defined(nimHasSymOwnerInMacro):
|
||||||
when defined(nimHasInstantiationOfInMacro):
|
when defined(nimHasInstantiationOfInMacro):
|
||||||
proc isInstantiationOf*(instanceProcSym, genProcSym: NimNode): bool {.magic: "SymIsInstantiationOf", noSideEffect.}
|
proc isInstantiationOf*(instanceProcSym, genProcSym: NimNode): bool {.magic: "SymIsInstantiationOf", noSideEffect.}
|
||||||
## check if proc symbol is instance of the generic proc symbol
|
## check if proc symbol is instance of the generic proc symbol
|
||||||
## useful to check proc symbols against generic symbols
|
## useful to check proc symbols against generic symbols
|
||||||
## returned by `bindSym`
|
## returned by `bindSym`
|
||||||
|
|
||||||
proc getType*(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.}
|
proc getType*(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.}
|
||||||
## with 'getType' you can access the node's `type`:idx:. A Nim type is
|
## with 'getType' you can access the node's `type`:idx:. A Nim type is
|
||||||
## mapped to a Nim AST too, so it's slightly confusing but it means the same
|
## mapped to a Nim AST too, so it's slightly confusing but it means the same
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue