public void Parse() { int paranthesisCounter = 1; for (int i = 1; i < ContentArray.Length; i++) { char current = ContentArray[i]; if (current == '(' && paranthesisCounter < 2) { Paranthesis.Add(new Parantheses(Content.Substring(i, Content.Length - i))); paranthesisCounter++; } else if (current == '(') { paranthesisCounter++; } if (current == ')') { paranthesisCounter--; } if (paranthesisCounter == 0) { break; } ParsedContent += current; } Formatting(); }
//--------------------------------------------------------------------- public void Validate() { if (_paranthesisStack.Count > 0) { Paranthesis paranthesis = _paranthesisStack.Pop(); throw ParsingException.ParanthesisNotMatched(paranthesis); } }
//--------------------------------------------------------------------- internal static ParsingException ParanthesisNotMatched(Paranthesis paranthesis) { string msg = $"Paranthesis {paranthesis.Name} at position {paranthesis.Position} is not matched by counterpart."; return(new ParsingException(msg)); }
public void Resolve() { if (Paranthesis.Count == 0) { for (int i = 0; i < ContentArray.Length; i++) { char current = ContentArray[i]; if (!char.IsDigit(current) && current != ',') { Operators.Add(new Operator(current, Utilities.ExtractDoubleAtOperator(ContentArray, i, false), Utilities.ExtractDoubleAtOperator(ContentArray, i, true))); if (Operators.Count > 1) { Operators[Operators.Count - 1].LeftOperator = Operators[Operators.Count - 2]; Operators[Operators.Count - 2].RightOperator = Operators[Operators.Count - 1]; } } } if (Operators.Count == 0) { Value = double.Parse(FormattedContent); return; } List <Operator> operatorBuffer = new List <Operator>(Operators); int priorityCounter = 0; for (int i = 0; i < Operators.Count; i++) { switch (Operators[i].HasPriority) { case true: Operators[i].Process(); if (i + 1 < Operators.Count) { Operators[i + 1].Left = Operators[i].Result; } if (i - 1 >= 0) { if (!Operators[i - 1].HasPriority) { Operators[i - 1].Right = Operators[i].Result; } else { Operators[i - 1].Result = Operators[i].Result; Operators[i].UpdateLeft(); } } priorityCounter++; operatorBuffer.Remove(Operators[i]); break; } } if (priorityCounter == Operators.Count) { Value = Operators[Operators.Count - 1].Result; return; } for (int i = 0; i < operatorBuffer.Count; i++) { operatorBuffer[i].Process(); if (i + 1 >= operatorBuffer.Count) { break; } operatorBuffer[i + 1].Left = operatorBuffer[i].Result; } Value = operatorBuffer[operatorBuffer.Count - 1].Result; operatorBuffer.Clear(); return; } for (int i = 0; i < Paranthesis.Count; i++) { Paranthesis[i].Resolve(); } for (int i = 0; i < Paranthesis.Count; i++) { FormattedContent = FormattedContent.Replace("[" + i + "]", Paranthesis[i].Value + ""); } ContentArray = FormattedContent.ToCharArray(); Paranthesis.Clear(); Resolve(); }