'inject' for 'for' loop variables
This commit is contained in:
parent
61b3048323
commit
9b9a180947
5 changed files with 41 additions and 30 deletions
|
|
@ -817,7 +817,7 @@ proc parseTuple(p: var TParser, indentAllowed = false): PNode =
|
||||||
if not sameInd(p): break
|
if not sameInd(p): break
|
||||||
|
|
||||||
proc parseParamList(p: var TParser, retColon = true): PNode =
|
proc parseParamList(p: var TParser, retColon = true): PNode =
|
||||||
#| paramList = '(' identColonEquals ^* (comma/semicolon) ')'
|
#| paramList = '(' declColonEquals ^* (comma/semicolon) ')'
|
||||||
#| paramListArrow = paramList? ('->' optInd typeDesc)?
|
#| paramListArrow = paramList? ('->' optInd typeDesc)?
|
||||||
#| paramListColon = paramList? (':' optInd typeDesc)?
|
#| paramListColon = paramList? (':' optInd typeDesc)?
|
||||||
var a: PNode
|
var a: PNode
|
||||||
|
|
@ -829,7 +829,7 @@ proc parseParamList(p: var TParser, retColon = true): PNode =
|
||||||
while true:
|
while true:
|
||||||
case p.tok.tokType
|
case p.tok.tokType
|
||||||
of tkSymbol, tkAccent:
|
of tkSymbol, tkAccent:
|
||||||
a = parseIdentColonEquals(p, {withBothOptional})
|
a = parseIdentColonEquals(p, {withBothOptional, withPragma})
|
||||||
of tkParRi:
|
of tkParRi:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
|
@ -1278,15 +1278,15 @@ proc parseExceptBlock(p: var TParser, kind: TNodeKind): PNode =
|
||||||
addSon(result, parseStmt(p))
|
addSon(result, parseStmt(p))
|
||||||
|
|
||||||
proc parseFor(p: var TParser): PNode =
|
proc parseFor(p: var TParser): PNode =
|
||||||
#| forStmt = 'for' symbol (comma symbol)* 'in' expr colcom stmt
|
#| forStmt = 'for' (identWithPragma ^+ comma) 'in' expr colcom stmt
|
||||||
result = newNodeP(nkForStmt, p)
|
result = newNodeP(nkForStmt, p)
|
||||||
getTokNoInd(p)
|
getTokNoInd(p)
|
||||||
var a = parseSymbol(p)
|
var a = identWithPragma(p)
|
||||||
addSon(result, a)
|
addSon(result, a)
|
||||||
while p.tok.tokType == tkComma:
|
while p.tok.tokType == tkComma:
|
||||||
getTok(p)
|
getTok(p)
|
||||||
optInd(p, a)
|
optInd(p, a)
|
||||||
a = parseSymbol(p)
|
a = identWithPragma(p)
|
||||||
addSon(result, a)
|
addSon(result, a)
|
||||||
eat(p, tkIn)
|
eat(p, tkIn)
|
||||||
addSon(result, parseExpr(p))
|
addSon(result, parseExpr(p))
|
||||||
|
|
|
||||||
|
|
@ -570,6 +570,10 @@ proc addForVarDecl(c: PContext, v: PSym) =
|
||||||
Message(v.info, warnShadowIdent, v.name.s)
|
Message(v.info, warnShadowIdent, v.name.s)
|
||||||
addDecl(c, v)
|
addDecl(c, v)
|
||||||
|
|
||||||
|
proc symForVar(c: PContext, n: PNode): PSym =
|
||||||
|
let m = if n.kind == nkPragmaExpr: n.sons[0] else: n
|
||||||
|
result = newSymG(skForVar, m, c)
|
||||||
|
|
||||||
proc semForVars(c: PContext, n: PNode): PNode =
|
proc semForVars(c: PContext, n: PNode): PNode =
|
||||||
result = n
|
result = n
|
||||||
var length = sonsLen(n)
|
var length = sonsLen(n)
|
||||||
|
|
@ -578,7 +582,7 @@ proc semForVars(c: PContext, n: PNode): PNode =
|
||||||
# and thus no tuple unpacking:
|
# and thus no tuple unpacking:
|
||||||
if iter.kind != tyTuple or length == 3:
|
if iter.kind != tyTuple or length == 3:
|
||||||
if length == 3:
|
if length == 3:
|
||||||
var v = newSymG(skForVar, n.sons[0], c)
|
var v = symForVar(c, n.sons[0])
|
||||||
if getCurrOwner().kind == skModule: incl(v.flags, sfGlobal)
|
if getCurrOwner().kind == skModule: incl(v.flags, sfGlobal)
|
||||||
# BUGFIX: don't use `iter` here as that would strip away
|
# BUGFIX: don't use `iter` here as that would strip away
|
||||||
# the ``tyGenericInst``! See ``tests/compile/tgeneric.nim``
|
# the ``tyGenericInst``! See ``tests/compile/tgeneric.nim``
|
||||||
|
|
@ -592,7 +596,7 @@ proc semForVars(c: PContext, n: PNode): PNode =
|
||||||
LocalError(n.info, errWrongNumberOfVariables)
|
LocalError(n.info, errWrongNumberOfVariables)
|
||||||
else:
|
else:
|
||||||
for i in countup(0, length - 3):
|
for i in countup(0, length - 3):
|
||||||
var v = newSymG(skForVar, n.sons[i], c)
|
var v = symForVar(c, n.sons[i])
|
||||||
if getCurrOwner().kind == skModule: incl(v.flags, sfGlobal)
|
if getCurrOwner().kind == skModule: incl(v.flags, sfGlobal)
|
||||||
v.typ = iter.sons[i]
|
v.typ = iter.sons[i]
|
||||||
n.sons[i] = newSymNode(v)
|
n.sons[i] = newSymNode(v)
|
||||||
|
|
|
||||||
|
|
@ -176,6 +176,18 @@ proc semRoutineInTemplBody(c: var TemplCtx, n: PNode, k: TSymKind): PNode =
|
||||||
n.sons[i] = semTemplBody(c, n.sons[i])
|
n.sons[i] = semTemplBody(c, n.sons[i])
|
||||||
closeScope(c)
|
closeScope(c)
|
||||||
|
|
||||||
|
proc semTemplSomeDecl(c: var TemplCtx, n: PNode, symKind: TSymKind) =
|
||||||
|
for i in countup(ord(symkind == skConditional), sonsLen(n) - 1):
|
||||||
|
var a = n.sons[i]
|
||||||
|
if a.kind == nkCommentStmt: continue
|
||||||
|
if (a.kind != nkIdentDefs) and (a.kind != nkVarTuple): IllFormedAst(a)
|
||||||
|
checkMinSonsLen(a, 3)
|
||||||
|
var L = sonsLen(a)
|
||||||
|
a.sons[L-2] = semTemplBody(c, a.sons[L-2])
|
||||||
|
a.sons[L-1] = semTemplBody(c, a.sons[L-1])
|
||||||
|
for j in countup(0, L-3):
|
||||||
|
addLocalDecl(c, a.sons[j], symKind)
|
||||||
|
|
||||||
proc semPattern(c: PContext, n: PNode): PNode
|
proc semPattern(c: PContext, n: PNode): PNode
|
||||||
proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
|
proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
|
||||||
result = n
|
result = n
|
||||||
|
|
@ -203,7 +215,15 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
|
||||||
nil
|
nil
|
||||||
of nkIfStmt:
|
of nkIfStmt:
|
||||||
for i in countup(0, sonsLen(n)-1):
|
for i in countup(0, sonsLen(n)-1):
|
||||||
n.sons[i] = semTemplBodyScope(c, n.sons[i])
|
var it = n.sons[i]
|
||||||
|
if it.len == 2:
|
||||||
|
when newScopeForIf: openScope(c)
|
||||||
|
it.sons[0] = semTemplBody(c, it.sons[0])
|
||||||
|
when not newScopeForIf: openScope(c)
|
||||||
|
it.sons[1] = semTemplBody(c, it.sons[1])
|
||||||
|
closeScope(c)
|
||||||
|
else:
|
||||||
|
n.sons[i] = semTemplBodyScope(c, it)
|
||||||
of nkWhileStmt:
|
of nkWhileStmt:
|
||||||
openScope(c)
|
openScope(c)
|
||||||
for i in countup(0, sonsLen(n)-1):
|
for i in countup(0, sonsLen(n)-1):
|
||||||
|
|
@ -248,18 +268,8 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
|
||||||
for j in countup(0, L-2):
|
for j in countup(0, L-2):
|
||||||
a.sons[j] = semTemplBody(c, a.sons[j])
|
a.sons[j] = semTemplBody(c, a.sons[j])
|
||||||
a.sons[L-1] = semTemplBodyScope(c, a.sons[L-1])
|
a.sons[L-1] = semTemplBodyScope(c, a.sons[L-1])
|
||||||
of nkVarSection, nkLetSection:
|
of nkVarSection: semTemplSomeDecl(c, n, skVar)
|
||||||
let symKind = if n.kind == nkLetSection: skLet else: skVar
|
of nkLetSection: semTemplSomeDecl(c, n, skLet)
|
||||||
for i in countup(0, sonsLen(n) - 1):
|
|
||||||
var a = n.sons[i]
|
|
||||||
if a.kind == nkCommentStmt: continue
|
|
||||||
if (a.kind != nkIdentDefs) and (a.kind != nkVarTuple): IllFormedAst(a)
|
|
||||||
checkMinSonsLen(a, 3)
|
|
||||||
var L = sonsLen(a)
|
|
||||||
a.sons[L-2] = semTemplBody(c, a.sons[L-2])
|
|
||||||
a.sons[L-1] = semTemplBody(c, a.sons[L-1])
|
|
||||||
for j in countup(0, L-3):
|
|
||||||
addLocalDecl(c, a.sons[j], symKind)
|
|
||||||
of nkConstSection:
|
of nkConstSection:
|
||||||
for i in countup(0, sonsLen(n) - 1):
|
for i in countup(0, sonsLen(n) - 1):
|
||||||
var a = n.sons[i]
|
var a = n.sons[i]
|
||||||
|
|
@ -424,8 +434,6 @@ proc semTemplateDef(c: PContext, n: PNode): PNode =
|
||||||
addInterfaceOverloadableSymAt(c, s, curScope)
|
addInterfaceOverloadableSymAt(c, s, curScope)
|
||||||
else:
|
else:
|
||||||
SymTabReplace(c.tab.stack[curScope], proto, s)
|
SymTabReplace(c.tab.stack[curScope], proto, s)
|
||||||
# XXX this seems wrong: We need to check for proto before and overwrite
|
|
||||||
# proto.ast ...
|
|
||||||
if n.sons[patternPos].kind != nkEmpty:
|
if n.sons[patternPos].kind != nkEmpty:
|
||||||
c.patterns.add(s)
|
c.patterns.add(s)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ inlTupleDecl = 'tuple'
|
||||||
[' optInd (identColonEquals (comma/semicolon)?)* optPar ']'
|
[' optInd (identColonEquals (comma/semicolon)?)* optPar ']'
|
||||||
extTupleDecl = 'tuple'
|
extTupleDecl = 'tuple'
|
||||||
COMMENT? (IND{>} identColonEquals (IND{=} identColonEquals)*)?
|
COMMENT? (IND{>} identColonEquals (IND{=} identColonEquals)*)?
|
||||||
paramList = '(' identColonEquals ^* (comma/semicolon) ')'
|
paramList = '(' declColonEquals ^* (comma/semicolon) ')'
|
||||||
paramListArrow = paramList? ('->' optInd typeDesc)?
|
paramListArrow = paramList? ('->' optInd typeDesc)?
|
||||||
paramListColon = paramList? (':' optInd typeDesc)?
|
paramListColon = paramList? (':' optInd typeDesc)?
|
||||||
doBlock = 'do' paramListArrow pragmas? colcom stmt
|
doBlock = 'do' paramListArrow pragmas? colcom stmt
|
||||||
|
|
@ -132,7 +132,7 @@ tryStmt = 'try' colcom stmt &(IND{=}? 'except'|'finally')
|
||||||
(IND{=}? 'except' exprList colcom stmt)*
|
(IND{=}? 'except' exprList colcom stmt)*
|
||||||
(IND{=}? 'finally' colcom stmt)?
|
(IND{=}? 'finally' colcom stmt)?
|
||||||
exceptBlock = 'except' colcom stmt
|
exceptBlock = 'except' colcom stmt
|
||||||
forStmt = 'for' symbol (comma symbol)* 'in' expr colcom stmt
|
forStmt = 'for' (identWithPragma ^+ comma) 'in' expr colcom stmt
|
||||||
blockStmt = 'block' symbol? colcom stmt
|
blockStmt = 'block' symbol? colcom stmt
|
||||||
staticStmt = 'static' colcom stmt
|
staticStmt = 'static' colcom stmt
|
||||||
asmStmt = 'asm' pragma? (STR_LIT | RSTR_LIT | TRIPLE_STR_LIT)
|
asmStmt = 'asm' pragma? (STR_LIT | RSTR_LIT | TRIPLE_STR_LIT)
|
||||||
|
|
|
||||||
|
|
@ -132,8 +132,7 @@ template filterIt*(seq1, pred: expr): expr {.immediate.} =
|
||||||
## assert acceptable == @[-2.0, 24.5, 44.31]
|
## assert acceptable == @[-2.0, 24.5, 44.31]
|
||||||
## assert notAcceptable == @[-272.15, 99.9, -113.44]
|
## assert notAcceptable == @[-272.15, 99.9, -113.44]
|
||||||
var result {.gensym.}: type(seq1) = @[]
|
var result {.gensym.}: type(seq1) = @[]
|
||||||
for internalit in items(seq1):
|
for it {.inject.} in items(seq1):
|
||||||
let it {.inject.} = internalit
|
|
||||||
if pred: result.add(it)
|
if pred: result.add(it)
|
||||||
result
|
result
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue