فهرست منبع

Adding for-in loop iteration over slices

Frederic Kehrein 11 ماه پیش
والد
کامیت
0417504d12
3فایلهای تغییر یافته به همراه61 افزوده شده و 4 حذف شده
  1. 24 4
      compiler/nflw.pas
  2. 18 0
      tests/test/tslice1.pp
  3. 19 0
      tests/test/tslice2.pp

+ 24 - 4
compiler/nflw.pas

@@ -666,8 +666,18 @@ implementation
               end
             else
               begin
-                lowbound:=cinlinenode.create(in_low_x,false,ctemprefnode.create(arrayvar));
-                highbound:=cinlinenode.create(in_high_x,false,ctemprefnode.create(arrayvar));
+                { Iterating throug slice }
+                if (expression.nodetype=vecn) and (tvecnode(expression).right.nodetype=rangen) then
+                  begin
+                    lowbound:=trangenode(tvecnode(expression).right).left.getcopy;
+                    highbound:=trangenode(tvecnode(expression).right).right.getcopy;
+                    expression:=tvecnode(expression).left.getcopy;
+                  end
+                else
+                  begin
+                    lowbound:=cinlinenode.create(in_low_x,false,ctemprefnode.create(arrayvar));
+                    highbound:=cinlinenode.create(in_high_x,false,ctemprefnode.create(arrayvar));
+                  end;
               end;
 
             addstatement(loopstatement,arrayvar);
@@ -683,8 +693,18 @@ implementation
               end
             else
               begin
-                lowbound:=cinlinenode.create(in_low_x,false,expression.getcopy);
-                highbound:=cinlinenode.create(in_high_x,false,expression.getcopy);
+                { Iterating throug slice }
+                if (expression.nodetype=vecn) and (tvecnode(expression).right.nodetype=rangen) then
+                  begin
+                    lowbound:=trangenode(tvecnode(expression).right).left.getcopy;
+                    highbound:=trangenode(tvecnode(expression).right).right.getcopy;
+                    expression:=tvecnode(expression).left.getcopy;
+                  end
+                else
+                  begin
+                    lowbound:=cinlinenode.create(in_low_x,false,expression.getcopy);
+                    highbound:=cinlinenode.create(in_high_x,false,expression.getcopy);
+                  end;
               end;
           end;
 

+ 18 - 0
tests/test/tslice1.pp

@@ -0,0 +1,18 @@
+program Test;
+
+{$Mode ObjFPC}{$H+}
+
+function sumafter1(arr: Array of Integer): Integer;
+var
+  i: Integer;
+begin
+  Result:=0;
+  for i in arr[1..High(arr)] do
+    Result:=Result+i;
+end;
+
+begin
+  if sumafter1([1,2,3,4]) <> 2+3+4 then
+    Halt(1);
+  WriteLn('ok');
+end.

+ 19 - 0
tests/test/tslice2.pp

@@ -0,0 +1,19 @@
+program Test;
+
+{$Mode ObjFPC}{$H+}
+
+type TIntArray = array of Integer;
+function sumafter1(arr: TIntArray): Integer;
+var
+  i: Integer;
+begin
+  Result:=0;
+  for i in arr[1..High(arr)] do
+    Result:=Result+i;
+end;
+
+begin
+  if sumafter1([1,2,3,4]) <> 2+3+4 then
+    Halt(1);
+  WriteLn('ok');
+end.