void CMPolynomial::level7(double& result) // functions, (), numbers, { // user functions, parameters if (token == LPar || token == LBracket) { get_token(); level1(result); } else if (token == If) result = eval_condition(); else if (token == Function) result = eval_function(); else if (token==Variable||token==VariableOrig) result = eval_variable(token==Variable); else if (token == Constant) result = constants[offset]; /* else if (token == Definition) { result = CMDefinitions::GetDefinitionValue(offset); sdebug << "Definition " << offset << result << ENDL; } */ else if (isspecialfunc(token)) result = eval_special_function(); get_token(); }
lisp_obj *eval_expression(lisp_expr *expr, lisp_env *env, lisp_err *err) { lisp_obj *value = NULL; assert(expr != NULL); switch (expr->type){ case MKLAMBDA: return make_lambda(expr, env); case SELFEVAL: value = expr->value.selfeval.value; if (! value){ return NULL; } return retain(value); case LOOKUP: value = lookup(env, expr->value.lookup.name); if (! value){ raise_error(err, UNKNOW_IDENTIFIER, "Unknow identifier \"%s\"", expr->value.lookup.name); return NULL; } return retain(value); case APPLICATION: return apply(&(expr->value.application), env, err); case CONDITION: return eval_condition(&(expr->value.condition), env, err); case DEFINE: value = FORCE_VALUE(expr->value.define.expr, env, err); if (! value){ return NULL; } release(set_env(env, expr->value.define.name, value)); default: return NIL; } }
void cldr_plural_rule_list_print (struct cldr_plural_rule_list_ty *rules, FILE *fp) { size_t i; size_t count; size_t nplurals; int modulus_max = 0; /* Prune trivial conditions. */ for (i = 0; i < rules->nitems; i++) { struct cldr_plural_rule_ty *rule = rules->items[i]; eval_condition (rule->condition); } /* Omit trivial rules (e.g., the last rule for "ru") with the following algorithm: 1. From all rules, find the largest modulus M 2. Prepare a bit vector with M elements and initialize it with zeros 3. Loop over the rules, until all bits are set: For each value in the range [1, M], apply a rule, and flip the corresponding bit if it evaluates true */ /* Find the largest modulus. */ for (i = 0; i < rules->nitems; i++) { struct cldr_plural_rule_ty *rule = rules->items[i]; int modulus = find_largest_modulus (rule->condition); int number = find_largest_number (rule->condition); /* If the rule contains a range whose end is larger than MODULUS, we can't use MODULUS as the upper bound. Skip it. */ if (modulus >= number && modulus > modulus_max) modulus_max = modulus; } if (modulus_max > 0) { bool *values = XNMALLOC (modulus_max, bool); memset (values, 0, sizeof (bool) * modulus_max); for (i = 0; i < rules->nitems; i++) { struct cldr_plural_rule_ty *rule = rules->items[i]; int j; for (j = 0; j < modulus_max; j++) { bool result = apply_condition (rule->condition, j + 1); if (result) values[j] = true; } /* Check if all bits are set. Then we can omit one more rule. */ for (j = 0; j < modulus_max; j++) if (values[j] == false) break; if (j == modulus_max) break; } free (values); while (i < rules->nitems) cldr_plural_rule_free (rules->items[--rules->nitems]); }
static void eval_condition (struct cldr_plural_condition_ty *condition) { if (condition->type == CLDR_PLURAL_CONDITION_AND) { eval_condition (condition->value.conditions[0]); eval_condition (condition->value.conditions[1]); if (condition->value.conditions[0]->type == CLDR_PLURAL_CONDITION_FALSE || condition->value.conditions[1]->type == CLDR_PLURAL_CONDITION_FALSE) { cldr_plural_condition_free (condition->value.conditions[0]); cldr_plural_condition_free (condition->value.conditions[1]); condition->type = CLDR_PLURAL_CONDITION_FALSE; } else if (condition->value.conditions[0]->type == CLDR_PLURAL_CONDITION_TRUE && condition->value.conditions[1]->type == CLDR_PLURAL_CONDITION_TRUE) { cldr_plural_condition_free (condition->value.conditions[0]); cldr_plural_condition_free (condition->value.conditions[1]); condition->type = CLDR_PLURAL_CONDITION_TRUE; } else if (condition->value.conditions[0]->type == CLDR_PLURAL_CONDITION_TRUE) { struct cldr_plural_condition_ty *original = condition->value.conditions[1]; cldr_plural_condition_free (condition->value.conditions[0]); condition->type = condition->value.conditions[1]->type; condition->value = condition->value.conditions[1]->value; free (original); } else if (condition->value.conditions[1]->type == CLDR_PLURAL_CONDITION_TRUE) { struct cldr_plural_condition_ty *original = condition->value.conditions[0]; cldr_plural_condition_free (condition->value.conditions[1]); condition->type = condition->value.conditions[0]->type; condition->value = condition->value.conditions[0]->value; free (original); } } else if (condition->type == CLDR_PLURAL_CONDITION_OR) { eval_condition (condition->value.conditions[0]); eval_condition (condition->value.conditions[1]); if (condition->value.conditions[0]->type == CLDR_PLURAL_CONDITION_TRUE || condition->value.conditions[1]->type == CLDR_PLURAL_CONDITION_TRUE) { cldr_plural_condition_free (condition->value.conditions[0]); cldr_plural_condition_free (condition->value.conditions[1]); condition->type = CLDR_PLURAL_CONDITION_TRUE; } else if (condition->value.conditions[0]->type == CLDR_PLURAL_CONDITION_FALSE && condition->value.conditions[1]->type == CLDR_PLURAL_CONDITION_FALSE) { cldr_plural_condition_free (condition->value.conditions[0]); cldr_plural_condition_free (condition->value.conditions[1]); condition->type = CLDR_PLURAL_CONDITION_FALSE; } else if (condition->value.conditions[0]->type == CLDR_PLURAL_CONDITION_FALSE) { struct cldr_plural_condition_ty *original = condition->value.conditions[1]; cldr_plural_condition_free (condition->value.conditions[0]); condition->type = condition->value.conditions[1]->type; condition->value = condition->value.conditions[1]->value; free (original); } else if (condition->value.conditions[1]->type == CLDR_PLURAL_CONDITION_FALSE) { struct cldr_plural_condition_ty *original = condition->value.conditions[0]; cldr_plural_condition_free (condition->value.conditions[1]); condition->type = condition->value.conditions[0]->type; condition->value = condition->value.conditions[0]->value; free (original); } } else { enum cldr_plural_condition value = eval_relation (condition->value.relation); if (value == CLDR_PLURAL_CONDITION_TRUE || value == CLDR_PLURAL_CONDITION_FALSE) { cldr_plural_relation_free (condition->value.relation); condition->type = value; } } }