about summary refs log tree commit diff stats
path: root/046closure_name.cc
blob: 35486ed7e9cbb805fd75cea3c34e4c51ce7f8ae6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//: 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: recipe increment-counter is surrounded by 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) {
  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;
      if (SIZE(inst.products.at(j).types) != 3
          || inst.products.at(j).types.at(0) != Type_ordinal["address"]
          || inst.products.at(j).types.at(1) != Type_ordinal["array"]
          || inst.products.at(j).types.at(2) != Type_ordinal["location"]) {
        raise << "slot 0 should always have type address:array:location, but is " << inst.products.at(j).to_string() << '\n' << end();
        continue;
      }
      vector<string> s = property(inst.products.at(j), "names");
      if (s.empty()) {
        raise << "slot 0 requires a /names property in recipe " << Recipe[r].name << end();
        continue;
      }
      if (SIZE(s) > 1) raise << "slot 0 should have a single value in /names, but got " << inst.products.at(j).to_string() << '\n' << end();
      string surrounding_recipe_name = s.at(0);
      if (Surrounding_space.find(r) != Surrounding_space.end()
          && Surrounding_space[r] != Recipe_ordinal[surrounding_recipe_name]) {
        raise << "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("name") << "recipe " << Recipe[r].name << " is surrounded by " << 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) {
//?   cout << "AAA " << default_recipe << " " << Recipe[default_recipe].name << '\n'; //? 2
//?   cout << "AAA " << x.to_string() << '\n'; //? 1
  if (!has_property(x, "space")) {
    if (Name[default_recipe].empty()) raise << "name not found: " << x.name << '\n' << end();
    return Name[default_recipe][x.name];
  }
  vector<string> p = property(x, "space");
  if (SIZE(p) != 1) raise << "/space property should have exactly one (non-negative integer) value\n" << end();
  long long int n = to_integer(p.at(0));
  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 << "can't compute address of " << x.to_string() << " because " << end();
    for (long long int i = 1; i < SIZE(path); ++i) {
      raise << path.at(i-1) << " requires computing names of " << path.at(i) << '\n' << end();
    }
    raise << 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 << "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 warnings 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")) {
    vector<string> p = property(r, "space");
    if (SIZE(p) != 1) {
      raise << "/space property should have exactly one (non-negative integer) value in " << r.original_string << '\n' << end();
      return false;
    }
    if (p.at(0) != "0") return true;
  }
  return names.find(r.name) != names.end();
}
+); get_or_insert(Type, array).name = "array"; // End Mu Types Initialization } void teardown_types() { for (map<type_ordinal, type_info>::iterator p = Type.begin(); p != Type.end(); ++p) { for (int i = 0; i < SIZE(p->second.elements); ++i) p->second.elements.clear(); } Type_ordinal.clear(); } :(before "End One-time Setup") setup_types(); atexit(teardown_types); :(before "End Types") // You can construct arbitrary new types. New types are either 'containers' // with multiple 'elements' of other types, or 'exclusive containers' containing // one of multiple 'variants'. (These are similar to C structs and unions, // respectively, though exclusive containers implicitly include a tag element // recording which variant they should be interpreted as.) // // For example, storing bank balance and name for an account might require a // container, but if bank accounts may be either for individuals or groups, // with different properties for each, that may require an exclusive container // whose variants are individual-account and joint-account containers. enum kind_of_type { PRIMITIVE, CONTAINER, EXCLUSIVE_CONTAINER }; struct type_info { string name; kind_of_type kind; vector<reagent> elements; // End type_info Fields type_info() :kind(PRIMITIVE) { // End type_info Constructor } }; enum primitive_recipes { IDLE = 0, COPY, // End Primitive Recipe Declarations MAX_PRIMITIVE_RECIPES, }; :(code) //: It's all very well to construct recipes out of other recipes, but we need //: to know how to do *something* out of the box. For the following //: recipes there are only codes, no entries in the book, because mu just knows //: what to do for them. void setup_recipes() { Recipe.clear(); Recipe_ordinal.clear(); put(Recipe_ordinal, "idle", IDLE); // Primitive Recipe Numbers put(Recipe_ordinal, "copy", COPY); // End Primitive Recipe Numbers } //: We could just reset the recipe table after every test, but that gets slow //: all too quickly. Instead, initialize the common stuff just once at //: startup. Later layers will carefully undo each test's additions after //: itself. :(before "End One-time Setup") setup_recipes(); assert(MAX_PRIMITIVE_RECIPES < 200); // level 0 is primitives; until 199 Next_recipe_ordinal = 200; put(Recipe_ordinal, "main", Next_recipe_ordinal++); // End Load Recipes :(before "End Commandline Parsing") assert(Next_recipe_ordinal < 1000); // recipes being tested didn't overflow into test space :(before "End Setup") Next_recipe_ordinal = 1000; // consistent new numbers for each test //: One final detail: tests can modify our global tables of recipes and types, //: so we need some way to clean up after each test is done so it doesn't //: influence later ones. :(before "End Globals") map<string, recipe_ordinal> Recipe_ordinal_snapshot; map<recipe_ordinal, recipe> Recipe_snapshot; map<string, type_ordinal> Type_ordinal_snapshot; map<type_ordinal, type_info> Type_snapshot; :(before "End One-time Setup") save_snapshots(); :(before "End Setup") restore_snapshots(); :(code) void save_snapshots() { Recipe_ordinal_snapshot = Recipe_ordinal; Recipe_snapshot = Recipe; Type_ordinal_snapshot = Type_ordinal; Type_snapshot = Type; // End save_snapshots } void restore_snapshots() { Recipe = Recipe_snapshot; Recipe_ordinal = Recipe_ordinal_snapshot; Type_ordinal = Type_ordinal_snapshot; Type = Type_snapshot; // End restore_snapshots } //:: Helpers :(code) recipe::recipe() { // End recipe Constructor } instruction::instruction() :is_label(false), operation(IDLE) { // End instruction Constructor } void instruction::clear() { is_label=false; label.clear(); name.clear(); old_name.clear(); operation=IDLE; ingredients.clear(); products.clear(); original_string.clear(); } bool instruction::is_empty() { return !is_label && name.empty(); } // Reagents have the form <name>:<type>:<type>:.../<property>/<property>/... reagent::reagent(string s) :original_string(s), type(NULL), value(0), initialized(false) { // Parsing reagent(string s) istringstream in(s); in >> std::noskipws; // name and type istringstream first_row(slurp_until(in, '/')); first_row >> std::noskipws; name = slurp_until(first_row, ':'); string_tree* type_names = parse_property_list(first_row); type = new_type_tree(type_names); delete type_names; // special cases if (is_integer(name) && type == NULL) type = new type_tree("literal", get(Type_ordinal, "literal")); if (name == "_" && type == NULL) type = new type_tree("literal", get(Type_ordinal, "literal")); // other properties while (has_data(in)) { istringstream row(slurp_until(in, '/')); row >> std::noskipws; string key = slurp_until(row, ':'); string_tree* value = parse_property_list(row); properties.push_back(pair<string, string_tree*>(key, value)); } // End Parsing reagent } string_tree* parse_property_list(istream& in) { skip_whitespace_but_not_newline(in); if (!has_data(in)) return NULL; string_tree* result = new string_tree(slurp_until(in, ':')); result->right = parse_property_list(in); return result; } type_tree* new_type_tree(const string_tree* properties) { if (!properties) return NULL; type_tree* result = new type_tree("", 0); if (!properties->value.empty()) { const string& type_name = result->name = properties->value; if (contains_key(Type_ordinal, type_name)) result->value = get(Type_ordinal, type_name); else if (is_integer(type_name)) // sometimes types will contain non-type tags, like numbers for the size of an array result->value = 0; else if (properties->value != "->") // used in recipe types result->value = -1; // should never happen; will trigger errors later } result->left = new_type_tree(properties->left); result->right = new_type_tree(properties->right); return result; } //: avoid memory leaks for the type tree reagent::reagent(const reagent& old) { original_string = old.original_string; name = old.name; value = old.value; initialized = old.initialized; for (int i = 0; i < SIZE(old.properties); ++i) { properties.push_back(pair<string, string_tree*>(old.properties.at(i).first, old.properties.at(i).second ? new string_tree(*old.properties.at(i).second) : NULL)); } type = old.type ? new type_tree(*old.type) : NULL; // End reagent Copy Constructor } type_tree::type_tree(const type_tree& old) { name = old.name; value = old.value; left = old.left ? new type_tree(*old.left) : NULL; right = old.right ? new type_tree(*old.right) : NULL; } string_tree::string_tree(const string_tree& old) { // :value(old.value) { value = old.value; left = old.left ? new string_tree(*old.left) : NULL; right = old.right ? new string_tree(*old.right) : NULL; } reagent& reagent::operator=(const reagent& old) { original_string = old.original_string; for (int i = 0; i < SIZE(properties); ++i) if (properties.at(i).second) delete properties.at(i).second; properties.clear(); for (int i = 0; i < SIZE(old.properties); ++i) properties.push_back(pair<string, string_tree*>(old.properties.at(i).first, old.properties.at(i).second ? new string_tree(*old.properties.at(i).second) : NULL)); name = old.name; value = old.value; initialized = old.initialized; if (type) delete type; type = old.type ? new type_tree(*old.type) : NULL; // End reagent Copy Operator return *this; } reagent::~reagent() { clear(); } void reagent::clear() { for (int i = 0; i < SIZE(properties); ++i) { if (properties.at(i).second) { delete properties.at(i).second; properties.at(i).second = NULL; } } delete type; type = NULL; } type_tree::~type_tree() { delete left; delete right; } string_tree::~string_tree() { delete left; delete right; } string slurp_until(istream& in, char delim) { ostringstream out; char c; while (in >> c) { if (c == delim) { // drop the delim break; } out << c; } return out.str(); } bool has_property(reagent x, string name) { for (int i = 0; i < SIZE(x.properties); ++i) { if (x.properties.at(i).first == name) return true; } return false; } string_tree* property(const reagent& r, const string& name) { for (int p = 0; p != SIZE(r.properties); ++p) { if (r.properties.at(p).first == name) return r.properties.at(p).second; } return NULL; } :(before "End Globals") const string Ignore(","); // commas are ignored in mu except within [] strings :(code) void skip_whitespace_but_not_newline(istream& in) { while (true) { if (!has_data(in)) break; else if (in.peek() == '\n') break; else if (isspace(in.peek())) in.get(); else if (Ignore.find(in.peek()) != string::npos) in.get(); else break; } } void dump_memory() { for (map<int, double>::iterator p = Memory.begin(); p != Memory.end(); ++p) { cout << p->first << ": " << no_scientific(p->second) << '\n'; } } //:: Helpers for converting various values to string //: Use to_string() in trace(), and try to avoid relying on unstable codes that //: will perturb .traces/ from commit to commit. //: Use debug_string() while debugging, and throw everything into it. //: Use inspect() only for emitting a canonical format that can be parsed back //: into the value. string to_string(const recipe& r) { ostringstream out; out << "recipe " << r.name << " [\n"; for (int i = 0; i < SIZE(r.steps); ++i) out << " " << to_string(r.steps.at(i)) << '\n'; out << "]\n"; return out.str(); } string debug_string(const recipe& x) { ostringstream out; out << "- recipe " << x.name << '\n'; // Begin debug_string(recipe x) for (int index = 0; index < SIZE(x.steps); ++index) { const instruction& inst = x.steps.at(index); out << "inst: " << to_string(inst) << '\n'; out << " ingredients\n"; for (int i = 0; i < SIZE(inst.ingredients); ++i) out << " " << debug_string(inst.ingredients.at(i)) << '\n'; out << " products\n"; for (int i = 0; i < SIZE(inst.products); ++i) out << " " << debug_string(inst.products.at(i)) << '\n'; } return out.str(); } string to_original_string(const instruction& inst) { if (inst.is_label) return inst.label; ostringstream out; for (int i = 0; i < SIZE(inst.products); ++i) { if (i > 0) out << ", "; out << inst.products.at(i).original_string; } if (!inst.products.empty()) out << " <- "; out << inst.name << ' '; for (int i = 0; i < SIZE(inst.ingredients); ++i) { if (i > 0) out << ", "; out << inst.ingredients.at(i).original_string; } return out.str(); } string to_string(const instruction& inst) { if (inst.is_label) return inst.label; ostringstream out; for (int i = 0; i < SIZE(inst.products); ++i) { if (i > 0) out << ", "; out << to_string(inst.products.at(i)); } if (!inst.products.empty()) out << " <- "; out << inst.name << ' '; for (int i = 0; i < SIZE(inst.ingredients); ++i) { if (i > 0) out << ", "; out << to_string(inst.ingredients.at(i)); } return out.str(); } string to_string(const reagent& r) { if (is_dummy(r)) return "_"; ostringstream out; out << "{"; out << r.name << ": " << names_to_string(r.type); if (!r.properties.empty()) { for (int i = 0; i < SIZE(r.properties); ++i) out << ", \"" << r.properties.at(i).first << "\": " << to_string(r.properties.at(i).second); } out << "}"; return out.str(); } // special name for ignoring some products inline bool is_dummy(const reagent& x) { return x.name == "_"; } string debug_string(const reagent& x) { ostringstream out; out << x.name << ": " << x.value << ' ' << to_string(x.type) << " -- " << to_string(x); return out.str(); } string to_string(const string_tree* property) { if (!property) return "()"; ostringstream out; if (!property->left && !property->right) // abbreviate a single-node tree to just its contents out << '"' << property->value << '"'; else dump(property, out); return out.str(); } void dump(const string_tree* x, ostream& out) { if (!x->left && !x->right) { out << x->value; return; } out << '('; for (const string_tree* curr = x; curr; curr = curr->right) { if (curr != x) out << ' '; if (curr->left) dump(curr->left, out); else out << '"' << curr->value << '"'; } out << ')'; } string to_string(const type_tree* type) { // abbreviate a single-node tree to just its contents if (!type) return "NULLNULLNULL"; // should never happen ostringstream out; dump(type, out); return out.str(); } void dump(const type_tree* x, ostream& out) { if (!x->left && !x->right) { dump(x->value, out); return; } out << '('; for (const type_tree* curr = x; curr; curr = curr->right) { if (curr != x) out << ' '; if (curr->left) dump(curr->left, out); else dump(curr->value, out); } out << ')'; } void dump(type_ordinal type, ostream& out) { if (contains_key(Type, type)) out << get(Type, type).name; else out << "?" << type; } string names_to_string(const type_tree* type) { // abbreviate a single-node tree to just its contents if (!type) return "()"; // should never happen ostringstream out; dump_names(type, out); return out.str(); } void dump_names(const type_tree* type, ostream& out) { if (!type->left && !type->right) { out << '"' << type->name << '"'; return; } out << '('; for (const type_tree* curr = type; curr; curr = curr->right) { if (curr != type) out << ' '; if (curr->left) dump_names(curr->left, out); else out << '"' << curr->name << '"'; } out << ')'; } string names_to_string_without_quotes(const type_tree* type) { // abbreviate a single-node tree to just its contents if (!type) return "NULLNULLNULL"; // should never happen ostringstream out; dump_names_without_quotes(type, out); return out.str(); } void dump_names_without_quotes(const type_tree* type, ostream& out) { if (!type->left && !type->right) { out << type->name; return; } out << '('; for (const type_tree* curr = type; curr; curr = curr->right) { if (curr != type) out << ' '; if (curr->left) dump_names_without_quotes(curr->left, out); else out << curr->name; } out << ')'; } //: helper to print numbers without excessive precision :(before "End Types") struct no_scientific { double x; explicit no_scientific(double y) :x(y) {} }; :(code) ostream& operator<<(ostream& os, no_scientific x) { if (!isfinite(x.x)) { // Infinity or NaN os << x.x; return os; } ostringstream tmp; tmp << std::fixed << x.x; os << trim_floating_point(tmp.str()); return os; } string trim_floating_point(const string& in) { if (in.empty()) return ""; int len = SIZE(in); while (len > 1) { if (in.at(len-1) != '0') break; --len; } if (in.at(len-1) == '.') --len; return in.substr(0, len); } void test_trim_floating_point() { CHECK_EQ("", trim_floating_point("")); CHECK_EQ("0", trim_floating_point("000000000")); CHECK_EQ("1.5", trim_floating_point("1.5000")); CHECK_EQ("1.000001", trim_floating_point("1.000001")); CHECK_EQ("23", trim_floating_point("23.000000")); CHECK_EQ("23", trim_floating_point("23.0")); CHECK_EQ("23", trim_floating_point("23.")); CHECK_EQ("23", trim_floating_point("23")); CHECK_EQ("3", trim_floating_point("3.000000")); CHECK_EQ("3", trim_floating_point("3.0")); CHECK_EQ("3", trim_floating_point("3.")); CHECK_EQ("3", trim_floating_point("3")); } :(before "End Includes") #include<utility> using std::pair; #include<math.h>