From 9542bb112419d575190a72baf7f964c3e32df223 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 25 Jul 2015 22:15:51 -0700 Subject: 1853 --- html/040brace.cc.html | 107 +++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 53 deletions(-) (limited to 'html/040brace.cc.html') diff --git a/html/040brace.cc.html b/html/040brace.cc.html index f5f3921c..a4313315 100644 --- a/html/040brace.cc.html +++ b/html/040brace.cc.html @@ -13,14 +13,15 @@ pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; } body { font-family: monospace; color: #eeeeee; background-color: #080808; } * { font-size: 1.05em; } -.traceContains { color: #008000; } .cSpecial { color: #008000; } .Constant { color: #00a0a0; } +.CommentedCode { color: #6c6c6c; } +.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; } .Comment { color: #9090ff; } .Delimiter { color: #a04060; } .Special { color: #ff6060; } .Identifier { color: #804000; } -.CommentedCode { color: #6c6c6c; } +.traceContains { color: #008000; } --> @@ -70,64 +71,64 @@ recipe main [ Transform.push_back(transform_braces); :(code) -void transform_braces(const recipe_ordinal r) { +void transform_braces(const recipe_ordinal r) { //? cout << "AAA transform_braces\n"; //? 1 //? exit(0); //? 1 - const int OPEN = 0, CLOSE = 1; + 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; - for (long long int index = 0; index < SIZE(Recipe[r].steps); ++index) { - const instruction& inst = Recipe[r].steps.at(index); - if (inst.label == "{") { - trace("brace") << r << ": push (open, " << index << ")"; - braces.push_back(pair<int,long long int>(OPEN, index)); + list<pair<int/*OPEN/CLOSE*/, /*step*/long long int> > braces; + for (long long int index = 0; index < SIZE(Recipe[r].steps); ++index) { + const instruction& inst = Recipe[r].steps.at(index); + if (inst.label == "{") { + trace("brace") << r << ": push (open, " << index << ")" << end(); + braces.push_back(pair<int,long long int>(OPEN, index)); } - if (inst.label == "}") { - trace("brace") << "push (close, " << index << ")"; - braces.push_back(pair<int,long long int>(CLOSE, index)); + if (inst.label == "}") { + trace("brace") << "push (close, " << index << ")" << end(); + braces.push_back(pair<int,long long int>(CLOSE, index)); } } - stack</*step*/long long int> open_braces; - trace("after-brace") << "recipe " << Recipe[r].name; - for (long long int index = 0; index < SIZE(Recipe[r].steps); ++index) { + stack</*step*/long long int> open_braces; + trace("after-brace") << "recipe " << Recipe[r].name << end(); + for (long long int index = 0; index < SIZE(Recipe[r].steps); ++index) { instruction& inst = Recipe[r].steps.at(index); - if (inst.label == "{") { + if (inst.label == "{") { open_braces.push(index); continue; } - if (inst.label == "}") { + if (inst.label == "}") { open_braces.pop(); continue; } - if (inst.is_label) continue; - if (inst.operation != Recipe_ordinal["loop"] + if (inst.is_label) continue; + if (inst.operation != Recipe_ordinal["loop"] && inst.operation != Recipe_ordinal["loop-if"] && inst.operation != Recipe_ordinal["loop-unless"] && inst.operation != Recipe_ordinal["break"] && inst.operation != Recipe_ordinal["break-if"] && inst.operation != Recipe_ordinal["break-unless"]) { - trace("after-brace") << inst.name << " ..."; + trace("after-brace") << inst.name << " ..." << end(); continue; } // update instruction operation - if (inst.name.find("-if") != string::npos) + if (inst.name.find("-if") != string::npos) inst.operation = Recipe_ordinal["jump-if"]; - else if (inst.name.find("-unless") != string::npos) + else if (inst.name.find("-unless") != string::npos) inst.operation = Recipe_ordinal["jump-unless"]; - else + else inst.operation = Recipe_ordinal["jump"]; // check for explicitly provided targets - if (inst.name.find("-if") != string::npos || inst.name.find("-unless") != string::npos) { + if (inst.name.find("-if") != string::npos || inst.name.find("-unless") != string::npos) { // conditional branches check arg 1 - if (SIZE(inst.ingredients) > 1 && is_literal(inst.ingredients.at(1))) { - trace("after-brace") << "jump " << inst.ingredients.at(1).name << ":offset"; + if (SIZE(inst.ingredients) > 1 && is_literal(inst.ingredients.at(1))) { + trace("after-brace") << "jump " << 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))) { - trace("after-brace") << "jump " << inst.ingredients.at(0).name << ":offset"; + if (!inst.ingredients.empty() && is_literal(inst.ingredients.at(0))) { + trace("after-brace") << "jump " << inst.ingredients.at(0).name << ":offset" << end(); continue; } } @@ -135,38 +136,38 @@ void transform_braces(const recipe_ordinal r; target.types.push_back(Type_ordinal["offset"]); target.set_value(0); - if (open_braces.empty()) - raise << inst.name << " wasn't inside {}\n"; - else if (inst.name.find("loop") != string::npos) + if (open_braces.empty()) + raise << inst.name << " needs a '{' before\n" << end(); + else if (inst.name.find("loop") != string::npos) target.set_value(open_braces.top()-index); - else // break instruction - target.set_value(matching_brace(open_braces.top(), braces) - index - 1); + 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.find("-if") != string::npos) - trace("after-brace") << "jump-if " << inst.ingredients.at(0).name << ", " << target.value << ":offset"; - else if (inst.name.find("-unless") != string::npos) - trace("after-brace") << "jump-unless " << inst.ingredients.at(0).name << ", " << target.value << ":offset"; - else - trace("after-brace") << "jump " << target.value << ":offset"; + if (inst.name.find("-if") != string::npos) + trace("after-brace") << "jump-if " << inst.ingredients.at(0).name << ", " << target.value << ":offset" << end(); + else if (inst.name.find("-unless") != string::npos) + trace("after-brace") << "jump-unless " << inst.ingredients.at(0).name << ", " << target.value << ":offset" << end(); + else + trace("after-brace") << "jump " << 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) { - 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; } - assert(false); - return -1; + raise << Recipe[r].name << ": unbalanced '{'\n" << end(); + return SIZE(Recipe[r].steps); // exit current routine } // temporarily suppress run -void transform(string form) { +void transform(string form) { load(form); transform_all(); } @@ -254,7 +255,7 @@ recipe main [ 1:number <- copy 0:literal 2:number <- copy 0:literal { - break-if 2:number + break-if 2:number 3:number <- copy 0:literal } { @@ -355,7 +356,7 @@ recipe main [ { 3:number <- copy 0:literal } - loop-if 4:boolean + loop-if 4:boolean 5:number <- copy 0:literal } ] @@ -381,7 +382,7 @@ recipe test-factorial [ 2:number <- copy 1:literal { 3:boolean <- equal 1:number, 1:literal - break-if 3:boolean + break-if 3:boolean # $print 1:number 2:number <- multiply 2:number, 1:number 1:number <- subtract 1:number, 1:literal @@ -396,7 +397,7 @@ recipe test-factorial [ recipe main [ break ] -+warn: break wasn't inside {} ++warn: break needs a '{' before -- cgit 1.4.1-2-gfad0