private void ParseUnary() { // We only have '+' and '-' as unary operators so we don't need to check precedence and pop from stack if (OperatorHelper.HasUnary(tokenizer.Token)) { operators.Push(tokenizer.Token); } }
private void ParseNumber() { output.Enqueue(new TokenNumber(tokenizer.Number)); // Need to enqueue unary operators after a number while (operators.Count != 0 && OperatorHelper.HasUnary(operators.Peek())) { EnqueueOperator(); } }
void OnCalculate(object sender, EventArgs e) //This method is called when we have both first number and second number and we are going to evaluate those number { if (currentState == 2) { var result = OperatorHelper.Calculate(firstNumber, secondNumber, myoperator); this.resultText.Text = result.ToString(); firstNumber = result; currentState = -1; } }
private void ParseBinary() { if (OperatorHelper.HasBinary(tokenizer.Token)) { int currentPrecedence = precedence[tokenizer.Token]; while (operators.Count != 0 && currentPrecedence <= precedence[operators.Peek()]) { EnqueueOperator(); } operators.Push(tokenizer.Token); } }
private void ParseCloseParenthesis() { // If there is no '(' on the stack we get an exception try { while (operators.Peek() != TokenType.OpenParenthesis) { EnqueueOperator(); } } catch (InvalidOperationException) { throw new SyntaxException("Missing open parenthesis"); } operators.Pop(); // Pop '(' // Need to enqueue unary operators after a pair of parenthesis while (operators.Count != 0 && OperatorHelper.HasUnary(operators.Peek())) { EnqueueOperator(); } }
private void EnqueueOperator() => output.Enqueue(OperatorHelper.GetOperator(operators.Pop()));