summary refs log tree commit diff stats
path: root/test
ModeNameSize
-rw-r--r--__init__.py1218log stats plain blame
-rw-r--r--tc_bookmarks.py2102log stats plain blame
-rw-r--r--tc_colorscheme.py1616log stats plain blame
-rw-r--r--tc_commandlist.py2930log stats plain blame
-rw-r--r--tc_directory.py3710log stats plain blame
-rw-r--r--tc_displayable.py4378log stats plain blame
-rw-r--r--tc_ext.py4070log stats plain blame
-rw-r--r--tc_history.py1692log stats plain blame
-rw-r--r--tc_keyapi.py1250log stats plain blame
-rw-r--r--tc_newkeys.py17366log stats plain blame
-rw-r--r--tc_ui.py1739log stats plain blame
-rw-r--r--test.py867log stats plain blame
d---------testdir153log stats plain
ackground-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 */
//: Writing to a literal (not computed) address of 0 in a recipe chains two
//: spaces together. When a variable has a property of /space:1, it looks up
//: the variable in the chained/surrounding space. /space:2 looks up the
//: surrounding space of the surrounding space, etc.

:(scenario closure)
recipe main [
  default-space:address:array:location <- new location:type, 30
  1:address:array:location/names:new-counter <- new-counter
  2:number/raw <- increment-counter 1:address:array:location/names:new-counter
  3:number/raw <- increment-counter 1:address:array:location/names:new-counter
]

recipe new-counter [
  default-space:address:array:location <- new location:type, 30
  x:number <- copy 23
  y:number <- copy 3  # variable that will be incremented
  reply default-space:address:array:location
]

recipe increment-counter [
  default-space:address:array:location <- new location:type, 30
  0:address:array:location/names:new-counter <- next-ingredient  # outer space must be created by 'new-counter' above
  y:number/space:1 <- add y:number/space:1, 1  # increment
  y:number <- copy 234  # dummy
  reply y:number/space:1
]

+name: lexically surrounding space for recipe increment-counter comes from new-counter
+mem: storing 5 in location 3

//: To make this work, compute the recipe that provides names for the
//: surrounding space of each recipe. This must happen before transform_names.

:(before "End Globals")
map<recipe_ordinal, recipe_ordinal> Surrounding_space;

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

:(code)
void collect_surrounding_spaces(const recipe_ordinal r) {
  trace(9991, "transform") << "--- collect surrounding spaces for recipe " << Recipe[r].name << end();
  for (long long int i = 0; i < SIZE(Recipe[r].steps); ++i) {
    const instruction& inst = Recipe[r].steps.at(i);
    if (inst.is_label) continue;
    for (long long int j = 0; j < SIZE(inst.products); ++j) {
      if (is_literal(inst.products.at(j))) continue;
      if (inst.products.at(j).name != "0") continue;
      type_tree* type = inst.products.at(j).type;
      if (!type
          || type->value != Type_ordinal["address"]
          || !type->right
          || type->right->value != Type_ordinal["array"]
          || !type->right->right
          || type->right->right->value != Type_ordinal["location"]
          || type->right->right->right) {
        raise_error << "slot 0 should always have type address:array:location, but is " << inst.products.at(j).to_string() << '\n' << end();
        continue;
      }
      string_tree* s = property(inst.products.at(j), "names");
      if (!s) {
        raise_error << "slot 0 requires a /names property in recipe " << Recipe[r].name << end();
        continue;
      }
      if (s->right) raise_error << "slot 0 should have a single value in /names, but got " << inst.products.at(j).to_string() << '\n' << end();
      const string& surrounding_recipe_name = s->value;
      if (Surrounding_space.find(r) != Surrounding_space.end()
          && Surrounding_space[r] != Recipe_ordinal[surrounding_recipe_name]) {
        raise_error << "recipe " << Recipe[r].name << " can have only one 'surrounding' recipe but has " << Recipe[Surrounding_space[r]].name << " and " << surrounding_recipe_name << '\n' << end();
        continue;
      }
      trace(9993, "name") << "lexically surrounding space for recipe " << Recipe[r].name << " comes from " << surrounding_recipe_name << end();
      Surrounding_space[r] = Recipe_ordinal[surrounding_recipe_name];
    }
  }
}

//: Once surrounding spaces are available, transform_names uses them to handle
//: /space properties.

:(replace{} "long long int lookup_name(const reagent& r, const recipe_ordinal default_recipe)")
long long int lookup_name(const reagent& x, const recipe_ordinal default_recipe) {
  if (!has_property(x, "space")) {
    if (Name[default_recipe].empty()) raise_error << "name not found: " << x.name << '\n' << end();
    return Name[default_recipe][x.name];
  }
  string_tree* p = property(x, "space");
  if (!p || p->right) raise_error << "/space property should have exactly one (non-negative integer) value\n" << end();
  long long int n = to_integer(p->value);
  assert(n >= 0);
  recipe_ordinal surrounding_recipe = lookup_surrounding_recipe(default_recipe, n);
  set<recipe_ordinal> done;
  vector<recipe_ordinal> path;
  return lookup_name(x, surrounding_recipe, done, path);
}

// If the recipe we need to lookup this name in doesn't have names done yet,
// recursively call transform_names on it.
long long int lookup_name(const reagent& x, const recipe_ordinal r, set<recipe_ordinal>& done, vector<recipe_ordinal>& path) {
  if (!Name[r].empty()) return Name[r][x.name];
  if (done.find(r) != done.end()) {
    raise_error << "can't compute address of " << x.to_string() << " because " << end();
    for (long long int i = 1; i < SIZE(path); ++i) {
      raise_error << path.at(i-1) << " requires computing names of " << path.at(i) << '\n' << end();
    }
    raise_error << path.at(SIZE(path)-1) << " requires computing names of " << r << "..ad infinitum\n" << end();
    return 0;
  }
  done.insert(r);
  path.push_back(r);
  transform_names(r);  // Not passing 'done' through. Might this somehow cause an infinite loop?
  assert(!Name[r].empty());
  return Name[r][x.name];
}

recipe_ordinal lookup_surrounding_recipe(const recipe_ordinal r, long long int n) {
  if (n == 0) return r;
  if (Surrounding_space.find(r) == Surrounding_space.end()) {
    raise_error << "don't know surrounding recipe of " << Recipe[r].name << '\n' << end();
    return 0;
  }
  assert(Surrounding_space[r]);
  return lookup_surrounding_recipe(Surrounding_space[r], n-1);
}

//: weaken use-before-set detection just a tad
:(replace{} "bool already_transformed(const reagent& r, const map<string, long long int>& names)")
bool already_transformed(const reagent& r, const map<string, long long int>& names) {
  if (has_property(r, "space")) {
    string_tree* p = property(r, "space");
    if (!p || p->right) {
      raise_error << "/space property should have exactly one (non-negative integer) value in " << r.original_string << '\n' << end();
      return false;
    }
    if (p->value != "0") return true;
  }
  return names.find(r.name) != names.end();
}