//: 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 //: recipes '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(); //? cerr << "--- transform braces for recipe " << get(Recipe, r).name << '\n'; 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 << "missing '{' in '" << get(Recipe, r).name << "'\n" << end(); return; } open_braces.pop(); continue; } if (inst.is_label) continue; if (inst.old_name != "loop" && inst.old_name != "loop-if" && inst.old_name != "loop-unless" && inst.old_name != "break" && inst.old_name != "break-if" && inst.old_name != "break-unless") { trace(9992, "transform") << inst.old_name << " ..." << end(); continue; } // check for errors 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) { inst.name = "jump-if"; inst.operation = JUMP_IF; } else if (inst.old_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.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))) { 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; target.type = new type_tree("offset"); target.set_value(0); if (open_brac
(selective-load "mu.arc" section-level)
(set allow-raw-addresses*)

(section 100

(reset)
(new-trace "new-screen")
(add-code:readfile "edit.mu")
(add-code
  '((function test-new-screen [
      (1:screen-address/global <- new-screen 5:literal 5:literal)
     ])))
;? (each stmt function*!new-screen
;?   (prn stmt))
(let routine make-routine!test-new-screen
  (let before rep.routine!alloc
;?     (= dump-trace* (obj blacklist '("sz" "m" "setm" "addr&qu