Ignore:
Timestamp:
Sep 5, 2008, 3:45:52 AM (17 years ago)
Author:
bird
Message:

kmk: Two new functions $(if-expr cond,exp-on-true,exp-on-false) and $(expr expression) that uses the expreval.c expression evaluator.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/function.c

    r1716 r1727  
    27242724    if (e1 - s1 != e2 - s2)
    27252725      return comp_vars_ne (o, s1, e1, s2, e2, argv[2], funcname);
    2726 l_simple_compare:
    27272726    if (!memcmp (s1, s2, e1 - s1))
    27282727      return variable_buffer_output (o, "", 0);     /* eq */
     
    30893088  return variable_buffer_output (o, "", 0);
    30903089}
    3091 #endif
     3090#endif /* CONFIG_WITH_WHICH */
     3091
     3092#ifdef CONFIG_WITH_IF_CONDITIONALS
     3093
     3094/* Evaluates the expression given in the argument using the
     3095   same evaluator as for the new 'if' statements, except now
     3096   we don't force the result into a boolean like for 'if' and
     3097   '$(if-expr ,,)'. */
     3098static char *
     3099func_expr (char *o, char **argv, const char *funcname UNUSED)
     3100{
     3101  o = expr_eval_to_string (o, argv[0]);
     3102  return o;
     3103}
     3104
     3105/* Same as '$(if ,,)' except the first argument is evaluated
     3106   using the same evaluator as for the new 'if' statements. */
     3107static char *
     3108func_if_expr (char *o, char **argv, const char *funcname UNUSED)
     3109{
     3110    int rc;
     3111    char *to_expand;
     3112
     3113    /* Evaluate the condition in argv[0] and expand the 2nd or
     3114       3rd argument according to the result. */
     3115    rc = expr_eval_if_conditionals (argv[0], NULL);
     3116    to_expand = rc == 0 ? argv[1] : argv[2];
     3117    if (*to_expand)
     3118      {
     3119        char *expansion = expand_argument (to_expand, NULL);
     3120
     3121        o = variable_buffer_output (o, expansion, strlen (expansion));
     3122
     3123        free (expansion);
     3124      }
     3125
     3126    return o;
     3127}
     3128
     3129#endif /* CONFIG_WITH_IF_CONDITIONALS */
    30923130
    30933131#ifdef CONFIG_WITH_STACK
     
    31083146  struct variable *stack_var;
    31093147  const char *stack = argv[0];
    3110   const int return_item = argv[0][sizeof("stack-pop") - 1] == '\0';
    31113148
    31123149  stack_var = lookup_variable (stack, strlen (stack) );
     
    39593996  { STRING_SIZE_TUPLE("which"),         0,  0,  1,  func_which},
    39603997#endif
     3998#ifdef CONFIG_WITH_IF_CONDITIONALS
     3999  { STRING_SIZE_TUPLE("expr"),          1,  1,  0,  func_expr},
     4000  { STRING_SIZE_TUPLE("if-expr"),       2,  3,  0,  func_if_expr},
     4001#endif
    39614002#ifdef CONFIG_WITH_STACK
    39624003  { STRING_SIZE_TUPLE("stack-push"),    2,  2,  1,  func_stack_push},
Note: See TracChangeset for help on using the changeset viewer.