From 4690ce81e079fc58cae8d6d583e5e3eb3ed81a83 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 9 Mar 2016 02:56:27 -0800 Subject: 2743 Looks like "TOhtml | " doesn't work on Mac OS X for some reason.. --- html/040brace.cc.html | 162 ++++++++++++++++++++++++-------------------------- 1 file changed, 77 insertions(+), 85 deletions(-) (limited to 'html/040brace.cc.html') diff --git a/html/040brace.cc.html b/html/040brace.cc.html index 3dbfd8d3..49afa366 100644 --- a/html/040brace.cc.html +++ b/html/040brace.cc.html @@ -3,35 +3,28 @@ Mu - 040brace.cc - - + + - - + - - -
+
 //: Structured programming
 //:
 //: Our jump recipes are quite inconvenient to use, so mu provides a
@@ -55,7 +48,7 @@ body { font-family: monospace; color: #eeeeee; background-color: #080808; }
 
 :(scenarios transform)
 :(scenario brace_conversion)
-recipe main [
+def main [
   {
     break
     1:number <- copy 0
@@ -69,40 +62,40 @@ recipe main [
 Transform.push_back(transform_braces);  // idempotent
 
 :(code)
-void transform_braces(const recipe_ordinal r) {
-  const int OPEN = 0, CLOSE = 1;
+void transform_braces(const recipe_ordinal r) {
+  const int OPEN = 0, CLOSE = 1;
   // use signed integer for step index because we'll be doing arithmetic on it
-  list<pair<int/*OPEN/CLOSE*/, /*step*/long long int> > braces;
+  list<pair<int/*OPEN/CLOSE*/, /*step*/long long int> > braces;
   trace(9991, "transform") << "--- transform braces for recipe " << get(Recipe, r).name << end();
 //?   cerr << "--- transform braces for recipe " << get(Recipe, r).name << '\n';
-  for (long long int index = 0; index < SIZE(get(Recipe, r).steps); ++index) {
-    const instruction& inst = get(Recipe, r).steps.at(index);
-    if (inst.label == "{") {
+  for (long long int index = 0; index < SIZE(get(Recipe, r).steps); ++index) {
+    const instruction& inst = get(Recipe, r).steps.at(index);
+    if (inst.label == "{") {
       trace(9993, "transform") << maybe(get(Recipe, r).name) << "push (open, " << index << ")" << end();
-      braces.push_back(pair<int,long long int>(OPEN, index));
+      braces.push_back(pair<int,long long int>(OPEN, index));
     }
-    if (inst.label == "}") {
+    if (inst.label == "}") {
       trace(9993, "transform") << "push (close, " << index << ")" << end();
-      braces.push_back(pair<int,long long int>(CLOSE, index));
+      braces.push_back(pair<int,long long int>(CLOSE, index));
     }
   }
-  stack</*step*/long long int> open_braces;
-  for (long long int index = 0; index < SIZE(get(Recipe, r).steps); ++index) {
+  stack</*step*/long long int> open_braces;
+  for (long long int index = 0; index < SIZE(get(Recipe, r).steps); ++index) {
     instruction& inst = get(Recipe, r).steps.at(index);
-    if (inst.label == "{") {
+    if (inst.label == "{") {
       open_braces.push(index);
       continue;
     }
-    if (inst.label == "}") {
-      if (open_braces.empty()) {
-        raise_error << "missing '{' in " << get(Recipe, r).name << '\n' << end();
+    if (inst.label == "}") {
+      if (open_braces.empty()) {
+        raise << "missing '{' in " << get(Recipe, r).name << '\n' << end();
         return;
       }
       open_braces.pop();
       continue;
     }
-    if (inst.is_label) continue;
-    if (inst.old_name != "loop"
+    if (inst.is_label) continue;
+    if (inst.old_name != "loop"
          && inst.old_name != "loop-if"
          && inst.old_name != "loop-unless"
          && inst.old_name != "break"
@@ -112,74 +105,74 @@ void transform_braces(const recipe_ordinal rcontinue;
     }
     // check for errors
-    if (inst.old_name.find("-if") != string::npos || inst.old_name.find("-unless") != string::npos) {
-      if (inst.ingredients.empty()) {
-        raise_error << inst.old_name << " expects 1 or 2 ingredients, but got none\n" << end();
+    if (inst.old_name.find("-if") != string::npos || inst.old_name.find("-unless") != string::npos) {
+      if (inst.ingredients.empty()) {
+        raise << inst.old_name << " expects 1 or 2 ingredients, but got none\n" << end();
         continue;
       }
     }
     // update instruction operation
-    if (inst.old_name.find("-if") != string::npos) {
+    if (inst.old_name.find("-if") != string::npos) {
       inst.name = "jump-if";
       inst.operation = JUMP_IF;
     }
-    else if (inst.old_name.find("-unless") != string::npos) {
+    else if (inst.old_name.find("-unless") != string::npos) {
       inst.name = "jump-unless";
       inst.operation = JUMP_UNLESS;
     }
-    else {
+    else {
       inst.name = "jump";
       inst.operation = JUMP;
     }
     // check for explicitly provided targets
-    if (inst.old_name.find("-if") != string::npos || inst.old_name.find("-unless") != string::npos) {
+    if (inst.old_name.find("-if") != string::npos || inst.old_name.find("-unless") != string::npos) {
       // conditional branches check arg 1
-      if (SIZE(inst.ingredients) > 1 && is_literal(inst.ingredients.at(1))) {
+      if (SIZE(inst.ingredients) > 1 && is_literal(inst.ingredients.at(1))) {
         trace(9992, "transform") << inst.name << ' ' << inst.ingredients.at(1).name << ":offset" << end();
         continue;
       }
     }
-    else {
+    else {
       // unconditional branches check arg 0
-      if (!inst.ingredients.empty() && is_literal(inst.ingredients.at(0))) {
+      if (!inst.ingredients.empty() && is_literal(inst.ingredients.at(0))) {
         trace(9992, "transform") << "jump " << inst.ingredients.at(0).name << ":offset" << end();
         continue;
       }
     }
     // if implicit, compute target
     reagent target;
-    target.type = new type_tree("offset", get(Type_ordinal, "offset"));
+    target.type = new type_tree("offset", get(Type_ordinal, "offset"));
     target.set_value(0);
-    if (open_braces.empty())
-      raise_error << inst.old_name << " needs a '{' before\n" << end();
-    else if (inst.old_name.find("loop") != string::npos)
+    if (open_braces.empty())
+      raise << inst.old_name << " needs a '{' before\n" << end();
+    else if (inst.old_name.find("loop") != string::npos)
       target.set_value(open_braces.top()-index);
-    else  // break instruction
+    else  // break instruction
       target.set_value(matching_brace(open_braces.top(), braces, r) - index - 1);
     inst.ingredients.push_back(target);
     // log computed target
-    if (inst.name == "jump")
+    if (inst.name == "jump")
       trace(9992, "transform") << "jump " << no_scientific(target.value) << ":offset" << end();
-    else
+    else
       trace(9992, "transform") << inst.name << ' ' << inst.ingredients.at(0).name << ", " << no_scientific(target.value) << ":offset" << end();
   }
 }
 
 // returns a signed integer not just so that we can return -1 but also to
 // enable future signed arithmetic
-long long int matching_brace(long long int index, const list<pair<int, long long int> >& braces, recipe_ordinal r) {
-  int stacksize = 0;
-  for (list<pair<int, long long int> >::const_iterator p = braces.begin(); p != braces.end(); ++p) {
-    if (p->second < index) continue;
+long long int matching_brace(long long int index, const list<pair<int, long long int> >& braces, recipe_ordinal r) {
+  int stacksize = 0;
+  for (list<pair<int, long long int> >::const_iterator p = braces.begin(); p != braces.end(); ++p) {
+    if (p->second < index) continue;
     stacksize += (p->first ? 1 : -1);
-    if (stacksize == 0) return p->second;
+    if (stacksize == 0) return p->second;
   }
-  raise_error << maybe(get(Recipe, r).name) << "unbalanced '{'\n" << end();
+  raise << maybe(get(Recipe, r).name) << "unbalanced '{'\n" << end();
   return SIZE(get(Recipe, r).steps);  // exit current routine
 }
 
 :(scenario loop)
-recipe main [
+def main [
   1:number <- copy 0
   2:number <- copy 0
   {
@@ -194,7 +187,7 @@ recipe main [
 +transform: jump -2:offset
 
 :(scenario break_empty_block)
-recipe main [
+def main [
   1:number <- copy 0
   {
     break
@@ -205,7 +198,7 @@ recipe main [
 +transform: jump 0:offset
 
 :(scenario break_cascading)
-recipe main [
+def main [
   1:number <- copy 0
   {
     break
@@ -220,7 +213,7 @@ recipe main [
 +transform: jump 0:offset
 
 :(scenario break_cascading_2)
-recipe main [
+def main [
   1:number <- copy 0
   2:number <- copy 0
   {
@@ -239,11 +232,11 @@ recipe main [
 +transform: jump 0:offset
 
 :(scenario break_if)
-recipe main [
+def main [
   1:number <- copy 0
   2:number <- copy 0
   {
-    break-if 2:number
+    break-if 2:number
     3:number <- copy 0
   }
   {
@@ -258,7 +251,7 @@ recipe main [
 +transform: jump 0:offset
 
 :(scenario break_nested)
-recipe main [
+def main [
   1:number <- copy 0
   {
     2:number <- copy 0
@@ -272,7 +265,7 @@ recipe main [
 +transform: jump 4:offset
 
 :(scenario break_nested_degenerate)
-recipe main [
+def main [
   1:number <- copy 0
   {
     2:number <- copy 0
@@ -285,7 +278,7 @@ recipe main [
 +transform: jump 3:offset
 
 :(scenario break_nested_degenerate_2)
-recipe main [
+def main [
   1:number <- copy 0
   {
     2:number <- copy 0
@@ -298,7 +291,7 @@ recipe main [
 
 :(scenario break_label)
 % Hide_errors = true;
-recipe main [
+def main [
   1:number <- copy 0
   {
     break +foo:offset
@@ -307,7 +300,7 @@ recipe main [
 +transform: jump +foo:offset
 
 :(scenario break_unless)
-recipe main [
+def main [
   1:number <- copy 0
   2:number <- copy 0
   {
@@ -322,7 +315,7 @@ recipe main [
 +transform: copy ...
 
 :(scenario loop_unless)
-recipe main [
+def main [
   1:number <- copy 0
   2:number <- copy 0
   {
@@ -337,14 +330,14 @@ recipe main [
 +transform: copy ...
 
 :(scenario loop_nested)
-recipe main [
+def main [
   1:number <- copy 0
   {
     2:number <- copy 0
     {
       3:number <- copy 0
     }
-    loop-if 4:boolean
+    loop-if 4:boolean
     5:number <- copy 0
   }
 ]
@@ -352,7 +345,7 @@ recipe main [
 +transform: jump-if 4, -5:offset
 
 :(scenario loop_label)
-recipe main [
+def main [
   1:number <- copy 0
   +foo
   2:number <- copy 0
@@ -364,12 +357,12 @@ recipe main [
 //: test how things actually run
 :(scenarios run)
 :(scenario brace_conversion_and_run)
-recipe test-factorial [
+def test-factorial [
   1:number <- copy 5
   2:number <- copy 1
   {
     3:boolean <- equal 1:number, 1
-    break-if 3:boolean
+    break-if 3:boolean
 #    $print 1:number
     2:number <- multiply 2:number, 1:number
     1:number <- subtract 1:number, 1
@@ -381,16 +374,16 @@ recipe test-factorial [
 
 :(scenario break_outside_braces_fails)
 % Hide_errors = true;
-recipe main [
+def main [
   break
 ]
 +error: break needs a '{' before
 
 :(scenario break_conditional_without_ingredient_fails)
 % Hide_errors = true;
-recipe main [
+def main [
   {
-    break-if
+    break-if
   }
 ]
 +error: break-if expects 1 or 2 ingredients, but got none
@@ -414,13 +407,12 @@ put(Recipe_ordinal,(Recipe_ordinal, "loop-if", LOOP_IF);
 put(Recipe_ordinal, "loop-unless", LOOP_UNLESS);
 :(before "End Primitive Recipe Checks")
-case BREAK: break;
-case BREAK_IF: break;
-case BREAK_UNLESS: break;
-case LOOP: break;
-case LOOP_IF: break;
-case LOOP_UNLESS: break;
+case BREAK: break;
+case BREAK_IF: break;
+case BREAK_UNLESS: break;
+case LOOP: break;
+case LOOP_IF: break;
+case LOOP_UNLESS: break;
 
- -- cgit 1.4.1-2-gfad0