about summary refs log tree commit diff stats
path: root/Readme.md
Commit message (Expand)AuthorAgeFilesLines
...
* 1777 - consistent terminology: 'product'Kartik K. Agaram2015-07-131-5/+5
* 1775Kartik K. Agaram2015-07-131-2/+2
* 1774Kartik K. Agaram2015-07-131-3/+3
* 1713Kartik K. Agaram2015-07-051-8/+8
* 1712Kartik K. Agaram2015-07-051-1/+5
* 1664Kartik K. Agaram2015-06-251-1/+1
* 1633Kartik K. Agaram2015-06-241-2/+2
* 1550Kartik K. Agaram2015-06-101-0/+7
* 1546 - show crucial code samples in colorKartik K. Agaram2015-06-081-23/+36
* 1381Kartik K. Agaram2015-05-151-1/+14
* 1363 - rename 'integer' to 'number'Kartik K. Agaram2015-05-131-20/+20
* 1348Kartik K. Agaram2015-05-111-1/+1
* 1345Kartik K. Agaram2015-05-111-3/+7
* 1341Kartik K. Agaram2015-05-111-0/+17
* 1286Kartik K. Agaram2015-05-061-4/+4
* 1276 - make C++ version the defaultKartik K. Agaram2015-05-051-64/+47
* 958Kartik K. Agaram2015-03-191-3/+3
* 880 - new personal convention: capitalize names in proseKartik K. Agaram2015-03-101-12/+12
* 876Kartik K. Agaram2015-03-091-0/+2
* 704Kartik K. Agaram2015-02-021-1/+1
* 703Kartik K. Agaram2015-02-021-0/+6
* 508Kartik K. Agaram2015-01-071-1/+8
* 500Kartik K. Agaram2015-01-021-1/+1
* 499Kartik K. Agaram2015-01-021-1/+1
* 498 - how mu provides closuresKartik K. Agaram2015-01-021-26/+93
* 491Kartik K. Agaram2015-01-021-0/+5
* 429Kartik K. Agaram2014-12-141-3/+3
* 428 - cleanup odds and endsKartik K. Agaram2014-12-141-35/+60
* 405 - permit loading just low levels of codebaseKartik K. Agaram2014-12-131-4/+4
* 403 - 'function' is more clear than 'def'Kartik K. Agaram2014-12-121-2/+2
* 402Kartik K. Agaram2014-12-121-0/+1
* 401 - stop abbreviating opsKartik K. Agaram2014-12-121-12/+12
* 390Kartik K. Agaram2014-11-291-2/+2
* 388Kartik K. Agaram2014-11-291-2/+2
* 387Kartik K. Agaram2014-11-291-2/+3
* 385Kartik K. Agaram2014-11-291-3/+3
* 331Kartik K. Agaram2014-11-261-3/+1
* 330Kartik K. Agaram2014-11-261-12/+52
* 329Kartik K. Agaram2014-11-261-1/+1
* 328Kartik K. Agaram2014-11-261-35/+34
* Rename file so markdown will compileKyle Mathews2014-11-251-0/+216
cial { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
//: Allow code for recipes to be pulled in from multiple places and inserted
//: at special labels called 'waypoints'. Unlike jump targets, a recipe can
//: have multiple ambiguous waypoints with the same name. Any 'before' and
//: 'after' fragments will simply be inserted at all applicable waypoints.
//: Waypoints are always surrounded by '<>', e.g. <handle-request>.
//:
//: TODO: switch recipe.steps to a more efficient data structure.

:(scenario tangle_before)
recipe main [
  1:number <- copy 0
  <label1>
  3:number <- copy 0
]

before <label1> [
  2:number <- copy 0
]
+mem: storing 0 in location 1
+mem: storing 0 in location 2
+mem: storing 0 in location 3
# nothing else
$mem: 3

//: while loading recipes, load before/after fragments

:(before "End Globals")
map<string /*label*/, recipe> Before_fragments, After_fragments;
set<string /*label*/> Fragments_used;
:(before "End Setup")
Before_fragments.clear();
After_fragments.clear();
Fragments_used.clear();

:(before "End Command Handlers")
else if (command == "before") {
  string label = next_word(in);
  recipe tmp;
  slurp_body(in, tmp);
  if (is_waypoint(label))
    Before_fragments[label].steps.insert(Before_fragments[label].steps.end(), tmp.steps.begin(), tmp.steps.end());
  else
    raise_error << "can't tangle before label " << label << '\n' << end();
}
else if (command == "after") {
  string label = next_word(in);
  recipe tmp;
  slurp_body(in, tmp);
  if (is_waypoint(label))
    After_fragments[label].steps.insert(After_fragments[label].steps.begin(), tmp.steps.begin(), tmp.steps.end());
  else
    raise_error << "can't tangle after label " << label << '\n' << end();
}

//: after all recipes are loaded, insert fragments at appropriate labels.

:(after "int main")
  Transform.push_back(insert_fragments);

//; We might need to perform multiple passes, in case inserted fragments
//: include more labels that need further insertions. Track which labels we've
//: already processed using an extra field.
:(before "End instruction Fields")
mutable bool tangle_done;
:(before "End instruction Constructor")
tangle_done = false;

:(code)
void insert_fragments(const recipe_ordinal r) {
  bool made_progress = true;
  long long int pass = 0;
  while (made_progress) {
    made_progress = false;
    // create a new vector because insertions invalidate iterators
    vector<instruction> result;
    for (long long int i = 0; i < SIZE(Recipe[r].steps); ++i) {
      const instruction& inst = Recipe[r].steps.at(i);
      if (!inst.is_label || !is_waypoint(inst.label) || inst.tangle_done) {
        result.push_back(inst);
        continue;
      }
      inst.tangle_done = true;
      made_progress = true;
      Fragments_used.insert(inst.label);
      ostringstream prefix;
      prefix << '+' << Recipe[r].name << '_' << pass << '_' << i;
      if (Before_fragments.find(inst.label) != Before_fragments.end()) {
        append_fragment(result, Before_fragments[inst.label].steps, prefix.str());
      }
      result.push_back(inst);
      if (After_fragments.find(inst.label) != After_fragments.end()) {
        append_fragment(result, After_fragments[inst.label].steps, prefix.str());
      }
    }
    Recipe[r].steps.swap(result);
    ++pass;
  }
}

void append_fragment(vector<instruction>& base, const vector<instruction>& patch, const string prefix) {
  // append 'patch' to 'base' while keeping 'base' oblivious to any new jump
  // targets in 'patch' oblivious to 'base' by prepending 'prefix' to them.
  // we might tangle the same fragment at multiple points in a single recipe,
  // and we need to avoid duplicate jump targets.
  // so we'll keep jump targets local to the specific before/after fragment
  // that introduces them.
  set<string> jump_targets;
  for (long long int i = 0; i < SIZE(patch); ++i) {
    const instruction& inst = patch.at(i);
    if (inst.is_label && is_jump_target(inst.label))
      jump_targets.insert(inst.label);
  }
  for (long long int i = 0; i < SIZE(patch); ++i) {
    instruction inst = patch.at(i);
    if (inst.is_label) {
      if (jump_targets.find(inst.label) != jump_targets.end())
        inst.label = prefix+inst.label;
      base.push_back(inst);
      continue;
    }
    for (long long int j = 0; j < SIZE(inst.ingredients); ++j) {
      reagent& x = inst.ingredients.at(j);
      if (!is_literal(x)) continue;
      if (x.properties.at(0).second->value == "label" && jump_targets.find(x.name) != jump_targets.end())
        x.name = prefix+x.name;
    }
    base.push_back(inst);
  }
}

bool is_waypoint(string label) {
  return *label.begin() == '<' && *label.rbegin() == '>';
}

//: complain about unapplied fragments
:(before "End Globals")
bool Transform_check_insert_fragments_Ran = false;
:(before "End One-time Setup")
Transform.push_back(check_insert_fragments);  // final transform
:(code)
void check_insert_fragments(unused recipe_ordinal) {
  if (Transform_check_insert_fragments_Ran) return;
  Transform_check_insert_fragments_Ran = true;
  for (map<string, recipe>::iterator p = Before_fragments.begin(); p != Before_fragments.end(); ++p) {
    if (Fragments_used.find(p->first) == Fragments_used.end())
      raise_error << "could not locate insert before " << p->first << '\n' << end();
  }
  for (map<string, recipe>::iterator p = After_fragments.begin(); p != After_fragments.end(); ++p) {
    if (Fragments_used.find(p->first) == Fragments_used.end())
      raise_error << "could not locate insert after " << p->first << '\n' << end();
  }
}

:(scenario tangle_before_and_after)
recipe main [
  1:number <- copy 0
  <label1>
  4:number <- copy 0
]
before <label1> [
  2:number <- copy 0
]
after <label1> [
  3:number <- copy 0
]
+mem: storing 0 in location 1
+mem: storing 0 in location 2
# label1
+mem: storing 0 in location 3
+mem: storing 0 in location 4
# nothing else
$mem: 4

:(scenario tangle_ignores_jump_target)
% Hide_errors = true;
recipe main [
  1:number <- copy 0
  +label1
  4:number <- copy 0
]
before +label1 [
  2:number <- copy 0
]
+error: can't tangle before label +label1
+mem: storing 0 in location 1
+mem: storing 0 in location 4
# label1
-mem: storing 0 in location 2
# nothing else
$mem: 2

:(scenario tangle_keeps_labels_separate)
recipe main [
  1:number <- copy 0
  <label1>
  <label2>
  6:number <- copy 0
]
before <label1> [
  2:number <- copy 0
]
after <label1> [
  3:number <- copy 0
]
before <label2> [
  4:number <- copy 0
]
after <label2> [
  5:number <- copy 0
]
+mem: storing 0 in location 1
+mem: storing 0 in location 2
# label1
+mem: storing 0 in location 3
# 'after' fragments for earlier label always go before 'before' fragments for later label
+mem: storing 0 in location 4
# label2
+mem: storing 0 in location 5
+mem: storing 0 in location 6
# nothing else
$mem: 6

:(scenario tangle_stacks_multiple_fragments)
recipe main [
  1:number <- copy 0
  <label1>
  6:number <- copy 0
]
before <label1> [
  2:number <- copy 0
]
after <label1> [
  3:number <- copy 0
]
before <label1> [
  4:number <- copy 0
]
after <label1> [
  5:number <- copy 0
]
+mem: storing 0 in location 1
# 'before' fragments stack in order
+mem: storing 0 in location 2
+mem: storing 0 in location 4
# label1
# 'after' fragments stack in reverse order
+mem: storing 0 in location 5
+mem: storing 0 in location 3
+mem: storing 0 in location 6
# nothing else
$mem: 6

:(scenario tangle_supports_fragments_with_multiple_instructions)
recipe main [
  1:number <- copy 0
  <label1>
  6:number <- copy 0
]
before <label1> [
  2:number <- copy 0
  3:number <- copy 0
]
after <label1> [
  4:number <- copy 0
  5:number <- copy 0
]
+mem: storing 0 in location 1
+mem: storing 0 in location 2
+mem: storing 0 in location 3
# label1
+mem: storing 0 in location 4
+mem: storing 0 in location 5
+mem: storing 0 in location 6
# nothing else
$mem: 6

:(scenario tangle_tangles_into_all_labels_with_same_name)
recipe main [
  1:number <- copy 10
  <label1>
  4:number <- copy 10
  recipe2
]
recipe recipe2 [
  1:number <- copy 11
  <label1>
  4:number <- copy 11
]
before <label1> [
  2:number <- copy 12
]
after <label1> [
  3:number <- copy 12
]
+mem: storing 10 in location 1
+mem: storing 12 in location 2
# label1
+mem: storing 12 in location 3
+mem: storing 10 in location 4
# recipe2
+mem: storing 11 in location 1
+mem: storing 12 in location 2
# label1
+mem: storing 12 in location 3
+mem: storing 11 in location 4
# nothing else
$mem: 8

:(scenario tangle_tangles_into_all_labels_with_same_name_2)
recipe main [
  1:number <- copy 10
  <label1>
  <label1>
  4:number <- copy 10
]
before <label1> [
  2:number <- copy 12
]
after <label1> [
  3:number <- copy 12
]
+mem: storing 10 in location 1
+mem: storing 12 in location 2
# label1
+mem: storing 12 in location 3
+mem: storing 12 in location 2
# label1
+mem: storing 12 in location 3
+mem: storing 10 in location 4
# nothing else
$mem: 6

:(scenario tangle_tangles_into_all_labels_with_same_name_3)
recipe main [
  1:number <- copy 10
  <label1>
  <foo>
  4:number <- copy 10
]
before <label1> [
  2:number <- copy 12
]
after <label1> [
  3:number <- copy 12
]
after <foo> [
  <label1>
]
+mem: storing 10 in location 1
+mem: storing 12 in location 2
# label1
+mem: storing 12 in location 3
+mem: storing 12 in location 2
# foo/label1
+mem: storing 12 in location 3
+mem: storing 10 in location 4
# nothing else
$mem: 6

:(scenario tangle_handles_jump_target_inside_fragment)
recipe main [
  1:number <- copy 10
  <label1>
  4:number <- copy 10
]
before <label1> [
  jump +label2:label
  2:number <- copy 12
  +label2
  3:number <- copy 12
]
+mem: storing 10 in location 1
# label1
+mem: storing 12 in location 3
+mem: storing 10 in location 4
# ignored by jump
-mem: storing 12 in label 2
# nothing else
$mem: 3

:(scenario tangle_renames_jump_target)
recipe main [
  1:number <- copy 10
  <label1>
  +label2
  4:number <- copy 10
]
before <label1> [
  jump +label2:label
  2:number <- copy 12
  +label2  # renamed
  3:number <- copy 12
]
+mem: storing 10 in location 1
# label1
+mem: storing 12 in location 3
+mem: storing 10 in location 4
# ignored by jump
-mem: storing 12 in label 2
# nothing else
$mem: 3

:(scenario tangle_jump_to_base_recipe)
recipe main [
  1:number <- copy 10
  <label1>
  +label2
  4:number <- copy 10
]
before <label1> [
  jump +label2:label
  2:number <- copy 12
  3:number <- copy 12
]
+mem: storing 10 in location 1
# label1
+mem: storing 10 in location 4
# ignored by jump
-mem: storing 12 in label 2
-mem: storing 12 in location 3
# nothing else
$mem: 2