shithub: mc

Download patch

ref: e810bb4e2b87089f4910f7e489a429cc5e62e47d
parent: b0b32305ca510490c3edb78950655b26c9b22947
author: Ori Bernstein <[email protected]>
date: Thu Jun 12 09:03:11 EDT 2014

Add support for matching string arguments.

    Holy crap, works on the first try.

--- a/6/simp.c
+++ b/6/simp.c
@@ -605,7 +605,9 @@
 {
     Node *v, *x, *y;
     Node *deeper, *next;
-    Node **patarg;
+    Node **patarg, *lit, *idx;
+    char *str;
+    size_t len;
     Ucon *uc;
     size_t i;
     size_t off;
@@ -622,9 +624,38 @@
         /* Never supported */
         case Tyvoid: case Tybad: case Tyvalist: case Tyvar:
         case Typaram: case Tyunres: case Tyname: case Ntypes:
-        /* Should never show up */
+            die("Unsupported type for pattern");
+            break;
+        /* only valid for string literals */
         case Tyslice:
-            die("Unsupported type for compare");
+            lit = pat->expr.args[0];
+            if (exprop(pat) != Olit || lit->lit.littype != Lstr)
+                die("Unsupported pattern");
+            str = lit->lit.strval;
+
+            /* load slice length */
+            next = genlbl();
+            x = slicelen(s, val);
+            len = strlen(str);
+            y = mkintlit(lit->line, len);
+            y->expr.type = tyintptr;
+            v = mkexpr(pat->line, Oeq, x, y, NULL);
+            cjmp(s, v, next, iffalse);
+            append(s, next);
+
+            for (i = 0; i < len; i++) {
+                next = genlbl();
+                x = mkintlit(pat->line, str[i]);
+                x->expr.type = mktype(-1, Tybyte);
+                idx = mkintlit(pat->line, i);
+                idx->expr.type = tyintptr;
+                y = load(idxaddr(s, val, idx));
+                v = mkexpr(pat->line, Oeq, x, y, NULL);
+                v->expr.type = mktype(pat->line, Tybool);
+                cjmp(s, v, next, iffalse);
+                append(s, next);
+            }
+            jmp(s, iftrue);
             break;
         case Tybool: case Tychar: case Tybyte:
         case Tyint8: case Tyint16: case Tyint32: case Tyint:
--- /dev/null
+++ b/test/data/matchargstr-expected
@@ -1,0 +1,1 @@
+Correct `Str "asdf"!
binary files /dev/null b/test/matchargstr differ
--- a/test/tests
+++ b/test/tests
@@ -96,6 +96,7 @@
 B matchstruct	E	42
 B matcharray	E	42
 B matchargunion	E	69
+B matchargstr	C
 B matchunion_sl	P	foo
 B matchbind	E	8
 F matchmixed