Browse Source

* State tracker automatically changes while loops into repeat loops

daniel 23 years ago
parent
commit
bec9b5f988
1 changed files with 27 additions and 3 deletions
  1. 27 3
      compiler/nflw.pas

+ 27 - 3
compiler/nflw.pas

@@ -368,13 +368,24 @@ implementation
 	done:boolean;
 	done:boolean;
 	value:boolean;
 	value:boolean;
 	change:boolean;
 	change:boolean;
+	firsttest:boolean;
 	factval:Tnode;
 	factval:Tnode;
     
     
     begin
     begin
 	track_state_pass:=false;
 	track_state_pass:=false;
 	done:=false;
 	done:=false;
+	firsttest:=true;
+	{For repeat until statements, first do a pass through the code.}
+	if not testatbegin then
+	    begin
+		code:=right.getcopy;
+		if code.track_state_pass(exec_known) then
+		    track_state_pass:=true;
+		code.destroy;
+	    end;
 	repeat
 	repeat
 	    condition:=left.getcopy;
 	    condition:=left.getcopy;
+	    code:=right.getcopy;
 	    change:=condition.track_state_pass(exec_known);
 	    change:=condition.track_state_pass(exec_known);
 	    factval:=aktstate.find_fact(left);
 	    factval:=aktstate.find_fact(left);
 	    if factval<>nil then
 	    if factval<>nil then
@@ -390,9 +401,14 @@ implementation
 		    condition.resulttype.def:=nil;
 		    condition.resulttype.def:=nil;
 		    do_resulttypepass(condition);
 		    do_resulttypepass(condition);
 		end;
 		end;
-	    code:=right.getcopy;
 	    if is_constboolnode(condition) then
 	    if is_constboolnode(condition) then
 		begin
 		begin
+		    {Try to turn a while loop into a repeat loop.}
+		    if firsttest then
+			begin
+			testatbegin:=false;
+			    writeln('while->repeat');
+			end;
 		    value:=(Tordconstnode(condition).value<>0) xor checknegate;
 		    value:=(Tordconstnode(condition).value<>0) xor checknegate;
 		    if value then
 		    if value then
 			begin
 			begin
@@ -410,6 +426,7 @@ implementation
 		end;
 		end;
 	    code.destroy;
 	    code.destroy;
 	    condition.destroy;
 	    condition.destroy;
+	    firsttest:=false;
 	until done;
 	until done;
 	{The loop condition is also known, for example:
 	{The loop condition is also known, for example:
 	 while i<10 do
 	 while i<10 do
@@ -427,7 +444,11 @@ implementation
     		condition.resulttype.def:=nil;
     		condition.resulttype.def:=nil;
 		do_resulttypepass(condition);
 		do_resulttypepass(condition);
 	    end;
 	    end;
-	aktstate.store_fact(condition,cordconstnode.create(byte(checknegate),booltype));
+	if not is_constboolnode(condition) then
+	    aktstate.store_fact(condition,
+	     cordconstnode.create(byte(checknegate),booltype))
+	else
+	    condition.destroy;
     end;
     end;
 {$endif}
 {$endif}
 
 
@@ -1213,7 +1234,10 @@ begin
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.39  2002-07-19 12:55:27  daniel
+  Revision 1.40  2002-07-20 08:19:31  daniel
+  * State tracker automatically changes while loops into repeat loops
+
+  Revision 1.39  2002/07/19 12:55:27  daniel
   * Further developed state tracking in whilerepeatn
   * Further developed state tracking in whilerepeatn
 
 
   Revision 1.38  2002/07/19 11:41:35  daniel
   Revision 1.38  2002/07/19 11:41:35  daniel