proper scoping for 'if'

This commit is contained in:
Araq
2013-04-30 14:40:54 +02:00
parent d64d6a70d4
commit e70cc64e90
3 changed files with 12 additions and 11 deletions

View File

@@ -454,10 +454,6 @@ proc complexOrSimpleStmt(p: var TParser): PNode
proc simpleExpr(p: var TParser, mode = pmNormal): PNode
proc semiStmtList(p: var TParser, result: PNode) =
if p.tok.tokType == tkSemicolon:
# '(;' enforces 'stmt' context:
getTok(p)
optInd(p, result)
result.add(complexOrSimpleStmt(p))
while p.tok.tokType == tkSemicolon:
getTok(p)
@@ -468,7 +464,7 @@ proc semiStmtList(p: var TParser, result: PNode) =
proc parsePar(p: var TParser): PNode =
#| parKeyw = 'discard' | 'include' | 'if' | 'while' | 'case' | 'try'
#| | 'finally' | 'except' | 'for' | 'block' | 'const' | 'let'
#| | 'when' | 'var' | 'bind' | 'mixin'
#| | 'when' | 'var' | 'mixin'
#| par = '(' optInd (&parKeyw complexOrSimpleStmt ^+ ';'
#| | simpleExpr ('=' expr (';' complexOrSimpleStmt ^+ ';' )? )?
#| | (':' expr)? (',' (exprColonEqExpr comma?)*)? )?
@@ -481,8 +477,15 @@ proc parsePar(p: var TParser): PNode =
optInd(p, result)
if p.tok.tokType in {tkDiscard, tkInclude, tkIf, tkWhile, tkCase,
tkTry, tkFinally, tkExcept, tkFor, tkBlock,
tkConst, tkLet, tkWhen, tkVar, tkBind,
tkMixin, tkSemicolon}:
tkConst, tkLet, tkWhen, tkVar,
tkMixin}:
# XXX 'bind' used to be an expression, so we exclude it here;
# tests/reject/tbind2 fails otherwise.
semiStmtList(p, result)
elif p.tok.tokType == tkSemicolon:
# '(;' enforces 'stmt' context:
getTok(p)
optInd(p, result)
semiStmtList(p, result)
elif p.tok.tokType != tkParRi:
var a = simpleExpr(p)
@@ -1778,7 +1781,7 @@ proc parseStmt(p: var TParser): PNode =
if p.tok.indent >= 0: parMessage(p, errInvalidIndentation)
result = simpleStmt(p)
if result.kind == nkEmpty: parMessage(p, errExprExpected, p.tok)
while p.tok.tokType == tkSemicolon: getTok(p)
#while p.tok.tokType == tkSemicolon: getTok(p)
proc parseAll(p: var TParser): PNode =
result = newNodeP(nkStmtList, p)

View File

@@ -1504,8 +1504,8 @@ proc semIf(c: PContext, n: PNode): PNode =
for i in countup(0, sonsLen(n) - 1):
var it = n.sons[i]
if it.len == 2:
it.sons[0] = forceBool(c, semExprWithType(c, it.sons[0]))
openScope(c.tab)
it.sons[0] = forceBool(c, semExprWithType(c, it.sons[0]))
it.sons[1] = semExprBranch(c, it.sons[1])
typ = commonType(typ, it.sons[1].typ)
closeScope(c.tab)

View File

@@ -8,8 +8,6 @@ version 0.9.2
- CGEN: ``restrict`` pragma + backend support; computed goto support
- document NimMain and check whether it works for threading
- parser/grammar:
* check that of branches can only receive even simpler expressions, don't
allow 'of (var x = 23; nkIdent)'
* document (var x = 12; for i in ... ; x) construct
- make use of commonType relation in expressions
- further expr/stmt unification: