eval-plural.h 2.7 KB

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