about summary refs log tree commit diff stats
path: root/047jump_label.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-05-10 08:34:12 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-05-10 08:55:18 -0700
commitdc1323e936fb79823767f85529be15e0456b3169 (patch)
treea3313c1c41ffd67a506f92dabc77c7867a8c8dd9 /047jump_label.cc
parent3f367cb9466131f95c11c24df3aac7057143587b (diff)
downloadmu-dc1323e936fb79823767f85529be15e0456b3169.tar.gz
1323 - keyboard supports backspace and newline
Lots mixed into this commit:
  some off-by-one errors in display.cc
  a new transform to translate jump labels that I'd somehow never gotten around to supporting
Diffstat (limited to '047jump_label.cc')
-rw-r--r--047jump_label.cc93
1 files changed, 93 insertions, 0 deletions
diff --git a/047jump_label.cc b/047jump_label.cc
new file mode 100644
index 00000000..9bc0c442
--- /dev/null
+++ b/047jump_label.cc
@@ -0,0 +1,93 @@
+//: Support jumps to labels.
+//: We'll also treat 'break' and 'continue' as jumps. The choice of name is
+//: just documentation about intent.
+
+:(scenario jump_to_label)
+recipe main [
+  jump +target:offset
+  1:integer <- copy 0:literal
+  +target
+]
+-mem: storing 0 in location 1
+
+:(after "int main")
+  Transform.push_back(transform_labels);
+
+:(code)
+void transform_labels(const recipe_number r) {
+  map<string, index_t> offset;
+  for (index_t i = 0; i < Recipe[r].steps.size(); ++i) {
+    const instruction& inst = Recipe[r].steps.at(i);
+    if (!inst.label.empty()) offset[inst.label] = i;
+  }
+  for (index_t i = 0; i < Recipe[r].steps.size(); ++i) {
+    instruction& inst = Recipe[r].steps.at(i);
+    if (inst.operation == Recipe_number["jump"]) {
+//?       cerr << inst.to_string() << '\n'; //? 1
+      replace_offset(inst.ingredients.at(0), offset, r);
+    }
+    if (inst.operation == Recipe_number["jump-if"] || inst.operation == Recipe_number["jump-unless"]) {
+      replace_offset(inst.ingredients.at(1), offset, r);
+    }
+    if ((inst.operation == Recipe_number["loop"] || inst.operation == Recipe_number["break"])
+        && inst.ingredients.size() == 1) {
+      replace_offset(inst.ingredients.at(0), offset, r);
+    }
+    if ((inst.operation == Recipe_number["loop-if"] || inst.operation == Recipe_number["loop-unless"]
+            || inst.operation == Recipe_number["break-if"] || inst.operation == Recipe_number["break-unless"])
+        && inst.ingredients.size() == 2) {
+      replace_offset(inst.ingredients.at(1), offset, r);
+    }
+  }
+}
+
+:(code)
+void replace_offset(reagent& x, /*const*/ map<string, index_t>& offset, const recipe_number r) {
+//?   cerr << "AAA " << x.to_string() << '\n'; //? 1
+  assert(isa_literal(x));
+//?   cerr << "BBB " << x.to_string() << '\n'; //? 1
+  assert(!x.initialized);
+//?   cerr << "CCC " << x.to_string() << '\n'; //? 1
+  if (is_number(x.name)) return;  // non-labels will be handled like other integer operands
+//?   cerr << "DDD " << x.to_string() << '\n'; //? 1
+  if (offset.find(x.name) == offset.end())
+    raise << "can't find label " << x.name << " in routine " << Recipe[r].name << '\n';
+  x.set_value(offset[x.name]);
+}
+
+:(scenario break_to_label)
+recipe main [
+#?   $print [aaa]
+  {
+    {
+      break +target:offset
+      1:integer <- copy 0:literal
+    }
+  }
+  +target
+]
+-mem: storing 0 in location 1
+
+:(scenario jump_if_to_label)
+recipe main [
+  {
+    {
+      jump-if 1:literal, +target:offset
+      1:integer <- copy 0:literal
+    }
+  }
+  +target
+]
+-mem: storing 0 in location 1
+
+:(scenario loop_unless_to_label)
+recipe main [
+  {
+    {
+      loop-unless 0:literal, +target:offset  # loop/break with a label don't care about braces
+      1:integer <- copy 0:literal
+    }
+  }
+  +target
+]
+-mem: storing 0 in location 1