//: Structured programming //: //: Our jump recipes are quite inconvenient to use, so Mu provides a //: lightweight tool called 'transform_braces' to work in a slightly more //: convenient format with nested braces: //: //: { //: some instructions //: { //: more instructions //: } //: } //: //: Braces are just labels, they require no special parsing. The pseudo //: instructions 'loop' and 'break' jump to just after the enclosing '{' and //: '}' respectively. //: //: Conditional and unconditional 'loop' and 'break' should give us 80% of the //: benefits of the control-flow primitives we're used to in other languages, //: like 'if', 'while', 'for', etc. :(scenarios transform) :(scenario brace_conversion) def main [ { break 1:num <- copy 0 } ] +transform: --- transform braces for recipe main +transform: jump 1:offset +transform: copy ... :(before "End Instruction Modifying Transforms") Transform.push_back(transform_braces); // idempotent :(code) 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 > braces; trace(9991, "transform") << "--- transform braces for recipe " << get(Recipe, r).name << end(); for (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(OPEN, index)); } if (inst.label == "}") { trace(9993, "transform") << "push (close, " << index << ")" << end(); braces.push_back(pair(CLOSE, index)); } } stack open_braces; for (int index = 0; index < SIZE(get(Recipe, r).steps); ++index) { instruction& inst = get(Recipe, r).steps.at(index); if (inst.label == "{") { open_braces.push(index); continue; } if (inst.label == "}") { if (open_braces.empty()) { raise << maybe(get(Recipe, r).name) << "unbalanced '}'\n" << end(); return; } open_braces.pop(); continue; } if (inst.is_label) continue; if (inst.name != "loop" && inst.name != "loop-if" && inst.name != "loop-unless" && inst.name != "break" && inst.name != "break-if" && inst.name != "break-unless") { trace(9992, "transform") << inst.name << " ..." << end(); continue; } // check for errors if (inst.name.find("-if") != string::npos || inst.name.find("-unless") != string::npos) { if (inst.ingredients.empty()) { raise << maybe(get(Recipe, r).name) << "'" << inst.name << "' expects 1 or 2 ingredients, but got none\n" << end(); continue; } } // update instruction operation string old_name = inst.name; // save a copy if (inst.name.find("-if") != string::npos) { inst.name = "jump-if"; inst.operation = JUMP_IF; } else if (inst.name.find("-unless") != string::npos) { inst.name = "jump-unless"; inst.operation = JUMP_UNLESS; } else { inst.name = "jump"; inst.operation = JUMP; } // check for explicitly provided targets 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(9992, "transform") << inst.name << ' ' << inst.ingredients.at(1).name << ":offset" << end(); continue; } } else { // unconditional branches check arg 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(new type_tree("offset")); target.set_value(0); if (open_braces.empty()) raise << maybe(get(Recipe, r).name) << "'" << old_name << "' needs a '{' before\n" << end(); else if (old_name.find("loop") != string::npos) target.set_value(open_braces.top()-index); 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") trace(9992, "transform") << "jump " << no_scientific(target.value) << ":offset" << end(); 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 int matching_brace(int index, const list >& braces, recipe_ordinal r) { int stacksize = 0; for (list >::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; } raise << maybe(get(Recipe, r).name) << "unbalanced '{'\n" << end(); return SIZE(get(Recipe, r).steps); // exit current routine } :(scenario loop) def main [ 1:num <- copy 0 2:num <- copy 0 { 3:num <- copy 0 loop } ] +transform: --- transform braces for recipe main +transform: copy ... +transform: copy ... +transform: copy ... +transform: jump -2:offset :(scenario break_empty_block) def main [ 1:num <- copy 0 { break } ] +transform: --- transform braces for recipe main +transform: copy ... +transform: jump 0:offset :(scenario break_cascading) def main [ 1:num <- copy 0 { break } { break } ] +transform: --- transform braces for recipe main +transform: copy ... +transform: jump 0:offset +transform: jump 0:offset :(scenario break_cascading_2) def main [ 1:num <- copy 0 2:num <- copy 0 { break 3:num <- copy 0 } { break } ] +transform: --- transform braces for recipe main +transform: copy ... +transform: copy ... +transform: jump 1:offset +transform: copy ... +transform: jump 0:offset :(scenario break_if) def main [ 1:num <- copy 0 2:num <- copy 0 { break-if 2:num 3:num <- copy 0 } { break } ] +transform: --- transform braces for recipe main +transform: copy ... +transform: copy ... +transform: jump-if 2, 1:offset +transform: copy ... +transform: jump 0:offset :(scenario break_nested) def main [ 1:num <- copy 0 { 2:num <- copy 0 break { 3:num <- copy 0 } 4:num <- copy 0 } ] +transform: jump 4:offset :(scenario break_nested_degenerate) def main [ 1:num <- copy 0 { 2:num <- copy 0 break { } 4:num <- copy 0 } ] +transform: jump 3:offset :(scenario break_nested_degenerate_2) def main [ 1:num <- copy 0 { 2:num <- copy 0 break { } } ] +transform: jump 2:offset :(scenario break_label) % Hide_errors = true; def main [ 1:num <- copy 0 { break +foo:offset } ] +transform: jump +foo:offset :(scenario break_unless) def main [ 1:num <- copy 0 2:num <- copy 0 { break-unless 2:num 3:num <- copy 0 } ] +transform: --- transform braces for recipe main +transform: copy ... +transform: copy ... +transform: jump-unless 2, 1:offset +transform: copy ... :(scenario loop_unless) def main [ 1:num <- copy 0 2:num <- copy 0 { loop-unless 2:num 3:num <- copy 0 } ] +transform: --- transform braces for recipe main +transform: copy ... +transform: copy ... +transform: jump-unless 2, -1:offset +transform: copy ... :(scenario loop_nested) def main [ 1:num <- copy 0 { 2:num <- copy 0 { 3:num <- copy 0 } loop-if 4:bool 5:num <- copy 0 } ] +transform: --- transform braces for recipe main +transform: jump-if 4, -5:offset :(scenario loop_label) def main [ 1:num <- copy 0 +foo 2:num <- copy 0 ] +transform: --- transform braces for recipe main +transform: copy ... +transform: copy ... //: test how things actually run :(scenarios run) :(scenario brace_conversion_and_run) def test-factorial [ 1:num <- copy 5 2:num <- copy 1 { 3:bool <- equal 1:num, 1 break-if 3:bool # $print 1:num 2:num <- multiply 2:num, 1:num 1:num <- subtract