shithub: mc

Download patch

ref: 92e25d24edc4cd7c954ecb2b0bd88956566cbe22
parent: b194f947b512b1791e614a5941ebc785cdd28730
author: Ori Bernstein <[email protected]>
date: Sat Jan 4 09:02:01 EST 2014

Fix bug preventing complex exprs on 'for in' loops

    'for in' loops had a bug where we would try to evaluate the
    sequence for the loop every time, instead of doing it once. This
    also caused the side effect of expecting lvalues instead of rvalues
    for the index expressions.

    This change fixes it.

--- a/6/simp.c
+++ b/6/simp.c
@@ -505,7 +505,7 @@
 static void simpiter(Simp *s, Node *n)
 {
     Node *lbody, *lstep, *lcond, *lmatch, *lend;
-    Node *idx, *len, *dcl, *val, *done;
+    Node *idx, *len, *dcl, *seq, *val, *done;
     Node *zero;
 
     lbody = genlbl();
@@ -517,6 +517,7 @@
     zero = mkintlit(n->line, 0);
     zero->expr.type = tyintptr;
 
+    seq = rval(s, n->iterstmt.seq, NULL);
     idx = gentemp(s, n, tyintptr, &dcl);
     declarelocal(s, dcl);
 
@@ -531,11 +532,11 @@
     simp(s, assign(s, idx, addk(idx, 1)));
     /* condition */
     simp(s, lcond);
-    len = seqlen(s, n->iterstmt.seq, tyintptr);
+    len = seqlen(s, seq, tyintptr);
     done = mkexpr(n->line, Olt, idx, len, NULL);
     cjmp(s, done, lmatch, lend);
     simp(s, lmatch);
-    val = load(idxaddr(s, n->iterstmt.seq, idx));
+    val = load(idxaddr(s, seq, idx));
     umatch(s, n->iterstmt.elt, val, val->expr.type, lbody, lstep);
     simp(s, lend);
 }
--- /dev/null
+++ b/test/subrangefor.myr
@@ -1,0 +1,8 @@
+use std
+
+const main = {
+	for i in [1,2,3,4][:2]
+		std.put("%i", i)
+	;;
+	std.put("\n")
+}
--- a/test/tests
+++ b/test/tests
@@ -56,6 +56,7 @@
 B nestfn	E	42
 # B closure	E	55      ## BUGGERED
 B loop		E	45
+B subrangefor	P       12
 B patiter	P	23512
 B condiftrue	E	7
 B condiffalse	E	9