Пример #1
0
        public RPNVariable FindVariable(string pToken)
        {
            Variable var = VariableByName(pToken);

            if (var != null)
            {
                RPNVariable result = new RPNVariable(this, RPNObjectType.Variable);
                result.Name = pToken;
                return(result);
            }
            return(null);
        }
Пример #2
0
        private void Factor()
        {
            RPNOperand  operand;
            RPNOperator aOperator;

            switch (TokenType)
            {
            case RPNTokenType.Plus:
            case RPNTokenType.Minus:
                aOperator = new RPNOperator(Owner, RPNObjectType.Operator);
                switch (TokenType)
                {
                case RPNTokenType.Plus:
                    (aOperator as RPNOperator).OperatorType = RPNTokenType.JustPlus;
                    break;

                case RPNTokenType.Minus:
                    (aOperator as RPNOperator).OperatorType = RPNTokenType.JustMinus;
                    break;
                }
                aOperator.OperatorStr = Token;
                NextToken();
                Term();
                Owner.RPNList.Add(aOperator);
                break;

            case RPNTokenType.StartParentheses:
                NextToken();
                Evaluate();

                CheckToken(")");

                NextToken();
                break;

            case RPNTokenType.SquareBracket:
                RPNDataField dataField = new RPNDataField(Owner, RPNObjectType.DataField);
                dataField.Field = Token;
                Owner.RPNList.Add(dataField);
                NextToken();
                break;

            case RPNTokenType.Ident:
                RPNFunction func;
                if (Owner.Environment != null)
                {
                    func = Owner.Environment.FindFunction(Owner, Token.ToUpper());
                }
                else
                {
                    func = null;
                }
                if (func != null)
                {
                    FunctionAttribute funcAttrib = (FunctionAttribute)Attribute.GetCustomAttribute(func.GetType(), typeof(FunctionAttribute));
                    if (funcAttrib.ParamTypes != null && funcAttrib.ParamTypes.Length > 0)
                    {
                        NextToken();
                        for (int i = 0; i < funcAttrib.ParamTypes.Length; i++)
                        {
                            if (i == 0)
                            {
                                CheckToken("(");
                            }
                            else
                            {
                                CheckToken(";");
                            }
                            NextToken();
                            Evaluate();
                        }
                        CheckToken(")");
                    }
                    Owner.RPNList.Add(func);
                }
                else
                {
                    RPNVariable var = Owner.FindVariable(Token.ToUpper());
                    if (var != null)
                    {
                        Owner.RPNList.Add(var);
                    }
                    else
                    {
                        throw new Exception("Unknown ident >" + Token + "< in expression >" + Owner.expression + "<.");
                    }
                }
                NextToken();
                break;

            case RPNTokenType.Number:
                operand = new RPNOperand(Owner, RPNObjectType.Operand);
                float number = float.Parse(Token);
                operand.Value = (object)number;
                Owner.RPNList.Add(operand);
                NextToken();
                break;

            case RPNTokenType.String:
                operand       = new RPNOperand(Owner, RPNObjectType.Operand);
                operand.Value = Token;
                Owner.RPNList.Add(operand);
                NextToken();
                break;

            case RPNTokenType.DateTime:
                operand = new RPNOperand(Owner, RPNObjectType.Operand);
                try
                {
                    operand.Value = Convert.ToDateTime(Token);
                }
                catch
                {
                    throw new Exception(Token + " is not a valid date.");
                }
                Owner.RPNList.Add(operand);
                NextToken();
                break;

            case RPNTokenType.Boolean:
                operand       = new RPNOperand(Owner, RPNObjectType.Operand);
                operand.Value = Convert.ToBoolean(Token);
                Owner.RPNList.Add(operand);
                NextToken();
                break;

            case RPNTokenType.Not:
                aOperator = new RPNOperator(Owner, RPNObjectType.Operator);
                aOperator.OperatorType = TokenType;
                aOperator.OperatorStr  = Token;
                NextToken();
                Factor();
                Owner.RPNList.Add(aOperator);
                break;

            default:
                UnexpectedToken();
                break;
            }
        }