proper scoping for 'if'
This commit is contained in:
parent
d64d6a70d4
commit
e70cc64e90
3 changed files with 12 additions and 11 deletions
|
|
@ -454,10 +454,6 @@ proc complexOrSimpleStmt(p: var TParser): PNode
|
||||||
proc simpleExpr(p: var TParser, mode = pmNormal): PNode
|
proc simpleExpr(p: var TParser, mode = pmNormal): PNode
|
||||||
|
|
||||||
proc semiStmtList(p: var TParser, result: 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))
|
result.add(complexOrSimpleStmt(p))
|
||||||
while p.tok.tokType == tkSemicolon:
|
while p.tok.tokType == tkSemicolon:
|
||||||
getTok(p)
|
getTok(p)
|
||||||
|
|
@ -468,7 +464,7 @@ proc semiStmtList(p: var TParser, result: PNode) =
|
||||||
proc parsePar(p: var TParser): PNode =
|
proc parsePar(p: var TParser): PNode =
|
||||||
#| parKeyw = 'discard' | 'include' | 'if' | 'while' | 'case' | 'try'
|
#| parKeyw = 'discard' | 'include' | 'if' | 'while' | 'case' | 'try'
|
||||||
#| | 'finally' | 'except' | 'for' | 'block' | 'const' | 'let'
|
#| | 'finally' | 'except' | 'for' | 'block' | 'const' | 'let'
|
||||||
#| | 'when' | 'var' | 'bind' | 'mixin'
|
#| | 'when' | 'var' | 'mixin'
|
||||||
#| par = '(' optInd (&parKeyw complexOrSimpleStmt ^+ ';'
|
#| par = '(' optInd (&parKeyw complexOrSimpleStmt ^+ ';'
|
||||||
#| | simpleExpr ('=' expr (';' complexOrSimpleStmt ^+ ';' )? )?
|
#| | simpleExpr ('=' expr (';' complexOrSimpleStmt ^+ ';' )? )?
|
||||||
#| | (':' expr)? (',' (exprColonEqExpr comma?)*)? )?
|
#| | (':' expr)? (',' (exprColonEqExpr comma?)*)? )?
|
||||||
|
|
@ -481,8 +477,15 @@ proc parsePar(p: var TParser): PNode =
|
||||||
optInd(p, result)
|
optInd(p, result)
|
||||||
if p.tok.tokType in {tkDiscard, tkInclude, tkIf, tkWhile, tkCase,
|
if p.tok.tokType in {tkDiscard, tkInclude, tkIf, tkWhile, tkCase,
|
||||||
tkTry, tkFinally, tkExcept, tkFor, tkBlock,
|
tkTry, tkFinally, tkExcept, tkFor, tkBlock,
|
||||||
tkConst, tkLet, tkWhen, tkVar, tkBind,
|
tkConst, tkLet, tkWhen, tkVar,
|
||||||
tkMixin, tkSemicolon}:
|
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)
|
semiStmtList(p, result)
|
||||||
elif p.tok.tokType != tkParRi:
|
elif p.tok.tokType != tkParRi:
|
||||||
var a = simpleExpr(p)
|
var a = simpleExpr(p)
|
||||||
|
|
@ -1778,7 +1781,7 @@ proc parseStmt(p: var TParser): PNode =
|
||||||
if p.tok.indent >= 0: parMessage(p, errInvalidIndentation)
|
if p.tok.indent >= 0: parMessage(p, errInvalidIndentation)
|
||||||
result = simpleStmt(p)
|
result = simpleStmt(p)
|
||||||
if result.kind == nkEmpty: parMessage(p, errExprExpected, p.tok)
|
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 =
|
proc parseAll(p: var TParser): PNode =
|
||||||
result = newNodeP(nkStmtList, p)
|
result = newNodeP(nkStmtList, p)
|
||||||
|
|
|
||||||
|
|
@ -1504,8 +1504,8 @@ proc semIf(c: PContext, n: PNode): PNode =
|
||||||
for i in countup(0, sonsLen(n) - 1):
|
for i in countup(0, sonsLen(n) - 1):
|
||||||
var it = n.sons[i]
|
var it = n.sons[i]
|
||||||
if it.len == 2:
|
if it.len == 2:
|
||||||
it.sons[0] = forceBool(c, semExprWithType(c, it.sons[0]))
|
|
||||||
openScope(c.tab)
|
openScope(c.tab)
|
||||||
|
it.sons[0] = forceBool(c, semExprWithType(c, it.sons[0]))
|
||||||
it.sons[1] = semExprBranch(c, it.sons[1])
|
it.sons[1] = semExprBranch(c, it.sons[1])
|
||||||
typ = commonType(typ, it.sons[1].typ)
|
typ = commonType(typ, it.sons[1].typ)
|
||||||
closeScope(c.tab)
|
closeScope(c.tab)
|
||||||
|
|
|
||||||
2
todo.txt
2
todo.txt
|
|
@ -8,8 +8,6 @@ version 0.9.2
|
||||||
- CGEN: ``restrict`` pragma + backend support; computed goto support
|
- CGEN: ``restrict`` pragma + backend support; computed goto support
|
||||||
- document NimMain and check whether it works for threading
|
- document NimMain and check whether it works for threading
|
||||||
- parser/grammar:
|
- 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
|
* document (var x = 12; for i in ... ; x) construct
|
||||||
- make use of commonType relation in expressions
|
- make use of commonType relation in expressions
|
||||||
- further expr/stmt unification:
|
- further expr/stmt unification:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue