1*a8fa202aSchristos /* $NetBSD: eval-plural.h,v 1.1.1.1 2016/01/10 21:36:17 christos Exp $ */
2*a8fa202aSchristos
3*a8fa202aSchristos /* Plural expression evaluation.
4*a8fa202aSchristos Copyright (C) 2000, 2001 Free Software Foundation, Inc.
5*a8fa202aSchristos
6*a8fa202aSchristos This program is free software; you can redistribute it and/or modify
7*a8fa202aSchristos it under the terms of the GNU General Public License as published by
8*a8fa202aSchristos the Free Software Foundation; either version 2, or (at your option)
9*a8fa202aSchristos any later version.
10*a8fa202aSchristos
11*a8fa202aSchristos This program is distributed in the hope that it will be useful,
12*a8fa202aSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
13*a8fa202aSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*a8fa202aSchristos GNU General Public License for more details.
15*a8fa202aSchristos
16*a8fa202aSchristos You should have received a copy of the GNU General Public License
17*a8fa202aSchristos along with this program; if not, write to the Free Software Foundation,
18*a8fa202aSchristos Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19*a8fa202aSchristos
20*a8fa202aSchristos #ifndef STATIC
21*a8fa202aSchristos #define STATIC static
22*a8fa202aSchristos #endif
23*a8fa202aSchristos
24*a8fa202aSchristos /* Evaluate the plural expression and return an index value. */
25*a8fa202aSchristos STATIC unsigned long int plural_eval PARAMS ((struct expression *pexp,
26*a8fa202aSchristos unsigned long int n))
27*a8fa202aSchristos internal_function;
28*a8fa202aSchristos
29*a8fa202aSchristos STATIC
30*a8fa202aSchristos unsigned long int
31*a8fa202aSchristos internal_function
plural_eval(pexp,n)32*a8fa202aSchristos plural_eval (pexp, n)
33*a8fa202aSchristos struct expression *pexp;
34*a8fa202aSchristos unsigned long int n;
35*a8fa202aSchristos {
36*a8fa202aSchristos switch (pexp->nargs)
37*a8fa202aSchristos {
38*a8fa202aSchristos case 0:
39*a8fa202aSchristos switch (pexp->operation)
40*a8fa202aSchristos {
41*a8fa202aSchristos case var:
42*a8fa202aSchristos return n;
43*a8fa202aSchristos case num:
44*a8fa202aSchristos return pexp->val.num;
45*a8fa202aSchristos default:
46*a8fa202aSchristos break;
47*a8fa202aSchristos }
48*a8fa202aSchristos /* NOTREACHED */
49*a8fa202aSchristos break;
50*a8fa202aSchristos case 1:
51*a8fa202aSchristos {
52*a8fa202aSchristos /* pexp->operation must be lnot. */
53*a8fa202aSchristos unsigned long int arg = plural_eval (pexp->val.args[0], n);
54*a8fa202aSchristos return ! arg;
55*a8fa202aSchristos }
56*a8fa202aSchristos case 2:
57*a8fa202aSchristos {
58*a8fa202aSchristos unsigned long int leftarg = plural_eval (pexp->val.args[0], n);
59*a8fa202aSchristos if (pexp->operation == lor)
60*a8fa202aSchristos return leftarg || plural_eval (pexp->val.args[1], n);
61*a8fa202aSchristos else if (pexp->operation == land)
62*a8fa202aSchristos return leftarg && plural_eval (pexp->val.args[1], n);
63*a8fa202aSchristos else
64*a8fa202aSchristos {
65*a8fa202aSchristos unsigned long int rightarg = plural_eval (pexp->val.args[1], n);
66*a8fa202aSchristos
67*a8fa202aSchristos switch (pexp->operation)
68*a8fa202aSchristos {
69*a8fa202aSchristos case mult:
70*a8fa202aSchristos return leftarg * rightarg;
71*a8fa202aSchristos case divide:
72*a8fa202aSchristos return leftarg / rightarg;
73*a8fa202aSchristos case module:
74*a8fa202aSchristos return leftarg % rightarg;
75*a8fa202aSchristos case plus:
76*a8fa202aSchristos return leftarg + rightarg;
77*a8fa202aSchristos case minus:
78*a8fa202aSchristos return leftarg - rightarg;
79*a8fa202aSchristos case less_than:
80*a8fa202aSchristos return leftarg < rightarg;
81*a8fa202aSchristos case greater_than:
82*a8fa202aSchristos return leftarg > rightarg;
83*a8fa202aSchristos case less_or_equal:
84*a8fa202aSchristos return leftarg <= rightarg;
85*a8fa202aSchristos case greater_or_equal:
86*a8fa202aSchristos return leftarg >= rightarg;
87*a8fa202aSchristos case equal:
88*a8fa202aSchristos return leftarg == rightarg;
89*a8fa202aSchristos case not_equal:
90*a8fa202aSchristos return leftarg != rightarg;
91*a8fa202aSchristos default:
92*a8fa202aSchristos break;
93*a8fa202aSchristos }
94*a8fa202aSchristos }
95*a8fa202aSchristos /* NOTREACHED */
96*a8fa202aSchristos break;
97*a8fa202aSchristos }
98*a8fa202aSchristos case 3:
99*a8fa202aSchristos {
100*a8fa202aSchristos /* pexp->operation must be qmop. */
101*a8fa202aSchristos unsigned long int boolarg = plural_eval (pexp->val.args[0], n);
102*a8fa202aSchristos return plural_eval (pexp->val.args[boolarg ? 1 : 2], n);
103*a8fa202aSchristos }
104*a8fa202aSchristos }
105*a8fa202aSchristos /* NOTREACHED */
106*a8fa202aSchristos return 0;
107*a8fa202aSchristos }
108