about summary refs log tree commit diff stats
path: root/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp')
-rw-r--r--cpp/literate/002trace4
-rw-r--r--cpp/literate/002trace.tests12
-rw-r--r--cpp/literate/010vm34
-rw-r--r--cpp/literate/011load126
-rw-r--r--cpp/literate/tangle/030tangle.cc2
-rw-r--r--cpp/literate/tangle/030tangle.test.cc18
6 files changed, 176 insertions, 20 deletions
diff --git a/cpp/literate/002trace b/cpp/literate/002trace
index 046f4154..4d584cb1 100644
--- a/cpp/literate/002trace
+++ b/cpp/literate/002trace
@@ -161,7 +161,7 @@ trace_stream* Trace_stream = NULL;
 #define trace(layer)  !Trace_stream ? cerr /*print nothing*/ : Trace_stream->stream(layer)
 // Warnings should go straight to cerr by default since calls to trace() have
 // some unfriendly constraints (they delay printing, they can't nest)
-#define RAISE  ((!Trace_stream || !Hide_warnings) ? cerr /*do print*/ : Trace_stream->stream("warn")) << __FILE__ << ":" << __LINE__ << " "
+#define raise  ((!Trace_stream || !Hide_warnings) ? cerr /*do print*/ : Trace_stream->stream("warn")) << __FILE__ << ":" << __LINE__ << " "
 
 // A separate helper for debugging. We should only trace domain-specific
 // facts. For everything else use log.
@@ -170,7 +170,7 @@ trace_stream* Trace_stream = NULL;
 #define log cerr
 
 :(before "End Types")
