From a654e4ecace2d506d1b10f1dde2c287ebe84ef37 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 26 Mar 2016 23:59:59 -0700 Subject: 2812 --- html/010vm.cc.html | 135 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 97 insertions(+), 38 deletions(-) (limited to 'html/010vm.cc.html') diff --git a/html/010vm.cc.html b/html/010vm.cc.html index 6814a1be..5cf2cb71 100644 --- a/html/010vm.cc.html +++ b/html/010vm.cc.html @@ -3,27 +3,35 @@ Mu - 010vm.cc - - + + - + + + + -
+
 //: A program is a book of 'recipes' (functions)
 :(before "End Globals")
 //: Each recipe is stored at a specific page number, or ordinal.
@@ -37,7 +45,7 @@ recipe_ordinal Next_recipe_ordinal = 1//: adding two phone numbers is meaningless. Here each recipe does something
 //: incommensurable with any other recipe.
 :(after "Types")
-typedef long long int recipe_ordinal;
+typedef int recipe_ordinal;
 
 :(before "End Types")
 // Recipes are lists of instructions. To perform or 'run' a recipe, the
@@ -60,7 +68,7 @@ recipe_ordinal Next_recipe_ordinal = 1;  // only if is_label
   string name;  // only if !is_label
   string old_name;  // before our automatic rewrite rules
-  string original_string;
+  string original_string;  // for error messages
   recipe_ordinal operation;  // get(Recipe_ordinal, name)
   vector<reagent> ingredients;  // only if !is_label
   vector<reagent> products;  // only if !is_label
@@ -125,7 +133,7 @@ recipe_ordinal Next_recipe_ordinal = 1:(before "End Globals")
 // Locations refer to a common 'memory'. Each location can store a number.
-map<long long int, double> Memory;
+map<int, double> Memory;
 :(before "End Setup")
 Memory.clear();
 
@@ -138,7 +146,7 @@ Memory.clear();
 // Unlike most computers today, mu stores types in a single big table, shared
 // by all the mu programs on the computer. This is useful in providing a
 // seamless experience to help understand arbitrary mu programs.
-typedef long long int type_ordinal;
+typedef int type_ordinal;
 :(before "End Globals")
 map<string, type_ordinal> Type_ordinal;
 map<type_ordinal, type_info> Type;
@@ -166,7 +174,7 @@ type_ordinal Next_type_ordinal = 1}
 void teardown_types() {
   for (map<type_ordinal, type_info>::iterator p = Type.begin(); p != Type.end(); ++p) {
-    for (long long int i = 0; i < SIZE(p->second.elements); ++i)
+    for (int i = 0; i < SIZE(p->second.elements); ++i)
       p->second.elements.clear();
   }
   Type_ordinal.clear();
@@ -195,7 +203,7 @@ atexit(teardown_types);struct type_info {
   string name;
   kind_of_type kind;
-  long long int size;  // only if type is not primitive; primitives and addresses have size 1 (except arrays are dynamic)
+  int size;  // only if type is not primitive; primitives and addresses have size 1 (except arrays are dynamic)
   vector<reagent> elements;
   // End type_info Fields
   type_info() :kind(PRIMITIVE), size(0) {}
@@ -234,6 +242,36 @@ assert(Next_recipe_ordinal < :(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
+}
+
 ^L
 
 //:: Helpers
@@ -309,7 +347,7 @@ reagent::reagent(const.name;
   value = old.value;
   initialized = old.initialized;
-  for (long long int i = 0; i < SIZE(old.properties); ++i) {
+  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));
   }
@@ -331,10 +369,10 @@ string_tree::string_tree(con
 
 reagent& reagent::operator=(const reagent& old) {
   original_string = old.original_string;
-  for (long long int i = 0; i < SIZE(properties); ++i)
+  for (int i = 0; i < SIZE(properties); ++i)
     if (properties.at(i).second) delete properties.at(i).second;
   properties.clear();
-  for (long long int i = 0; i < SIZE(old.properties); ++i)
+  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;
@@ -349,7 +387,7 @@ reagent::~reagent() {}
 
 void reagent::clear() {
-  for (long long int i = 0; i < SIZE(properties); ++i) {
+  for (int i = 0; i < SIZE(properties); ++i) {
     if (properties.at(i).second) {
       delete properties.at(i).second;
       properties.at(i).second = NULL;
@@ -381,14 +419,14 @@ string slurp_until(istream& in}
 
 bool has_property(reagent x, string name) {
-  for (long long int i = 0; i < SIZE(x.properties); ++i) {
+  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 (long long int p = 0; p != SIZE(r.properties); ++p) {
+  for (int p = 0; p != SIZE(r.properties); ++p) {
     if (r.properties.at(p).first == name)
       return r.properties.at(p).second;
   }
@@ -409,7 +447,7 @@ string_tree* property(const<
 }
 
 void dump_memory() {
-  for (map<long long int, double>::iterator p = Memory.begin(); p != Memory.end(); ++p) {
+  for (map<int, double>::iterator p = Memory.begin(); p != Memory.end(); ++p) {
     cout << p->first << ": " << no_scientific(p->second) << '\n';
   }
 }
@@ -424,7 +462,7 @@ string_tree* property(const<
 string to_string(const recipe& r) {
   ostringstream out;
   out << "recipe " << r.name << " [\n";
-  for (long long int i = 0; i < SIZE(r.steps); ++i)
+  for (int i = 0; i < SIZE(r.steps); ++i)
     out << "  " << to_string(r.steps.at(i)) << '\n';
   out << "]\n";
   return out.str();
@@ -434,49 +472,69 @@ string debug_string(const;
   out << "- recipe " << x.name << '\n';
   // Begin debug_string(recipe x)
-  for (long long int index = 0; index < SIZE(x.steps); ++index) {
+  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 (long long int i = 0; i < SIZE(inst.ingredients); ++i)
+    for (int i = 0; i < SIZE(inst.ingredients); ++i)
       out << "    " << debug_string(inst.ingredients.at(i)) << '\n';
     out << "  products\n";
-    for (long long int i = 0; i < SIZE(inst.products); ++i)
+    for (int i = 0; i < SIZE(inst.products); ++i)
       out << "    " << debug_string(inst.products.at(i)) << '\n';
   }
   return out.str();
 }
 
-string to_string(const instruction& inst) {
+string to_original_string(const instruction& inst) {
   if (inst.is_label) return inst.label;
   ostringstream out;
-  for (long long int i = 0; i < SIZE(inst.products); ++i) {
+  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 (long long int i = 0; i < SIZE(inst.ingredients); ++i) {
+  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()) {
-    out << ", {";
-    for (long long int i = 0; i < SIZE(r.properties); ++i) {
-      if (i > 0) out << ", ";
-      out << "\"" << r.properties.at(i).first << "\": " << to_string(r.properties.at(i).second);
-    }
-    out << "}";
+    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);
@@ -612,7 +670,7 @@ ostream& operator<<(const string& in) {
   if (in.empty()) return "";
-  long long int len = SIZE(in);
+  int len = SIZE(in);
   while (len > 1) {
     if (in.at(len-1) != '0') break;
     --len;
@@ -643,3 +701,4 @@ string trim_floating_point(c
 
+ -- cgit 1.4.1-2-gfad0