
/**  Stack Maninpuation  **/
/*A: K McIsaac*/
/*S: University of Western Australia, Nedlands 6009, Australia*/
/*D: January, 1988 */
/*K: Stack */

/*: Initstack[$stack] 
	Initilise $stack. */
Initstack_:Tier
_Initstack[Smp] : {0}
Initstack[$stack] :: ($stack : Initstack[])
Initstack[] :: {}

/*: Push[$expr, ($stack:{})] 
	Push $expr onto $stack. If stack is unassigned, then a new stack
	is greated. Returns the new stack */
Push_:Tier
_Push[Smp]:{,0}
Push[$expr] :: Cat[Initstack[],{$expr}]
Push[$expr, $stack_=Symbp[$stack]] :: \
	(Initstack[$stack];Push[$expr,$stack])
Push[$expr, $stack_=Listp[$stack]] :: \
	( Set[$stack[Len[$stack]+1], $expr]; $stack)

/*: Pop[$stack] 
	Pop top element off $stack */
Pop_:Tier
_Pop[Smp]:{0}
Pop[$stack_=(Listp[$stack]&Len[$stack]=1)] :: \
	(Lcl[%top]; %top : Top[$stack]; Set[$stack,{}];  %top)
Pop[$stack_=(Listp[$stack]&Len[$stack]>1)] :: \
	(Lcl[%top]; %top : Top[$stack]; Set[$stack[Len[$stack]]];  %top)

/*: Top[$stack]
	Returns the top of the stack. Does not alter the stack */
Top_:Tier;
_Top[Smp] :{0}
Top[$stack_=Listp[$stack]&Len[$stack]>0] :: $stack[Len[$stack]]

