2015-03-17 06:41:01 +00:00
|
|
|
//: Structured programming
|
|
|
|
//:
|
2016-10-22 23:56:07 +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
|
2016-10-22 18:05:18 +00:00
|
|
|
//: instructions 'loop' and 'break' jump to just after the enclosing '{' and
|
|
|
|
//: '}' respectively.
|
2015-03-17 06:41:01 +00:00
|
|
|
//:
|
|
|
|
//: 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)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2015-03-17 06:41:01 +00:00
|
|
|
{
|
|
|
|
break
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- copy 0
|
2015-03-17 06:41:01 +00:00
|
|
|
}
|
|
|
|
]
|
2015-10-29 18:56:10 +00:00
|
|
|
+transform: --- transform braces for recipe main
|
|
|
|
+transform: jump 1:offset
|
|
|
|
+transform: copy ...
|
2015-03-17 06:41:01 +00:00
|
|
|
|
2015-11-29 06:17:47 +00:00
|
|
|
:(before "End Instruction Modifying Transforms")
|
2015-11-07 01:29:52 +00:00
|
|
|
Transform.push_back(transform_braces); // idempotent
|
2015-03-17 06:41:01 +00:00
|
|
|
|
|
|
|
:(code)
|
2015-07-04 16:40:50 +00:00
|
|
|
void transform_braces(const recipe_ordinal r) {
|
2018-08-04 06:08:09 +00:00
|
|
|
const bool OPEN = false, CLOSE = true;
|
2015-05-17 07:52:23 +00:00
|
|
|
// use signed integer for step index because we'll be doing arithmetic on it
|
2018-08-04 06:08:09 +00:00
|
|
|
list<pair<bool/*OPEN/CLOSE*/, /*step*/int> > braces;
|
2019-02-25 08:17:46 +00:00
|
|
|
trace(101, "transform") << "--- transform braces for recipe " << get(Recipe, r).name << end();
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int index = 0; index < SIZE(get(Recipe, r).steps); ++index) {
|
2015-11-06 19:06:58 +00:00
|
|
|
const instruction& inst = get(Recipe, r).steps.at(index);
|
2015-03-17 06:41:01 +00:00
|
|
|
if (inst.label == "{") {
|
2019-02-25 08:17:46 +00:00
|
|
|
trace(103, "transform") << maybe(get(Recipe, r).name) << "push (open, " << index << ")" << end();
|
2018-08-04 06:08:09 +00:00
|
|
|
braces.push_back(pair<bool,int>(OPEN, index));
|
2015-03-17 06:41:01 +00:00
|
|
|
}
|
|
|
|
if (inst.label == "}") {
|
2019-02-25 08:17:46 +00:00
|
|
|
trace(103, "transform") << "push (close, " << index << ")" << end();
|
2018-08-04 06:08:09 +00:00
|
|
|
braces.push_back(pair<bool,int>(CLOSE, index));
|
2015-03-17 06:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-14 03:26:47 +00:00
|
|
|
stack</*step*/int> open_braces;
|
2016-10-20 05:10:35 +00:00
|
|
|
for (int index = 0; index < SIZE(get(Recipe, r).steps); ++index) {
|
2015-11-06 19:06:58 +00:00
|
|
|
instruction& inst = get(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 == "}") {
|
2015-11-05 22:10:29 +00:00
|
|
|
if (open_braces.empty()) {
|
2017-04-04 17:49:13 +00:00
|
|
|
raise << maybe(get(Recipe, r).name) << "unbalanced '}'\n" << end();
|
2015-11-05 22:10:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-12 03:57:08 +00:00
|
|
|
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;
|
2017-02-07 07:44:46 +00:00
|
|
|
if (inst.name != "loop"
|
|
|
|
&& inst.name != "loop-if"
|
|
|
|
&& inst.name != "loop-unless"
|
|
|
|
&& inst.name != "break"
|
|
|
|
&& inst.name != "break-if"
|
|
|
|
&& inst.name != "break-unless") {
|
2019-02-25 08:17:46 +00:00
|
|
|
trace(102, "transform") << inst.name << " ..." << end();
|
2015-07-12 03:57:08 +00:00
|
|
|
continue;
|
2015-03-17 07:26:12 +00:00
|
|
|
}
|
2015-08-25 03:25:37 +00:00
|
|
|
// check for errors
|
2017-02-07 07:44:46 +00:00
|
|
|
if (inst.name.find("-if") != string::npos || inst.name.find("-unless") != string::npos) {
|
2015-08-25 03:25:37 +00:00
|
|
|
if (inst.ingredients.empty()) {
|
2017-04-04 07:18:44 +00:00
|
|
|
raise << maybe(get(Recipe, r).name) << "'" << inst.name << "' expects 1 or 2 ingredients, but got none\n" << end();
|
2015-08-25 03:25:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2015-07-12 03:57:08 +00:00
|
|
|
// update instruction operation
|
2017-02-07 07:44:46 +00:00
|
|
|
string old_name = inst.name; // save a copy
|
|
|
|
if (inst.name.find("-if") != string::npos) {
|
2015-10-30 00:15:09 +00:00
|
|
|
inst.name = "jump-if";
|
2015-11-05 07:44:46 +00:00
|
|
|
inst.operation = JUMP_IF;
|
|
|
|
}
|
2017-02-07 07:44:46 +00:00
|
|
|
else if (inst.name.find("-unless") != string::npos) {
|
2015-10-30 00:15:09 +00:00
|
|
|
inst.name = "jump-unless";
|
2015-11-05 07:44:46 +00:00
|
|
|
inst.operation = JUMP_UNLESS;
|
|
|
|
}
|
|
|
|
else {
|
2015-10-30 00:15:09 +00:00
|
|
|
inst.name = "jump";
|
2015-11-05 07:44:46 +00:00
|
|
|
inst.operation = JUMP;
|
|
|
|
}
|
2015-07-12 03:57:08 +00:00
|
|
|
// check for explicitly provided targets
|
2017-02-07 07:44:46 +00:00
|
|
|
if (inst.name.find("-if") != string::npos || inst.name.find("-unless") != string::npos) {
|
2015-07-12 03:57:08 +00:00
|
|
|
// conditional branches check arg 1
|
2015-06-17 19:39:09 +00:00
|
|
|
if (SIZE(inst.ingredients) > 1 && is_literal(inst.ingredients.at(1))) {
|
2019-02-25 08:17:46 +00:00
|
|
|
trace(102, "transform") << inst.name << ' ' << 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))) {
|
2019-02-25 08:17:46 +00:00
|
|
|
trace(102, "transform") << "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
|
2018-03-14 07:59:41 +00:00
|
|
|
reagent target(new type_tree("offset"));
|
2015-07-12 03:57:08 +00:00
|
|
|
target.set_value(0);
|
|
|
|
if (open_braces.empty())
|
2017-04-04 07:18:44 +00:00
|
|
|
raise << maybe(get(Recipe, r).name) << "'" << old_name << "' needs a '{' before\n" << end();
|
2017-02-07 07:44:46 +00:00
|
|
|
else if (old_name.find("loop") != string::npos)
|
2015-07-12 03:57:08 +00:00
|
|
|
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
|
2015-10-30 00:15:09 +00:00
|
|
|
if (inst.name == "jump")
|
2019-02-25 08:17:46 +00:00
|
|
|
trace(102, "transform") << "jump " << no_scientific(target.value) << ":offset" << end();
|
2015-10-30 00:15:09 +00:00
|
|
|
else
|
2019-02-25 08:17:46 +00:00
|
|
|
trace(102, "transform") << inst.name << ' ' << inst.ingredients.at(0).name << ", " << no_scientific(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
|
2018-08-04 06:08:09 +00:00
|
|
|
int matching_brace(int index, const list<pair<bool, int> >& braces, recipe_ordinal r) {
|
2015-04-17 21:58:00 +00:00
|
|
|
int stacksize = 0;
|
2018-08-04 06:08:09 +00:00
|
|
|
for (list<pair<bool, 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
|
|
|
}
|
2016-02-26 21:04:55 +00:00
|
|
|
raise << maybe(get(Recipe, r).name) << "unbalanced '{'\n" << end();
|
2015-11-06 19:06:58 +00:00
|
|
|
return SIZE(get(Recipe, r).steps); // exit current routine
|
2015-03-17 06:41:01 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario loop)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- copy 0
|
|
|
|
2:num <- copy 0
|
2015-03-17 06:41:01 +00:00
|
|
|
{
|
2016-09-17 07:43:13 +00:00
|
|
|
3:num <- copy 0
|
2015-03-17 06:41:01 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
]
|
2015-10-29 18:56:10 +00:00
|
|
|
+transform: --- transform braces for recipe main
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: jump -2:offset
|
2015-03-17 07:06:48 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_empty_block)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- copy 0
|
2015-03-17 07:06:48 +00:00
|
|
|
{
|
|
|
|
break
|
|
|
|
}
|
|
|
|
]
|
2015-10-29 18:56:10 +00:00
|
|
|
+transform: --- transform braces for recipe main
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: jump 0:offset
|
2015-03-17 07:06:48 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_cascading)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- copy 0
|
2015-03-17 07:06:48 +00:00
|
|
|
{
|
|
|
|
break
|
|
|
|
}
|
|
|
|
{
|
|
|
|
break
|
|
|
|
}
|
|
|
|
]
|
2015-10-29 18:56:10 +00:00
|
|
|
+transform: --- transform braces for recipe main
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: jump 0:offset
|
|
|
|
+transform: jump 0:offset
|
2015-03-17 07:06:48 +00:00
|
|
|
|
2015-08-09 19:26:31 +00:00
|
|
|
:(scenario break_cascading_2)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- copy 0
|
|
|
|
2:num <- copy 0
|
2015-03-17 07:06:48 +00:00
|
|
|
{
|
|
|
|
break
|
2016-09-17 07:43:13 +00:00
|
|
|
3:num <- copy 0
|
2015-03-17 07:06:48 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
break
|
|
|
|
}
|
|
|
|
]
|
2015-10-29 18:56:10 +00:00
|
|
|
+transform: --- transform braces for recipe main
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: jump 1:offset
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: jump 0:offset
|
2015-03-17 07:26:12 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_if)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- copy 0
|
|
|
|
2:num <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
{
|
2016-09-17 07:43:13 +00:00
|
|
|
break-if 2:num
|
|
|
|
3:num <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
break
|
|
|
|
}
|
|
|
|
]
|
2015-10-29 18:56:10 +00:00
|
|
|
+transform: --- transform braces for recipe main
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: jump-if 2, 1:offset
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: jump 0:offset
|
2015-03-17 07:26:12 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_nested)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
{
|
2016-09-17 07:43:13 +00:00
|
|
|
2:num <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
break
|
|
|
|
{
|
2016-09-17 07:43:13 +00:00
|
|
|
3:num <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
}
|
2016-09-17 07:43:13 +00:00
|
|
|
4:num <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
}
|
|
|
|
]
|
2015-10-29 18:56:10 +00:00
|
|
|
+transform: jump 4:offset
|
2015-03-17 07:26:12 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_nested_degenerate)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
{
|
2016-09-17 07:43:13 +00:00
|
|
|
2:num <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
break
|
|
|
|
{
|
|
|
|
}
|
2016-09-17 07:43:13 +00:00
|
|
|
4:num <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
}
|
|
|
|
]
|
2015-10-29 18:56:10 +00:00
|
|
|
+transform: jump 3:offset
|
2015-03-17 07:26:12 +00:00
|
|
|
|
2015-08-09 19:26:31 +00:00
|
|
|
:(scenario break_nested_degenerate_2)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
{
|
2016-09-17 07:43:13 +00:00
|
|
|
2:num <- copy 0
|
2015-03-17 07:26:12 +00:00
|
|
|
break
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2015-10-29 18:56:10 +00:00
|
|
|
+transform: jump 2:offset
|
2015-03-17 07:26:12 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_label)
|
2015-10-07 05:15:45 +00:00
|
|
|
% Hide_errors = true;
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- 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-10-29 18:56:10 +00:00
|
|
|
+transform: jump +foo:offset
|
2015-03-17 20:22:40 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario break_unless)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- copy 0
|
|
|
|
2:num <- copy 0
|
2015-03-17 20:22:40 +00:00
|
|
|
{
|
2016-09-17 07:43:13 +00:00
|
|
|
break-unless 2:num
|
|
|
|
3:num <- copy 0
|
2015-03-17 20:22:40 +00:00
|
|
|
}
|
|
|
|
]
|
2015-10-29 18:56:10 +00:00
|
|
|
+transform: --- transform braces for recipe main
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: jump-unless 2, 1:offset
|
|
|
|
+transform: copy ...
|
2015-03-17 20:22:40 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario loop_unless)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- copy 0
|
|
|
|
2:num <- copy 0
|
2015-03-17 20:22:40 +00:00
|
|
|
{
|
2016-09-17 07:43:13 +00:00
|
|
|
loop-unless 2:num
|
|
|
|
3:num <- copy 0
|
2015-03-17 20:22:40 +00:00
|
|
|
}
|
|
|
|
]
|
2015-10-29 18:56:10 +00:00
|
|
|
+transform: --- transform braces for recipe main
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: jump-unless 2, -1:offset
|
|
|
|
+transform: copy ...
|
2015-03-18 07:01:43 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario loop_nested)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- copy 0
|
2015-03-18 07:01:43 +00:00
|
|
|
{
|
2016-09-17 07:43:13 +00:00
|
|
|
2:num <- copy 0
|
2015-03-18 07:01:43 +00:00
|
|
|
{
|
2016-09-17 07:43:13 +00:00
|
|
|
3:num <- copy 0
|
2015-03-18 07:01:43 +00:00
|
|
|
}
|
2016-09-17 07:46:03 +00:00
|
|
|
loop-if 4:bool
|
2016-09-17 07:43:13 +00:00
|
|
|
5:num <- copy 0
|
2015-03-18 07:01:43 +00:00
|
|
|
}
|
|
|
|
]
|
2015-10-29 18:56:10 +00:00
|
|
|
+transform: --- transform braces for recipe main
|
|
|
|
+transform: jump-if 4, -5:offset
|
2015-03-18 07:01:43 +00:00
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario loop_label)
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- copy 0
|
2015-03-18 07:01:43 +00:00
|
|
|
+foo
|
2016-09-17 07:43:13 +00:00
|
|
|
2:num <- copy 0
|
2015-03-18 07:01:43 +00:00
|
|
|
]
|
2015-10-29 18:56:10 +00:00
|
|
|
+transform: --- transform braces for recipe main
|
|
|
|
+transform: copy ...
|
|
|
|
+transform: copy ...
|
2015-03-18 07:01:43 +00:00
|
|
|
|
|
|
|
//: test how things actually run
|
|
|
|
:(scenarios run)
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario brace_conversion_and_run)
|
2016-03-08 09:30:14 +00:00
|
|
|
def test-factorial [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- copy 5
|
|
|
|
2:num <- copy 1
|
2015-03-18 07:01:43 +00:00
|
|
|
{
|
2016-09-17 07:46:03 +00:00
|
|
|
3:bool <- equal 1:num, 1
|
|
|
|
break-if 3:bool
|
2016-09-17 07:43:13 +00:00
|
|
|
# $print 1:num
|
|
|
|
2:num <- multiply 2:num, 1:num
|
|
|
|
1:num <- subtract 1:num, 1
|
2015-03-18 07:01:43 +00:00
|
|
|
loop
|
|
|
|
}
|
2016-09-17 07:43:13 +00:00
|
|
|
4:num <- copy 2:num # 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
|
|
|
|
2015-10-07 05:15:45 +00:00
|
|
|
:(scenario break_outside_braces_fails)
|
|
|
|
% Hide_errors = true;
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2015-07-12 03:57:08 +00:00
|
|
|
break
|
|
|
|
]
|
2017-04-04 07:18:44 +00:00
|
|
|
+error: main: 'break' needs a '{' before
|
2015-08-25 03:25:37 +00:00
|
|
|
|
2015-10-07 05:15:45 +00:00
|
|
|
:(scenario break_conditional_without_ingredient_fails)
|
|
|
|
% Hide_errors = true;
|
2016-03-08 09:30:14 +00:00
|
|
|
def main [
|
2015-08-25 03:25:37 +00:00
|
|
|
{
|
|
|
|
break-if
|
|
|
|
}
|
|
|
|
]
|
2017-04-04 07:18:44 +00:00
|
|
|
+error: main: 'break-if' expects 1 or 2 ingredients, but got none
|
2015-11-05 07:44:46 +00:00
|
|
|
|
2016-04-27 22:37:09 +00:00
|
|
|
//: Using break we can now implement conditional returns.
|
2016-04-27 22:23:44 +00:00
|
|
|
|
2016-04-27 22:37:09 +00:00
|
|
|
:(scenario return_if)
|
2016-04-27 22:23:44 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- test1
|
2016-04-27 22:23:44 +00:00
|
|
|
]
|
|
|
|
def test1 [
|
|
|
|
return-if 0, 34
|
|
|
|
return 35
|
|
|
|
]
|
|
|
|
+mem: storing 35 in location 1
|
|
|
|
|
2016-04-27 22:37:09 +00:00
|
|
|
:(scenario return_if_2)
|
2016-04-27 22:23:44 +00:00
|
|
|
def main [
|
2016-09-17 07:43:13 +00:00
|
|
|
1:num <- test1
|
2016-04-27 22:23:44 +00:00
|
|
|
]
|
|
|
|
def test1 [
|
|
|
|
return-if 1, 34
|
|
|
|
return 35
|
|
|
|
]
|
|
|
|
+mem: storing 34 in location 1
|
|
|
|
|
|
|
|
:(before "End Rewrite Instruction(curr, recipe result)")
|
2017-09-01 08:50:10 +00:00
|
|
|
// rewrite 'return-if a, b, c, ...' to
|
2016-04-27 22:23:44 +00:00
|
|
|
// ```
|
|
|
|
// {
|
|
|
|
// break-unless a
|
2016-04-27 22:37:09 +00:00
|
|
|
// return b, c, ...
|
2016-04-27 22:23:44 +00:00
|
|
|
// }
|
|
|
|
// ```
|
2016-04-27 22:37:09 +00:00
|
|
|
if (curr.name == "return-if" || curr.name == "reply-if") {
|
2016-04-27 22:23:44 +00:00
|
|
|
if (curr.products.empty()) {
|
2016-11-10 18:34:16 +00:00
|
|
|
emit_return_block(result, "break-unless", curr);
|
2016-04-27 22:23:44 +00:00
|
|
|
curr.clear();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
raise << "'" << curr.name << "' never yields any products\n" << end();
|
|
|
|
}
|
|
|
|
}
|
2017-09-01 08:50:10 +00:00
|
|
|
// rewrite 'return-unless a, b, c, ...' to
|
2016-04-27 22:23:44 +00:00
|
|
|
// ```
|
|
|
|
// {
|
|
|
|
// break-if a
|
2016-04-27 22:37:09 +00:00
|
|
|
// return b, c, ...
|
2016-04-27 22:23:44 +00:00
|
|
|
// }
|
|
|
|
// ```
|
2016-04-27 22:37:09 +00:00
|
|
|
if (curr.name == "return-unless" || curr.name == "reply-unless") {
|
2016-04-27 22:23:44 +00:00
|
|
|
if (curr.products.empty()) {
|
2016-11-10 18:34:16 +00:00
|
|
|
emit_return_block(result, "break-if", curr);
|
2016-04-27 22:23:44 +00:00
|
|
|
curr.clear();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
raise << "'" << curr.name << "' never yields any products\n" << end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:(code)
|
2016-11-10 18:34:16 +00:00
|
|
|
void emit_return_block(recipe& out, const string& break_command, const instruction& inst) {
|
|
|
|
const vector<reagent>& ingredients = inst.ingredients;
|
2016-05-06 07:46:39 +00:00
|
|
|
reagent/*copy*/ condition = ingredients.at(0);
|
2016-04-27 22:37:09 +00:00
|
|
|
vector<reagent> return_ingredients;
|
|
|
|
copy(++ingredients.begin(), ingredients.end(), inserter(return_ingredients, return_ingredients.end()));
|
2016-04-27 22:23:44 +00:00
|
|
|
|
|
|
|
// {
|
|
|
|
instruction open_label; open_label.is_label=true; open_label.label = "{";
|
|
|
|
out.steps.push_back(open_label);
|
|
|
|
|
|
|
|
// <break command> <condition>
|
|
|
|
instruction break_inst;
|
|
|
|
break_inst.operation = get(Recipe_ordinal, break_command);
|
2017-02-07 07:44:46 +00:00
|
|
|
break_inst.name = break_command;
|
2016-04-27 22:23:44 +00:00
|
|
|
break_inst.ingredients.push_back(condition);
|
|
|
|
out.steps.push_back(break_inst);
|
|
|
|
|
2016-04-27 22:37:09 +00:00
|
|
|
// return <return ingredients>
|
|
|
|
instruction return_inst;
|
|
|
|
return_inst.operation = get(Recipe_ordinal, "return");
|
|
|
|
return_inst.name = "return";
|
|
|
|
return_inst.ingredients.swap(return_ingredients);
|
2016-11-10 18:34:16 +00:00
|
|
|
return_inst.original_string = inst.original_string;
|
2016-04-27 22:37:09 +00:00
|
|
|
out.steps.push_back(return_inst);
|
2016-04-27 22:23:44 +00:00
|
|
|
|
|
|
|
// }
|
|
|
|
instruction close_label; close_label.is_label=true; close_label.label = "}";
|
|
|
|
out.steps.push_back(close_label);
|
|
|
|
}
|
|
|
|
|
2015-11-05 07:44:46 +00:00
|
|
|
//: Make sure these pseudo recipes get consistent numbers in all tests, even
|
|
|
|
//: though they aren't implemented. Allows greater flexibility in ordering
|
|
|
|
//: transforms.
|
|
|
|
|
|
|
|
:(before "End Primitive Recipe Declarations")
|
|
|
|
BREAK,
|
|
|
|
BREAK_IF,
|
|
|
|
BREAK_UNLESS,
|
|
|
|
LOOP,
|
|
|
|
LOOP_IF,
|
|
|
|
LOOP_UNLESS,
|
|
|
|
:(before "End Primitive Recipe Numbers")
|
2015-11-06 19:06:58 +00:00
|
|
|
put(Recipe_ordinal, "break", BREAK);
|
|
|
|
put(Recipe_ordinal, "break-if", BREAK_IF);
|
|
|
|
put(Recipe_ordinal, "break-unless", BREAK_UNLESS);
|
|
|
|
put(Recipe_ordinal, "loop", LOOP);
|
|
|
|
put(Recipe_ordinal, "loop-if", LOOP_IF);
|
|
|
|
put(Recipe_ordinal, "loop-unless", LOOP_UNLESS);
|
2015-11-05 07:44:46 +00:00
|
|
|
:(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;
|