Corrected nkExceptBranch transformation

This commit is contained in:
Yuriy Glukhov 2018-05-08 01:47:19 +03:00
commit c854865d3e

View file

@ -313,33 +313,44 @@ proc newNullifyCurExc(ctx: var Ctx): PNode =
nilnode.typ = curExc.typ nilnode.typ = curExc.typ
result.add(nilnode) result.add(nilnode)
proc newOr(a, b: PNode): PNode =
result = newNode(nkCall)
result.add(newSymNode(getSysMagic("or", mOr)))
result.add(a)
result.add(b)
result.typ = getSysType(tyBool)
proc collectExceptState(ctx: var Ctx, n: PNode): PNode = proc collectExceptState(ctx: var Ctx, n: PNode): PNode =
var ifStmt = newNode(nkIfStmt) var ifStmt = newNode(nkIfStmt)
for c in n: for c in n:
if c.kind == nkExceptBranch: if c.kind == nkExceptBranch:
var ifBranch: PNode var ifBranch: PNode
var branchBody: PNode
if c[0].kind == nkType: if c.len > 1:
assert(c.len == 2) var cond: PNode
ifBranch = newNode(nkElifBranch) for i in 0 .. c.len - 2:
let expression = newNodeI(nkCall, n.info) assert(c[i].kind == nkType)
expression.add(newSymNode(getSysMagic("of", mOf))) let nextCond = newNodeI(nkCall, n.info)
expression.add(callCodegenProc("getCurrentException", emptyNode)) nextCond.add(newSymNode(getSysMagic("of", mOf)))
expression.add(c[0]) nextCond.add(callCodegenProc("getCurrentException", emptyNode))
expression.typ = getSysType(tyBool) nextCond.add(c[i])
ifBranch.add(expression) nextCond.typ = getSysType(tyBool)
branchBody = c[1]
if cond.isNil:
cond = nextCond
else:
cond = newOr(cond, nextCond)
ifBranch = newNode(nkElifBranch)
ifBranch.add(cond)
else: else:
assert(c.len == 1)
if ifStmt.len == 0: if ifStmt.len == 0:
ifStmt = newNode(nkStmtList) ifStmt = newNode(nkStmtList)
ifBranch = newNode(nkStmtList) ifBranch = newNode(nkStmtList)
else: else:
ifBranch = newNode(nkElse) ifBranch = newNode(nkElse)
branchBody = c[0]
ifBranch.add(branchBody) ifBranch.add(c[^1])
ifStmt.add(ifBranch) ifStmt.add(ifBranch)
if ifStmt.len != 0: if ifStmt.len != 0: