Пример #1
0
        public override object GetValue()
        {
            Owner.Position--;
            FunctionAttribute funcAttrib = (FunctionAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(FunctionAttribute));

            if (funcAttrib.ParamTypes != null)
            {
                for (int i = 0; i < funcAttrib.ParamTypes.Length; i++)
                {
                    Params.Insert(0, Owner.RPNList[Owner.Position].GetValue());
                }
            }
            CheckParams();

            return(Calc());
        }
Пример #2
0
        public virtual void CheckParams()
        {
            FunctionAttribute funcAttrib = (FunctionAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(FunctionAttribute));

            if (funcAttrib.ParamTypes != null)
            {
                for (int i = 0; i < funcAttrib.ParamTypes.Length - 1; i++)
                {
                    switch (funcAttrib.ParamTypes[i])
                    {
                    case 'B':
                        if (!(Params[i] is bool))
                        {
                            DoRaise("Boolean", Convert.ToString(Params[i]), i);
                        }
                        break;

                    case 'D':
                        if (!(Params[i] is DateTime))
                        {
                            DoRaise("DateTime", Convert.ToString(Params[i]), i);
                        }
                        break;

                    case 'N':
                        if (!(IsNumeric(Params[i])))
                        {
                            DoRaise("Number", Convert.ToString(Params[i]), i);
                        }
                        break;

                    case 'S':
                        if (!(Params[i] is string))
                        {
                            DoRaise("String", Convert.ToString(Params[i]), i);
                        }
                        break;
                    }
                }
            }
        }
Пример #3
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;
            }
        }
Пример #4
0
        public void RegisterFunction(Type funcType)
        {
            FunctionAttribute funcAttrib = (FunctionAttribute)Attribute.GetCustomAttribute(funcType, typeof(FunctionAttribute));

            functionList.Add(funcAttrib.FunctionName.ToUpper(), funcType);
        }
Пример #5
0
        public override string ToString()
        {
            FunctionAttribute funcAttrib = (FunctionAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(FunctionAttribute));

            return(funcAttrib.FunctionName);
        }