Back to index

glibc  2.9
plural-eval.c
Go to the documentation of this file.
00001 /* Plural expression evaluation.
00002    Copyright (C) 2000, 2001, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 static unsigned long int plural_eval (const struct expression *pexp,
00021                                   unsigned long int n)
00022      internal_function;
00023 
00024 static unsigned long int
00025 internal_function
00026 plural_eval (pexp, n)
00027      const struct expression *pexp;
00028      unsigned long int n;
00029 {
00030   switch (pexp->nargs)
00031     {
00032     case 0:
00033       switch (pexp->operation)
00034        {
00035        case var:
00036          return n;
00037        case num:
00038          return pexp->val.num;
00039        default:
00040          break;
00041        }
00042       /* NOTREACHED */
00043       break;
00044     case 1:
00045       {
00046        /* pexp->operation must be lnot.  */
00047        unsigned long int arg = plural_eval (pexp->val.args[0], n);
00048        return ! arg;
00049       }
00050     case 2:
00051       {
00052        unsigned long int leftarg = plural_eval (pexp->val.args[0], n);
00053        if (pexp->operation == lor)
00054          return leftarg || plural_eval (pexp->val.args[1], n);
00055        else if (pexp->operation == land)
00056          return leftarg && plural_eval (pexp->val.args[1], n);
00057        else
00058          {
00059            unsigned long int rightarg = plural_eval (pexp->val.args[1], n);
00060 
00061            switch (pexp->operation)
00062              {
00063              case mult:
00064               return leftarg * rightarg;
00065              case divide:
00066               return leftarg / rightarg;
00067              case module:
00068               return leftarg % rightarg;
00069              case plus:
00070               return leftarg + rightarg;
00071              case minus:
00072               return leftarg - rightarg;
00073              case less_than:
00074               return leftarg < rightarg;
00075              case greater_than:
00076               return leftarg > rightarg;
00077              case less_or_equal:
00078               return leftarg <= rightarg;
00079              case greater_or_equal:
00080               return leftarg >= rightarg;
00081              case equal:
00082               return leftarg == rightarg;
00083              case not_equal:
00084               return leftarg != rightarg;
00085              default:
00086               break;
00087              }
00088          }
00089        /* NOTREACHED */
00090        break;
00091       }
00092     case 3:
00093       {
00094        /* pexp->operation must be qmop.  */
00095        unsigned long int boolarg = plural_eval (pexp->val.args[0], n);
00096        return plural_eval (pexp->val.args[boolarg ? 1 : 2], n);
00097       }
00098     }
00099   /* NOTREACHED */
00100   return 0;
00101 }