eval-plural.h
上传用户:tjfeida
上传日期:2013-03-10
资源大小:1917k
文件大小:3k
源码类别:

Ftp客户端

开发平台:

Visual C++

  1. /* Plural expression evaluation.
  2.    Copyright (C) 2000-2002 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 unsigned long int plural_eval PARAMS ((struct expression *pexp,
  20.       unsigned long int n))
  21.      internal_function;
  22. STATIC
  23. unsigned long int
  24. internal_function
  25. plural_eval (pexp, n)
  26.      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.     switch (pexp->operation)
  60.       {
  61.       case mult:
  62. return leftarg * rightarg;
  63.       case divide:
  64. #if !INTDIV0_RAISES_SIGFPE
  65. if (rightarg == 0)
  66.   raise (SIGFPE);
  67. #endif
  68. return leftarg / rightarg;
  69.       case module:
  70. #if !INTDIV0_RAISES_SIGFPE
  71. if (rightarg == 0)
  72.   raise (SIGFPE);
  73. #endif
  74. return leftarg % rightarg;
  75.       case plus:
  76. return leftarg + rightarg;
  77.       case minus:
  78. return leftarg - rightarg;
  79.       case less_than:
  80. return leftarg < rightarg;
  81.       case greater_than:
  82. return leftarg > rightarg;
  83.       case less_or_equal:
  84. return leftarg <= rightarg;
  85.       case greater_or_equal:
  86. return leftarg >= rightarg;
  87.       case equal:
  88. return leftarg == rightarg;
  89.       case not_equal:
  90. return leftarg != rightarg;
  91.       default:
  92. break;
  93.       }
  94.   }
  95. /* NOTREACHED */
  96. break;
  97.       }
  98.     case 3:
  99.       {
  100. /* pexp->operation must be qmop.  */
  101. unsigned long int boolarg = plural_eval (pexp->val.args[0], n);
  102. return plural_eval (pexp->val.args[boolarg ? 1 : 2], n);
  103.       }
  104.     }
  105.   /* NOTREACHED */
  106.   return 0;
  107. }