about summary refs log tree commit diff stats
path: root/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp')
-rw-r--r--cpp/.traces/container5
-rw-r--r--cpp/.traces/exclusive_container5
-rw-r--r--cpp/014types89
-rw-r--r--cpp/020run4
4 files changed, 102 insertions, 1 deletions
diff --git a/cpp/.traces/container b/cpp/.traces/container
new file mode 100644
index 00000000..4f0dac52
--- /dev/null
+++ b/cpp/.traces/container
@@ -0,0 +1,5 @@
+parse/0: reading container foo
+parse/0:   element name: x
+parse/0:   type: 1
+parse/0:   element name: y
+parse/0:   type: 1
diff --git a/cpp/.traces/exclusive_container b/cpp/.traces/exclusive_container
new file mode 100644
index 00000000..ec1376fa
--- /dev/null
+++ b/cpp/.traces/exclusive_container
@@ -0,0 +1,5 @@
+parse/0: reading exclusive-container foo
+parse/0:   element name: x
+parse/0:   type: 1
+parse/0:   element name: y
+parse/0:   type: 1
diff --git a/cpp/014types b/cpp/014types
new file mode 100644
index 00000000..867cb28d
--- /dev/null
+++ b/cpp/014types
@@ -0,0 +1,89 @@
+//: Textual form for types.
+
+:(scenario "container")
+container foo [
+  x:integer
+  y:integer
+]
++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'; //? 1
+  assert(Type_number.find(name) == Type_number.end());
+  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_comments_and_newlines(in);
+    skip_whitespace(in);
+    string element = next_word(in);
+    skip_comments_and_newlines(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:integer
+  y:integer
+]
++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 (size_t i = 0; i < recently_added_types.size(); ++i) {
+//?   cout << "erasing " << Type[recently_added_types[i]].name << '\n'; //? 1
+  Type_number.erase(Type[recently_added_types[i]].name);
+  Type.erase(recently_added_types[i]);
+}
+recently_added_types.clear();
+Next_type_number = 1000;
+:(before "End One-time Setup")
+assert(Next_type_number < 1000);
+Next_type_number = 1000;
+
+//: lastly, ensure scenarios are consistent by always starting 
diff --git a/cpp/020run b/cpp/020run
index 1de1c6ae..400524e2 100644
--- a/cpp/020run
+++ b/cpp/020run
@@ -125,7 +125,9 @@ void load(string filename) {
 //: On startup, load everything in core.mu
 :(before "End Load Recipes")
 load("core.mu");
-recently_added_recipes.clear();  // freeze everything so it doesn't get cleared by tests
+// freeze everything so it doesn't get cleared by tests
+recently_added_recipes.clear();
+recently_added_types.clear();
 
 :(code)
 // helper for tests