about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-05-14 10:30:01 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-05-14 10:30:01 -0700
commitb98d3876b67a35f1d913ba374749bc97103b7790 (patch)
tree7619491997d932ce5f59ef4a0af0a1e801e7cd1f
parent064aaaa4327f2ea9eb9a52b8ff2b22e168861eb1 (diff)
downloadmu-b98d3876b67a35f1d913ba374749bc97103b7790.tar.gz
1367
-rw-r--r--014types.cc100
-rw-r--r--020run.cc2
-rw-r--r--030container.cc87
-rw-r--r--034exclusive_container.cc18
4 files changed, 106 insertions, 101 deletions
diff --git a/014types.cc b/014types.cc
deleted file mode 100644
index 8ff3f722..00000000
--- a/014types.cc
+++ /dev/null
@@ -1,100 +0,0 @@
-//: Textual form for types.
-
-:(scenarios load)
-:(scenario container)
-container foo [
-  x:number
-  y:number
-]
-+parse: reading container foo
-+parse:   element name: x
-+parse:   type: 1
-+parse:   element name: y
-+parse:   type: 1
-
-:(before "End Command Handlers")
-else if (command == "container") {
-  insert_container(command, container, in);
-}
-
-:(code)
-void insert_container(const string& command, kind_of_type kind, istream& in) {
-  skip_whitespace(in);
-  string name = next_word(in);
-  trace("parse") << "reading " << command << ' ' << name;
-//?   cout << name << '\n'; //? 2
-//?   if (Type_number.find(name) != Type_number.end()) //? 1
-//?     cerr << Type_number[name] << '\n'; //? 1
-  if (Type_number.find(name) == Type_number.end()
-      || Type_number[name] == 0) {
-    Type_number[name] = Next_type_number++;
-  }
-  skip_bracket(in, "'container' must begin with '['");
-  assert(Type.find(Type_number[name]) == Type.end());
-  type_info& t = Type[Type_number[name]];
-  recently_added_types.push_back(Type_number[name]);
-  t.name = name;
-  t.kind = kind;
-  while (!in.eof()) {
-    skip_whitespace_and_comments(in);
-    string element = next_word(in);
-    if (element == "]") break;
-    istringstream inner(element);
-    t.element_names.push_back(slurp_until(inner, ':'));
-    trace("parse") << "  element name: " << t.element_names.back();
-    vector<type_number> types;
-    while (!inner.eof()) {
-      string type_name = slurp_until(inner, ':');
-      if (Type_number.find(type_name) == Type_number.end())
-        raise << "unknown type " << type_name << '\n';
-      types.push_back(Type_number[type_name]);
-      trace("parse") << "  type: " << types.back();
-    }
-    t.elements.push_back(types);
-  }
-  assert(t.elements.size() == t.element_names.size());
-  t.size = t.elements.size();
-}
-
-//:: Similarly for exclusive_container.
-
-:(scenario exclusive_container)
-exclusive-container foo [
-  x:number
-  y:number
-]
-+parse: reading exclusive-container foo
-+parse:   element name: x
-+parse:   type: 1
-+parse:   element name: y
-+parse:   type: 1
-
-:(before "End Command Handlers")
-else if (command == "exclusive-container") {
-  insert_container(command, exclusive_container, in);
-}
-
-//:: ensure types created in one scenario don't leak outside it.
-:(before "End Globals")
-vector<type_number> recently_added_types;
-:(before "End Setup")
-for (index_t i = 0; i < recently_added_types.size(); ++i) {
-//?   cout << "erasing " << Type[recently_added_types.at(i)].name << '\n'; //? 1
-  Type_number.erase(Type[recently_added_types.at(i)].name);
-  Type.erase(recently_added_types.at(i));
-}
-recently_added_types.clear();
-//: lastly, ensure scenarios are consistent by always starting them at the
-//: same type number.
-Next_type_number = 1000;
-:(before "End Test Run Initialization")
-assert(Next_type_number < 1000);
-:(before "End Setup")
-Next_type_number = 1000;
-
-:(code)
-void skip_bracket(istream& in, string message) {
-  skip_whitespace_and_comments(in);
-  if (in.get() != '[')
-    raise << message << '\n';
-}
diff --git a/020run.cc b/020run.cc
index 288ed708..635356c4 100644
--- a/020run.cc
+++ b/020run.cc
@@ -160,7 +160,7 @@ void load_permanently(string filename) {
   fin.close();
   // freeze everything so it doesn't get cleared by tests
   recently_added_recipes.clear();
-  recently_added_types.clear();
+  // End load_permanently.
 }
 
 //:: On startup, load everything in core.mu
