eval-plural.h 2.6 KB

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