home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / _gs / c / zcontrol < prev    next >
Encoding:
Text File  |  1991-10-25  |  7.9 KB  |  328 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zcontrol.c */
  21. /* Control operators for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "estack.h"
  26. #include "iutil.h"
  27. #include "store.h"
  28.  
  29. /* Forward references */
  30. private es_ptr find_stopped(P0());
  31.  
  32. /* exec */
  33. int
  34. zexec(register os_ptr op)
  35. {    check_op(1);
  36.     check_estack(1);
  37.     ++esp;
  38.     ref_assign(esp, op);
  39.     pop(1);
  40.     return o_check_estack;
  41. }
  42.  
  43. /* if */
  44. int
  45. zif(register os_ptr op)
  46. {    check_type(op[-1], t_boolean);
  47.     if ( op[-1].value.index )        /* true */
  48.        {    check_estack(1);
  49.         ++esp;
  50.         ref_assign(esp, op);
  51.        }
  52.     pop(2);
  53.     return o_check_estack;
  54. }
  55.  
  56. /* ifelse */
  57. int
  58. zifelse(register os_ptr op)
  59. {    check_type(op[-2], t_boolean);
  60.     check_estack(1);
  61.     ++esp;
  62.     if ( op[-2].value.index )
  63.        {    ref_assign(esp, op - 1);
  64.        }
  65.     else
  66.        {    ref_assign(esp, op);
  67.        }
  68.     pop(3);
  69.     return o_check_estack;
  70. }
  71.  
  72. /* for */
  73. private int
  74.   for_int_continue(P1(os_ptr)),
  75.   for_real_continue(P1(os_ptr));
  76. int
  77. zfor(register os_ptr op)
  78. {    int code;
  79.     float params[3];
  80.     if ( r_has_type(op - 1, t_integer) &&
  81.          r_has_type(op - 2, t_integer) &&
  82.          r_has_type(op - 3, t_integer)
  83.        )
  84.         code = 7;
  85.     else if ( (code = num_params(op - 1, 3, params)) < 0 )
  86.         return code;
  87.     check_estack(7);
  88.     /* Push a mark, the control variable, the initial value, */
  89.     /* the increment, the limit, and the procedure, */
  90.     /* and invoke the continuation operator. */
  91.     mark_estack(es_for);
  92.     esp += 4;
  93.     if ( code == 7 )        /* all integers */
  94.         esp[-3] = op[-3], esp[-2] = op[-2], esp[-1] = op[-1];
  95.     else
  96.        {    make_real(esp - 3, params[0]);
  97.         make_real(esp - 2, params[1]);
  98.         make_real(esp - 1, params[2]);
  99.        }
  100.     *esp = *op;
  101.     pop(4);
  102.     op -= 4;
  103.     return (code == 7 ? for_int_continue(op) : for_real_continue(op));
  104. }
  105. /* Continuation operators for for, separate for integer and real. */
  106. /* Execution stack contains mark, control variable, increment, */
  107. /* limit, and procedure (procedure is topmost.) */
  108. /* Continuation operator for integers. */
  109. private int
  110. for_int_continue(register os_ptr op)
  111. {    ref proc;
  112.     long var = esp[-3].value.intval;
  113.     long incr = esp[-2].value.intval;
  114.     if ( incr >= 0 ? (var > esp[-1].value.intval) :
  115.         (var < esp[-1].value.intval) )
  116.            {    esp -= 5;    /* pop everything */
  117.             return o_check_estack;
  118.            }
  119.     push(1);
  120.     *op = esp[-3];
  121.     esp[-3].value.intval = var + incr;
  122.     proc = *esp;        /* saved proc */
  123.     push_op_estack(for_int_continue);    /* push continuation */
  124.     *++esp = proc;
  125.     return o_check_estack;
  126. }
  127. /* Continuation operator for reals. */
  128. private int
  129. for_real_continue(register os_ptr op)
  130. {    ref proc;
  131.     float var = esp[-3].value.realval;
  132.     float incr = esp[-2].value.realval;
  133.     if ( incr >= 0 ? (var > esp[-1].value.realval) :
  134.         (var < esp[-1].value.realval) )
  135.            {    esp -= 5;    /* pop everything */
  136.             return o_check_estack;
  137.            }
  138.     push(1);
  139.     *op = esp[-3];
  140.     esp[-3].value.realval = var + incr;
  141.     proc = *esp;        /* saved proc */
  142.     push_op_estack(for_real_continue);    /* push continuation */
  143.     *++esp = proc;
  144.     return o_check_estack;
  145. }
  146.  
  147. /* repeat */
  148. private int repeat_continue(P1(os_ptr));
  149. int
  150. zrepeat(register os_ptr op)
  151. {    check_type(op[-1], t_integer);
  152.     if ( op[-1].value.intval < 0 ) return e_rangecheck;
  153.     check_estack(5);
  154.     /* Push a mark, the count, and the procedure, and invoke */
  155.     /* the continuation operator. */
  156.     mark_estack(es_for);
  157.     *++esp = op[-1];
  158.     *++esp = *op;
  159.     pop(2);
  160.     return repeat_continue(op - 2);
  161. }
  162. /* Continuation operator for repeat */
  163. private int
  164. repeat_continue(register os_ptr op)
  165. {    ref proc;
  166.     proc = *esp;        /* saved proc */
  167.     if ( --(esp[-1].value.intval) >= 0 )    /* continue */
  168.        {    push_op_estack(repeat_continue);    /* push continuation */
  169.         *++esp = proc;
  170.        }
  171.     else                /* done */
  172.        {    esp -= 3;        /* pop mark, count, proc */
  173.        }
  174.     return o_check_estack;
  175. }
  176.  
  177. /* loop */
  178. private int loop_continue(P1(os_ptr));
  179. int
  180. zloop(register os_ptr op)
  181. {    check_op(1);
  182.     check_estack(4);
  183.     /* Push a mark and the procedure, and invoke */
  184.     /* the continuation operator. */
  185.     mark_estack(es_for);
  186.     *++esp = *op;
  187.     pop(1);
  188.     return loop_continue(op - 1);
  189. }
  190. /* Continuation operator for loop */
  191. private int
  192. loop_continue(register os_ptr op)
  193. {    ref proc;
  194.     proc = *esp;        /* saved proc */
  195.     push_op_estack(loop_continue);    /* push continuation */
  196.     *++esp = proc;
  197.     return o_check_estack;
  198. }
  199.  
  200. /* exit */
  201. int
  202. zexit(register os_ptr op)
  203. {    es_ptr ep = esp;
  204.     while ( ep >= esbot )
  205.        {    if ( r_has_type(ep, t_null) )    /* control mark */
  206.             switch ( (ep--)->value.index )
  207.                {
  208.             case es_for: esp = ep; return o_check_estack;
  209.             case es_stopped: return e_invalidexit;    /* not a loop */
  210.                }
  211.         else
  212.             ep--;
  213.        }
  214.     /* Return e_invalidexit if there is no mark at all. */
  215.     /* This is different from PostScript, which aborts. */
  216.     /* It shouldn't matter in practice. */
  217.     return e_invalidexit;
  218. }
  219.  
  220. /* stop */
  221. int
  222. zstop(register os_ptr op)
  223. {    es_ptr ep = find_stopped();
  224.     if ( ep )
  225.        {    esp = ep - 1;
  226.         push(1);
  227.         make_bool(op, 1);
  228.         return o_check_estack;
  229.        }
  230.     /* Return e_invalidexit if there is no mark at all. */
  231.     /* This is different from PostScript, which aborts. */
  232.     /* It shouldn't matter in practice. */
  233.     return e_invalidexit;
  234. }
  235.  
  236. /* stopped */
  237. int
  238. zstopped(register os_ptr op)
  239. {    check_op(1);
  240.     /* Mark the execution stack, and push a false in case */
  241.     /* control returns normally. */
  242.     check_estack(3);
  243.     mark_estack(es_stopped);
  244.     ++esp; make_false(esp);
  245.     *++esp = *op;            /* execute the operand */
  246.     pop(1);
  247.     return o_check_estack;
  248. }
  249.  
  250. /* .instopped */
  251. int
  252. zinstopped(register os_ptr op)
  253. {    push(1);
  254.     make_bool(op, find_stopped() != 0);
  255.     return 0;
  256. }
  257.  
  258. /* countexecstack */
  259. int
  260. zcountexecstack(register os_ptr op)
  261. {    push(1);
  262.     make_int(op, esp - esbot + 1);
  263.     return 0;
  264. }
  265.  
  266. /* execstack */
  267. private int continue_execstack(P1(os_ptr));
  268. int
  269. zexecstack(register os_ptr op)
  270. {    /* We can't do this directly, because the interpreter */
  271.     /* might have cached some state.  To force the interpreter */
  272.     /* to update the stored state, we push a continuation on */
  273.     /* the exec stack; the continuation is executed immediately, */
  274.     /* and does the actual transfer. */
  275.     int depth = esp - esbot + 1;
  276.     check_write_type(*op, t_array);
  277.     if ( depth > r_size(op) ) return e_rangecheck;
  278.     check_estack(1);
  279.     r_set_subrange_size(op, depth);
  280.     push_op_estack(continue_execstack);
  281.     return o_check_estack;
  282. }
  283. /* Continuation operator to do the actual transfer */
  284. private int
  285. continue_execstack(register os_ptr op)
  286. {    int depth = r_size(op);        /* was set above */
  287.     refcpy_to_old(op->value.refs, esbot, depth, "execstack");
  288.     return 0;
  289. }
  290.  
  291. /* quit */
  292. int
  293. zquit(register os_ptr op)
  294. {    gs_exit(0);
  295. }
  296.  
  297. /* ------ Initialization procedure ------ */
  298.  
  299. op_def zcontrol_op_defs[] = {
  300.     {"0countexecstack", zcountexecstack},
  301.     {"1exec", zexec},
  302.     {"0execstack", zexecstack},
  303.     {"0exit", zexit},
  304.     {"2if", zif},
  305.     {"3ifelse", zifelse},
  306.     {"0.instopped", zinstopped},
  307.     {"4for", zfor},
  308.     {"1loop", zloop},
  309.     {"0quit", zquit},
  310.     {"2repeat", zrepeat},
  311.     {"0stop", zstop},
  312.     {"1stopped", zstopped},
  313.     op_def_end(0)
  314. };
  315.  
  316. /* Internal routines */
  317.  
  318. /* Find a `stopped' mark on the e-stack. */
  319. /* Return the e-stack pointer or 0. */
  320. private es_ptr
  321. find_stopped()
  322. {    register es_ptr ep;
  323.     for ( ep = esp; ep >= esbot; --ep )
  324.       if ( r_has_type(ep, t_null) && ep->value.index == es_stopped )
  325.         return ep;
  326.     return 0;
  327. }
  328.