about summary refs log tree commit diff stats
path: root/cpp/024brace
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-03-17 00:26:12 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-03-17 00:26:12 -0700
commitc062697c9ff3c8cb0938f56bed3df2af3d122bd6 (patch)
tree3499fc91b20183f9d87a8fe716a34dacd6f7f993 /cpp/024brace
parent3d45bf386169bfdea5d8654cf1869cc6c2a719f6 (diff)
downloadmu-c062697c9ff3c8cb0938f56bed3df2af3d122bd6.tar.gz
944 - more break/loop tests
I still can't handle jumps to labels. That'll require some rejiggering..
Diffstat (limited to 'cpp/024brace')
-rw-r--r--cpp/024brace99
1 files changed, 97 insertions, 2 deletions
diff --git a/cpp/024brace b/cpp/024brace
index 2402923b..0385b3b1 100644
--- a/cpp/024brace
+++ b/cpp/024brace
@@ -55,11 +55,13 @@ void transform_braces(const recipe_number r) {
     instruction& inst = Recipe[r].steps[index];
     if (inst.label == "{") open_braces.push(index);
     else if (inst.label == "}") open_braces.pop();
+    else if (inst.is_label)
+      ;  // do nothing
     else if (inst.operation == Recipe_number["loop"]) {
       inst.operation = Recipe_number["jump"];
       if (inst.ingredients.size() > 0 && inst.ingredients[0].types[0] == 0) {
         // explicit target; a later phase will handle it
-        ;
+        trace("after-brace") << "jump " << inst.ingredients[0].name << ":offset";
       }
       else {
         reagent ing(0);  // literal
@@ -72,7 +74,7 @@ void transform_braces(const recipe_number r) {
       inst.operation = Recipe_number["jump"];
       if (inst.ingredients.size() > 0 && inst.ingredients[0].types[0] == 0) {
         // explicit target; a later phase will handle it
-        ;
+        trace("after-brace") << "jump " << inst.ingredients[0].name << ":offset";
       }
       else {
         reagent ing(0);  // literal
@@ -81,6 +83,32 @@ void transform_braces(const recipe_number r) {
         trace("after-brace") << "jump " << ing.value << ":offset";
       }
     }
+    else if (inst.operation == Recipe_number["loop-if"]) {
+      inst.operation = Recipe_number["jump-if"];
+      if (inst.ingredients.size() > 1 && inst.ingredients[1].types[0] == 0) {
+        // explicit target; a later phase will handle it
+        trace("after-brace") << "jump " << inst.ingredients[1].name << ":offset";
+      }
+      else {
+        reagent ing(0);  // literal
+        ing.value = open_braces.top()-index;
+        inst.ingredients.push_back(ing);
+        trace("after-brace") << "jump-if " << inst.ingredients[0].name << ", " << ing.value << ":offset";
+      }
+    }
+    else if (inst.operation == Recipe_number["break-if"]) {
+      inst.operation = Recipe_number["jump-if"];
+      if (inst.ingredients.size() > 1 && inst.ingredients[1].types[0] == 0) {
+        // explicit target; a later phase will handle it
+        trace("after-brace") << "jump " << inst.ingredients[1].name << ":offset";
+      }
+      else {
+        reagent ing(0);  // literal
+        ing.value = matching_brace(open_braces.top(), braces) - index - 1;
+        inst.ingredients.push_back(ing);
+        trace("after-brace") << "jump-if " << inst.ingredients[0].name << ", " << ing.value << ":offset";
+      }
+    }
     else {
       trace("after-brace") << inst.name << " ...";
     }
@@ -157,3 +185,70 @@ recipe main [
 +after-brace: jump 1:offset
 +after-brace: copy ...
 +after-brace: jump 0:offset
+
+:(scenario "break_if")
+recipe main [
+  1:integer <- copy 0:literal
+  2:integer <- copy 0:literal
+  {
+    break-if 2:integer
+    3:integer <- copy 0:literal
+  }
+  {
+    break
+  }
+]
++after-brace: recipe main
++after-brace: copy ...
++after-brace: copy ...
++after-brace: jump-if 2, 1:offset
++after-brace: copy ...
++after-brace: jump 0:offset
+
+:(scenario "break_nested")
+recipe main [
+  1:integer <- copy 0:literal
+  {
+    2:integer <- copy 0:literal
+    break
+    {
+      3:integer <- copy 0:literal
+    }
+    4:integer <- copy 0:literal
+  }
+]
++after-brace: jump 4:offset
+
+:(scenario "break_nested_degenerate")
+recipe main [
+  1:integer <- copy 0:literal
+  {
+    2:integer <- copy 0:literal
+    break
+    {
+    }
+    4:integer <- copy 0:literal
+  }
+]
++after-brace: jump 3:offset
+
+:(scenario "break_nested_degenerate2")
+recipe main [
+  1:integer <- copy 0:literal
+  {
+    2:integer <- copy 0:literal
+    break
+    {
+    }
+  }
+]
++after-brace: jump 2:offset
+
+:(scenario "break_label")
+recipe main [
+  1:integer <- copy 0:literal
+  {
+    break 3:offset
+  }
+]
++after-brace: jump 3:offset