#include "calc.h"

ErrorCode empty(ElementStack * inout_stack)
{
	if (inout_stack == NULL)
		return FAILURE;
	
	inout_stack->element_count = 0;
	inout_stack->elements = NULL;

	return SUCCESS;
}

ErrorCode pop(ElementStack * inout_stack,
              Element * out_element)
{
	if ((inout_stack == NULL) || is_empty(*inout_stack))
	{
		return FAILURE;
	}
	
	*out_element =	inout_stack->elements[inout_stack->element_count - 1];
	inout_stack->element_count = inout_stack->element_count - 1;
	inout_stack->elements = realloc(inout_stack->elements, inout_stack->element_count *sizeof(Element));

	return SUCCESS;
}

ErrorCode push(ElementStack * inout_stack,
               Element in_element)
{
	if (inout_stack == NULL)
	{
		return FAILURE;
	}
	
	inout_stack->element_count = inout_stack->element_count + 1;
	inout_stack->elements = realloc(inout_stack->elements, inout_stack->element_count *sizeof(Element));
	inout_stack->elements[inout_stack->element_count - 1] = in_element;

	return SUCCESS;
}

bool is_empty(ElementStack in_stack)
{
	return (in_stack.element_count == 0);
}