-// RAISE << die exits after printing -- unless Hide_warnings is set.
+// raise << die exits after printing -- unless Hide_warnings is set.
 struct die {};
 :(before "End Tracing")
 ostream& operator<<(ostream& os, unused die) {
diff --git a/cpp/literate/002trace.tests b/cpp/literate/002trace.tests
index e0db457c..00705346 100644
--- a/cpp/literate/002trace.tests
+++ b/cpp/literate/002trace.tests
@@ -117,12 +117,12 @@ void test_trace_supports_count2() {
 
 // pending: DUMP tests
 // pending: readable_contents() adds newline if necessary.
-// pending: RAISE also prints to stderr.
-// pending: RAISE doesn't print to stderr if Hide_warnings is set.
-// pending: RAISE doesn't have to be saved if Hide_warnings is set, just printed.
-// pending: RAISE prints to stderr if Trace_stream is NULL.
-// pending: RAISE prints to stderr if Trace_stream is NULL even if Hide_warnings is set.
-// pending: RAISE << ... die() doesn't die if Hide_warnings is set.
+// pending: raise also prints to stderr.
+// pending: raise doesn't print to stderr if Hide_warnings is set.
+// pending: raise doesn't have to be saved if Hide_warnings is set, just printed.
+// pending: raise prints to stderr if Trace_stream is NULL.
+// pending: raise prints to stderr if Trace_stream is NULL even if Hide_warnings is set.
+// pending: raise << ... die() doesn't die if Hide_warnings is set.
 
 
 
diff --git a/cpp/literate/010vm b/cpp/literate/010vm
index 8d427c43..4ae5c16a 100644
--- a/cpp/literate/010vm
+++ b/cpp/literate/010vm
@@ -14,8 +14,6 @@ struct recipe {
   vector<instruction> step;
 };
 
-const int idle = 0;  // always the first entry in the recipe book
-
 :(before "struct recipe")
 // Each instruction is either of the form:
 //   product1, product2, product3, ... <- operation ingredient1, ingredient2, ingredient3, ...
@@ -53,6 +51,8 @@ struct property {
 :(before "End Globals")
 // Locations refer to a common 'memory'. Each location can store a number.
 unordered_map<int, int> Memory;
+:(before "End Setup")
+  Memory.clear();
 
 :(after "Types")
 // Types encode how the numbers stored in different parts of memory are
@@ -68,6 +68,16 @@ typedef int type_number;
 unordered_map<string, type_number> Type_number;
 unordered_map<type_number, type_info> Type;
 int Next_type_number = 1;
+:(code)
+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;
+}
+:(before "End Setup")
+  setup_types();
 
 :(before "End Types")
 // You can construct arbitrary new types. Types are either 'records', containing
@@ -83,6 +93,22 @@ struct type_info {
   type_info() :size(0) {}
 };
 
+:(code)
+// It's all very well to construct recipes out of other recipes, but we need
+// to know how to do *something* out of the box. For the following
+// recipes there are only codes, no entries in the book, because mu just knows
+// what to do for them.
+void setup_recipes() {
+  Recipe.clear();  Recipe_number.clear();
+  Recipe_number["idle"] = 0;
+  Next_recipe_number = 1;
+  Recipe_number["copy"] = Next_recipe_number++;
+}
+:(before "End Types")
+const int idle = 0;  // always the first entry in the recipe book
+:(before "End Setup")
+  setup_recipes();
+
 
 
 :(code)
@@ -113,3 +139,7 @@ string slurp_until(istream& in, char delim) {
   }
   return out.str();
 }
+
+
+
+:(before "End Setup")
diff --git a/cpp/literate/011load b/cpp/literate/011load
new file mode 100644
index 00000000..5811f77a
--- /dev/null
+++ b/cpp/literate/011load
@@ -0,0 +1,126 @@
+// It's often convenient to express recipes in a textual fashion.
+:(scenario add_recipe)
+recipe main [
+  1:integer <- copy 23:literal
+]
++parse: instruction: 1
++parse:   ingredient: {name: "23", type: 0}
++parse:   product: {name: "1", type: 1}
+
+:(code)
+void run(string text) {
+  add_recipe(text);
+}
+
+void add_recipe(string form) {
+  istringstream in(form);
+  in >> std::noskipws;
+
+  string _recipe = next_word(in);
+  if (_recipe != "recipe")
+    raise << "top-level forms must be of the form 'recipe _name_ [ _instruction_ ... ]'\n";
+
+  string recipe_name = next_word(in);
+  if (recipe_name.empty())
+    raise << "empty recipe name in " << form << '\n';
+  int r = Recipe_number[recipe_name] = Next_recipe_number++;
+
+  if (next_word(in) != "[")
+    raise << "recipe body must begin with '['\n";
+
+  skip_newlines(in);
+
+  instruction curr;
+  while (next_instruction(in, &curr)) {
+    Recipe[r].step.push_back(curr);
+  }
+}
+
+bool next_instruction(istream& in, instruction* curr) {
+  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> 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;
+
+  if (words.size() == 1 && *(words[0].end()-1) == ':') {
+    curr->is_label = true;
+    words[0].erase(words[0].end()-1);
+    curr->label = words[0];
+    trace("parse") << "label: " << curr->label;
+    return !in.eof();
+  }
+
+  vector<string>::iterator p = words.begin();
+  if (find(words.begin(), words.end(), "<-") != words.end()) {
+    for (; *p != "<-"; ++p) {
+      if (*p == ",") continue;
+      curr->products.push_back(reagent(*p));
+    }
+    ++p;  // skip <-
+  }
+
+  curr->operation = Recipe_number[*p];  ++p;
+
+  for (; p != words.end(); ++p) {
+    if (*p == ",") continue;
+    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();
+}
+
+string next_word(istream& in) {
+  ostringstream out;
+  skip_whitespace(in);
+  slurp_word(in, out);
+  return out.str();
+}
+
+void slurp_word(istream& in, ostream& out) {
+  char c;
+  if (in.peek() == ',') {
+    in >> c;
+    out << c;
+    return;
+  }
+  while (in >> c) {
+    if (isspace(c) || c == ',') {
+      in.putback(c);
+      break;
+    }
+    out << c;
+  }
+}
+
+void skip_whitespace(istream& in) {
+  while (isspace(in.peek()) && in.peek() != '\n') {
+    in.get();
+  }
+}
+
+void skip_newlines(istream& in) {
+  while (in.peek() == '\n')
+    in.get();
+}
+
+void skip_comma(istream& in) {
+  skip_whitespace(in);
+  if (in.peek() == ',') in.get();
+  skip_whitespace(in);
+}
diff --git a/cpp/literate/tangle/030tangle.cc b/cpp/literate/tangle/030tangle.cc
index 2bf193f4..2dda8667 100644
--- a/cpp/literate/tangle/030tangle.cc
+++ b/cpp/literate/tangle/030tangle.cc
@@ -210,7 +210,7 @@ void emit_test(const string& name, list<string>& lines, list<string>& result) {
 }
 
 void emit_session(list<string>& lines, list<string>& result) {
-  result.push_back("  rmref("+Toplevel+"(\""+input_lines(lines)+"\"));");
+  result.push_back("  "+Toplevel+"(\""+input_lines(lines)+"\");");
 }
 
 void emit_result_checking_session(list<string>& lines, list<string>& result) {
diff --git a/cpp/literate/tangle/030tangle.test.cc b/cpp/literate/tangle/030tangle.test.cc
index 307486fa..36ce2d1f 100644
--- a/cpp/literate/tangle/030tangle.test.cc
+++ b/cpp/literate/tangle/030tangle.test.cc
@@ -85,7 +85,7 @@ void test_tangle_supports_scenarios() {
   list<string> lines;
   tangle(in, lines);
   CHECK_EQ(lines.front(), "void test_does_bar() {");  lines.pop_front();
-  CHECK_EQ(lines.front(), "  rmref(run(\"abc def\\n\"));");  lines.pop_front();
+  CHECK_EQ(lines.front(), "  run(\"abc def\\n\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "  CHECK_TRACE_CONTENTS(\"layer1: pqrlayer2: xyz\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "}");  lines.pop_front();
   CHECK(lines.empty());
@@ -96,7 +96,7 @@ void test_tangle_supports_configurable_toplevel() {
   list<string> lines;
   tangle(in, lines);
   CHECK_EQ(lines.front(), "void test_does_bar() {");  lines.pop_front();
-  CHECK_EQ(lines.front(), "  rmref(foo(\"abc def\\n\"));");  lines.pop_front();
+  CHECK_EQ(lines.front(), "  foo(\"abc def\\n\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "  CHECK_TRACE_CONTENTS(\"layer1: pqr\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "}");  lines.pop_front();
   CHECK(lines.empty());
@@ -110,7 +110,7 @@ void test_tangle_supports_strings_in_scenarios() {
   list<string> lines;
   tangle(in, lines);
   CHECK_EQ(lines.front(), "void test_does_bar() {");  lines.pop_front();
-  CHECK_EQ(lines.front(), "  rmref(run(\"abc \\\"def\\\"\\n\"));");  lines.pop_front();
+  CHECK_EQ(lines.front(), "  run(\"abc \\\"def\\\"\\n\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "  CHECK_TRACE_CONTENTS(\"layer1: pqrlayer2: \\\"xyz\\\"\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "}");  lines.pop_front();
   CHECK(lines.empty());
@@ -121,7 +121,7 @@ void test_tangle_supports_strings_in_scenarios2() {
   list<string> lines;
   tangle(in, lines);
   CHECK_EQ(lines.front(), "void test_does_bar() {");  lines.pop_front();
-  CHECK_EQ(lines.front(), "  rmref(run(\"abc \\\"\\\"\\n\"));");  lines.pop_front();
+  CHECK_EQ(lines.front(), "  run(\"abc \\\"\\\"\\n\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "  CHECK_TRACE_CONTENTS(\"layer1: pqrlayer2: \\\"\\\"\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "}");  lines.pop_front();
   CHECK(lines.empty());
@@ -132,7 +132,7 @@ void test_tangle_supports_multiline_input_in_scenarios() {
   list<string> lines;
   tangle(in, lines);
   CHECK_EQ(lines.front(), "void test_does_bar() {");  lines.pop_front();
-  CHECK_EQ(lines.front(), "  rmref(run(\"abc def\\n  efg\\n\"));");  lines.pop_front();
+  CHECK_EQ(lines.front(), "  run(\"abc def\\n  efg\\n\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "  CHECK_TRACE_CONTENTS(\"layer1: pqrlayer2: \\\"\\\"\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "}");  lines.pop_front();
   CHECK(lines.empty());
@@ -143,9 +143,9 @@ void test_tangle_supports_reset_in_scenarios() {
   list<string> lines;
   tangle(in, lines);
   CHECK_EQ(lines.front(), "void test_does_bar() {");  lines.pop_front();
-  CHECK_EQ(lines.front(), "  rmref(run(\"abc def\\n\"));");  lines.pop_front();
+  CHECK_EQ(lines.front(), "  run(\"abc def\\n\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "  CLEAR_TRACE;");  lines.pop_front();
-  CHECK_EQ(lines.front(), "  rmref(run(\"efg\\n\"));");  lines.pop_front();
+  CHECK_EQ(lines.front(), "  run(\"efg\\n\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "  CHECK_TRACE_CONTENTS(\"layer1: pqrlayer2: \\\"\\\"\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "}");  lines.pop_front();
   CHECK(lines.empty());
@@ -156,7 +156,7 @@ void test_tangle_can_check_for_absence_at_end_of_scenarios() {
   list<string> lines;
   tangle(in, lines);
   CHECK_EQ(lines.front(), "void test_does_bar() {");  lines.pop_front();
-  CHECK_EQ(lines.front(), "  rmref(run(\"abc def\\n  efg\\n\"));");  lines.pop_front();
+  CHECK_EQ(lines.front(), "  run(\"abc def\\n  efg\\n\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "  CHECK_TRACE_CONTENTS(\"layer1: pqr\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "  CHECK_TRACE_DOESNT_CONTAIN(\"layer1: xyz\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "}");  lines.pop_front();
@@ -168,7 +168,7 @@ void test_tangle_can_check_for_absence_at_end_of_scenarios2() {
   list<string> lines;
   tangle(in, lines);
   CHECK_EQ(lines.front(), "void test_does_bar() {");  lines.pop_front();
-  CHECK_EQ(lines.front(), "  rmref(run(\"abc def\\n  efg\\n\"));");  lines.pop_front();
+  CHECK_EQ(lines.front(), "  run(\"abc def\\n  efg\\n\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "  CHECK_TRACE_DOESNT_CONTAIN(\"layer1: pqr\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "  CHECK_TRACE_DOESNT_CONTAIN(\"layer1: xyz\");");  lines.pop_front();
   CHECK_EQ(lines.front(), "}");  lines.pop_front();
016-12-21 05:06:55 +0100 committer nfnty <git@nfnty.se> 2017-01-17 05:59:02 +0100 linting: pylint and flake8' href='/akspecs/ranger/commit/ranger/ext/rifle.py?id=b3d031a913814900467358b2adf20a148bf6de1a'>b3d031a9 ^
d1a1173d ^









b3d031a9 ^

d1a1173d ^

b3d031a9 ^

d1a1173d ^

b3d031a9 ^

d1a1173d ^

b3d031a9 ^

d1a1173d ^






b3d031a9 ^


d1a1173d ^

51ec08da ^
d1a1173d ^


76791a70 ^
d1a1173d ^




b3d031a9 ^
d1a1173d ^
80993595 ^
b3d031a9 ^
80993595 ^
d1a1173d ^
17aa0b54 ^

1687e0f4 ^






b3d031a9 ^
d1a1173d ^











b3d031a9 ^

d1a1173d ^








88c1fd2f ^





d1a1173d ^










185c022e ^
d1a1173d ^
76788499 ^





d1a1173d ^














c7d5b898 ^
0e750416 ^

d1a1173d ^


185c022e ^
d1a1173d ^







b3d031a9 ^
d1a1173d ^

51ec08da ^
d1a1173d ^








51ec08da ^
d1a1173d ^


b68d28c1 ^

d1a1173d ^





















b3d031a9 ^

b68d28c1 ^
d1a1173d ^
































b3d031a9 ^
d1a1173d ^

























65d452b9 ^





d1a1173d ^

76791a70 ^
d1a1173d ^






b3d031a9 ^

d1a1173d ^

45dd1a43 ^
4ead0882 ^
24dd1af4 ^
d1a1173d ^




8bcfc198 ^
d1a1173d ^

76791a70 ^
8bcfc198 ^








24dd1af4 ^










d1a1173d ^
b3d031a9 ^
ab67be4f ^
d1a1173d ^
76791a70 ^

d1a1173d ^
76791a70 ^
d1a1173d ^
76791a70 ^


d1a1173d ^
76791a70 ^
24dd1af4 ^

d1a1173d ^




24dd1af4 ^












8bcfc198 ^
d1a1173d ^







b3d031a9 ^

d1a1173d ^



76791a70 ^
d1a1173d ^



51ec08da ^
d1a1173d ^

b3d031a9 ^
185c022e ^
b3d031a9 ^
49665d24 ^
6f8ca83b ^
d1a1173d ^




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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479