diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2009-01-07 17:03:25 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2009-01-07 17:03:25 +0100 |
commit | 439aa2d04d5528b5aed288f70895515d1da2dc3d (patch) | |
tree | cda2d0bc4d4f2bab189c4a0567cae3c1428c5ed0 /nim/transf.pas | |
parent | 1c8ddca7e08af9075a930edaca6c522d5e6fd8b5 (diff) | |
download | Nim-439aa2d04d5528b5aed288f70895515d1da2dc3d.tar.gz |
version 0.7.4
Diffstat (limited to 'nim/transf.pas')
-rw-r--r-- | nim/transf.pas | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/nim/transf.pas b/nim/transf.pas index d74f2aa83..98d1e89ea 100644 --- a/nim/transf.pas +++ b/nim/transf.pas @@ -22,7 +22,7 @@ interface uses sysutils, nsystem, charsets, strutils, lists, options, ast, astalgo, trees, treetab, - msgs, nos, idents, rnimsyn, types, passes, semfold; + msgs, nos, idents, rnimsyn, types, passes, semfold, magicsys; const genPrefix = ':tmp'; // prefix for generated names @@ -139,7 +139,7 @@ More efficient, but not implementable: function newAsgnStmt(c: PTransf; le, ri: PNode): PNode; begin - result := newNodeI(nkAsgn, ri.info); + result := newNodeI(nkFastAsgn, ri.info); addSon(result, le); addSon(result, ri); end; @@ -224,6 +224,28 @@ begin end end; +function newTupleAccess(tup: PNode; i: int): PNode; +var + lit: PNode; +begin + result := newNodeIT(nkBracketExpr, tup.info, tup.typ.sons[i]); + addSon(result, copyTree(tup)); + lit := newNodeIT(nkIntLit, tup.info, getSysType(tyInt)); + lit.intVal := i; + addSon(result, lit); +end; + +procedure unpackTuple(c: PTransf; n, father: PNode); +var + i: int; +begin + // XXX: BUG: what if `n` is an expression with side-effects? + for i := 0 to sonsLen(n)-1 do begin + addSon(father, newAsgnStmt(c, c.transCon.forStmt.sons[i], + transform(c, newTupleAccess(n, i)))); + end +end; + function transformYield(c: PTransf; n: PNode): PNode; var e: PNode; @@ -239,10 +261,8 @@ begin transform(c, copyTree(e.sons[i])))); end end - else begin - // XXX: tuple unpacking: - internalError(n.info, 'tuple unpacking is not implemented'); - end + else + unpackTuple(c, e, result); end else begin e := transform(c, copyTree(e)); @@ -523,7 +543,7 @@ end; (* # example: proc map(f: proc (x: int): int {.closure}, a: seq[int]): seq[int] = - result = [] + result = @[] for elem in a: add result, f(a) @@ -534,12 +554,12 @@ end; proc map(f: proc(x: int): int, closure: pointer, a: seq[int]): seq[int] = - result = [] + result = @[] for elem in a: add result, f(a, closure) type - PMyClosure = ref record + PMyClosure = ref object y: var int proc myLambda(x: int, closure: pointer) = |