#include<stdio.h> 
 
/* 




                 VV                   VV    EEEEEEEEEEEEEE   
                  VV                 VV     EE  
                   VV               VV      EE
                    VV             VV       EE
                     VV           VV        EE
                      VV         VV         EEEEEEEEEEEEEE
                       VV       VV          EE
                        VV     VV           EE
                         VV   VV            EE
                          VV VV             EE
                           VVV              EE
                            V               EEEEEEEEEEEEEE
                                                       
 
 
 
 
                       MODULE  : stacks.c
 
                       VERSION : v1.0
 
                       REVISED : 16 Sep 2004
 
                       AUTHOR  : Michael A. Campbell
 
 
 
 
 
 


                          FUNCTIONAL DESCRIPTION
                          ----------------------
 
               This module contains a stacking set, which allows VE
 
               to stack pointers to text strings in a FIFO fashion.
 
               The stack implementation used is quite general, and can
 
               easily be applied to other applications.










*/

/* .page */

/*****************
 * INCLUDE FILES *
 *****************/

#include "../def/common.def"


#include "../str/stacks.str"


#include "../ext/strprim.ext"

/* .page */

/*********************
 * PUBLIC PROCEDURES *
 *********************/

void init_stack  (stack, stack_size)

/******************************************************************************
 *                                                                            *
 *  This routine is called to initialize a user stack.  The caller supplies   *
 *  a pointer to the stack to be intialized, and the length of the stack.     *
 *                                                                            *
 ******************************************************************************/

 struct stack_type *stack;  /* Pointer to the stack                         */
 int    stack_size;         /* The length, in pointers, of the stack        */

 begin
    /* Initialize the stack by filling it with idle flag (0xc7), and        */
    /* setting up its pointer.                                              */

    stack->pointer    = stack_size;
    stack->stack_size = stack_size;

    setb ( 0xc7, &stack->element[0], stack_size * sizeof(char *) );

end /* init_stack */

/* .page */

int stack_empty ( stack )

/******************************************************************************
 *                                                                            *
 *  This routine returns TRUE if the stack whose pointer is supplied is       *
 *  empty.  Else, it return FALSE.                                            *
 *                                                                            *
 ******************************************************************************/

 struct stack_type *stack;  /* Pointer to the stack                         */

 begin

    if ( stack->pointer == stack->stack_size ) then
         return ( TRUE );

    else
         return ( FALSE );

end /* stack_empty */

/* .page */

char *tos ( stack )

/******************************************************************************
 *                                                                            *
 *  This routine returns the pointer at the top of the stack, or 0, if the    *
 *  stack is empty.                                                           *
 *                                                                            *
 ******************************************************************************/

 struct stack_type *stack;  /* Pointer to the stack                         */

 begin

    /* If the stack is empty, return 0. */

    if ( stack->pointer == stack->stack_size ) then
         return ( NULL );

    else

        /* If the stack is non-empty, return the top value. */

        return ( stack->element[stack->pointer + 1] );

end /* tos */

/* .page */

void push ( stack, val )

/******************************************************************************
 *                                                                            *
 *  This routine pushes the supplied value onto the supplied stack.  If the   *
 *  stack is full, the supplied value overwrites the current top-of-stack.    *
 *                                                                            *
 ******************************************************************************/

 struct stack_type *stack;    /* Pointer to the stack                         */
 char   *val;                 /* The pointer to push onto the stack           */

 begin

    stack->element[stack->pointer] = val;

    if ( stack->pointer != (int) NULL ) then
         stack->pointer--;

end /* push */

/* .page */

char *pop ( stack )

/******************************************************************************
 *                                                                            *
 *  This routine pops the top value off of the stack, and returns it.  It     *
 *  returns 0 if the stack was empty.                                         *
 *                                                                            *
 ******************************************************************************/

 struct stack_type *stack;  /* Pointer to the stack                         */

 begin

    if ( stack->pointer == stack->stack_size ) then
         return ( NULL );

    else
         return ( stack->element[++stack->pointer] );

end /* pop */

