Esempio n. 1
0
        private ILogicResult ParseLogicResult(string aExpression, int aMaxRecursion)
        {
            --aMaxRecursion;
            m_ParsingContext.Preprocess(this, ref aExpression);
            aExpression = aExpression.Trim();
            if (aExpression.Contains(" or "))
            {
                string[]            parts = aExpression.Split(new string[] { " or " }, StringSplitOptions.None);
                List <ILogicResult> exp   = new List <ILogicResult>(parts.Length);
                for (int i = 0; i < parts.Length; i++)
                {
                    string s = parts[i].Trim();
                    if (!string.IsNullOrEmpty(s))
                    {
                        exp.Add(ParseLogicResult(s, aMaxRecursion));
                    }
                }
                if (exp.Count == 1)
                {
                    return(exp[0]);
                }
                return(new CombineOr {
                    inputs = exp
                });
            }
            else if (aExpression.Contains("||"))
            {
                string[]            parts = aExpression.Split(new string[] { "||" }, StringSplitOptions.None);
                List <ILogicResult> exp   = new List <ILogicResult>(parts.Length);
                for (int i = 0; i < parts.Length; i++)
                {
                    string s = parts[i].Trim();
                    if (!string.IsNullOrEmpty(s))
                    {
                        exp.Add(ParseLogicResult(s, aMaxRecursion));
                    }
                }
                if (exp.Count == 1)
                {
                    return(exp[0]);
                }
                return(new CombineOr {
                    inputs = exp
                });
            }
            if (aExpression.Contains(" xor "))
            {
                string[]            parts = aExpression.Split(new string[] { " xor " }, StringSplitOptions.None);
                List <ILogicResult> exp   = new List <ILogicResult>(parts.Length);
                for (int i = 0; i < parts.Length; i++)
                {
                    string s = parts[i].Trim();
                    if (!string.IsNullOrEmpty(s))
                    {
                        exp.Add(ParseLogicResult(s, aMaxRecursion));
                    }
                }
                if (exp.Count == 1)
                {
                    return(exp[0]);
                }
                return(new CombineXor {
                    inputs = exp
                });
            }
            else if (aExpression.Contains("^"))
            {
                string[]            parts = aExpression.Split(new string[] { "^" }, StringSplitOptions.None);
                List <ILogicResult> exp   = new List <ILogicResult>(parts.Length);
                for (int i = 0; i < parts.Length; i++)
                {
                    string s = parts[i].Trim();
                    if (!string.IsNullOrEmpty(s))
                    {
                        exp.Add(ParseLogicResult(s, aMaxRecursion));
                    }
                }
                if (exp.Count == 1)
                {
                    return(exp[0]);
                }
                return(new CombineXor {
                    inputs = exp
                });
            }
            else if (aExpression.Contains(" and "))
            {
                string[]            parts = aExpression.Split(new string[] { " and " }, StringSplitOptions.None);
                List <ILogicResult> exp   = new List <ILogicResult>(parts.Length);
                for (int i = 0; i < parts.Length; i++)
                {
                    string s = parts[i].Trim();
                    if (!string.IsNullOrEmpty(s))
                    {
                        exp.Add(ParseLogicResult(s, aMaxRecursion));
                    }
                }
                if (exp.Count == 1)
                {
                    return(exp[0]);
                }
                return(new CombineAnd {
                    inputs = exp
                });
            }
            else if (aExpression.Contains("&&"))
            {
                string[]            parts = aExpression.Split(new string[] { "&&" }, StringSplitOptions.None);
                List <ILogicResult> exp   = new List <ILogicResult>(parts.Length);
                for (int i = 0; i < parts.Length; i++)
                {
                    string s = parts[i].Trim();
                    if (!string.IsNullOrEmpty(s))
                    {
                        exp.Add(ParseLogicResult(s, aMaxRecursion));
                    }
                }
                if (exp.Count == 1)
                {
                    return(exp[0]);
                }
                return(new CombineAnd {
                    inputs = exp
                });
            }
            else if (aExpression.Contains("=="))
            {
                string[] parts = aExpression.Split(new string[] { "==" }, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length != 2)
                {
                    throw new ParseException("== operator needs an expression on either side");
                }
                return(new CompareEqual {
                    op1 = ParseNumber(parts[0].Trim(), aMaxRecursion), op2 = ParseNumber(parts[1].Trim(), aMaxRecursion)
                });
            }
            else if (aExpression.Contains("!="))
            {
                string[] parts = aExpression.Split(new string[] { "!=" }, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length != 2)
                {
                    throw new ParseException("!= operator needs an expression on either side");
                }
                return(new CompareNotEqual {
                    op1 = ParseNumber(parts[0].Trim(), aMaxRecursion), op2 = ParseNumber(parts[1].Trim(), aMaxRecursion)
                });
            }
            else if (aExpression.Contains(">="))
            {
                string[] parts = aExpression.Split(new string[] { ">=" }, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length != 2)
                {
                    throw new ParseException(">= operator needs an expression on either side");
                }
                return(new CompareGreaterOrEqual {
                    op1 = ParseNumber(parts[0].Trim(), aMaxRecursion), op2 = ParseNumber(parts[1].Trim(), aMaxRecursion)
                });
            }
            else if (aExpression.Contains(">"))
            {
                string[] parts = aExpression.Split(new string[] { ">" }, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length != 2)
                {
                    throw new ParseException("> operator needs an expression on either side");
                }
                return(new CompareGreater {
                    op1 = ParseNumber(parts[0].Trim(), aMaxRecursion), op2 = ParseNumber(parts[1].Trim(), aMaxRecursion)
                });
            }
            else if (aExpression.Contains("<="))
            {
                string[] parts = aExpression.Split(new string[] { "<=" }, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length != 2)
                {
                    throw new ParseException("<= operator needs an expression on either side");
                }
                return(new CompareLowerOrEqual {
                    op1 = ParseNumber(parts[0].Trim(), aMaxRecursion), op2 = ParseNumber(parts[1].Trim(), aMaxRecursion)
                });
            }
            else if (aExpression.Contains("<"))
            {
                string[] parts = aExpression.Split(new string[] { "<" }, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length != 2)
                {
                    throw new ParseException("< operator needs an expression on either side");
                }
                return(new CompareLower {
                    op1 = ParseNumber(parts[0].Trim(), aMaxRecursion), op2 = ParseNumber(parts[1].Trim(), aMaxRecursion)
                });
            }
            else if (aExpression.StartsWith("not "))
            {
                return(new CombineNot {
                    input = ParseLogicResult(aExpression.Substring(4), aMaxRecursion)
                });
            }
            else if (aExpression.StartsWith("!"))
            {
                return(new CombineNot {
                    input = ParseLogicResult(aExpression.Substring(1), aMaxRecursion)
                });
            }
            else if (aExpression == "true")
            {
                return(new ConstantBool {
                    constantValue = true
                });
            }
            else if (aExpression == "false")
            {
                return(new ConstantBool {
                    constantValue = false
                });
            }


            string bracketContent = m_ParsingContext.GetBracket(ref aExpression);

            if (!string.IsNullOrEmpty(bracketContent))
            {
                return(ParseLogicResult(bracketContent, aMaxRecursion));
            }
            ValueProvider value = m_ParsingContext.GetCommand(ref aExpression);

            if (value != null)
            {
                return(value);
            }

            if (ValidIdentifier(aExpression))
            {
                return(context.GetVariable(aExpression.Trim()));
            }

            if (aMaxRecursion > 0)
            {
                return new NumberToBool {
                           val = ParseNumber(aExpression, aMaxRecursion)
                }
            }
            ;
            throw new ParseException("Unexpected end / expression");
        } // ParseLogicResult(string, int)
