示例#1
0
        private void SetMethod(ExpressionCompiler ec, MethodInfo methodInfo)
        {
            _pos++;

            while (_pos < _input.Length && _input[_pos] == ' ')
            {
                _pos++;
            }

            if (_pos >= _input.Length || _input[_pos] != '(')
            {
                throw new ParenthesisException(_input, _pos, Resources.Opening);
            }

            _pos++;
            SetExpression(ec, Expression.Call(methodInfo, Parse(true)));
        }
示例#2
0
        private Expression Parse(bool bParentheses)
        {
            ExpressionCompiler ec = new ExpressionCompiler();

            while (_pos < _input.Length)
            {
                switch (_input[_pos])
                {
                case ' ':
                    break;

                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                case '.':
                    HandleNumber(ec);
                    break;

                case 'x':
                case 'X':
                    SetExpression(ec, _params.X);
                    break;

                case 'y':
                case 'Y':
                    SetExpression(ec, _params.Y);
                    break;

                case 'z':
                case 'Z':
                    SetExpression(ec, _params.Z);
                    break;

                case 't':
                case 'T':
                    SetExpression(ec, _params.T);
                    break;

                case '+':
                    SetOperator(ec, new OperatorAdd());
                    break;

                case '-':
                    HandleMinus(ec);
                    break;

                case '/':
                    SetOperator(ec, new OperatorDivide());
                    break;

                case '*':
                    SetOperator(ec, new OperatorMultiply());
                    break;

                case '^':
                    SetOperator(ec, new OperatorPower());
                    break;

                case '(':
                    _pos++;
                    SetExpression(ec, Parse(true));
                    break;

                case ')':
                    if (!bParentheses)
                    {
                        throw new ParenthesisException(_input, _pos, Resources.Opening);
                    }
                    return(GetResultExpression(ec));

                case 'a':
                case 'A':
                    if (ConsumeWord("abs"))
                    {
                        SetMethod(ec, Functions.Abs);
                    }
                    else
                    {
                        throw new SyntaxException(_input, _pos);
                    }
                    break;

                case 'c':
                case 'C':
                    if (ConsumeWord("cos"))
                    {
                        SetMethod(ec, Functions.Cos);
                    }
                    else
                    {
                        throw new SyntaxException(_input, _pos);
                    }
                    break;

                case 'p':
                case 'P':
                    if (ConsumeWord("pi"))
                    {
                        SetExpression(ec, Expression.Constant(Math.PI));
                    }
                    else
                    {
                        throw new SyntaxException(_input, _pos);
                    }
                    break;

                case 's':
                case 'S':
                    if (ConsumeWord("sin"))
                    {
                        SetMethod(ec, Functions.Sin);
                    }
                    else if (ConsumeWord("sqrt"))
                    {
                        SetMethod(ec, Functions.Sqrt);
                    }
                    else
                    {
                        throw new SyntaxException(_input, _pos);
                    }
                    break;

                default:
                    throw new UnknownTokenException(_input, _pos);
                }

                _pos++;
            }

            if (bParentheses)
            {
                throw new ParenthesisException(_input, _pos, Resources.Closing);
            }

            return(GetResultExpression(ec));
        }