about summary refs log tree commit diff stats
path: root/cpp
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-03-18 22:44:04 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-03-18 22:44:04 -0700
commit80b781ccf8c0b991725e0713ab936443bb3252cf (patch)
treeb255541dbc04ae8558dd11d42f4e6985127ab2dc /cpp
parent9cc16d04958753a62474d13bd823f13a33a6a0a3 (diff)
downloadmu-80b781ccf8c0b991725e0713ab936443bb3252cf.tar.gz
957 - starting to do name translation
Diffstat (limited to 'cpp')
-rw-r--r--cpp/.traces/convert_names9
-rw-r--r--cpp/025name56
2 files changed, 65 insertions, 0 deletions
diff --git a/cpp/.traces/convert_names b/cpp/.traces/convert_names
new file mode 100644
index 00000000..3a7e2568
--- /dev/null
+++ b/cpp/.traces/convert_names
@@ -0,0 +1,9 @@
+parse/0: instruction: 1
+parse/0:   ingredient: {name: "0", value: 0, type: 0, properties: [0: literal]}
+parse/0:   product: {name: "x", value: 0, type: 1, properties: [x: integer]}
+name/0: assign x 1
+after-brace/0: recipe main
+after-brace/0: copy ...
+run/0: instruction main/0
+run/0: ingredient 0 is 0
+mem/0: storing in location 1
diff --git a/cpp/025name b/cpp/025name
new file mode 100644
index 00000000..73406a81
--- /dev/null
+++ b/cpp/025name
@@ -0,0 +1,56 @@
+//: A big convenience high-level languages provide is the ability to name memory
+//: locations. In mu, a lightweight tool called 'convert-names' provides this
+//: convenience.
+
+:(scenarios run)
+:(scenario "convert_names")
+recipe main [
+  x:integer <- copy 0:literal
+]
++name: assign x 1
++run: instruction main/0
++mem: storing in location 1
+
+:(after "int main")
+Transform.push_back(transform_names);
+
+:(before "End Globals")
+unordered_map<recipe_number, unordered_map<string, int> > Name;
+
+:(code)
+void transform_names(const recipe_number r) {
+  unordered_map<string, int>& names = Name[r];
+  int curr_idx = 1;
+//?   cout << "Recipe " << r << '\n'; //? 2
+//?   cout << Recipe[r].steps.size(); //? 1
+  for (size_t i = 0; i < Recipe[r].steps.size(); ++i) {
+//?     cout << "instruction " << i << '\n'; //? 2
+    instruction& inst = Recipe[r].steps[i];
+    for (size_t in = 0; in < inst.ingredients.size(); ++in) {
+//?       cout << "ingredient " << inst.ingredients[in].name << '\n'; //? 1
+      if (inst.ingredients[in].types[0]
+          && inst.ingredients[in].name.find_first_not_of("0123456789-.") != string::npos) {
+        if (names.find(inst.ingredients[in].name) == names.end()) {
+          // todo: test
+          cerr << "user before set: " << inst.ingredients[in].name << " in " << Recipe[r].name << '\n';
+        }
+        inst.ingredients[in].value = names[inst.ingredients[in].name];
+        inst.ingredients[in].initialized = true;
+      }
+    }
+    for (size_t out = 0; out < inst.products.size(); ++out) {
+//?       cout << "product " << out << '/' << inst.products.size() << " " << inst.products[out].name << '\n'; //? 2
+//?       cout << inst.products[out].types[0] << '\n'; //? 1
+      if (inst.products[out].types[0]
+          && inst.products[out].name.find_first_not_of("0123456789-.") != string::npos) {
+        if (names.find(inst.products[out].name) == names.end()) {
+          trace("name") << "assign " << inst.products[out].name << " " << curr_idx;
+          names[inst.products[out].name] = curr_idx;
+          ++curr_idx;
+        }
+        inst.products[out].value = names[inst.products[out].name];
+        inst.products[out].initialized = true;
+      }
+    }
+  }
+}