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

  1. /* Copyright (C) 1989, 1992, 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. /* zcontrol.c */
  20. /* Control operators */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "estack.h"
  25. #include "iutil.h"
  26. #include "store.h"
  27.  
  28. /* Make an invalid file object. */
  29. extern void make_invalid_file(P1(ref *)); /* in zfile.c */
  30.  
  31. /* Forward references */
  32. private int no_cleanup(P1(os_ptr));
  33. private uint count_to_stopped(P0());
  34.  
  35. /* See the comment in opdef.h for an invariant which allows */
  36. /* more efficient implementation of for, loop, and repeat. */
  37.  
  38. /* <obj> exec - */
  39. int
  40. zexec(register os_ptr op)
  41. {    check_op(1);
  42.     if ( !r_has_attr(op, a_executable) )
  43.       return 0;        /* literal object just gets pushed back */
  44.     check_estack(1);
  45.     ++esp;
  46.     ref_assign(esp, op);
  47.     esfile_check_cache();
  48.     pop(1);
  49.     return o_push_estack;
  50. }
  51.  
  52. /* <bool> <proc> if - */
  53. int
  54. zif(register os_ptr op)
  55. {    check_type(op[-1], t_boolean);
  56.     if ( op[-1].value.boolval )
  57.        {    check_estack(1);
  58.         ++esp;
  59.         ref_assign(esp, op);
  60.         esfile_check_cache();
  61.        }
  62.     pop(2);
  63.     return o_push_estack;
  64. }
  65.  
  66. /* <bool> <proc_true> <proc_false> ifelse - */
  67. int
  68. zifelse(register os_ptr op)
  69. {    check_type(op[-2], t_boolean);
  70.     check_estack(1);
  71.     ++esp;
  72.     if ( op[-2].value.boolval )
  73.        {    ref_assign(esp, op - 1);
  74.        }
  75.     else
  76.        {    ref_assign(esp, op);
  77.        }
  78.     esfile_check_cache();
  79.     pop(3);
  80.     return o_push_estack;
  81. }
  82.  
  83. /* <init> <step> <limit> <proc> for - */
  84. private int
  85.   for_pos_int_continue(P1(os_ptr)),
  86.   for_neg_int_continue(P1(os_ptr)),
  87.   for_real_continue(P1(os_ptr));
  88. int
  89. zfor(register os_ptr op)
  90. {    int code;
  91.     float params[3];
  92.     register es_ptr ep;
  93.     check_proc(*op);
  94.     if ( r_has_type(op - 1, t_integer) &&
  95.          r_has_type(op - 2, t_integer) &&
  96.          r_has_type(op - 3, t_integer)
  97.        )
  98.         code = 7;
  99.     else if ( (code = num_params(op - 1, 3, params)) < 0 )
  100.         return code;
  101.     check_estack(7);
  102.     /* Push a mark, the control variable, the initial value, */
  103.     /* the increment, the limit, and the procedure, */
  104.     /* and invoke the continuation operator. */
  105.     push_mark_estack(es_for, no_cleanup);
  106.     ep = esp += 5;
  107.     if ( (code & 3) == 3 )        /* initial & increment are ints */
  108.        {    ep[-4] = op[-3];
  109.         ep[-3] = op[-2];
  110.         if ( code == 7 )
  111.             ep[-2] = op[-1];
  112.         else
  113.             make_int(ep - 2, (long)params[2]);
  114.         if ( ep[-3].value.intval >= 0 )
  115.             make_op_estack(ep, for_pos_int_continue);
  116.         else
  117.             make_op_estack(ep, for_neg_int_continue);
  118.        }
  119.     else
  120.        {    make_real(ep - 4, params[0]);
  121.         make_real(ep - 3, params[1]);
  122.         make_real(ep - 2, params[2]);
  123.         make_op_estack(ep, for_real_continue);
  124.        }
  125.     ep[-1] = *op;
  126.     pop(4);
  127.     return o_push_estack;
  128. }
  129. /* Continuation operators for for, separate for positive integer, */
  130. /* negative integer, and real. */
  131. /* Execution stack contains mark, control variable, increment, */
  132. /* limit, and procedure (procedure is topmost.) */
  133. /* Continuation operator for positive integers. */
  134. private int
  135. for_pos_int_continue(register os_ptr op)
  136. {    register es_ptr ep = esp;
  137.     long var = ep[-3].value.intval;
  138.     if ( var > ep[-1].value.intval )
  139.        {    esp -= 5;    /* pop everything */
  140.         return o_pop_estack;
  141.        }
  142.     push(1);
  143.     make_int(op, var);
  144.     ep[-3].value.intval = var + ep[-2].value.intval;
  145.     ref_assign_inline(ep + 2, ep);        /* saved proc */
  146.     esp = ep + 2;
  147.     return o_push_estack;
  148. }
  149. /* Continuation operator for negative integers. */
  150. private int
  151. for_neg_int_continue(register os_ptr op)
  152. {    register es_ptr ep = esp;
  153.     long var = ep[-3].value.intval;
  154.     if ( var < ep[-1].value.intval )
  155.        {    esp -= 5;    /* pop everything */
  156.         return o_pop_estack;
  157.        }
  158.     push(1);
  159.     make_int(op, var);
  160.     ep[-3].value.intval = var + ep[-2].value.intval;
  161.     ref_assign(ep + 2, ep);        /* saved proc */
  162.     esp = ep + 2;
  163.     return o_push_estack;
  164. }
  165. /* Continuation operator for reals. */
  166. private int
  167. for_real_continue(register os_ptr op)
  168. {    es_ptr ep = esp;
  169.     float var = ep[-3].value.realval;
  170.     float incr = ep[-2].value.realval;
  171.     if ( incr >= 0 ? (var > ep[-1].value.realval) :
  172.         (var < ep[-1].value.realval) )
  173.            {    esp -= 5;    /* pop everything */
  174.             return o_pop_estack;
  175.            }
  176.     push(1);
  177.     ref_assign(op, ep - 3);
  178.     ep[-3].value.realval = var + incr;
  179.     esp = ep + 2;
  180.     ref_assign(ep + 2, ep);        /* saved proc */
  181.     return o_push_estack;
  182. }
  183.  
  184. /* <int> <proc> repeat - */
  185. private int repeat_continue(P1(os_ptr));
  186. int
  187. zrepeat(register os_ptr op)
  188. {    check_type(op[-1], t_integer);
  189.     check_proc(*op);
  190.     if ( op[-1].value.intval < 0 )
  191.         return_error(e_rangecheck);
  192.     check_estack(5);
  193.     /* Push a mark, the count, and the procedure, and invoke */
  194.     /* the continuation operator. */
  195.     push_mark_estack(es_for, no_cleanup);
  196.     *++esp = op[-1];
  197.     *++esp = *op;
  198.     make_op_estack(esp + 1, repeat_continue);
  199.     pop(2);
  200.     return repeat_continue(op - 2);
  201. }
  202. /* Continuation operator for repeat */
  203. private int
  204. repeat_continue(register os_ptr op)
  205. {    es_ptr ep = esp;        /* saved proc */
  206.     if ( --(ep[-1].value.intval) >= 0 )    /* continue */
  207.        {    esp += 2;
  208.         ref_assign(esp, ep);
  209.         return o_push_estack;
  210.        }
  211.     else                /* done */
  212.        {    esp -= 3;        /* pop mark, count, proc */
  213.         return o_pop_estack;
  214.        }
  215. }
  216.  
  217. /* <proc> loop */
  218. private int loop_continue(P1(os_ptr));
  219. int
  220. zloop(register os_ptr op)
  221. {    check_proc(*op);
  222.     check_estack(4);
  223.     /* Push a mark and the procedure, and invoke */
  224.     /* the continuation operator. */
  225.     push_mark_estack(es_for, no_cleanup);
  226.     *++esp = *op;
  227.     make_op_estack(esp + 1, loop_continue);
  228.     pop(1);
  229.     return loop_continue(op - 1);
  230. }
  231. /* Continuation operator for loop */
  232. private int
  233. loop_continue(register os_ptr op)
  234. {    register es_ptr ep = esp;        /* saved proc */
  235.     ref_assign(ep + 2, ep);
  236.     esp = ep + 2;
  237.     return o_push_estack;
  238. }
  239.  
  240. /* - exit - */
  241. int
  242. zexit(register os_ptr op)
  243. {    uint scanned = 0;
  244.     STACK_LOOP_BEGIN(&e_stack, ep, used)
  245.     {    uint count = used;
  246.         ep += used - 1;
  247.         for ( ; count; count--, ep-- )
  248.           if ( r_is_estack_mark(ep) )
  249.             switch ( estack_mark_index(ep) )
  250.             {
  251.             case es_for:
  252.                 pop_estack(scanned + (used - count + 1));
  253.                 return o_pop_estack;
  254.             case es_stopped:
  255.                 return_error(e_invalidexit);    /* not a loop */
  256.             }
  257.         scanned += used;
  258.     }
  259.     STACK_LOOP_END(ep, used)
  260.     /* Return e_invalidexit if there is no mark at all. */
  261.     /* This is different from PostScript, which aborts. */
  262.     /* It shouldn't matter in practice. */
  263.     return_error(e_invalidexit);
  264. }
  265.  
  266. /* <result> .stop - */
  267. int
  268. zstop(register os_ptr op)
  269. {    uint count = count_to_stopped();
  270.     if ( count )
  271.     {    pop_estack((uint)count);
  272.         return o_pop_estack;
  273.     }
  274.     /* Return e_invalidexit if there is no mark at all. */
  275.     /* This is different from PostScript, which aborts. */
  276.     /* It shouldn't matter in practice. */
  277.     return_error(e_invalidexit);
  278. }
  279.  
  280. /* <obj> <result> .stopped <result> */
  281. int
  282. zstopped(register os_ptr op)
  283. {    check_op(2);
  284.     /* Mark the execution stack, and push the default result */
  285.     /* in case control returns normally. */
  286.     check_estack(3);
  287.     push_mark_estack(es_stopped, no_cleanup);
  288.     *++esp = *op;            /* save the result */
  289.     *++esp = op[-1];        /* execute the operand */
  290.     esfile_check_cache();
  291.     pop(2);
  292.     return o_push_estack;
  293. }
  294.  
  295. /* - .instopped false */
  296. /* - .instopped <result> true */
  297. int
  298. zinstopped(register os_ptr op)
  299. {    uint count = count_to_stopped();
  300.     if ( count )
  301.     {    push(2);
  302.         op[-1] = *ref_stack_index(&e_stack, count - 2);    /* default result */
  303.         make_true(op);
  304.     }
  305.     else
  306.     {    push(1);
  307.         make_false(op);
  308.     }
  309.     return 0;
  310. }
  311.  
  312. /* - countexecstack <int> */
  313. int
  314. zcountexecstack(register os_ptr op)
  315. {    push(1);
  316.     make_int(op, ref_stack_count(&e_stack));
  317.     return 0;
  318. }
  319.  
  320. /* <array> execstack <subarray> */
  321. private int execstack_continue(P1(os_ptr));
  322. int
  323. zexecstack(register os_ptr op)
  324. {    /* We can't do this directly, because the interpreter */
  325.     /* might have cached some state.  To force the interpreter */
  326.     /* to update the stored state, we push a continuation on */
  327.     /* the exec stack; the continuation is executed immediately, */
  328.     /* and does the actual transfer. */
  329.     uint depth = ref_stack_count(&e_stack);
  330.     check_write_type(*op, t_array);
  331.     if ( depth > r_size(op) )
  332.         return_error(e_rangecheck);
  333.     check_estack(1);
  334.     r_set_size(op, (uint)depth);
  335.     push_op_estack(execstack_continue);
  336.     return o_push_estack;
  337. }
  338. /* Continuation operator to do the actual transfer. */
  339. /* r_size(op) was set just above. */
  340. private int
  341. execstack_continue(register os_ptr op)
  342. {    return ref_stack_store(&e_stack, op, r_size(op), 0, 0, true, "execstack");
  343. }
  344.  
  345. /* <obj> <int> .quit - */
  346. int
  347. zquit(register os_ptr op)
  348. {    check_ostack(2);
  349.     check_type(*op, t_integer);
  350.     return e_Quit;        /* Interpreter will do the exit */
  351. }
  352.  
  353. /* - currentfile <file> */
  354. private ref *zget_current_file(P0());
  355. int
  356. zcurrentfile(register os_ptr op)
  357. {    ref *fp;
  358.     push(1);
  359.     /* Check the cache first */
  360.     if ( esfile != 0 )
  361.     {
  362. #ifdef DEBUG
  363.         /* Check that esfile is valid range. */
  364.         ref *efp = zget_current_file();
  365.         if ( esfile != efp )
  366.           { lprintf2("currentfile: esfile=0x%lx, efp=0x%lx\n",
  367.                  (ulong)esfile, (ulong)efp);
  368.             ref_assign(op, efp);
  369.           }
  370.         else
  371. #endif
  372.         ref_assign(op, esfile);
  373.     }
  374.     else if ( (fp = zget_current_file()) == 0 )
  375.     {    /* Return an invalid file object. */
  376.         /* This doesn't make a lot of sense to me, */
  377.         /* but it's what the PostScript manual specifies. */
  378.         make_invalid_file(op);
  379.     }
  380.     else
  381.     {    ref_assign(op, fp);
  382.         esfile_set_cache(fp);
  383.     }
  384.     /* Make the returned value literal. */
  385.     r_clear_attrs(op, a_executable);
  386.     return 0;
  387. }
  388. /* Get the current file from which the interpreter is reading. */
  389. private ref *
  390. zget_current_file(void)
  391. {    STACK_LOOP_BEGIN(&e_stack, ep, used)
  392.     {    uint count = used;
  393.         ep += used - 1;
  394.         for ( ; count; count--, ep-- )
  395.           if ( r_has_type_attrs(ep, t_file, a_executable) )
  396.             return ep;
  397.     }
  398.     STACK_LOOP_END(ep, used)
  399.     return 0;
  400. }
  401.  
  402. /* ------ Non-operator routines ------ */
  403.  
  404. /* Test whether we are inside a `stopped'. */
  405. /* The top level of the interpreter uses this. */
  406. int
  407. in_stopped(void)
  408. {    return count_to_stopped() != 0;
  409. }
  410.  
  411. /* ------ Initialization procedure ------ */
  412.  
  413. op_def zcontrol_op_defs[] = {
  414.     {"0countexecstack", zcountexecstack},
  415.     {"0currentfile", zcurrentfile},
  416.     {"1exec", zexec},
  417.     {"0execstack", zexecstack},
  418.     {"0exit", zexit},
  419.     {"2if", zif},
  420.     {"3ifelse", zifelse},
  421.     {"0.instopped", zinstopped},
  422.     {"4for", zfor},
  423.     {"1loop", zloop},
  424.     {"2.quit", zquit},
  425.     {"2repeat", zrepeat},
  426.     {"1.stop", zstop},
  427.     {"2.stopped", zstopped},
  428.         /* Internal operators */
  429.     {"0%execstack_continue", execstack_continue},
  430.     {"0%for_pos_int_continue", for_pos_int_continue},
  431.     {"0%for_neg_int_continue", for_neg_int_continue},
  432.     {"0%for_real_continue", for_real_continue},
  433.     {"0%loop_continue", loop_continue},
  434.     {"0%repeat_continue", repeat_continue},
  435.     op_def_end(0)
  436. };
  437.  
  438. /* ------ Internal routines ------ */
  439.  
  440. /* Vacuous cleanup routine */
  441. private int
  442. no_cleanup(os_ptr op)
  443. {    return 0;
  444. }
  445.  
  446. /* Count the number of elements down to and including the first 'stopped' */
  447. /* mark on the e-stack.  Return 0 if there is no 'stopped' mark. */
  448. private uint
  449. count_to_stopped(void)
  450. {    uint scanned = 0;
  451.     STACK_LOOP_BEGIN(&e_stack, ep, used)
  452.     {    uint count = used;
  453.         ep += used - 1;
  454.         for ( ; count; count--, ep-- )
  455.           if ( r_is_estack_mark(ep) &&
  456.                estack_mark_index(ep) == es_stopped
  457.              )
  458.             return scanned + (used - count + 1);
  459.         scanned += used;
  460.     }
  461.     STACK_LOOP_END(ep, used)
  462.     return 0;
  463. }
  464.  
  465. /* Pop the e-stack, executing cleanup procedures as needed. */
  466. /* We could make this more efficient using the STACK_LOOP macros, */
  467. /* but it isn't used enough to make this worthwhile. */
  468. void
  469. pop_estack(uint count)
  470. {    uint idx = 0;
  471.     uint popped = 0;
  472.     esfile_clear_cache();
  473.     for ( ; idx < count; idx++ )
  474.     {    ref *ep = ref_stack_index(&e_stack, idx - popped);
  475.         if ( r_is_estack_mark(ep) )
  476.         {    ref_stack_pop(&e_stack, idx + 1 - popped);
  477.             popped = idx + 1;
  478.             (*real_opproc(ep))(osp);
  479.         }
  480.     }
  481.     ref_stack_pop(&e_stack, count - popped);
  482. }
  483.