2015-03-17 06:41:01 +00:00
|
|
|
//: Structured programming
|
|
|
|
//:
|
2015-04-17 17:31:17 +00:00
|
|
|
//: Our jump recipes are quite inconvenient to use, so mu provides a
|
2015-03-17 06:41:01 +00:00
|
|
|
//: lightweight tool called 'transform_braces' to work in a slightly more
|
|
|
|
//: convenient format with nested braces:
|
|
|
|
//:
|
|
|
|
//: {
|
|
|
|
//: some instructions
|
|
|
|
//: {
|
|
|
|
//: more instructions
|
|
|
|
//: }
|
|
|
|
//: }
|
|
|
|
//:
|
2015-04-08 06:48:22 +00:00
|
|
|
//: Braces are just labels, they require no special parsing. The pseudo
|
|
|
|
//: recipes 'loop' and 'break' jump to just after the enclosing '{' and '}'
|
2015-03-17 06:41:01 +00:00
|
|
|
//: 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.
|
|
|
|
|
2015-04-24 07:28:24 +00:00
|
|
|
:(scenarios transform)
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario brace_conversion)
|
2015-03-17 06:41:01 +00:00
|
|
|
recipe main [
|
|
|
|
{
|
|
|
|
break
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 0
|
2015-03-17 06:41:01 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
+after-brace: recipe main
|
|
|
|
+after-brace: jump 1:offset
|
|
|
|
+after-brace: copy ...
|
|
|
|
|
|
|
|
//: one-time setup
|
|
|
|
:(after "int main")
|
2015-04-08 18:17:00 +00:00
|
|
|
Transform.push_back(transform_braces);
|
2015-03-17 06:41:01 +00:00
|
|
|
|
|
|
|
:(code)
|
2015-07-04 16:40:50 +00:00
|
|
|
void transform_braces(const recipe_ordinal r) {
|
2015-04-08 06:48:22 +00:00
|
|
|
//? cout << "AAA transform_braces\n"; //? 1
|
|
|
|
//? exit(0); //? 1
|
2015-03-17 06:41:01 +00:00
|
|
|
const int OPEN = 0, CLOSE = 1;
|
2015-05-17 07:52:23 +00:00
|
|
|
// use signed integer for step index because we'll be doing arithmetic on it
|
|
|
|
list<pair<int/*OPEN/CLOSE*/, /*step*/long long int> > braces;
|
2015-05-17 09:22:41 +00:00
|
|
|
for (long long int index = 0; index < SIZE(Recipe[r].steps); ++index) {
|
2015-05-07 22:49:40 +00:00
|
|
|
const instruction& inst = Recipe[r].steps.at(index);
|
2015-03-17 06:41:01 +00:00
|
|
|
if (inst.label == "{") {
|
2015-07-25 07:02:20 +00:00
|
|
|
trace("brace") << r << ": push (open, " << index << ")" << end();
|
2015-05-17 07:52:23 +00:00
|
|
|
braces.push_back(pair<int,long long int>(OPEN, index));
|
2015-03-17 06:41:01 +00:00
|
|
|
}
|
|
|
|
if (inst.label == "}") {
|
2015-07-25 07:02:20 +00:00
|
|
|
trace("brace") << "push (close, " << index << ")" << end();
|
2015-05-17 07:52:23 +00:00
|
|
|
braces.push_back(pair<int,long long int>(CLOSE, index));
|
2015-03-17 06:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-17 07:52:23 +00:00
|
|
|
stack</*step*/long long int> open_braces;
|
2015-07-25 07:02:20 +00:00
|
|
|
trace("after-brace") << "recipe " << Recipe[r].name << end();
|
2015-05-17 09:22:41 +00:00
|
|
|
for (long long int index = 0; index < SIZE(Recipe[r].steps); ++index) {
|
2015-05-07 22:49:40 +00:00
|
|
|
instruction& inst = Recipe[r].steps.at(index);
|
2015-07-12 03:57:08 +00:00
|
|
|
if (inst.label == "{") {
|
|
|
|
open_braces.push(index);
|
|
|
|
continue;
|
2015-03-17 06:41:01 +00:00
|
|
|
}
|
2015-07-12 03:57:08 +00:00
|
|
|
if (inst.label == "}") {
|
|
|
|
open_braces.pop();
|
|
|
|
continue;
|
2015-03-17 06:41:01 +00:00
|
|
|
}
|
2015-07-12 03:57:08 +00:00
|
|
|
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"]) {
|
2015-07-25 07:02:20 +00:00
|
|
|
trace("after-brace") << inst.name << " ..." << end();
|
2015-07-12 03:57:08 +00:00
|
|
|
continue;
|
2015-03-17 07:26:12 +00:00
|
|
|
}
|
2015-07-12 03:57:08 +00:00
|
|
|
// update instruction operation
|
|
|
|
if (inst.name.find("-if") != string::npos)
|
2015-07-04 16:40:50 +00:00
|
|
|
inst.operation = Recipe_ordinal["jump-if"];
|
2015-07-12 03:57:08 +00:00
|
|
|
else if (inst.name.find("-unless") != string::npos)
|
2015-07-04 16:40:50 +00:00
|
|
|
inst.operation = Recipe_ordinal["jump-unless"];
|
2015-07-12 03:57:08 +00:00
|
|
|
else
|
|
|
|
inst.operation = Recipe_ordinal["jump"];
|
|
|
|
// check for explicitly provided targets
|
|
|
|
if (inst.name.find("-if") != string::npos || inst.name.find("-unless") != string::npos) {
|
|
|
|
// conditional branches check arg 1
|
2015-06-17 19:39:09 +00:00
|
|
|
if (SIZE(inst.ingredients) > 1 && is_literal(inst.ingredients.at(1))) {
|
2015-07-25 07:02:20 +00:00
|
|
|
trace("after-brace") << "jump " << inst.ingredients.at(1).name << ":offset" << end();
|
2015-07-12 03:57:08 +00:00
|
|
|
continue;
|
2015-03-17 20:22:40 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-17 06:41:01 +00:00
|
|
|
else {
|
2015-07-12 03:57:08 +00:00
|
|
|
// unconditional branches check arg 0
|
|
|
|
if (!inst.ingredients.empty() && is_literal(inst.ingredients.at(0))) {
|
2015-07-25 07:02:20 +00:00
|
|
|
trace("after-brace") << "jump " << inst.ingredients.at(0).name << ":offset" << end();
|
2015-07-12 03:57:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-03-17 06:41:01 +00:00
|
|
|
}
|
2015-07-12 03:57:08 +00:00
|
|
|
// if implicit, compute target
|
|
|
|
reagent target;
|
|
|
|
target.types.push_back(Type_ordinal["offset"]);
|
|
|
|
target.set_value(0);
|
|
|
|
if (open_braces.empty())
|
2015-07-25 21:19:16 +00:00
|
|
|
raise << inst.name << " needs a '{' before\n" << end();
|
2015-07-12 03:57:08 +00:00
|
|
|
else if (inst.name.find("loop") != string::npos)
|
|
|
|
target.set_value(open_braces.top()-index);
|
|
|
|
else // break instruction
|
2015-07-25 01:30:02 +00:00
|
|
|
target.set_value(matching_brace(open_braces.top(), braces, r) - index - 1);
|
2015-07-12 03:57:08 +00:00
|
|
|
inst.ingredients.push_back(target);
|
|
|
|
// log computed target
|
|
|
|
if (inst.name.find("-if") != string::npos)
|
2015-07-25 07:02:20 +00:00
|
|
|
trace("after-brace") << "jump-if " << inst.ingredients.at(0).name << ", " << target.value << ":offset" << end();
|
2015-07-12 03:57:08 +00:00
|
|
|
else if (inst.name.find("-unless") != string::npos)
|
2015-07-25 07:02:20 +00:00
|
|
|
trace("after-brace") << "jump-unless " << inst.ingredients.at(0).name << ", " << target.value << ":offset" << end();
|
2015-07-12 03:57:08 +00:00
|
|
|
else
|
2015-07-25 07:02:20 +00:00
|
|
|
trace("after-brace") << "jump " << target.value << ":offset" << end();
|
2015-03-17 06:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-17 07:52:23 +00:00
|
|
|
// returns a signed integer not just so that we can return -1 but also to
|
|
|
|
// enable future signed arithmetic
|
2015-07-25 01:30:02 +00:00
|
|
|
long long int matching_brace(long long int index, const list<pair<int, long long int> >& braces, recipe_ordinal r) {
|
2015-04-17 21:58:00 +00:00
|
|
|
int stacksize = 0;
|
2015-05-17 07:52:23 +00:00
|
|
|
for (list<pair<int, long long int> >::const_iterator p = braces.begin(); p != braces.end(); ++p) {
|
2015-03-17 07:03:33 +00:00
|
|
|
if (p->second < index) continue;
|
2015-03-17 06:41:01 +00:00
|
|
|
stacksize += (p->first ? 1 : -1);
|
2015-03-17 07:03:33 +00:00
|
|
|
if (stacksize == 0) return p->second;
|
2015-03-17 06:41:01 +00:00
|
|
|
}
|
2015-07-25 07:02:20 +00:00
|
|
|
raise << Recipe[r].name << ": unbalanced '{'\n" << end();
|
2015-07-25 01:30:02 +00:00
|
|
|
return SIZE(Recipe[r].steps); // exit current routine
|
2015-03-17 06:41:01 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 07:01:43 +00:00
|
|
|
// temporarily suppress run
|
2015-04-24 07:28:24 +00:00
|
|
|
void transform(string form) {
|
|
|
|
load(form);
|
2015-03-18 07:01:43 +00:00
|
|
|
transform_all();
|
|
|
|
}
|
|
|
|
|
2015-04-17 17:31:17 +00:00
|
|
|
//: Make sure these pseudo recipes get consistent numbers in all tests, even
|
|
|
|
//: though they aren't implemented.
|
|
|
|
|
|
|
|
:(before "End Primitive Recipe Declarations")
|
|
|
|
BREAK,
|
|
|
|
BREAK_IF,
|
|
|
|
BREAK_UNLESS,
|
|
|
|
LOOP,
|
|
|
|
LOOP_IF,
|
|
|
|
LOOP_UNLESS,
|
2015-04-08 06:48:22 +00:00
|
|
|
:(before "End Primitive Recipe Numbers")
|
2015-07-04 16:40:50 +00:00
|
|
|
Recipe_ordinal["break"] = BREAK;
|
|
|
|
Recipe_ordinal["break-if"] = BREAK_IF;
|
|
|
|
Recipe_ordinal["break-unless"] = BREAK_UNLESS;
|
|
|
|
Recipe_ordinal["loop"] = LOOP;
|
|
|
|
Recipe_ordinal["loop-if"] = LOOP_IF;
|
|
|
|
Recipe_ordinal["loop-unless"] = LOOP_UNLESS;
|
2015-04-08 06:48:22 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario loop)
|
2015-03-17 06:41:01 +00:00
|
|
|
recipe main [
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 0
|
|
|
|
2:number <- copy 0
|
2015-03-17 06:41:01 +00:00
|
|
|
{
|
2015-07-28 21:33:22 +00:00
|
|
|
3:number <- copy 0
|
2015-03-17 06:41:01 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
]
|
|
|
|
+after-brace: recipe main
|
|
|
|
+after-brace: copy ...
|
|
|
|
+after-brace: copy ...
|
|
|
|
+after-brace: copy ...
|
|
|
|
+after-brace: jump -2:offset
|
2015-03-17 07:06:48 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_empty_block)
|
2015-03-17 07:06:48 +00:00
|
|
|
recipe main [
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 0
|
2015-03-17 07:06:48 +00:00
|
|
|
{
|
|
|
|
break
|
|
|
|
}
|
|
|
|
]
|
|
|
|
+after-brace: recipe main
|
|
|
|
+after-brace: copy ...
|
|
|
|
+after-brace: jump 0:offset
|
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_cascading)
|
2015-03-17 07:06:48 +00:00
|
|
|
recipe main [
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 0
|
2015-03-17 07:06:48 +00:00
|
|
|
{
|
|
|
|
break
|
|
|
|
}
|
|
|
|
{
|
|
|
|
break
|
|
|
|
}
|
|
|
|
]
|
|
|
|
+after-brace: recipe main
|
|
|
|
+after-brace: copy ...
|
|
|
|
+after-brace: jump 0:offset
|
|
|
|
+after-brace: jump 0:offset
|
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_cascading2)
|
2015-03-17 07:06:48 +00:00
|
|
|
recipe main [
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 0
|
|
|
|
2:number <- copy 0
|
2015-03-17 07:06:48 +00:00
|
|
|
{
|
|
|
|
break
|
2015-07-28 21:33:22 +00:00
|
|
|
3:number <- copy 0
|
2015-03-17 07:06:48 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
break
|
|
|
|
}
|
|
|
|
]
|
|
|
|
+after-brace: recipe main
|
|
|
|
+after-brace: copy ...
|
|
|
|
+after-brace: copy ...
|
|
|
|
+after-brace: jump 1:offset
|
|
|
|
+after-brace: copy ...
|
|
|
|
+after-brace: jump 0:offset
|
2015-03-17 07:26:12 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_if)
|
2015-03-17 07:26:12 +00:00
|
|
|
recipe main [
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 0
|
|
|
|
2:number <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
{
|
2015-05-13 17:03:26 +00:00
|
|
|
break-if 2:number
|
2015-07-28 21:33:22 +00:00
|
|
|
3:number <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_nested)
|
2015-03-17 07:26:12 +00:00
|
|
|
recipe main [
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
{
|
2015-07-28 21:33:22 +00:00
|
|
|
2:number <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
break
|
|
|
|
{
|
2015-07-28 21:33:22 +00:00
|
|
|
3:number <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
}
|
2015-07-28 21:33:22 +00:00
|
|
|
4:number <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
+after-brace: jump 4:offset
|
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_nested_degenerate)
|
2015-03-17 07:26:12 +00:00
|
|
|
recipe main [
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
{
|
2015-07-28 21:33:22 +00:00
|
|
|
2:number <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
break
|
|
|
|
{
|
|
|
|
}
|
2015-07-28 21:33:22 +00:00
|
|
|
4:number <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
+after-brace: jump 3:offset
|
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_nested_degenerate2)
|
2015-03-17 07:26:12 +00:00
|
|
|
recipe main [
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
{
|
2015-07-28 21:33:22 +00:00
|
|
|
2:number <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
break
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
+after-brace: jump 2:offset
|
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_label)
|
2015-05-10 15:34:12 +00:00
|
|
|
% Hide_warnings = true;
|
2015-03-17 07:26:12 +00:00
|
|
|
recipe main [
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
{
|
2015-03-17 18:23:29 +00:00
|
|
|
break +foo:offset
|
2015-03-17 07:26:12 +00:00
|
|
|
}
|
|
|
|
]
|
2015-03-17 18:23:29 +00:00
|
|
|
+after-brace: jump +foo:offset
|
2015-03-17 20:22:40 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_unless)
|
2015-03-17 20:22:40 +00:00
|
|
|
recipe main [
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 0
|
|
|
|
2:number <- copy 0
|
2015-03-17 20:22:40 +00:00
|
|
|
{
|
2015-05-13 17:03:26 +00:00
|
|
|
break-unless 2:number
|
2015-07-28 21:33:22 +00:00
|
|
|
3:number <- copy 0
|
2015-03-17 20:22:40 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
+after-brace: recipe main
|
|
|
|
+after-brace: copy ...
|
|
|
|
+after-brace: copy ...
|
|
|
|
+after-brace: jump-unless 2, 1:offset
|
|
|
|
+after-brace: copy ...
|
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario loop_unless)
|
2015-03-17 20:22:40 +00:00
|
|
|
recipe main [
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 0
|
|
|
|
2:number <- copy 0
|
2015-03-17 20:22:40 +00:00
|
|
|
{
|
2015-05-13 17:03:26 +00:00
|
|
|
loop-unless 2:number
|
2015-07-28 21:33:22 +00:00
|
|
|
3:number <- copy 0
|
2015-03-17 20:22:40 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
+after-brace: recipe main
|
|
|
|
+after-brace: copy ...
|
|
|
|
+after-brace: copy ...
|
|
|
|
+after-brace: jump-unless 2, -1:offset
|
|
|
|
+after-brace: copy ...
|
2015-03-18 07:01:43 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario loop_nested)
|
2015-03-18 07:01:43 +00:00
|
|
|
recipe main [
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 0
|
2015-03-18 07:01:43 +00:00
|
|
|
{
|
2015-07-28 21:33:22 +00:00
|
|
|
2:number <- copy 0
|
2015-03-18 07:01:43 +00:00
|
|
|
{
|
2015-07-28 21:33:22 +00:00
|
|
|
3:number <- copy 0
|
2015-03-18 07:01:43 +00:00
|
|
|
}
|
|
|
|
loop-if 4:boolean
|
2015-07-28 21:33:22 +00:00
|
|
|
5:number <- copy 0
|
2015-03-18 07:01:43 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
+after-brace: recipe main
|
|
|
|
+after-brace: jump-if 4, -5:offset
|
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario loop_label)
|
2015-03-18 07:01:43 +00:00
|
|
|
recipe main [
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 0
|
2015-03-18 07:01:43 +00:00
|
|
|
+foo
|
2015-07-28 21:33:22 +00:00
|
|
|
2:number <- copy 0
|
2015-03-18 07:01:43 +00:00
|
|
|
]
|
|
|
|
+after-brace: recipe main
|
|
|
|
+after-brace: copy ...
|
|
|
|
+after-brace: copy ...
|
|
|
|
|
|
|
|
//: test how things actually run
|
|
|
|
:(scenarios run)
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario brace_conversion_and_run)
|
2015-05-07 22:06:53 +00:00
|
|
|
#? % Trace_stream->dump_layer = "run";
|
2015-04-22 18:39:29 +00:00
|
|
|
recipe test-factorial [
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- copy 5
|
|
|
|
2:number <- copy 1
|
2015-03-18 07:01:43 +00:00
|
|
|
{
|
2015-07-28 21:33:22 +00:00
|
|
|
3:boolean <- equal 1:number, 1
|
2015-03-18 07:01:43 +00:00
|
|
|
break-if 3:boolean
|
2015-05-13 17:03:26 +00:00
|
|
|
# $print 1:number
|
|
|
|
2:number <- multiply 2:number, 1:number
|
2015-07-28 21:33:22 +00:00
|
|
|
1:number <- subtract 1:number, 1
|
2015-03-18 07:01:43 +00:00
|
|
|
loop
|
|
|
|
}
|
2015-05-13 17:03:26 +00:00
|
|
|
4:number <- copy 2:number # trigger a read
|
2015-03-18 07:01:43 +00:00
|
|
|
]
|
2015-05-13 00:00:56 +00:00
|
|
|
+mem: location 2 is 120
|
2015-07-12 03:57:08 +00:00
|
|
|
|
|
|
|
:(scenario break_outside_braces_warns)
|
|
|
|
% Hide_warnings = true;
|
|
|
|
recipe main [
|
|
|
|
break
|
|
|
|
]
|
2015-07-25 21:19:16 +00:00
|
|
|
+warn: break needs a '{' before
|