[fpc-devel] Fwd: While - Otherwise Statement

MohsenTi mohsen.timar at gmail.com
Sun Oct 11 15:56:44 CEST 2015


Hi everybody

I add new feature to FPC compiler to simplify programming.
this is While - Otherwise working like While - Else in python and has
backwards compatibility.

Examples

Ex1: While (I<5) do begin Writeln(I); Dec(I); end otherwise
writeln('otherwise');

Ex2: While (I<5) do begin Writeln(I); Dec(I); break; end otherwise
writeln('otherwise');

Ex3: if (I<10) then While (I<5) do begin Writeln(I); Dec(I); break; end
otherwise writeln('otherwise') else writeln('else');

I attached a svn diff file to email and I'm going to add For-Otherwise and
more.

please test it and let me know what you think.

mohsen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20151011/88dc38f8/attachment.html>
-------------- next part --------------
Index: ncgflw.pas
===================================================================
--- ncgflw.pas	(revision 32016)
+++ ncgflw.pas	(working copy)
@@ -132,7 +132,7 @@
 
     procedure tcgwhilerepeatnode.pass_generate_code;
       var
-         lcont,lbreak,lloop,
+         lcont,lbreak,lloop,lotherwise,
          oldclabel,oldblabel : tasmlabel;
          truelabel,falselabel : tasmlabel;
          oldflowcontrol : tflowcontrol;
@@ -143,6 +143,7 @@
          current_asmdata.getjumplabel(lloop);
          current_asmdata.getjumplabel(lcont);
          current_asmdata.getjumplabel(lbreak);
+         current_asmdata.getjumplabel(lotherwise);
          { arrange continue and breaklabels: }
          oldflowcontrol:=flowcontrol;
          oldclabel:=current_procinfo.CurrContinueLabel;
@@ -184,17 +185,21 @@
          hlcg.a_label(current_asmdata.CurrAsmList,lcont);
          if lnf_checknegate in loopflags then
            begin
-             truelabel:=lbreak;
+             truelabel:=lotherwise;
              falselabel:=lloop;
            end
          else
            begin
              truelabel:=lloop;
-             falselabel:=lbreak;
+             falselabel:=lotherwise;
            end;
          secondpass(left);
+         hlcg.maketojumpboollabels(current_asmdata.CurrAsmList,left,truelabel,falselabel);
+         hlcg.a_label(current_asmdata.CurrAsmList,lotherwise);
+         if (Assigned(t1)) then begin
+           secondpass(t1);
+         end;
 
-         hlcg.maketojumpboollabels(current_asmdata.CurrAsmList,left,truelabel,falselabel);
          hlcg.a_label(current_asmdata.CurrAsmList,lbreak);
 
          sync_regvars(false);
Index: nflw.pas
===================================================================
--- nflw.pas	(revision 32016)
+++ nflw.pas	(working copy)
@@ -71,8 +71,11 @@
           function docompare(p: tnode): boolean; override;
        end;
 
+       { twhilerepeatnode }
+
        twhilerepeatnode = class(tloopnode)
           constructor create(l,r:Tnode;tab,cn:boolean);virtual;reintroduce;
+          constructor create(l,r,e:Tnode;tab,cn:boolean);virtual;reintroduce;
           function pass_typecheck:tnode;override;
           function pass_1 : tnode;override;
 {$ifdef state_tracking}
@@ -1046,7 +1049,7 @@
                                TWHILEREPEATNODE
 *****************************************************************************}
 
-    constructor Twhilerepeatnode.create(l,r:Tnode;tab,cn:boolean);
+        constructor twhilerepeatnode.create(l, r: Tnode; tab, cn: boolean);
       begin
           inherited create(whilerepeatn,l,r,nil,nil);
           if tab then
@@ -1055,6 +1058,15 @@
               include(loopflags,lnf_checknegate);
       end;
 
+    constructor twhilerepeatnode.create(l, r, e: Tnode; tab, cn: boolean);
+    begin
+            inherited create(whilerepeatn,l,r,e,nil);
+          if tab then
+              include(loopflags, lnf_testatbegin);
+          if cn then
+              include(loopflags,lnf_checknegate);
+    end;
+
     function twhilerepeatnode.pass_typecheck:tnode;
       var
          t:Tunarynode;
Index: pstatmnt.pas
===================================================================
--- pstatmnt.pas	(revision 32016)
+++ pstatmnt.pas	(working copy)
@@ -328,7 +328,7 @@
     function while_statement : tnode;
 
       var
-         p_e,p_a : tnode;
+         p_e,p_a,else_a : tnode;
 
       begin
          consume(_WHILE);
@@ -335,7 +335,11 @@
          p_e:=comp_expr(true,false);
          consume(_DO);
          p_a:=statement;
-         result:=cwhilerepeatnode.create(p_e,p_a,true,false);
+         if (try_to_consume(_OTHERWISE)) then
+          else_a := statement
+         else
+          else_a := nil;
+        Result := cwhilerepeatnode.Create(p_e, p_a, else_a, True, False);
       end;
 
     { a helper function which is used both by "with" and "for-in loop" nodes }


More information about the fpc-devel mailing list