1 //: Phase 2: Filter loaded recipes through an extensible list of 'transforms'.
  2 //:
  3 //:   The process of running Mu code:
  4 //:     load -> transform -> run
  5 //:
  6 //: The hope is that this framework of transform tools will provide a
  7 //: deconstructed alternative to conventional compilers.
  8 //:
  9 //: We're going to have many transforms in Mu, and getting their order right
 10 //: (not the same as ordering of layers) is a well-known problem. Some tips:
 11 //:   a) Design each layer to rely on as few previous layers as possible.
 12 //:
 13 //:   b) When positioning transforms, try to find the tightest constraint in
 14 //:   each transform relative to previous layers.
 15 //:
 16 //:   c) Even so you'll periodically need to try adjusting each transform
 17 //:   relative to those in previous layers to find a better arrangement.
 18 
 19 :(before "End recipe Fields")
 20 int transformed_until;
 21 :(before "End recipe Constructor")
 22 transformed_until = -1;
 23 
 24 :(before "End Types")
 25 typedef void (*transform_fn)(const recipe_ordinal);
 26 
 27 :(before "End Globals")
 28 vector<transform_fn> Transform;
 29 
 30 :(before "End One-time Setup")
 31 initialize_transforms();
 32 :(code)
 33 void initialize_transforms() {
 34   // Begin Transforms
 35     // Begin Instruction Inserting/Deleting Transforms
 36     // End Instruction Inserting/Deleting Transforms
 37 
 38     // Begin Instruction Modifying Transforms
 39     // End Instruction Modifying Transforms
 40   // End Transforms
 41 
 42   // Begin Checks
 43   // End Checks
 44 }
 45 
 46 void transform_all() {
 47   trace(9990, "transform") << "=== transform_all()" << end();
 48   for (int t = 0;  t < SIZE(Transform);  ++t) {
 49     for (map<recipe_ordinal, recipe>::iterator p = Recipe.begin();  p != Recipe.end();  ++p) {
 50       recipe& r = p->second;
 51       if (r.transformed_until != t-1) continue;
 52       // End Transform Checks
 53       (*Transform.at(t))(/*recipe_ordinal*/p->first);
 54       r.transformed_until = t;
 55     }
 56   }
 57   parse_int_reagents();  // do this after all other transforms have run
 58   // End transform_all
 59 }
 60 
 61 //: Even though a run will involve many calls to transform_all() for tests,
 62 //: our logical model is to load all code, then transform all code, then run.
 63 //: If you load new code that should cause already-transformed recipes to
 64 //: change, that's not supported. To help detect such situations and raise
 65 //: helpful errors we track a count of the number of calls made to
 66 //: transform_all().
 67 :(before "End Globals")
 68 int Num_calls_to_transform_all = 0;
 69 :(after "void transform_all()")
 70   ++Num_calls_to_transform_all;
 71 
 72 :(code)
 73 void parse_int_reagents() {
 74   trace(9991, "transform") << "--- parsing any uninitialized reagents as integers" << end();
 75   for (map<recipe_ordinal, recipe>::iterator p = Recipe.begin();  p != Recipe.end();  ++p) {
 76     recipe& r = p->second;
 77     if (r.steps.empty()) continue;
 78     for (int index = 0;  index < SIZE(r.steps);  ++index) {
 79       instruction& inst = r.steps.at(index);
 80       for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
 81         populate_value(inst.ingredients.at(i));
 82       }
 83       for (int i = 0;  i < SIZE(inst.products);  ++i) {
 84         populate_value(inst.products.at(i));
 85       }
 86     }
 87   }
 88 }
 89 
 90 void populate_value(reagent& r) {
 91   if (r.initialized) return;
 92   // End Reagent-parsing Exceptions
 93   if (!is_integer(r.name)) return;
 94   r.set_value(to_integer(r.name));
 95 }
 96 
 97 // helper for tests -- temporarily suppress run
 98 void transform(string form) {
 99   load(form);
100   transform_all();
101 }