Beispiel #1
0
        public Int32 InitExpression(string strContent)
        {
            string strValue = string.Empty;

            for (Int32 i = 0; i < strContent.Length; i++)
            {
                if (strContent[i] == '(')
                {
                    EquationExpression equation = new EquationExpression();
                    i += equation.InitExpression(strContent.Substring(i + 1));
                    _stackValues.Push(equation);
                }
                else if (_dicExpressions.ContainsKey(strContent[i]))
                {
                    if (!string.IsNullOrEmpty(strValue))
                    {
                        _stackValues.Push(new ValueExpression(strValue));
                        strValue = string.Empty;
                    }
                    Type type = _dicExpressions[strContent[i]];
                    INonTerminalExpression next = (INonTerminalExpression)Activator.CreateInstance(type);

                    if (_stackExpressions.Count > 0)
                    {
                        INonTerminalExpression top = _stackExpressions.Peek();
                        if (top.Level > next.Level)
                        {
                            ITerminalExpression value1 = _stackValues.Pop();
                            ITerminalExpression value2 = _stackValues.Pop();
                            _stackValues.Push(next.Interpret(value1, value2));
                            _stackExpressions.Pop();
                        }
                    }
                    _stackExpressions.Push(next);
                }
                else if (strContent[i] == ')')
                {
                    _stackValues.Push(new ValueExpression(strValue));
                    strValue = string.Empty;
                    return(i + 1);
                }
                else
                {
                    strValue += strContent[i];
                }
            }

            if (!string.IsNullOrEmpty(strValue))
            {
                _stackValues.Push(new ValueExpression(strValue));
                strValue = string.Empty;
            }

            return(-1);
        }
 public ITerminalExpression Interpret(ITerminalExpression db1, ITerminalExpression db2)
 {
     return(new ValueExpression(db1.Value / db2.Value));
 }