shithub: mc

Download patch

ref: 26e54d3c7cbaab5efa5e63a32b87b49328fd5f6b
parent: 7f04fcc98b500a390d5b89ca766982afa6feb168
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Oct 21 19:11:41 EDT 2015

Pass the basic tests. we still fail complex ones.

--- a/6/simp.c
+++ b/6/simp.c
@@ -510,6 +510,7 @@
 {
     Ucon *uc;
 
+    n = rval(s, n, NULL);
     if (exprop(n) != Oucon)
         return load(addr(s, n, mktype(n->loc, Tyuint)));
 
@@ -860,6 +861,10 @@
         case Oucon:     r = rval(s, n, NULL); break;
         case Oarr:      r = rval(s, n, NULL); break;
         case Ogap:      r = temp(s, n); break;
+
+        /* not actually expressible as lvalues in syntax, but we generate them */
+        case Oudata:    r = rval(s, n, NULL);   break;
+        case Outag:     r = rval(s, n, NULL);   break;
         default:
             fatal(n, "%s cannot be an lvalue", opstr[exprop(n)]);
             break;
@@ -1713,7 +1718,8 @@
         case Otupget:
             assert(exprop(args[1]) == Olit);
             i = args[1]->expr.args[0]->lit.intval;
-            r = tupget(s, args[0], i, dst);
+            t = rval(s, args[0], NULL);
+            r = tupget(s, t, i, dst);
             break;
         case Obad:
             die("bad operator");
@@ -1762,7 +1768,9 @@
         case Nifstmt:    simpif(s, n, NULL);    break;
         case Nloopstmt:  simploop(s, n);        break;
         case Niterstmt:  simpiter(s, n);        break;
-        case Nmatchstmt: simpmatch(s, n);       break;
+        case Nmatchstmt: /*simpmatch(s, n);       break;*/
+            simp(s, gensimpmatch(n));
+            break;
         case Nexpr:
             if (islbl(n))
                 append(s, n);
--- a/lib/std/test/ipparse.myr
+++ b/lib/std/test/ipparse.myr
@@ -60,5 +60,6 @@
 		;;
 	| `std.None:	-> [][:]
 	;;
+	-> [][:]
 }
 
--- a/mi/match.c
+++ b/mi/match.c
@@ -141,6 +141,17 @@
     return elt;
 }
 
+static Node *deadblock()
+{
+    Node *blk, *dead;
+
+    blk = mkblock(Zloc, NULL);
+    dead = mkexpr(Zloc, Odead, NULL);
+    dead->expr.type = mktype(Zloc, Tyvoid);
+    lappend(&blk->block.stmts, &blk->block.nstmts, dead);
+    return blk;
+}
+
 static Dtree *addwild(Dtree *t, Node *pat, Node *val, Node ***cap, size_t *ncap)
 {
     Node *asn;
@@ -203,23 +214,55 @@
     return sub;
 }
 
+static Dtree *addstr(Dtree *t, Node *pat, Node *val, Node ***cap, size_t *ncap)
+{
+    Node *p, *v, *lit;
+    size_t i, n;
+    Type *ty;
+    char *s;
+
+    lit = pat->expr.args[0];
+    n = lit->lit.strval.len;
+    s = lit->lit.strval.buf;
+
+    ty = mktype(pat->loc, Tyuint64);
+    p = mkintlit(lit->loc, n);
+    v = structmemb(val, mkname(pat->loc, "len"), ty);
+    p->expr.type = ty;
+
+    t = addpat(t, p, v, cap, ncap);
+
+    ty = mktype(pat->loc, Tybyte);
+    for (i = 0; i < n; i++) {
+        p = mkintlit(lit->loc, s[i]);
+        p->expr.type = ty;
+        v = arrayelt(val, i);
+        t = addpat(t, p, v, cap, ncap);
+    }
+    return t;
+}
+
 static Dtree *addlit(Dtree *t, Node *pat, Node *val, Node ***cap, size_t *ncap)
 {
     Dtree *sub;
     size_t i;
 
-    if (t->any)
-        return t->any;
-    for (i = 0; i < t->nval; i++) {
-        if (liteq(t->val[i]->expr.args[0], pat->expr.args[0]))
-            return t->sub[i];
-    }
+    if (pat->expr.args[0]->lit.littype == Lstr) {
+        sub = addstr(t, pat, val, cap, ncap);
+    } else {
+        if (t->any)
+            return t->any;
+        for (i = 0; i < t->nval; i++) {
+            if (liteq(t->val[i]->expr.args[0], pat->expr.args[0]))
+                return t->sub[i];
+        }
 
-    sub = mkdtree();
-    sub->patexpr = pat;
-    lappend(&t->val, &t->nval, pat);
-    lappend(&t->load, &t->nload, val);
-    lappend(&t->sub, &t->nsub, sub);
+        sub = mkdtree();
+        sub->patexpr = pat;
+        lappend(&t->val, &t->nval, pat);
+        lappend(&t->load, &t->nload, val);
+        lappend(&t->sub, &t->nsub, sub);
+    }
     return sub;
 }
 
@@ -397,11 +440,8 @@
     nblk = 0;
     blk = NULL;
 
-    /*
-    Disabled until we have all of this code working.
     for (i = 0; i < dt->ncap; i++)
         lappend(&blk, &nblk, dt->cap[i]);
-    */
     for (i = 0; i < n->block.nstmts; i++)
         lappend(&blk, &nblk, n->block.stmts[i]);
     lfree(&n->block.stmts, &n->block.nstmts);
@@ -470,6 +510,13 @@
     if (!exhaustivematch(m, t, exprtype(m->matchstmt.val)))
         fatal(m, "nonexhaustive pattern set in match statement");
     n = genmatch(m->loc, t);
+    assert(n->type == Nifstmt);
+    if (!n->ifstmt.iftrue) {
+        n->ifstmt.iftrue = deadblock();
+    }
+    if (!n->ifstmt.iffalse) {
+        n->ifstmt.iffalse = deadblock();
+    }
     return n;
 }