shithub: mc

Download patch

ref: af272f33d3f7f75006f78d925f57532d879bff31
parent: c8962ecdaa334c37668228b43db27c283ca15e03
author: Ori Bernstein <[email protected]>
date: Wed Aug 15 18:02:47 EDT 2012

Add syntax and tests for multiple constraints

    We need to be able to specify all integer-like
    constraints on a type in order to compare it to
    an int. Make that work.

--- a/parse/gram.y
+++ b/parse/gram.y
@@ -123,7 +123,7 @@
 %type <ty> type structdef uniondef tupledef compoundtype functype funcsig
 %type <ty> generictype
 %type <tylist> tuptybody
-%type <node> typaramlist
+%type <nodelist> typaramlist
 
 %type <tok> asnop cmpop addop mulop shiftop
 
@@ -275,16 +275,23 @@
         ;
 
 generictype
-        : Ttyparam typaramlist 
+        : Ttyparam
+            {$$ = mktyparam($1->line, $1->str);}
+        | Ttyparam Twith name
             {$$ = mktyparam($1->line, $1->str);
-            /* FIXME: this will only work for builtin cstrs */
-             if ($2)
-                constrainwith($$, $2->name.name);}
+             constrainwith($$, $3->name.name);}
+        | Ttyparam Twith Toparen typaramlist Tcparen
+            {size_t i;
+             for (i = 0; i < $4.nn; i++)
+                constrainwith($$, $4.nl[i]->name.name);}
         ;
 
 typaramlist
-        : /* empty */ {$$ = NULL;}
-        | Twith name {$$ = $2;}
+        : name
+            {$$.nl = NULL; $$.nn = 0;
+             lappend(&$$.nl, &$$.nn, $1);}
+        | typaramlist Tcomma name
+            {lappend(&$$.nl, &$$.nn, $3);}
         ;
 
 compoundtype
--- a/test/cstr-builtin.myr
+++ b/test/cstr-builtin.myr
@@ -6,6 +6,13 @@
 	;;
 }
 
+generic intlike_is42 = {a : @a::(tcnum,tctest,tcint)
+	-> a == 42
+}
 const main = {
-	-> max(12, 42)
+	if intlike_is42(123)
+	    -> 16
+	else
+	    -> max(12, 42)
+	;;
 }