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

  1. /*
  2.     o_ctrl.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.  
  26. #include "l_proto.h"
  27. #include "o_proto.h"
  28.  
  29.  
  30. retcode
  31. o_exec()
  32. {
  33.     load_1_arg(ei, ep);
  34.  
  35.     if (eptype(ep) != T_ARRAY && eptype(ep) != T_STRING &&
  36.         eptype(ep) != T_NAME && eptype(ep) != T_PROC)
  37.         return OK;
  38.  
  39.     if (current->esp >= MAXESTACK)
  40.         return ERR_ESTACK_OVERFLOW;
  41.     if (!(epattr(ep) & A_EXEC))
  42.         return ERR_ACCESS_CHECK;
  43.     current->osp--;
  44.  
  45.     if (!(epattr(ep)&A_EXECUTABLE)) {
  46.         eindex res = make_sub(current, ei, 0);
  47.         decrefp(current, ei, ep);
  48.         ei = res;
  49.         eattr(current,ei) |= A_EXECUTABLE;
  50.     }
  51.     current->es[current->esp++] = ei;
  52.  
  53.     return OK;
  54. }
  55.  
  56.  
  57. retcode
  58. o_exit()
  59. {
  60.     sshort i, j;
  61.     eindex *ip;
  62.  
  63.     if (current->esp == 0)
  64.         return ERR_NOT_IN_LOOP;
  65.     ip = current->es + current->esp - 1;
  66.     for (i=current->esp; i > 0; i--, ip--)
  67.         if (*ip == loop_mark || *ip == halt_mark)
  68.             break;
  69.     if (i == 0 || *ip == halt_mark)
  70.         return ERR_NOT_IN_LOOP;
  71.     i--;
  72.     for (j = current->esp - i; j > 0; j--)
  73.         decref(current, *ip++);
  74.     current->esp = i;
  75.  
  76.     return OK;
  77. }
  78.  
  79.  
  80. retcode
  81. o_halt()
  82. {
  83.     eindex ei, *ip;
  84.     sshort i, j;
  85.     eptr ep;
  86.  
  87.     if (current->esp == 0)
  88.         return ERR_NOT_IN_HALTED;
  89.     ip = current->es + current->esp - 1;
  90.     for (i=current->esp; i > 0; i--, ip--)
  91.         if (*ip == halt_mark)
  92.             break;
  93.     if (i == 0)
  94.         return ERR_NOT_IN_HALTED;
  95.     i--;
  96.     for (j = current->esp - i; j > 0; j--)
  97.         decref(current, *ip++);
  98.     current->esp = i;
  99.     ei = new_element(current, T_INT);
  100.     ep = eaddr(current,ei);
  101.     ep->V.i = 1;
  102.     current->es[current->esp++] = ei;
  103.  
  104.     return OK;
  105. }
  106.  
  107.  
  108. retcode
  109. o_halted()
  110. {
  111.     if (current->osp < 1)
  112.         return ERR_STACK_UNDERFLOW;
  113.     if (current->esp+3 >= MAXESTACK)
  114.         return ERR_ESTACK_OVERFLOW;
  115.     current->es[current->esp++] = halt_mark;
  116.     incref(current,halt_mark);
  117.     current->es[current->esp++] = halted_proc;
  118.     incref(current,halted_proc);
  119.     return o_exec();
  120. }
  121.  
  122.  
  123. retcode
  124. the_halted_proc()
  125. {
  126.     eindex ei;
  127.     eptr ep;
  128.  
  129.     /* drop halt_mark */
  130.     decref(current, current->es[current->esp-1]);
  131.     ei = new_element(current, T_INT);
  132.     ep = eaddr(current,ei);
  133.     ep->V.i = 0;
  134.     current->es[current->esp-1] = ei;
  135.     return OK;
  136. }
  137.  
  138.  
  139. retcode
  140. o_ifelse()
  141. {
  142.     eindex ei;
  143.     eptr ep;
  144.     byte flag;
  145.  
  146.     if (current->osp < 3)
  147.         return ERR_STACK_UNDERFLOW;
  148.     ei = current->os[current->osp-3];
  149.     ep = eaddr(current, ei);
  150.     if (eptype(ep)!=T_INT)
  151.         return ERR_TYPE_CHECK;
  152.     flag = ep->V.i != 0;
  153.     decref(current, ei);
  154.     if (flag) {
  155.         current->os[current->osp-3] = current->os[current->osp-2];
  156.         decref(current, current->os[current->osp-1]);
  157.     } else {
  158.         current->os[current->osp-3] = current->os[current->osp-1];
  159.         decref(current, current->os[current->osp-2]);
  160.     }
  161.     current->osp -= 2;
  162.     return o_exec();
  163. }
  164.  
  165.  
  166. retcode
  167. o_loop()
  168. {
  169.     eindex arg, cnt, proc, loop;
  170.     eptr ep;
  171.  
  172.     if (current->osp < 2)
  173.         return ERR_STACK_UNDERFLOW;
  174.  
  175.     proc = current->os[current->osp-1];
  176.     arg = current->os[current->osp-2];
  177.     ep = eaddr(current, arg);
  178.  
  179.     switch (eptype(ep)) {
  180.         case T_INT:
  181.         cnt = new_element(current, T_INT);
  182.         if (ep->V.i >= 0)     /* store the upper limit in the length field */
  183.             elen(current,cnt) = ep->V.i + 1;
  184.         else
  185.             eaddr(current,cnt)->V.i = -1;
  186.         decref(current, arg);
  187.         loop = loop_iproc;
  188.         break;
  189.         case T_ARRAY:
  190.         if (!(epattr(ep) & A_EXEC) || !(epattr(ep) & A_READ))
  191.             return ERR_ACCESS_CHECK;
  192.         cnt = make_sub(current, arg, 0);
  193.         decref(current, arg);
  194.         loop = loop_aproc;
  195.         break;
  196.         case T_DICT:
  197.         if (!(epattr(ep) & A_EXEC) || !(epattr(ep) & A_READ))
  198.             return ERR_ACCESS_CHECK;
  199.         cnt = make_sub(current, arg, 0);
  200.         decref(current, arg);
  201.         loop = loop_dproc;
  202.         break;
  203.         case T_STRING:
  204.         if (!(epattr(ep) & A_EXEC) || !(epattr(ep) & A_READ))
  205.             return ERR_ACCESS_CHECK;
  206.         cnt = make_sub(current, arg, 0);
  207.         decref(current, arg);
  208.         loop = loop_sproc;
  209.         break;
  210.         default:
  211.         return ERR_TYPE_CHECK;
  212.     }
  213.     current->es[current->esp++] = loop_mark;
  214.     incref(current, loop_mark);
  215.     current->es[current->esp++] = cnt;
  216.     current->es[current->esp++] = proc;
  217.     current->es[current->esp++] = loop;
  218.     incref(current, loop);
  219.     current->osp -= 2;
  220.     return OK;
  221. }
  222.  
  223.  
  224. retcode
  225. the_loop_iproc()
  226. {
  227.     eindex ei = current->es[current->esp-2];
  228.     eptr ep = eaddr(current,ei);
  229.  
  230.     TRACE(5, printf("loop_int_proc %d/%d\n", (int) ep->V.i, (int) eplen(ep)))
  231.  
  232.     if (eplen(ep))
  233.         ep->V.i += 1;
  234.     if (ep->V.i >= (sint) eplen(ep)) {    /* end of loop */
  235.         decref(current, current->es[current->esp-1]);
  236.         decref(current, current->es[current->esp-2]);
  237.         decref(current, current->es[current->esp-3]);
  238.         current->esp -= 3;
  239.         return OK;
  240.     }
  241.     if (eplen(ep)) {    /* counting loop: push the counter */
  242.         ei = new_element(current, T_INT);
  243.         eaddr(current, ei)->V.i = ep->V.i - 1;
  244.         current->os[current->osp++] = ei;
  245.     }
  246.     current->es[current->esp++] = loop_iproc;
  247.     incref(current, loop_iproc);
  248.     ei = current->es[current->esp-2];
  249.     ep = eaddr(current, ei);
  250.     if (eptype(ep) == T_STRING || eptype(ep) == T_ARRAY)
  251.         ei = make_sub(current, ei, 0);
  252.     else
  253.         increfp(ep);
  254.     current->es[current->esp++] = ei;
  255.  
  256.     return OK;
  257. }
  258.  
  259.  
  260. retcode
  261. the_loop_aproc()
  262. {
  263.     eindex ei = current->es[current->esp-2], topush;
  264.     eptr ep = eaddr(current,ei);
  265.  
  266.     TRACE(5, printf("loop_arrray_proc %d\n", (int) ep->V.sub.e))
  267.  
  268.     if (eplen(ep) == 0) {    /* end of loop */
  269.         decref(current, current->es[current->esp-1]);
  270.         decref(current, current->es[current->esp-2]);
  271.         decref(current, current->es[current->esp-3]);
  272.         current->esp -= 3;
  273.         return OK;
  274.     }
  275.     topush = array_get(current, ei, 0);
  276.     current->os[current->osp++] = topush;
  277.     incref(current, topush);
  278.     ep->V.sub.offset += 1;
  279.     eplen(ep) -= 1;
  280.  
  281.     current->es[current->esp++] = loop_aproc;
  282.     incref(current, loop_aproc);
  283.  
  284.     ei = current->es[current->esp-2];
  285.     ep = eaddr(current, ei);
  286.     if (eptype(ep) == T_STRING || eptype(ep) == T_ARRAY)
  287.         ei = make_sub(current, ei, 0);
  288.     else
  289.         increfp(ep);
  290.     current->es[current->esp++] = ei;
  291.  
  292.     return OK;
  293. }
  294.  
  295.  
  296. retcode
  297. the_loop_dproc()
  298. {
  299.     eindex ei = current->es[current->esp-2], di, *ip;
  300.     eptr ep = eaddr(current,ei), dp;
  301.  
  302.     TRACE(5, printf("loop_dict_proc %d\n", (int) ep->V.sub.e))
  303.  
  304.     di = ep->V.sub.e;
  305.     dp = eaddr(current, di);
  306.     ip = dp->V.dic.d + 2*ep->V.sub.offset;
  307.     while(ep->V.sub.offset < dp->V.dic.alen) {
  308.         if (!*ip || *ip == DICT_DELETED) {
  309.             ep->V.sub.offset += 1;
  310.             ip += 2;
  311.             continue;
  312.         }
  313.         current->os[current->osp++] = *ip;
  314.         incref(current, *ip);
  315.         current->os[current->osp++] = *(ip+1);
  316.         incref(current, *(ip+1));
  317.         ep->V.sub.offset += 1;
  318.         current->es[current->esp++] = loop_dproc;
  319.         incref(current, loop_dproc);
  320.  
  321.         ei = current->es[current->esp-2];
  322.         ep = eaddr(current, ei);
  323.         if (eptype(ep) == T_STRING || eptype(ep) == T_ARRAY)
  324.             ei = make_sub(current, ei, 0);
  325.         else
  326.             increfp(ep);
  327.         current->es[current->esp++] = ei;
  328.  
  329.         return OK;
  330.     }
  331.     /* end of loop */
  332.     decref(current, current->es[current->esp-1]);
  333.     decref(current, current->es[current->esp-2]);
  334.     decref(current, current->es[current->esp-3]);
  335.     current->esp -= 3;
  336.     return OK;
  337. }
  338.  
  339.  
  340. retcode
  341. the_loop_sproc()
  342. {
  343.     eindex ei = current->es[current->esp-2], topush;
  344.     eptr ep = eaddr(current,ei);
  345.  
  346.     TRACE(5, printf("loop_string_proc %d\n", (int) ep->V.sub.e))
  347.  
  348.     if (eplen(ep) == 0) {    /* end of loop */
  349.         decref(current, current->es[current->esp-1]);
  350.         decref(current, current->es[current->esp-2]);
  351.         decref(current, current->es[current->esp-3]);
  352.         current->esp -= 3;
  353.         return OK;
  354.     }
  355.     topush = new_element(current, T_INT);
  356.     eaddr(current, topush)->V.i = str_get(current, ei, 0);
  357.     current->os[current->osp++] = topush;
  358.     ep->V.sub.offset += 1;
  359.     eplen(ep) -= 1;
  360.  
  361.     current->es[current->esp++] = loop_sproc;
  362.     incref(current, loop_sproc);
  363.  
  364.     ei = current->es[current->esp-2];
  365.     ep = eaddr(current, ei);
  366.     if (eptype(ep) == T_STRING || eptype(ep) == T_ARRAY)
  367.         ei = make_sub(current, ei, 0);
  368.     else
  369.         increfp(ep);
  370.     current->es[current->esp++] = ei;
  371.  
  372.     return OK;
  373. }
  374.  
  375.