about summary refs log tree commit diff stats
path: root/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp')
-rw-r--r--cpp/002main.cc26
-rw-r--r--cpp/002main.test.cc2
2 files changed, 27 insertions, 1 deletions
diff --git a/cpp/002main.cc b/cpp/002main.cc
index 5f057946..0eaea16a 100644
--- a/cpp/002main.cc
+++ b/cpp/002main.cc
@@ -116,7 +116,12 @@ struct reagent {
   reagent(string s) {
     istringstream in(s);
     name = slurp_until(in, ':');
-    types.push_back(Type_number[slurp_until(in, '/')]);
+    types.push_back(Type_number[slurp_until(in, '/')]);  // todo: multiple types
+  }
+  string to_string() {
+    ostringstream out;
+    out << "{name: \"" << name << "\", type: " << types[0] << "}\n";  // todo: properties
+    return out.str();
   }
 };
 
@@ -152,10 +157,19 @@ void setup_memory() {
 }
 
 void setup_types() {
+  Type.clear();  Type_number.clear();
+  Type_number["literal"] = 0;
+  Next_type_number = 1;
   int integer = Type_number["integer"] = Next_type_number++;
   Type[integer].size = 1;
 }
 
+void setup_recipes() {
+  Recipe.clear();  Recipe_number.clear();  Next_recipe_number = 1;
+  Recipe_number["copy"] = Next_recipe_number++;
+//?   dbg << "AAA " << Recipe_number["copy"] << '\n'; //? 1
+}
+
 void compile(string form) {
   istringstream in(form);
   in >> std::noskipws;
@@ -222,9 +236,18 @@ bool next_instruction(istream& in, instruction* curr) {
   curr->operation = Recipe_number[*p];  ++p;
 
   for (; p != words.end(); ++p) {
+    if (*p == ",") continue;
 //?     cout << "ingredient: " << *p << '\n'; //? 1
     curr->ingredients.push_back(reagent(*p));
   }
+
+  trace("parse") << "instruction: " << curr->operation;
+  for (vector<reagent>::iterator p = curr->ingredients.begin(); p != curr->ingredients.end(); ++p) {
+    trace("parse") << "  ingredient: " << p->to_string();
+  }
+  for (vector<reagent>::iterator p = curr->products.begin(); p != curr->products.end(); ++p) {
+    trace("parse") << "  product: " << p->to_string();
+  }
   return !in.eof();
 }
 
@@ -316,6 +339,7 @@ void verify() {
 void setup() {
   setup_memory();
   setup_types();
+  setup_recipes();
   Passed = true;
 }
 
diff --git a/cpp/002main.test.cc b/cpp/002main.test.cc
index 36e6a669..52db64bb 100644
--- a/cpp/002main.test.cc
+++ b/cpp/002main.test.cc
@@ -2,6 +2,7 @@ void test_parse() {
   compile("recipe main [\n"
           "  1:integer <- copy 23:literal\n"
           "]\n");
+  cout << '\n'; DUMP("parse");
   CHECK(Recipe_number.find("main") != Recipe_number.end());
   recipe r = Recipe[Recipe_number["main"]];
   vector<instruction>::iterator i = r.step.begin();
@@ -24,6 +25,7 @@ void test_parse2() {
   compile("recipe main [\n"
           "  1:integer, 2:integer <- copy 23:literal\n"
           "]\n");
+  cout << '\n'; DUMP("parse");
   CHECK(Recipe_number.find("main") != Recipe_number.end());
   recipe r = Recipe[Recipe_number["main"]];
   vector<instruction>::iterator i = r.step.begin();