Update copyright notices with scripts/update-copyrights
[jlayton/glibc.git] / intl / plural-eval.c
1 /* Plural expression evaluation.
2    Copyright (C) 2000-2014 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 static unsigned long int plural_eval (const struct expression *pexp,
20                                       unsigned long int n)
21      internal_function;
22
23 static unsigned long int
24 internal_function
25 plural_eval (pexp, n)
26      const struct expression *pexp;
27      unsigned long int n;
28 {
29   switch (pexp->nargs)
30     {
31     case 0:
32       switch (pexp->operation)
33         {
34         case var:
35           return n;
36         case num:
37           return pexp->val.num;
38         default:
39           break;
40         }
41       /* NOTREACHED */
42       break;
43     case 1:
44       {
45         /* pexp->operation must be lnot.  */
46         unsigned long int arg = plural_eval (pexp->val.args[0], n);
47         return ! arg;
48       }
49     case 2:
50       {
51         unsigned long int leftarg = plural_eval (pexp->val.args[0], n);
52         if (pexp->operation == lor)
53           return leftarg || plural_eval (pexp->val.args[1], n);
54         else if (pexp->operation == land)
55           return leftarg && plural_eval (pexp->val.args[1], n);
56         else
57           {
58             unsigned long int rightarg = plural_eval (pexp->val.args[1], n);
59
60             switch (pexp->operation)
61               {
62               case mult:
63                 return leftarg * rightarg;
64               case divide:
65                 return leftarg / rightarg;
66               case module:
67                 return leftarg % rightarg;
68               case plus:
69                 return leftarg + rightarg;
70               case minus:
71                 return leftarg - rightarg;
72               case less_than:
73                 return leftarg < rightarg;
74               case greater_than:
75                 return leftarg > rightarg;
76               case less_or_equal:
77                 return leftarg <= rightarg;
78               case greater_or_equal:
79                 return leftarg >= rightarg;
80               case equal:
81                 return leftarg == rightarg;
82               case not_equal:
83                 return leftarg != rightarg;
84               default:
85                 break;
86               }
87           }
88         /* NOTREACHED */
89         break;
90       }
91     case 3:
92       {
93         /* pexp->operation must be qmop.  */
94         unsigned long int boolarg = plural_eval (pexp->val.args[0], n);
95         return plural_eval (pexp->val.args[boolarg ? 1 : 2], n);
96       }
97     }
98   /* NOTREACHED */
99   return 0;
100 }