home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume11 / rpl / part02 / stackcmd.c < prev    next >
C/C++ Source or Header  |  1990-03-10  |  1KB  |  100 lines

  1. /****************************************************************
  2.  
  3. Module:
  4.     StackCmd
  5.  
  6. Description:
  7.     Commands for manipulating the stack
  8.  
  9.  
  10. Modification history:
  11.  
  12.     0.0    hjp    89-06-26
  13.  
  14.         initial version: DROP, SWAP, CLEAR
  15.  
  16.     0.1    hjp    89-06-26
  17.  
  18.         DUP added
  19.  
  20. ****************************************************************/
  21.  
  22. #include <stddef.h>
  23.  
  24. #include "errors.h"
  25. #include "globvar.h"
  26. #include "rpl.h"
  27. #include "intcmd.h"
  28. #include "stackcmd.h"
  29. #include "debug.h"
  30.  
  31. /*
  32.     drop the element at the top of the stack
  33. */
  34.  
  35. void c_drop (void)
  36. {
  37.     listobj * l;
  38.  
  39.     l = stack;
  40.  
  41.     if (l) {
  42.         if (l->id == LIST) {
  43.             stack = l->next;
  44.             destroy ((genobj *)l, 0);
  45.         } else {
  46.             error ("drop", INT_STKNOLIST, NULL);
  47.         }
  48.     } else {
  49.         error ("drop", ERR_STKEMPTY, NULL);
  50.     }
  51. }
  52.  
  53.  
  54. /*
  55.     swap two topmost arguments
  56. */
  57.  
  58. void c_swap (void)
  59. {
  60.     listobj * a, * b;
  61.  
  62.     if ((a = stack) && (b = stack->next)) {
  63.         a->next = b->next;
  64.         b->next = a;
  65.         stack = b;
  66.     } else {
  67.         error ("swap", ERR_2FEWARG, NULL);
  68.     }
  69. }
  70.  
  71.  
  72. /*
  73.     clear stack
  74. */
  75. void c_clear (void)
  76. {
  77.     while (stack) {
  78.         c_drop ();
  79.     }
  80. }
  81.  
  82. /*
  83.     duplicate topmost element
  84. */
  85.  
  86. void c_dup (void)
  87. {
  88. #ifdef TRACE
  89.     printf ("c_dup () {\n");
  90. #endif
  91.     if (stack) {
  92.         push (stack->obj);
  93.     } else {
  94.         error ("DUP", ERR_2FEWARG);
  95.     }
  96. #ifdef TRACE
  97.     printf ("} c_dup\n");
  98. #endif
  99. }
  100.