/// <summary> /// Checks to see if a string is a variable. /// </summary> /// <remarks><pre> /// 2004-07-20 - Jeremy Roberts /// 2004-08-11 - Jeremy Roberts /// Changed to check to see if it is a number. /// </pre></remarks> /// <param name="token">String to check.</param> /// <returns></returns> public bool IsVariable(string token) { token = token.ToLower(); if (IsConstant(token)) { return(false); } if (!ExpressionKeywords.IsOperand(token)) { return(false); } if (IsNumber(token)) { return(false); } if (IsString(token)) { return(false); } return(true); }
//private void CheckPostVectorForEvaluability(string inFix, string[] tokens) private void CheckPostVectorForEvaluability(string[] tokens) { var workstack = new Stack <string>(); for (int index = 0; index < tokens.Length; index++) { string token = tokens[index]; if (ExpressionKeywords.IsOperator(token) || ExpressionKeywords.Functions.Contains(token) || ExpressionKeywords.ConditionalOperators.Contains(token)) { var kw = ExpressionKeywords.Keywords.OfType <Procedure>() .Where(x => x.Name == token) .Select(x => x) .Single(); if (kw.VariableOperandsCount) { var current = ""; while (current != "param_terminator") { current = workstack.Pop(); } } else { try { for (int i = 0; i < kw.NumParameters; i++) { workstack.Pop(); } } catch { throw new ExpressionException("Operator error! \"" + token + "\". "); } } if (kw.AlwaysReturnsValue) { workstack.Push("0"); } } else { workstack.Push(token); } } if (workstack.Count != 1) { throw new ExpressionException("Expression formatted incorrecty! "); } }
private void CheckGrouping(string inFix) { string errorMsg = "Grouping error! Open missing. " + " " + inFix; var groupingStack = new Stack <char>(); foreach (var token in inFix.ToArray()) { if (!ExpressionKeywords.GroupOperators.Contains(token.ToString())) { continue; } if (ExpressionKeywords.OpenGroupOperators.Contains(token.ToString())) { groupingStack.Push(token); continue; } if (groupingStack.Count == 0) { throw new ExpressionException(errorMsg); } var last = groupingStack.Pop(); if (ExpressionKeywords.ClosingGroupOperators.Contains(token.ToString())) { Grouping g = ExpressionKeywords.GetGroupingFromClose(token.ToString()); if (last.ToString() != g.Open) { throw new ExpressionException(errorMsg); } } } if (groupingStack.Count != 0) { throw new ExpressionException("Grouping error! Close missing. " + " " + inFix); } }
/// <summary> /// Convecs an infix string to a post fix string. /// </summary> /// <remarks><pre> /// 2004-07-19 - Jeremy Roberts /// </pre></remarks> /// <param name="func">The function to convert</param> /// <returns>A post fix string.</returns> protected string Infix2Postfix(string func) { func = new InfixExpression(func).Expression; string[] inFix = func.Split(new[] { ' ' }); var postFix = new Stack <string>(); var operators = new Stack <string>(); string currOperator; foreach (string token in inFix) { if (ExpressionKeywords.IsOperand(token)) { postFix.Push(token); } else if (ExpressionKeywords.OpenGroupOperators.Contains(token)) { if (operators.Count > 0) { var kw = ExpressionKeywords.Keywords.OfType <Procedure>() .Where(x => x.Name == operators.Peek()) .Select(x => x) .SingleOrDefault(); if (kw != null && kw.VariableOperandsCount) { postFix.Push("param_terminator"); } } operators.Push(token); } else if (ExpressionKeywords.ClosingGroupOperators.Contains(token)) { Grouping g = ExpressionKeywords.GetGroupingFromClose(token); currOperator = operators.Pop(); while (currOperator != g.Open) { postFix.Push(currOperator); currOperator = operators.Pop(); } if (operators.Count > 0 && ExpressionKeywords.Functions.Contains(operators.Peek())) { postFix.Push(operators.Pop()); } } else if (ExpressionKeywords.IsOperator(token)) { // while precedence of the operator is <= precedence of the token while (operators.Count > 0) { if (ExpressionKeywords.GetPrecedence(token) <= ExpressionKeywords.GetPrecedence(operators.Peek())) { currOperator = operators.Pop(); postFix.Push(currOperator); } else { break; } } operators.Push(token); } } while (operators.Count > 0) { currOperator = operators.Pop(); postFix.Push(currOperator); } // Build the post fix string. string psString = string.Empty; foreach (string item in postFix) { psString = item + " " + psString; } psString = psString.Trim(); return(psString); }
/// <summary> /// Convecs an infix string to a post fix string. /// </summary> /// <remarks><pre> /// 2004-07-19 - Jeremy Roberts /// </pre></remarks> /// <param name="func">The function to convert</param> /// <returns>A post fix string.</returns> protected string Infix2Postfix(string func) { func = new InfixExpression(func).Expression; string[] inFix = func.Split(new[] { ' ' }); var postFix = new Stack <string>(); var operators = new Stack <string>(); string currOperator; foreach (string token in inFix) { if (ExpressionKeywords.IsOperand(token)) { postFix.Push(token); } else if (ExpressionKeywords.OpenGroupOperators.Contains(token)) { operators.Push(token); } else if (ExpressionKeywords.ClosingGroupOperators.Contains(token)) { Grouping g = ExpressionKeywords.GetGroupingFromClose(token); currOperator = operators.Pop(); while (currOperator != g.Open) { postFix.Push(currOperator); currOperator = operators.Pop(); } } else if (ExpressionKeywords.IsOperator(token)) { // while precedence of the operator is <= precedence of the token while (operators.Count > 0) { if (ExpressionKeywords.GetPrecedence(token) <= ExpressionKeywords.GetPrecedence(operators.Peek())) { currOperator = operators.Pop(); postFix.Push(currOperator); } else { break; } } operators.Push(token); } } while (operators.Count > 0) { currOperator = operators.Pop(); postFix.Push(currOperator); } // Build the post fix string. string psString = string.Empty; foreach (string item in postFix) { psString = item + " " + psString; } psString = psString.Trim(); return(psString); }