Navit SVN

/work/compile/navit/src/intl/eval-plural.h

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