summary refs log tree commit diff stats
path: root/doc
Commit message (Expand)AuthorAgeFilesLines
* removed the cd-after-exit hackhut2010-03-291-161/+0
* updated TODO and pydochut2010-03-2120-52/+85
* doc/colorschemes: ugh, its no logical but bitwise OR!hut2010-03-171-1/+1
* incremented verison number v1.0.4hut2010-03-121-2/+2
* standardized formatting of headings in doc/hut2010-03-122-13/+31
* added doc/uml.txthut2010-03-121-0/+5
* updated pydochut2010-03-1217-161/+33
* updated pydochut2010-03-1270-3946/+816
* added two new colorschemes using 88 colorshut2010-03-121-0/+23
* added documentation on how colorschemes workhut2010-03-121-0/+91
* added symlink: doc/help => ranger/helphut2010-02-281-0/+1
* incremented version number and updated pydoc html files v1.0.3hut2010-02-1675-1972/+1415
* doc/cd-after-exit: updatedhut2010-02-141-21/+15
* doc/pick.sh: corrected commit orderhut2010-02-091-1/+1
* doc: what breaks cd-after-exit support in zshhut2010-02-091-0/+2
* pick.sh: added -m to checkout commadshut2010-02-051-3/+3
* pick.sh: added variables for easier customizationhut2010-02-041-7/+8
* added doc/pick.shhut2010-02-041-0/+24
* updated dochut2010-01-211-4/+20
* 1.0.2! v1.0.2hut2010-01-1430-84/+116
* updated pydoc documentationhut2010-01-1361-846/+795
* todo: added more info on bug #31hut2010-01-091-0/+5
* random cleanups and fixeshut2010-01-071-5/+6
* new minor version v1.0.1hut2010-01-022-4/+4
* updated pydoc documentationhut2010-01-0248-788/+3167
* notify: merged into statusbar, allow to view the log in the pagerhut2010-01-013-35/+2
* cleanupshut2009-12-311-1/+5
* rename filelist(container) to browsercolumn/browserviewhut2009-12-313-38/+76
* updated uml projecthut2009-12-305-73/+215
* shorten comment in ranger.pyhut2009-12-261-0/+4
* moved /uml to /doc/umlhut2009-12-2514-0/+2180
* Explained cd-after-exit featurehut2009-12-251-0/+132
* moved pydoc pages to doc/pydochut2009-12-2565-0/+0
* updated pydoc pageshut2009-12-2565-0/+10505
*/ .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 */
//: Construct types out of their constituent fields.

void test_merge() {
  run(
      "container foo [\n"
      "  x:num\n"
      "  y:num\n"
      "]\n"
      "def main [\n"
      "  1:foo <- merge 3, 4\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 3 in location 1\n"
      "mem: storing 4 in location 2\n"
  );
}

:(before "End Primitive Recipe Declarations")
MERGE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "merge", MERGE);
:(before "End Primitive Recipe Checks")
case MERGE: {
  // type-checking in a separate transform below
  break;
}
:(before "End Primitive Recipe Implementations")
case MERGE: {
  products.resize(1);
  for (int i = 0;  i < SIZE(ingredients);  ++i)
    for (int j = 0;  j < SIZE(ingredients.at(i));  ++j)
      products.at(0).push_back(ingredients.at(i).at(j));
  break;
}

//: type-check 'merge' to avoid interpreting numbers as addresses

:(code)
void test_merge_check() {
  run(
      "def main [\n"
      "  1:point <- merge 3, 4\n"
      "]\n"
  );
  CHECK_TRACE_COUNT("error", 0);
}

void test_merge_check_missing_element() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  1:point <- merge 3\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: too few ingredients in '1:point <- merge 3'\n"
  );
}

void test_merge_check_extra_element() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  1:point <- merge 3, 4, 5\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: too many ingredients in '1:point <- merge 3, 4, 5'\n"
  );
}

//: We want to avoid causing memory corruption, but other than that we want to
//: be flexible in how we construct containers of containers. It should be
//: equally easy to define a container out of primitives or intermediate
//: container fields.

void test_merge_check_recursive_containers() {
  run(
      "def main [\n"
      "  1:point <- merge 3, 4\n"
      "  1:point-number <- merge 1:point, 5\n"
      "]\n"
  );
  CHECK_TRACE_COUNT("error", 0);
}

void test_merge_check_recursive_containers_2() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  1:point <- merge 3, 4\n"
      "  2:point-number <- merge 1:point\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: too few ingredients in '2:point-number <- merge 1:point'\n"
  );
}

void test_merge_check_recursive_containers_3() {
  run(
      "def main [\n"
      "  1:point-number <- merge 3, 4, 5\n"
      "]\n"
  );
  CHECK_TRACE_COUNT("error", 0);
}

void test_merge_check_recursive_containers_4() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  1:point-number <- merge 3, 4\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: too few ingredients in '1:point-number <- merge 3, 4'\n"
  );
}

void test_merge_check_reflexive() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  1:point <- merge 3, 4\n"
      "  2:point <- merge 1:point\n"
      "]\n"
  );
  CHECK_TRACE_COUNT("error", 0);
}

//: Since a container can be merged in several ways, we need to be able to
//: backtrack through different possibilities. Later we'll allow creating
//: exclusive containers which contain just one of rather than all of their
//: elements. That will also require backtracking capabilities. Here's the
//: state we need to maintain for backtracking:

