[svn] r6055: nemerle/trunk/macros: Util.n core.n

malekith svnadmin at nemerle.org
Sat Jan 7 18:18:53 CET 2006


Log:
Restructure ExpandRange code a little bit. Make "x in $[1..10]" behave exactly like "x in [1..10]" (also performance-wise).

Author: malekith
Date: Sat Jan  7 18:18:52 2006
New Revision: 6055

Modified:
   nemerle/trunk/macros/Util.n
   nemerle/trunk/macros/core.n

Modified: nemerle/trunk/macros/Util.n
==============================================================================
--- nemerle/trunk/macros/Util.n	(original)
+++ nemerle/trunk/macros/Util.n	Sat Jan  7 18:18:52 2006
@@ -281,11 +281,12 @@
   public module ListComprehensionHelper
   {
     [Nemerle.Macros.Hygienic]
-    public ExpandRange (first : PExpr, second : option [PExpr], last : PExpr, pat : PExpr, acc : PExpr) : PExpr
+    public ExpandRange (inrange : PExpr, acc : PExpr) : option [PExpr]
     {
-      match (second) {
-        | Some (second) =>
-          <[
+      match (inrange) {
+        | <[ $pat in $[$first, $second .. $last] ]>
+        | <[ $pat in [$first, $second .. $last] ]> =>
+          Some (<[
              mutable i = $first;
              def delta = $second - i;
              def last = $last;
@@ -294,10 +295,11 @@
                $acc;
                i += delta;
              }
-          ]>
+          ]>)
 
-        | None =>
-          <[
+        | <[ $pat in $[$first .. $last] ]>
+        | <[ $pat in [$first .. $last] ]> =>
+          Some (<[
              mutable i = $first;
              def last = $last;
              while (i <= last) {
@@ -305,7 +307,9 @@
                $acc;
                i++;
              }
-          ]>
+          ]>)
+
+        | _ => None ()
       }
     }
   }
@@ -327,19 +331,17 @@
     def loops =
       exprs.Tail.Rev ().FoldLeft (adder,
         fun (e, acc) {
-          match (e) {
-            | <[ $e1 in [$first, $second .. $last] ]> =>
-              ListComprehensionHelper.ExpandRange (first, Some (second), last, e1, acc)
-               
-            | <[ $e1 in [$first .. $last] ]> =>
-              ListComprehensionHelper.ExpandRange (first, None (), last, e1, acc)
+          match (ListComprehensionHelper.ExpandRange (e, acc)) {
+            | Some (expr) => expr
                
+            | None =>
+              match (e) {
             | <[ $e1 in $e2 ]> =>
               <[ foreach ($e1 in $e2) $acc ]>
-              
             | cond =>
               <[ when ($cond) $acc ]>
           }
+          }
         });
 
     match (exprs) {

Modified: nemerle/trunk/macros/core.n
==============================================================================
--- nemerle/trunk/macros/core.n	(original)
+++ nemerle/trunk/macros/core.n	Sat Jan  7 18:18:52 2006
@@ -325,21 +325,14 @@
   macro @foreach (inexpr, body)
   syntax ("foreach", "(", inexpr, ")", body)
   {
-    mutable iter = null, collection = null;
+    match (ListComprehensionHelper.ExpandRange (inexpr, body)) {
+      | Some (expr) => Nemerle.Imperative.Return (expr)
+      | None => {}
+    }
     
+    def (iter, collection) =
     match (inexpr) {
-      | <[ $e1 in [$first, $second .. $last] ]> =>
-        Nemerle.Imperative.Return (
-          ListComprehensionHelper.ExpandRange (first, Some (second), last, e1, body))
-           
-      | <[ $e1 in [$first .. $last] ]> =>
-        Nemerle.Imperative.Return (
-          ListComprehensionHelper.ExpandRange (first, None (), last, e1, body))
-            
-      | <[ $i in $c ]> =>
-        iter = i;
-        collection = c;
-
+        | <[ $i in $c ]> => (i, c)
       | e =>
         Message.FatalError ($ "the syntax is 'foreach (x in collection)', "
                               "got $e");



More information about the svn mailing list