about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cpp/002main.cc70
-rw-r--r--cpp/002main.test.cc18
2 files changed, 82 insertions, 6 deletions
diff --git a/cpp/002main.cc b/cpp/002main.cc
index 1476150c..a2bc0729 100644
--- a/cpp/002main.cc
+++ b/cpp/002main.cc
@@ -113,6 +113,11 @@ struct reagent {
   string name;
   vector<type_number> types;
   vector<pair<string, property> > properties;
+  reagent(string s) {
+    istringstream in(s);
+    name = slurp_until(in, ':');
+    types.push_back(Type_number[slurp_until(in, '/')]);
+  }
 };
 
 const int idle = 0;
@@ -124,6 +129,7 @@ struct instruction {
   vector<reagent> ingredients;  // only if !is_label
   vector<reagent> products;  // only if !is_label
   instruction() :is_label(false), operation(idle) {}
+  void clear() { is_label=false; label.clear(); operation=idle; ingredients.clear(); products.clear(); }
 };
 
 struct recipe {
@@ -173,14 +179,51 @@ void compile(string form) {
   skip_newlines(in);
 
   instruction curr;
-//?   while (next_instruction(in, &curr)) { //? 1
-//?     Recipe[r].step.push_back(curr); //? 1
-//?   } //? 1
+  while (next_instruction(in, &curr)) {
+    Recipe[r].step.push_back(curr);
+  }
 }
 
 bool next_instruction(istream& in, instruction* curr) {
-  skip_whitespace(in);
-  skip_newlines(in);
+  curr->clear();
+  if (in.eof()) return false;
+  skip_whitespace(in);  if (in.eof()) return false;
+  skip_newlines(in);  if (in.eof()) return false;
+
+//?   vector<string> ingredients, products; //? 1
+  vector<string> words;
+  while (in.peek() != '\n') {
+    skip_whitespace(in);  if (in.eof()) return false;
+    string word = next_word(in);  if (in.eof()) return false;
+    words.push_back(word);
+    skip_whitespace(in);  if (in.eof()) return false;
+  }
+  skip_newlines(in);  if (in.eof()) return false;
+//?   cout << "words: "; //? 1
+//?   for (vector<string>::iterator p = words.begin(); p != words.end(); ++p) { //? 1
+//?     cout << *p << "; "; //? 1
+//?   } //? 1
+//?   cout << '\n'; //? 1
+//?   return true; //? 1
+
+  vector<string>::iterator p = words.begin();
+  if (find(words.begin(), words.end(), "<-") != words.end()) {
+//?     cout << "instruction yields products\n"; //? 1
+    for (; *p != "<-"; ++p) {
+//?       cout << "product: " << *p << '\n'; //? 1
+//?       products.push_back(*p); //? 1
+      curr->products.push_back(reagent(*p));
+    }
+    ++p;  // skip <-
+  }
+//?   return true; //? 1
+
+  curr->operation = Recipe_number[*p];  ++p;
+
+  for (; p != words.end(); ++p) {
+//?     cout << "ingredient: " << *p << '\n'; //? 1
+    curr->ingredients.push_back(reagent(*p));
+  }
   return !in.eof();
 }
 
@@ -197,6 +240,11 @@ string next_word(istream& in) {
 
 void slurp_word(istream& in, ostream& out) {
   char c;
+  if (in.peek() == ',') {
+    in >> c;
+    out << c;
+    return;
+  }
   while (in >> c) {
 //?     cout << c << '\n'; //? 1
     if (isspace(c) || c == ',') {
@@ -226,6 +274,18 @@ void skip_comma(istream& in) {
   skip_whitespace(in);
 }
 
+string slurp_until(istream& in, char delim) {
+  ostringstream out;
+  char c;
+  while (in >> c) {
+    if (c == delim) {
+      break;
+    }
+    out << c;
+  }
+  return out.str();
+}
+
 
 
 //// test harness
diff --git a/cpp/002main.test.cc b/cpp/002main.test.cc
index 8608d724..77d0ee38 100644
--- a/cpp/002main.test.cc
+++ b/cpp/002main.test.cc
@@ -1,8 +1,24 @@
-void test_compile() {
+void test_parse() {
   compile("recipe main [\n"
           "  1:integer <- copy 23:literal\n"
+//?           "  1:integer, 2:integer <- copy 23:literal\n"
           "]\n");
   CHECK(Recipe_number.find("main") != Recipe_number.end());
+  recipe r = Recipe[Recipe_number["main"]];
+  vector<instruction>::iterator i = r.step.begin();
+  CHECK_EQ(i->is_label, false);
+  CHECK_EQ(i->label, "");
+  CHECK_EQ(i->operation, Recipe_number["copy"]);
+  CHECK_EQ(i->ingredients.size(), 1);
+  CHECK_EQ(i->ingredients[0].name, string("23"));
+  CHECK_EQ(i->ingredients[0].types.size(), 1);
+  CHECK_EQ(i->ingredients[0].types[0], Type_number["literal"]);
+  CHECK_EQ(i->ingredients[0].properties.size(), 0);
+  CHECK_EQ(i->products.size(), 1);
+  CHECK_EQ(i->products[0].name, string("1"));
+  CHECK_EQ(i->products[0].types.size(), 1);
+  CHECK_EQ(i->products[0].types[0], Type_number["integer"]);
+  CHECK_EQ(i->products[0].properties.size(), 0);
 }
 
 void test_literal() {
le='Blame the previous revision' href='/ahoang/Nim/blame/compiler/parampatterns.nim?h=devel&id=355ae07b8f3362af4e90770477d344dcd2fef594'>^
af7c92c00 ^



438703f59 ^
af7c92c00 ^





438703f59 ^
af7c92c00 ^

355ae07b8 ^
af7c92c00 ^















438703f59 ^
92b8fac94 ^
af7c92c00 ^




5d63ecb3a ^
af7c92c00 ^

5d63ecb3a ^
af7c92c00 ^




30c00aba0 ^
af7c92c00 ^







b64eeeb43 ^

af7c92c00 ^









5d63ecb3a ^

af7c92c00 ^





d4bca58b7 ^

b64eeeb43 ^



bf90b9c83 ^

b64eeeb43 ^
ab26298a0 ^
b64eeeb43 ^



ab26298a0 ^


d4bca58b7 ^
b64eeeb43 ^



8dadeebd0 ^

bf90b9c83 ^


d4bca58b7 ^


b64eeeb43 ^
5b0d8246f ^
ab26298a0 ^
d4bca58b7 ^
b64eeeb43 ^
d4bca58b7 ^
5b0d8246f ^
d4bca58b7 ^
b64eeeb43 ^

ab26298a0 ^
d4bca58b7 ^
b64eeeb43 ^

5b0d8246f ^

ab26298a0 ^
b64eeeb43 ^

ab26298a0 ^
d4bca58b7 ^
b64eeeb43 ^
d4bca58b7 ^
ab26298a0 ^
d0438540d ^

2a797c362 ^
ab26298a0 ^
2a797c362 ^


b64eeeb43 ^
346443d1b ^
b64eeeb43 ^
d4bca58b7 ^
bf90b9c83 ^
d4bca58b7 ^
af7c92c00 ^
d4bca58b7 ^
af7c92c00 ^
438703f59 ^
af7c92c00 ^


d4bca58b7 ^
af7c92c00 ^


d4bca58b7 ^
af7c92c00 ^




d4bca58b7 ^
af7c92c00 ^


















b64eeeb43 ^

355ae07b8 ^

af7c92c00 ^

d4bca58b7 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282