scope gazelle_addCalculationFunctionsToRules.

sclass #Calculator {
  S var1, var2, var3;
  O function; // func(double, double) -> double
  bool debug;
 
  *() {}
  *(S *var1, S *var2, S *var3, O *function, bool *debug) {}
  
  SS get(SS map, LS tokC, LS tokI, RuleEngine2_MatchedRule matched) {
    if (map == null) null;
    double result;
    try {
      S value1 = getOrKeep(map, var1), value2 = getOrKeep(map, var2);
      result = (double) callAndMake_orF2(function, parseDouble(value1), parseDouble(value2));
    } catch {
      ret null;
    }
    if (!strictPutIC(map, var3, str(iround(result)))) null;
    ret map;
  }
}

svoid gazelle_addCalculationFunctionsToRules(RuleEngine2 engine, O... _) {
  bool debug = boolPar debug(_) || boolPar calcDebug(_);
  for (final RuleEngine2.Rule r : engine.rules)
    for (Matches m : getJMatches_all("compute var * from * and * using *", r.comments)) {
      r.addMapMassager(new Calculator($2, $3, $1, $4, debug));
      if (debug) print("Calculator made");
    }
}

end scope