home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ZRELBIT.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  7KB  |  282 lines

  1. /* Copyright (C) 1989, 1990, 1991, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zrelbit.c */
  20. /* Relational, boolean, and bit operators */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gsutil.h"
  25. #include "idict.h"
  26. #include "store.h"
  27.  
  28. /* Forward references */
  29. private int near obj_le(P2(os_ptr, os_ptr));
  30.  
  31. /* <obj1> <obj2> eq <bool> */
  32. int
  33. zeq(register os_ptr op)
  34. {    register os_ptr op1 = op - 1;
  35. #define eq_check_read(opp, dflt)\
  36.   switch ( r_type(opp) )\
  37.    {    case t_string: case t_array: case t_mixedarray: case t_shortarray:\
  38.       check_read(*opp); break;\
  39.     case t_dictionary: check_dict_read(*opp); break;\
  40.     default: dflt; break;\
  41.    }
  42.     eq_check_read(op1, check_op(2));
  43.     eq_check_read(op, DO_NOTHING);
  44.     make_bool(op1, (obj_eq(op1, op) ? 1 : 0));
  45.     pop(1);
  46.     return 0;
  47. }
  48.  
  49. /* <obj1> <obj2> ne <bool> */
  50. int
  51. zne(register os_ptr op)
  52. {    /* We'll just be lazy and use eq. */
  53.     int code = zeq(op);
  54.     if ( !code ) op[-1].value.boolval ^= 1;
  55.     return code;
  56. }
  57.  
  58. /* <num1> <num2> ge <bool> */
  59. /* <str1> <str2> ge <bool> */
  60. int
  61. zge(register os_ptr op)
  62. {    int code = obj_le(op, op - 1);
  63.     if ( code < 0 ) return code;
  64.     make_bool(op - 1, code);
  65.     pop(1);
  66.     return 0;
  67. }
  68.  
  69. /* <num1> <num2> gt <bool> */
  70. /* <str1> <str2> gt <bool> */
  71. int
  72. zgt(register os_ptr op)
  73. {    int code = obj_le(op - 1, op);
  74.     if ( code < 0 ) return code;
  75.     make_bool(op - 1, code ^ 1);
  76.     pop(1);
  77.     return 0;
  78. }
  79.  
  80. /* <num1> <num2> le <bool> */
  81. /* <str1> <str2> le <bool> */
  82. int
  83. zle(register os_ptr op)
  84. {    int code = obj_le(op - 1, op);
  85.     if ( code < 0 ) return code;
  86.     make_bool(op - 1, code);
  87.     pop(1);
  88.     return 0;
  89. }
  90.  
  91. /* <num1> <num2> lt <bool> */
  92. /* <str1> <str2> lt <bool> */
  93. int
  94. zlt(register os_ptr op)
  95. {    int code = obj_le(op, op - 1);
  96.     if ( code < 0 ) return code;
  97.     make_bool(op - 1, code ^ 1);
  98.     pop(1);
  99.     return 0;
  100. }
  101.  
  102. /* <num1> <num2> max <num> */
  103. /* <str1> <str2> max <str> */
  104. int
  105. zmax(register os_ptr op)
  106. {    int code = obj_le(op - 1, op);
  107.     if ( code < 0 ) return code;
  108.     if ( code )
  109.        {    ref_assign(op - 1, op);
  110.        }
  111.     pop(1);
  112.     return 0;
  113. }
  114.  
  115. /* <num1> <num2> min <num> */
  116. /* <str1> <str2> min <str> */
  117. int
  118. zmin(register os_ptr op)
  119. {    int code = obj_le(op - 1, op);
  120.     if ( code < 0 ) return code;
  121.     if ( !code )
  122.        {    ref_assign(op - 1, op);
  123.        }
  124.     pop(1);
  125.     return 0;
  126. }
  127.  
  128. /* <bool1> <bool2> and <bool> */
  129. /* <int1> <int2> and <int> */
  130. int
  131. zand(register os_ptr op)
  132. {    check_type(op[-1], r_type(op));
  133.     switch ( r_type(op) )
  134.        {
  135.     case t_boolean:
  136.         op[-1].value.boolval &= op->value.boolval;
  137.         break;
  138.     case t_integer:
  139.         op[-1].value.intval &= op->value.intval;
  140.         break;
  141.     default:
  142.         return_error(e_typecheck);
  143.        }
  144.     pop(1);
  145.     return 0;
  146. }
  147.  
  148. /* <bool> not <bool> */
  149. /* <int> not <int> */
  150. int
  151. znot(register os_ptr op)
  152. {    switch ( r_type(op) )
  153.        {
  154.     case t_boolean:
  155.         op->value.boolval = !op->value.boolval;
  156.         break;
  157.     case t_integer:
  158.         op->value.intval = ~op->value.intval;
  159.         break;
  160.     default:
  161.         return_error(e_typecheck);
  162.        }
  163.     return 0;
  164. }
  165.  
  166. /* <bool1> <bool2> or <bool> */
  167. /* <int1> <int2> or <int> */
  168. int
  169. zor(register os_ptr op)
  170. {    check_type(op[-1], r_type(op));
  171.     switch ( r_type(op) )
  172.        {
  173.     case t_boolean:
  174.         op[-1].value.boolval |= op->value.boolval;
  175.         break;
  176.     case t_integer:
  177.         op[-1].value.intval |= op->value.intval;
  178.         break;
  179.     default:
  180.         return_error(e_typecheck);
  181.        }
  182.     pop(1);
  183.     return 0;
  184. }
  185.  
  186. /* <bool1> <bool2> xor <bool> */
  187. /* <int1> <int2> xor <int> */
  188. int
  189. zxor(register os_ptr op)
  190. {    check_type(op[-1], r_type(op));
  191.     switch ( r_type(op) )
  192.        {
  193.     case t_boolean:
  194.         op[-1].value.boolval ^= op->value.boolval;
  195.         break;
  196.     case t_integer:
  197.         op[-1].value.intval ^= op->value.intval;
  198.         break;
  199.     default:
  200.         return_error(e_typecheck);
  201.        }
  202.     pop(1);
  203.     return 0;
  204. }
  205.  
  206. /* <int> <shift> bitshift <int> */
  207. int
  208. zbitshift(register os_ptr op)
  209. {    int shift;
  210.     check_type(op[-1], t_integer);
  211.     check_type(*op, t_integer);
  212. #define max_shift (arch_sizeof_long * 8 - 1)
  213.     if ( op->value.intval < -max_shift || op->value.intval > max_shift )
  214.         op[-1].value.intval = 0;
  215. #undef max_shift
  216.     else if ( (shift = op->value.intval) < 0 )
  217.         op[-1].value.intval = ((ulong)(op[-1].value.intval)) >> -shift;
  218.     else
  219.         op[-1].value.intval <<= shift;
  220.     pop(1);
  221.     return 0;
  222. }
  223.  
  224. /* ------ Initialization procedure ------ */
  225.  
  226. op_def zrelbit_op_defs[] = {
  227.     {"2and", zand},
  228.     {"2bitshift", zbitshift},
  229.     {"2eq", zeq},
  230.     {"2ge", zge},
  231.     {"2gt", zgt},
  232.     {"2le", zle},
  233.     {"2lt", zlt},
  234.     {"2max", zmax},
  235.     {"2min", zmin},
  236.     {"2ne", zne},
  237.     {"1not", znot},
  238.     {"2or", zor},
  239.     {"2xor", zxor},
  240.     op_def_end(0)
  241. };
  242.  
  243. /* ------ Internal routines ------ */
  244.  
  245. /* Compare two operands (both numeric, or both strings). */
  246. /* Return 1 if op[-1] <= op[0], 0 if op[-1] > op[0], */
  247. /* or a (negative) error code. */
  248. #define bcval(v1, rel, v2) (op1->value.v1 rel op->value.v2 ? 1 : 0)
  249. private int near
  250. obj_le(register os_ptr op1, register os_ptr op)
  251. {    switch ( r_type(op1) )
  252.       {
  253.       case t_integer:
  254.         switch ( r_type(op) )
  255.           {
  256.           case t_integer:
  257.               return bcval(intval, <=, intval);
  258.           case t_real:
  259.               return bcval(intval, <=, realval);
  260.           default:
  261.               return_error(e_typecheck);
  262.           }
  263.       case t_real:
  264.         switch ( r_type(op) )
  265.           {
  266.           case t_real:
  267.               return bcval(realval, <=, realval);
  268.           case t_integer:
  269.               return bcval(realval, <=, intval);
  270.           default:
  271.               return_error(e_typecheck);
  272.           }
  273.       case t_string:
  274.         check_read(*op1);
  275.         check_read_type(*op, t_string);
  276.         return (bytes_compare(op1->value.bytes, r_size(op1),
  277.                       op->value.bytes, r_size(op)) <= 0 ? 1 : 0);
  278.       default:
  279.           return_error(e_typecheck);
  280.       }
  281. }
  282.