#include <stdio.h>
#include <stdbool.h>
#include "stack.h"

bool isWhitespace(char c)
{
	return(c == ' ' || c == '\t' || c == '\n' || c == '\r' );
}

char *eatAllWhitespaces(char *At)
{
	while(*At && isWhitespace(*At))
		At++;
	return At;
}

bool isDigit(char Digit)
{
	return (Digit >= '0' && Digit <= '9');
}

int  isNumber(char *Number)
{
	int Result = 0;
	while(*Number && *Number != ' ')
	{
		if(!isDigit(*Number))
		{
			Result = -1;
			return Result;
		}
		Number++;
		Result++;
	}
	
	return Result;
}

int main(int argc, char **argv)
{
	stack Stack = {};
	float Result = 0;
	//
	// Parsing
	//	
	//char *Input = "6 3 + 4 - 20 *";
	char *Input = argv[1];
	char *At = Input;
	while(*At)
	{
		At = eatAllWhitespaces(At);
		switch(*At)
		{
		case '+':
		{
			float Addent1 = pop(&Stack);
			float Addent2 = pop(&Stack);
			push(&Stack, Addent1 + Addent2);
		}break;
		case '-':
		{
			float Subtrahend = pop(&Stack);
			float Minuend = pop(&Stack);
		       
			push(&Stack, Minuend - Subtrahend);
		}break;
		case '*':
		{
			float Multiplicand = pop(&Stack);
			float Multiplier = pop(&Stack);
			push(&Stack, Multiplier * Multiplicand);
		}break;
		case '/':
		{
			float divisor = pop(&Stack);
			float dividend = pop(&Stack);
			push(&Stack, dividend / divisor);
		}break;
		default:
		{
			int Length = isNumber(At);
			if(Length != -1)
			{
				push(&Stack, atof(At));
				At += Length -1;
			}
			else
			{
				printf("ERROR: Only Numbers and Operators + - * and / are allowed");
			}
		}
		}
		At++;
	}
	while(peak(&Stack))
	{
		printf("%f ", pop(&Stack));
	}
	printf("\n");
}
