Ver código fonte

Added transformation of while-loops to do-while loops

Taddeus Kroes 12 anos atrás
pai
commit
2084f76312
2 arquivos alterados com 10 adições e 4 exclusões
  1. 8 2
      phases/desug.ml
  2. 2 2
      test/constant_propagation.cvc

+ 8 - 2
phases/desug.ml

@@ -136,12 +136,18 @@ let for_to_while node =
                 Assign (_i, None, start, locof start);
                 Assign (_stop, None, stop, locof stop);
                 Assign (_step, None, step, locof step);
-                While (cond, traverse new_vars (Block (
+                traverse new_vars (While (cond, (Block (
                     block_body (replace_var counter _i body) @
                     [Assign (_i, None, Binop (Add, vi, vstep, noloc), noloc)]
-                )), loc);
+                )), loc));
             ]
 
+        (* Transform while-loops to do-while loops in if-statements *)
+        | While (cond, body, loc) ->
+            let cond = traverse new_vars cond in
+            let body = traverse new_vars body in
+            Block [If (cond, Block [DoWhile (cond, body, loc)], loc)]
+
         | node -> transform_children (traverse new_vars) node
     in
     traverse (ref []) node

+ 2 - 2
test/constant_propagation.cvc

@@ -1,7 +1,7 @@
 extern void printInt(int val);
 
-export int main(int a, int b) {
-    for (int i = a, b) {
+export int main() {
+    for (int i = 0, 10) {
         printInt(i);
     }