diff --git a/030container.cc b/030container.cc
index 5c27de16..6df68c0a 100644
--- a/030container.cc
+++ b/030container.cc
@@ -189,3 +189,90 @@ case GET_ADDRESS: {
   products.at(0).push_back(result);
   break;
 }
+
+//:: Allow containers to be defined in mu code.
+
+:(scenarios load)
+:(scenario container)
+container foo [
+  x:number
+  y:number
+]
++parse: reading container foo
++parse:   element name: x
++parse:   type: 1
++parse:   element name: y
++parse:   type: 1
+
+:(before "End Command Handlers")
+else if (command == "container") {
+  insert_container(command, container, in);
+}
+
+:(code)
+void insert_container(const string& command, kind_of_type kind, istream& in) {
+  skip_whitespace(in);
+  string name = next_word(in);
+  trace("parse") << "reading " << command << ' ' << name;
+//?   cout << name << '\n'; //? 2
+//?   if (Type_number.find(name) != Type_number.end()) //? 1
+//?     cerr << Type_number[name] << '\n'; //? 1
+  if (Type_number.find(name) == Type_number.end()
+      || Type_number[name] == 0) {
+    Type_number[name] = Next_type_number++;
+  }
+  skip_bracket(in, "'container' must begin with '['");
+  assert(Type.find(Type_number[name]) == Type.end());
+  type_info& t = Type[Type_number[name]];
+  recently_added_types.push_back(Type_number[name]);
+  t.name = name;
+  t.kind = kind;
+  while (!in.eof()) {
+    skip_whitespace_and_comments(in);
+    string element = next_word(in);
+    if (element == "]") break;
+    istringstream inner(element);
+    t.element_names.push_back(slurp_until(inner, ':'));
+    trace("parse") << "  element name: " << t.element_names.back();
+    vector<type_number> types;
+    while (!inner.eof()) {
+      string type_name = slurp_until(inner, ':');
+      if (Type_number.find(type_name) == Type_number.end())
+        raise << "unknown type " << type_name << '\n';
+      types.push_back(Type_number[type_name]);
+      trace("parse") << "  type: " << types.back();
+    }
+    t.elements.push_back(types);
+  }
+  assert(t.elements.size() == t.element_names.size());
+  t.size = t.elements.size();
+}
+
+//: ensure types created in one scenario don't leak outside it.
+:(before "End Globals")
+vector<type_number> recently_added_types;
+:(before "End load_permanently")  //: for non-tests
+recently_added_types.clear();
+:(before "End Setup")  //: for tests
+for (index_t i = 0; i < recently_added_types.size(); ++i) {
+//?   cout << "erasing " << Type[recently_added_types.at(i)].name << '\n'; //? 1
+  Type_number.erase(Type[recently_added_types.at(i)].name);
+  Type.erase(recently_added_types.at(i));
+}
+recently_added_types.clear();
+//: lastly, ensure scenarios are consistent by always starting them at the
+//: same type number.
+Next_type_number = 1000;
+:(before "End Test Run Initialization")
+assert(Next_type_number < 1000);
+:(before "End Setup")
+Next_type_number = 1000;
+
+//:: helpers
+
+:(code)
+void skip_bracket(istream& in, string message) {
+  skip_whitespace_and_comments(in);
+  if (in.get() != '[')
+    raise << message << '\n';
+}
diff --git a/034exclusive_container.cc b/034exclusive_container.cc
index 97150af4..6dc7a82b 100644
--- a/034exclusive_container.cc
+++ b/034exclusive_container.cc
@@ -105,3 +105,21 @@ case MAYBE_CONVERT: {
   products.at(0).push_back(result);
   break;
 }
+
+//:: Allow exclusive containers to be defined in mu code.
+
+:(scenario exclusive_container)
+exclusive-container foo [
+  x:number
+  y:number
+]
++parse: reading exclusive-container foo
++parse:   element name: x
++parse:   type: 1
++parse:   element name: y
++parse:   type: 1
+
+:(before "End Command Handlers")
+else if (command == "exclusive-container") {
+  insert_container(command, exclusive_container, in);
+}