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   // Begin transform_all
 49   for (int t = 0;  t < SIZE(Transform);  ++t) {
 50     for (map<recipe_ordinal, recipe>::iterator p = Recipe.begin();  p != Recipe.end();  ++p) {
 51       recipe& r = p->second;
 52       if (r.transformed_until != t-1) continue;
 53       // End Transform Checks
 54       (*Transform.at(t))(/*recipe_ordinal*/p->first);
 55       r.transformed_until = t;
 56     }
 57   }
 58   parse_int_reagents();  // do this after all other transforms have run
 59   // End transform_all
 60 }
 61 
 62 //: Even though a run will involve many calls to transform_all() for tests,
 63 //: our logical model is to load all code, then transform all code, then run.
 64 //: If you load new code that should cause already-transformed recipes to
 65 //: change, that's not supported. To help detect such situations and raise
 66 //: helpful errors we track a count of the number of calls made to
 67 //: transform_all().
 68 :(before "End Globals")
 69 int Num_calls_to_transform_all = 0;
 70 :(after "void transform_all()")
 71   ++Num_calls_to_transform_all;
 72 
 73 :(code)
 74 void parse_int_reagents() {
 75   trace(9991, "transform") << "--- parsing any uninitialized reagents as integers" << end();
 76   for (map<recipe_ordinal, recipe>::iterator p = Recipe.begin();  p != Recipe.end();  ++p) {
 77     recipe& r = p->second;
 78     if (r.steps.empty()) continue;
 79     for (int index = 0;  index < SIZE(r.steps);  ++index) {
 80       instruction& inst = r.steps.at(index);
 81       for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
 82         populate_value(inst.ingredients.at(i));
 83       }
 84       for (int i = 0;  i < SIZE(inst.products);  ++i) {
 85         populate_value(inst.products.at(i));
 86       }
 87     }
 88   }
 89 }
 90 
 91 void populate_value(reagent& r) {
 92   if (r.initialized) return;
 93   // End Reagent-parsing Exceptions
 94   if (!is_integer(r.name)) return;
 95   r.set_value(to_integer(r.name));
 96 }
 97 
 98 // helper for tests -- temporarily suppress run
 99 void transform(string form) {
100   load(form);
101   transform_all();
102 }