.
[glibc/history.git] / intl / plural-eval.c
blob3f01688c2eed4efe996ff132526bb7e432bf6e6b
1 /* Plural expression evaluation.
2 Copyright (C) 2000, 2001, 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 static unsigned long int plural_eval (const struct expression *pexp,
21 unsigned long int n)
22 internal_function;
24 static unsigned long int
25 internal_function
26 plural_eval (pexp, n)
27 const struct expression *pexp;
28 unsigned long int n;
30 switch (pexp->nargs)
32 case 0:
33 switch (pexp->operation)
35 case var:
36 return n;
37 case num:
38 return pexp->val.num;
39 default:
40 break;
42 /* NOTREACHED */
43 break;
44 case 1:
46 /* pexp->operation must be lnot. */
47 unsigned long int arg = plural_eval (pexp->val.args[0], n);
48 return ! arg;
50 case 2:
52 unsigned long int leftarg = plural_eval (pexp->val.args[0], n);
53 if (pexp->operation == lor)
54 return leftarg || plural_eval (pexp->val.args[1], n);
55 else if (pexp->operation == land)
56 return leftarg && plural_eval (pexp->val.args[1], n);
57 else
59 unsigned long int rightarg = plural_eval (pexp->val.args[1], n);
61 switch (pexp->operation)
63 case mult:
64 return leftarg * rightarg;
65 case divide:
66 return leftarg / rightarg;
67 case module:
68 return leftarg % rightarg;
69 case plus:
70 return leftarg + rightarg;
71 case minus:
72 return leftarg - rightarg;
73 case less_than:
74 return leftarg < rightarg;
75 case greater_than:
76 return leftarg > rightarg;
77 case less_or_equal:
78 return leftarg <= rightarg;
79 case greater_or_equal:
80 return leftarg >= rightarg;
81 case equal:
82 return leftarg == rightarg;
83 case not_equal:
84 return leftarg != rightarg;
85 default:
86 break;
89 /* NOTREACHED */
90 break;
92 case 3:
94 /* pexp->operation must be qmop. */
95 unsigned long int boolarg = plural_eval (pexp->val.args[0], n);
96 return plural_eval (pexp->val.args[boolarg ? 1 : 2], n);
99 /* NOTREACHED */
100 return 0;