diff options
author | Araq <rumpf_a@web.de> | 2011-03-07 00:33:43 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2011-03-07 00:33:43 +0100 |
commit | f9c6cec90c1a67e885b6172a8bccfb014175f60b (patch) | |
tree | dc6e8eef96705490238e58ca845c60fec3aa313d /rod | |
parent | 3005955d20033db12345eacd6eb8ff8cec92507c (diff) | |
download | Nim-f9c6cec90c1a67e885b6172a8bccfb014175f60b.tar.gz |
bugfix: tuple access in macros (issue #17)
Diffstat (limited to 'rod')
-rwxr-xr-x | rod/evals.nim | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/rod/evals.nim b/rod/evals.nim index c4b054437..9feb6875f 100755 --- a/rod/evals.nim +++ b/rod/evals.nim @@ -196,6 +196,23 @@ proc evalTry(c: PEvalContext, n: PNode): PNode = result = evalFinally(c, n, exc) else: result = evalFinally(c, n, emptyNode) +proc getNullValue(typ: PType, info: TLineInfo): PNode +proc getNullValueAux(obj: PNode, result: PNode) = + case obj.kind + of nkRecList: + for i in countup(0, sonsLen(obj) - 1): getNullValueAux(obj.sons[i], result) + of nkRecCase: + getNullValueAux(obj.sons[0], result) + for i in countup(1, sonsLen(obj) - 1): + getNullValueAux(lastSon(obj.sons[i]), result) + of nkSym: + var s = obj.sym + var p = newNodeIT(nkExprColonExpr, result.info, s.typ) + addSon(p, newSymNode(s, result.info)) + addSon(p, getNullValue(s.typ, result.info)) + addSon(result, p) + else: InternalError(result.info, "getNullValueAux") + proc getNullValue(typ: PType, info: TLineInfo): PNode = var t = skipTypes(typ, abstractRange) result = emptyNode @@ -209,7 +226,7 @@ proc getNullValue(typ: PType, info: TLineInfo): PNode = result = newNodeIT(nkNilLit, info, t) of tyObject: result = newNodeIT(nkPar, info, t) - internalError(info, "init to implement") # XXX + getNullValueAux(t.n, result) of tyArray, tyArrayConstr: result = newNodeIT(nkBracket, info, t) for i in countup(0, int(lengthOrd(t)) - 1): @@ -217,7 +234,12 @@ proc getNullValue(typ: PType, info: TLineInfo): PNode = of tyTuple: result = newNodeIT(nkPar, info, t) for i in countup(0, sonsLen(t) - 1): - addSon(result, getNullValue(t.sons[i], info)) + var p = newNodeIT(nkExprColonExpr, info, t.sons[i]) + var field = if t.n != nil: t.n.sons[i].sym else: newSym( + skField, getIdent(":tmp" & $i), t.owner) + addSon(p, newSymNode(field, info)) + addSon(p, getNullValue(t.sons[i], info)) + addSon(result, p) else: InternalError("getNullValue") proc evalVar(c: PEvalContext, n: PNode): PNode = @@ -286,7 +308,11 @@ proc evalArrayAccess(c: PEvalContext, n: PNode, flags: TEvalFlags): PNode = var idx = getOrdValue(result) result = emptyNode case x.kind - of nkBracket, nkPar, nkMetaNode: + of nkPar: + if (idx >= 0) and (idx < sonsLen(x)): result = x.sons[int(idx)].sons[1] + else: stackTrace(c, n, errIndexOutOfBounds) + if not aliasNeeded(result, flags): result = copyTree(result) + of nkBracket, nkMetaNode: if (idx >= 0) and (idx < sonsLen(x)): result = x.sons[int(idx)] else: stackTrace(c, n, errIndexOutOfBounds) if not aliasNeeded(result, flags): result = copyTree(result) @@ -303,18 +329,18 @@ proc evalArrayAccess(c: PEvalContext, n: PNode, flags: TEvalFlags): PNode = else: stackTrace(c, n, errNilAccess) proc evalFieldAccess(c: PEvalContext, n: PNode, flags: TEvalFlags): PNode = - # a real field access; proc calls have already been - # transformed + # a real field access; proc calls have already been transformed # XXX: field checks! result = evalAux(c, n.sons[0], flags) if isSpecial(result): return var x = result if x.kind != nkPar: InternalError(n.info, "evalFieldAccess") var field = n.sons[1].sym - for i in countup(0, sonsLen(n) - 1): - if x.sons[i].kind != nkExprColonExpr: - InternalError(n.info, "evalFieldAccess") - if x.sons[i].sons[0].sym.name.id == field.name.id: + for i in countup(0, sonsLen(x) - 1): + var it = x.sons[i] + if it.kind != nkExprColonExpr: + InternalError(it.info, "evalFieldAccess") + if it.sons[0].sym.name.id == field.name.id: result = x.sons[i].sons[1] if not aliasNeeded(result, flags): result = copyTree(result) return |