Esempio n. 2
0
        } // ParseLogicResult(string, int)

        private INumberProvider ParseNumber(string aExpression, int aMaxRecursion)
        {
            --aMaxRecursion;
            m_ParsingContext.Preprocess(this, ref aExpression);
            aExpression = aExpression.Trim();
            if (aExpression.Contains(","))
            {
                string[] parts = aExpression.Split(',');

                var paramList = new ParameterList();
                paramList.inputs.Capacity = parts.Length;
                for (int i = 0; i < parts.Length; i++)
                {
                    string s = parts[i].Trim();
                    if (!string.IsNullOrEmpty(s))
                    {
                        paramList.inputs.Add(ParseNumber(s, aMaxRecursion));
                    }
                }
                if (paramList.inputs.Count == 1)
                {
                    return(paramList.inputs[0]);
                }
                return(paramList);
            }
            else if (aExpression.Contains("+"))
            {
                string[] parts             = aExpression.Split('+');
                List <INumberProvider> exp = new List <INumberProvider>(parts.Length);
                for (int i = 0; i < parts.Length; i++)
                {
                    string s = parts[i].Trim();
                    if (!string.IsNullOrEmpty(s))
                    {
                        exp.Add(ParseNumber(s, aMaxRecursion));
                    }
                }
                if (exp.Count == 1)
                {
                    return(exp[0]);
                }
                return(new OperationAdd(exp.ToArray()));
            }
            else if (aExpression.Contains("-"))
            {
                string[] parts             = aExpression.Split('-');
                List <INumberProvider> exp = new List <INumberProvider>(parts.Length);
                if (!string.IsNullOrEmpty(parts[0].Trim()))
                {
                    exp.Add(ParseNumber(parts[0], aMaxRecursion));
                }
                for (int i = 1; i < parts.Length; i++)
                {
                    string s = parts[i].Trim();
                    if (!string.IsNullOrEmpty(s))
                    {
                        exp.Add(new OperationNegate(ParseNumber(s, aMaxRecursion)));
                    }
                }
                if (exp.Count == 1)
                {
                    return(exp[0]);
                }
                return(new OperationAdd(exp.ToArray()));
            }
            else if (aExpression.Contains("*"))
            {
                string[] parts             = aExpression.Split('*');
                List <INumberProvider> exp = new List <INumberProvider>(parts.Length);
                for (int i = 0; i < parts.Length; i++)
                {
                    exp.Add(ParseNumber(parts[i], aMaxRecursion));
                }
                if (exp.Count == 1)
                {
                    return(exp[0]);
                }
                return(new OperationProduct(exp.ToArray()));
            }
            else if (aExpression.Contains("/"))
            {
                string[] parts             = aExpression.Split('/');
                List <INumberProvider> exp = new List <INumberProvider>(parts.Length);
                if (!string.IsNullOrEmpty(parts[0].Trim()))
                {
                    exp.Add(ParseNumber(parts[0], aMaxRecursion));
                }
                for (int i = 1; i < parts.Length; i++)
                {
                    string s = parts[i].Trim();
                    if (!string.IsNullOrEmpty(s))
                    {
                        exp.Add(new OperationReciprocal(ParseNumber(s, aMaxRecursion)));
                    }
                }
                return(new OperationProduct(exp.ToArray()));
            }
            else if (aExpression.Contains("^"))
            {
                int pos = aExpression.IndexOf('^');
                var val = ParseNumber(aExpression.Substring(0, pos), aMaxRecursion);
                var pow = ParseNumber(aExpression.Substring(pos + 1), aMaxRecursion);
                return(new OperationPower(val, pow));
            }

            int p = aExpression.IndexOf("$B");

            if (p > 0)
            {
                string fName = aExpression.Substring(0, p).Trim();
                Func <ParameterList, double> func = m_ParsingContext.GetFunction(fName);
                if (func != null)
                {
                    aExpression = aExpression.Substring(p);
                    string inner = m_ParsingContext.GetBracket(ref aExpression);
                    var    param = ParseNumber(inner, aMaxRecursion);
                    if (param is ParameterList)
                    {
                        return(new CustomFunction(func, (ParameterList)param));
                    }
                    return(new CustomFunction(func, new ParameterList(param)));
                }
            }
            string bracketContent = m_ParsingContext.GetBracket(ref aExpression);

            if (!string.IsNullOrEmpty(bracketContent))
            {
                return(ParseNumber(bracketContent, aMaxRecursion));
            }
            ValueProvider value = m_ParsingContext.GetCommand(ref aExpression);

            if (value != null)
            {
                return(value);
            }

            double doubleValue;

            if (double.TryParse(aExpression.Trim(), out doubleValue))
            {
                return(new ConstantNumber {
                    constantValue = doubleValue
                });
            }

            if (ValidIdentifier(aExpression))
            {
                return(context.GetVariable(aExpression.Trim()));
            }

            if (aMaxRecursion > 0)
            {
                return new BoolToNumber {
                           val = ParseLogicResult(aExpression, aMaxRecursion)
                }
            }
            ;
            throw new ParseException("Unexpected end / expression");
        } //ParseNumber(string, int)