/*****************************************************************************

	PushEx()

	This function pushes an item onto the expression stack.  The
expression stack implement's TECO's expression handling capability.  For
instance,  if a command like 10+qa=$ is executed,  then three values are
pushed onto the expression stack: 10, plus sign and the value of qa.  Each
time a value is pushed onto the expression stack,  the Reduce function is
called to see if the stack can be reduced.  In the above example,  Reduce
would cause the stack to be reduced when the value of qa is pushed,  because
the expression can be evaluated at that time.

*****************************************************************************/

#include "ZPort.h"		/* define portability identifiers */
#include "DefTeco.h"		/* define general identifiers */
#include "DefError.h"		/* define identifiers for error messages */

extern	VOID	ErrMsg();	/* display an error message */
extern	DEFAULT	Reduce();	/* reduce the expression stack */
extern	VOID	ZAbort();	/* cleanup and exit TECO-C */
#if DEBUGGING
char	ZChrIt();		/* convert long to char */
#endif

EXTERN	struct	EStck EStack[]; /* expression stack */
EXTERN	WORD	EStBot;		/* expression stack bottom */
EXTERN	WORD	EStTop;		/* expression stack top */


DEFAULT PushEx(Item, EType)	/* push onto expression stack */
LONG Item;
BYTE EType;

{
#if DEBUGGING
DbgInd+=2;if(DbgLvl>=2){DbgMsg();DbgDBf("PushEx: called. ");
if (EType == OPERAND)
	{
	DbgDBf("EType = OPERAND, Item = ");
	MakDBf(Item, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);DbgDBf("\015\012");
	}
else if (EType == OPERATOR)
	{
	DbgDBf("EType = OPERATOR, Item = \"");
	ZDspCh(ZChrIt(Item));DbgDBf("\"\015\012");
	}
else
	{
	DbgDBf("received illegal Item type\015\012");
	ZAbort();
	}
DbgROf();}
#endif

	if (++EStTop >= EXS_SIZE)		/* if expression stack full */
		{
		ErrMsg(ERR_PDO);		/* push-down list overflow */
		return(FAILURE);
		}

	EStack[EStTop].Elemnt = Item;		/* put item on stack */
	EStack[EStTop].ElType = EType;		/* put item type on stack */
	if (EStTop > (EStBot + 1))		/* if stack might reduce */
		if (Reduce() == FAILURE)	/* reduce expression stack */
#if DEBUGGING
{if(DbgLvl>=1)
{DbgMsg();DbgDBf("PushEx: returning FAILURE.\015\012");DbgROf();}DbgInd-=2;
#endif
			return(FAILURE);
#if DEBUGGING
}
#endif

#if DEBUGGING
if(DbgLvl>=2){DbgMsg();DbgDBf("PushEx: returning");
DbgDBf(", EStTop = ");MakDBf(EStTop, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);
DbgDBf(", EStBot = ");MakDBf(EStBot, 10);ZDspBf(DBfBeg, DBfPtr-DBfBeg);
DbgDBf("\015\012");DbgROf();}DbgInd-=2;
#endif
	return(SUCCESS);
}