:(before "End Types")
struct merge_check_point {
  reagent container;
  int container_element_index;
  merge_check_point(const reagent& c, int i) :container(c), container_element_index(i) {}
};

struct merge_check_state {
  stack<merge_check_point> data;
};

:(before "End Checks")
Transform.push_back(check_merge_calls);  // idempotent
:(code)
void check_merge_calls(const recipe_ordinal r) {
  const recipe& caller = get(Recipe, r);
  trace(101, "transform") << "--- type-check merge instructions in recipe " << caller.name << end();
  for (int i = 0;  i < SIZE(caller.steps);  ++i) {
    const instruction& inst = caller.steps.at(i);
    if (inst.name != "merge") continue;
    if (SIZE(inst.products) != 1) {
      raise << maybe(caller.name) << "'merge' should yield a single product in '" << to_original_string(inst) << "'\n" << end();
      continue;
    }
    reagent/*copy*/ product = inst.products.at(0);
    // Update product While Type-checking Merge
    const type_tree* product_base_type = product.type->atom ? product.type : product.type->left;
    assert(product_base_type->atom);
    if (product_base_type->value == 0 || !contains_key(Type, product_base_type->value)) {
      raise << maybe(caller.name) << "'merge' should yield a container in '" << to_original_string(inst) << "'\n" << end();
      continue;
    }
    const type_info& info = get(Type, product_base_type->value);
    if (info.kind != CONTAINER && info.kind != EXCLUSIVE_CONTAINER) {
      raise << maybe(caller.name) << "'merge' should yield a container in '" << to_original_string(inst) << "'\n" << end();
      continue;
    }
    check_merge_call(inst.ingredients, product, caller, inst);
  }
}

void check_merge_call(const vector<reagent>& ingredients, const reagent& product, const recipe& caller, const instruction& inst) {
  int ingredient_index = 0;
  merge_check_state state;
  state.data.push(merge_check_point(product, 0));
  while (true) {
    assert(!state.data.empty());
    trace(102, "transform") << ingredient_index << " vs " << SIZE(ingredients) << end();
    if (ingredient_index >= SIZE(ingredients)) {
      raise << maybe(caller.name) << "too few ingredients in '" << to_original_string(inst) << "'\n" << end();
      return;
    }
    reagent& container = state.data.top().container;
    if (!container.type) return;  // error handled elsewhere
    const type_tree* top_root_type = container.type->atom ? container.type : container.type->left;
    assert(top_root_type->atom);
    type_info& container_info = get(Type, top_root_type->value);
    switch (container_info.kind) {
      case CONTAINER: {
        // degenerate case: merge with the same type always succeeds
        if (state.data.top().container_element_index == 0 && types_coercible(container, inst.ingredients.at(ingredient_index)))
          return;
        const reagent& expected_ingredient = element_type(container.type, state.data.top().container_element_index);
        trace(102, "transform") << "checking container " << to_string(container) << " || " << to_string(expected_ingredient) << " vs ingredient " << ingredient_index << end();
        // if the current element is the ingredient we expect, move on to the next element/ingredient
        if (types_coercible(expected_ingredient, ingredients.at(ingredient_index))) {
          ++ingredient_index;
          ++state.data.top().container_element_index;
          while (state.data.top().container_element_index >= SIZE(get(Type, get_base_type(state.data.top().container.type)->value).elements)) {
            state.data.pop();
            if (state.data.empty()) {
              if (ingredient_index < SIZE(ingredients))
                raise << maybe(caller.name) << "too many ingredients in '" << to_original_string(inst) << "'\n" << end();
              return;
            }
            ++state.data.top().container_element_index;
          }
        }
        // if not, maybe it's a field of the current element
        else {
          // no change to ingredient_index
          state.data.push(merge_check_point(expected_ingredient, 0));
        }
        break;
      }
      // End check_merge_call Special-cases
      default: {
        if (!types_coercible(container, ingredients.at(ingredient_index))) {
          raise << maybe(caller.name) << "incorrect type of ingredient " << ingredient_index << " in '" << to_original_string(inst) << "'\n" << end();
          raise << "  (expected '" << debug_string(container) << "')\n" << end();
          raise << "  (got '" << debug_string(ingredients.at(ingredient_index)) << "')\n" << end();
          return;
        }
        ++ingredient_index;
        // ++state.data.top().container_element_index;  // unnecessary, but wouldn't do any harm
        do {
          state.data.pop();
          if (state.data.empty()) {
            if (ingredient_index < SIZE(ingredients))
              raise << maybe(caller.name) << "too many ingredients in '" << to_original_string(inst) << "'\n" << end();
            return;
          }
          ++state.data.top().container_element_index;
        } while (state.data.top().container_element_index >= SIZE(get(Type, get_base_type(state.data.top().container.type)->value).elements));
      }
    }
  }
  // never gets here
  assert(false);
}

//: replaced in a later layer
//: todo: find some clean way to take this call completely out of this layer
const type_tree* get_base_type(const type_tree* t) {
  return t;
}

void test_merge_check_product() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  1:num <- merge 3\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: 'merge' should yield a container in '1:num <- merge 3'\n"
  );
}

:(before "End Includes")
#include <stack>
using std::stack;