home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / m0 / part05 / o_arith.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  7.9 KB  |  342 lines

  1. /*
  2.     o_arith.c
  3. */
  4. /*  Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
  5.  
  6.     Distributed under the terms of the GNU General Public License
  7.     version 2 of june 1991 as published by the Free Software
  8.     Foundation, Inc.
  9.  
  10.              This file is part of M0.
  11.  
  12. M0 is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY.  No author or distributor accepts responsibility to anyone for
  14. the consequences of using it or for whether it serves any particular
  15. purpose or works at all, unless he says so in writing.  Refer to the GNU
  16. General Public License for full details. 
  17.  
  18. Everyone is granted permission to copy, modify and redistribute M0, but
  19. only under the conditions described in the GNU General Public License. 
  20. A copy of this license is supposed to have been given to you along with
  21. M0 so you can know your rights and responsibilities.  It should be in a
  22. file named LICENSE.  Among other things, the copyright notice and this
  23. notice must be preserved on all copies.  */
  24.  
  25. #include "l_proto.h"
  26. #include "o_proto.h"
  27.  
  28. /* data types and possible operations:
  29.     array    array    +
  30.     string    string    + > <
  31.     time    time    - > <
  32.     time    int    + -
  33.         int    ~
  34.     int    int    + - * / < > | & ^ %
  35.     int    time    +
  36.         key    ~
  37.     key    key    | & ^
  38.     any    any    =
  39. */
  40.  
  41.  
  42. /* functions for integer types: */
  43.  
  44. static retcode divide(sint a, sint b, sint *r)
  45.     { if (!b) return ERR_DIVISION_BY_ZERO; *r = a/b; return OK; }
  46. static retcode mod(sint a, sint b, sint *r)
  47.     { if (!b) return ERR_DIVISION_BY_ZERO; *r = a%b; return OK; }
  48. static retcode mul(sint a, sint b, sint *r)    { *r = a*b; return OK; }
  49.  
  50.  
  51. static retcode
  52. do_binary(retcode (*fct)(sint a, sint b, sint *r))
  53. {    
  54.     eindex res;
  55.     sint v;
  56.     retcode rc;
  57.  
  58.     load_2_args(a, b, ap, bp);
  59.  
  60.     if (eptype(ap) != T_INT || eptype(bp) != T_INT)
  61.         return ERR_TYPE_CHECK;
  62.     rc = fct(ap->V.i, bp->V.i, &v);
  63.     if (rc != OK)
  64.         return rc;
  65.     res = new_element(current, T_INT);
  66.     eaddr(current, res)->V.i = v;
  67.  
  68.     decrefp(current, a, ap);
  69.     decrefp(current, b, bp);
  70.     return_ok_result(2, res);
  71. }
  72.  
  73.  
  74. retcode o_div()        { return do_binary(divide); }
  75. retcode o_mod()        { return do_binary(mod); }
  76. retcode o_mul()        { return do_binary(mul); }
  77.  
  78.  
  79. /* ------------------------------------------------------------------------- */
  80.  
  81. retcode o_add()
  82. {
  83.     eindex res;
  84.     eptr rp;
  85.  
  86.     load_2_args(e1, e2, p1, p2);
  87.  
  88.     if (eptype(p1) == T_INT && eptype(p2) == T_INT) {
  89.         res = new_element(current, T_INT);
  90.         eaddr(current, res)->V.i = p1->V.i + p2->V.i;
  91.     } else if (eptype(p1) == T_STRING && eptype(p2) == T_STRING) {
  92.         res = new_element(current, T_STRING);
  93.         rp = eaddr(current, res);
  94.         epattr(rp) = A_FRAG | (A_ALL & epattr(p1) & epattr(p2));
  95.         rp->V.fra.f[0] = e1;
  96.         rp->V.fra.f[1] = e2;
  97.         eplen(rp) = eplen(p1)+eplen(p2);
  98.         increfp(p1);
  99.         increfp(p2);
  100.     } else if (eptype(p1) == T_ARRAY && eptype(p2) == T_ARRAY) {
  101.         int i, j;
  102.         res = new_array(current, eplen(p1)+eplen(p2));
  103.  
  104.         for (i = 0; i < eplen(p1); i++)
  105.             array_put(current, res, i, array_get(current, e1, i));
  106.         for (i = 0, j = eplen(p1); i < eplen(p2); i++, j++)
  107.             array_put(current, res, j, array_get(current, e2, i));
  108.     } else if (eptype(p1) == T_TIME && eptype(p2) == T_INT)
  109.         res = time_addint(current, e1, p2->V.i);
  110.     else if (eptype(p1) == T_INT && eptype(p2) == T_TIME)
  111.         res = time_addint(current, e2, p1->V.i);
  112.     else
  113.         return ERR_TYPE_CHECK;
  114.  
  115.     rp = eaddr(current, res);
  116.     if (eptype(rp) == T_STRING || eptype(rp) == T_ARRAY || eptype(rp) == T_TIME)
  117.         epattr(rp) &= ~A_ALL | (epattr(p1) & epattr(p2));
  118.  
  119.     decrefp(current, e1, p1);
  120.     decrefp(current, e2, p2);
  121.     return_ok_result(2, res);
  122. }
  123.  
  124.  
  125. retcode o_and()
  126. {
  127.     eindex res;
  128.  
  129.     load_2_args(e1, e2, p1, p2);
  130.  
  131.     if (eptype(p1) == T_INT && eptype(p2) == T_INT) {
  132.         res = new_element(current, T_INT);
  133.         eaddr(current, res)->V.i = p1->V.i & p2->V.i;
  134.     } else if (eptype(p1) == T_KEY && eptype(p2) == T_KEY) {
  135.         byteptr s = p2->V.nam.u.s;
  136.         byte k[8];
  137.         int i;
  138.         memcpy(k, (char*)(p1->V.nam.u.s), 8);
  139.         for (i = 0; i < 8; i++, s++)
  140.             k[i] &= *s;
  141.         res = key_add(k);
  142.     } else
  143.         return ERR_TYPE_CHECK;
  144.  
  145.     decrefp(current, e1, p1);
  146.     decrefp(current, e2, p2);
  147.     return_ok_result(2, res);
  148. }
  149.  
  150.  
  151. retcode o_eq()
  152. {
  153.     eindex e1, e2, ei;
  154.  
  155.     if (current->osp < 2)
  156.         return ERR_STACK_UNDERFLOW;
  157.     e1 = current->os[current->osp-2];
  158.     e2 = current->os[current->osp-1];
  159.  
  160.     ei = new_element(current, T_INT);
  161.     eaddr(current, ei)->V.i = element_equal(current, e1, e2);
  162.  
  163.     decref(current, e1);
  164.     decref(current, e2);
  165.     return_ok_result(2, ei);
  166. }
  167.  
  168.  
  169. retcode o_gt()
  170. {
  171.     int flag;
  172.     eindex res;
  173.  
  174.     load_2_args(e1, e2, p1, p2);
  175.  
  176.     if (eptype(p1) == T_INT && eptype(p2) == T_INT)
  177.         flag = p1->V.i > p2->V.i ? 1 : 0;
  178.     else if (eptype(p1) == T_TIME && eptype(p2) == T_TIME) {
  179.         if (!(epattr(p1) & epattr(p2) & A_READ))
  180.             return ERR_ACCESS_CHECK;
  181.         flag = time_gt(&(p1->V.tim), &(p2->V.tim));
  182.     } else if (eptype(p1) == T_STRING && eptype(p2) == T_STRING) {
  183.         if (!(epattr(p1) & epattr(p2) & A_READ))
  184.             return ERR_ACCESS_CHECK;
  185.         flag = str_gt(current, e1, e2);
  186.     } else
  187.         return ERR_TYPE_CHECK;
  188.  
  189.     res = new_element(current, T_INT);
  190.     eaddr(current, res)->V.i = flag;
  191.     decrefp(current, e1, p1);
  192.     decrefp(current, e2, p2);
  193.     return_ok_result(2, res);
  194. }
  195.  
  196.  
  197. retcode o_lt()
  198. {
  199.     int flag;
  200.     eindex res;
  201.  
  202.     load_2_args(e1, e2, p1, p2);
  203.  
  204.     if (eptype(p1) == T_INT && eptype(p2) == T_INT)
  205.         flag = p1->V.i < p2->V.i ? 1 : 0;
  206.     else if (eptype(p1) == T_TIME && eptype(p2) == T_TIME) {
  207.         if (!(epattr(p1) & epattr(p2) & A_READ))
  208.             return ERR_ACCESS_CHECK;
  209.         flag = time_gt(&(p2->V.tim), &(p1->V.tim));
  210.     } else if (eptype(p1) == T_STRING && eptype(p2) == T_STRING) {
  211.         if (!(epattr(p1) & epattr(p2) & A_READ))
  212.             return ERR_ACCESS_CHECK;
  213.         flag = str_gt(current, e2, e1);
  214.     } else
  215.         return ERR_TYPE_CHECK;
  216.  
  217.     res = new_element(current, T_INT);
  218.     eaddr(current, res)->V.i = flag;
  219.     decrefp(current, e1, p1);
  220.     decrefp(current, e2, p2);
  221.     return_ok_result(2, res);
  222. }
  223.  
  224.  
  225. retcode o_neg()
  226. {
  227.     eindex res;
  228.  
  229.     load_1_arg(ei, ep);
  230.  
  231.     if (eptype(ep) != T_INT)
  232.         return ERR_TYPE_CHECK;
  233.     res = new_element(current, T_INT);
  234.     eaddr(current, res)->V.i = - ep->V.i;
  235.     decrefp(current, ei, ep);
  236.  
  237.     current->os[current->osp-1] = res;
  238.     return OK;
  239. }
  240.  
  241.  
  242. retcode o_not()
  243. {
  244.     eindex res;
  245.  
  246.     load_1_arg(e1, p1);
  247.  
  248.     if (eptype(p1) == T_INT) {
  249.         res = new_element(current, T_INT);
  250.         eaddr(current, res)->V.i = ~(p1->V.i);
  251.     } else if (eptype(p1) == T_KEY) {
  252.         byteptr s = p1->V.nam.u.s;
  253.         byte k[8];
  254.         int i;
  255.         for (i = 0; i < 8; i++)
  256.             k[i] = ~*s;
  257.         res = key_add(k);
  258.     } else
  259.         return ERR_TYPE_CHECK;
  260.  
  261.     decrefp(current, e1, p1);
  262.     return_ok_result(1, res);
  263. }
  264.  
  265.  
  266. retcode o_or()
  267. {
  268.     eindex res;
  269.  
  270.     load_2_args(e1, e2, p1, p2);
  271.  
  272.     if (eptype(p1) == T_INT && eptype(p2) == T_INT) {
  273.         res = new_element(current, T_INT);
  274.         eaddr(current, res)->V.i = p1->V.i | p2->V.i;
  275.     } else if (eptype(p1) == T_KEY && eptype(p2) == T_KEY) {
  276.         byteptr s = p2->V.nam.u.s;
  277.         byte k[8];
  278.         int i;
  279.         memcpy(k, (char*)(p1->V.nam.u.s), 8);
  280.         for (i = 0; i < 8; i++, s++)
  281.             k[i] |= *s;
  282.         res = key_add(k);
  283.     } else
  284.         return ERR_TYPE_CHECK;
  285.  
  286.     decrefp(current, e1, p1);
  287.     decrefp(current, e2, p2);
  288.     return_ok_result(2, res);
  289. }
  290.  
  291.  
  292. retcode o_sub()
  293. {
  294.     eindex res;
  295.  
  296.     load_2_args(e1, e2, p1, p2);
  297.  
  298.     if (eptype(p1) == T_INT && eptype(p2) == T_INT) {
  299.         res = new_element(current, T_INT);
  300.         eaddr(current, res)->V.i = p1->V.i - p2->V.i;
  301.     } else if (eptype(p1) == T_TIME && eptype(p2) == T_TIME) {
  302.         if (!(epattr(p1) & epattr(p2) & A_READ))
  303.             return ERR_ACCESS_CHECK;
  304.         res = time_diff(current, e1, e2);
  305.     } else if (eptype(p1) == T_TIME && eptype(p2) == T_INT) {
  306.         if (!(epattr(p1) & A_READ))
  307.             return ERR_ACCESS_CHECK;
  308.         res = time_addint(current, e1, - p2->V.i);
  309.     } else
  310.         return ERR_TYPE_CHECK;
  311.  
  312.     decrefp(current, e1, p1);
  313.     decrefp(current, e2, p2);
  314.     return_ok_result(2, res);
  315. }
  316.  
  317.  
  318. retcode o_xor()
  319. {
  320.     eindex res;
  321.  
  322.     load_2_args(e1, e2, p1, p2);
  323.  
  324.     if (eptype(p1) == T_INT && eptype(p2) == T_INT) {
  325.         res = new_element(current, T_INT);
  326.         eaddr(current, res)->V.i = p1->V.i ^ p2->V.i;
  327.     } else if (eptype(p1) == T_KEY && eptype(p2) == T_KEY) {
  328.         byteptr s = p2->V.nam.u.s;
  329.         byte k[8];
  330.         int i;
  331.         memcpy(k, (char*)(p1->V.nam.u.s), 8);
  332.         for (i = 0; i < 8; i++, s++)
  333.             k[i] ^= *s;
  334.         res = key_add(k);
  335.     } else
  336.         return ERR_TYPE_CHECK;
  337.  
  338.     decrefp(current, e1, p1);
  339.     decrefp(current, e2, p2);
  340.     return_ok_result(2, res);
  341. }
  342.