Beispiel #1
0
        public bool Evaluate(out AleTermResult result, TermEvaluate userEvaluate = null, TermAssign userAssign = null)
        {
            result = new AleTermResult();

            if (TypeOfTerm == AleTermType.Atom || TypeOfTerm == AleTermType.Unknown)
            {
                result.Value = _Value;
                return(true);
            }

            AleTermResult res = new AleTermResult();

            try
            {
                if (TypeOfTerm == AleTermType.Variable)
                {
                    return(UserEvaluate(Value.ToString(), HashCode, null, null, out result, userEvaluate));
                }

                if ((_Operation == null || _Operation.IsClassMethod) && result.SetError(AleTermResult.ERROR_UNKNOWNFUNCTION, Token.StartInOrigin))
                {
                    return(false);
                }

                OperationEvalParameters evalParams = new OperationEvalParameters();
                evalParams.userEvaluate = userEvaluate;
                evalParams.userAssign   = userAssign;

                if (_Operation.IsOperator)
                {
                    return(_Operation.Evaluator(this, ref evalParams, ref result));
                }

                int m = _Operation.ParametersCount;
                int n = _Elements == null ? 0 : _Elements.Count;
                evalParams.ActualParamsCount = n;

                if (m > 0 && m < 4)
                {
                    if (n > 0 && _Elements[0] != null)
                    {
                        if (!_Elements[0].Evaluate(out res, userEvaluate, userAssign) && result.SetError(res.ErrorCode, res.ErrorPos))
                        {
                            return(false);
                        }
                        if (!ValidForOperationType(res.Value, _Operation.Parameters[0].Item1) &&
                            result.SetError(AleTermResult.ERROR_INCOMPATIBLETYPES, _Elements[0].Token.StartInOrigin))
                        {
                            return(false);
                        }
                        evalParams.FirstParam = res.Value;
                    }
                    else
                    {
                        evalParams.FirstParam = _Operation.Parameters[0].Item2;
                    }

                    if (n > 1 && _Elements[1] != null)
                    {
                        if (!_Elements[1].Evaluate(out res, userEvaluate, userAssign) && result.SetError(res.ErrorCode, res.ErrorPos))
                        {
                            return(false);
                        }
                        if (!ValidForOperationType(res.Value, _Operation.Parameters[1].Item1) &&
                            result.SetError(AleTermResult.ERROR_INCOMPATIBLETYPES, _Elements[1].Token.StartInOrigin))
                        {
                            return(false);
                        }
                        evalParams.SecondParam = res.Value;
                    }
                    else if (m > 1)
                    {
                        evalParams.SecondParam = _Operation.Parameters[1].Item2;
                    }

                    if (n > 2 && _Elements[2] != null)
                    {
                        if (!_Elements[2].Evaluate(out res, userEvaluate, userAssign) && result.SetError(res.ErrorCode, res.ErrorPos))
                        {
                            return(false);
                        }
                        if (!ValidForOperationType(res.Value, _Operation.Parameters[2].Item1) &&
                            result.SetError(AleTermResult.ERROR_INCOMPATIBLETYPES, _Elements[2].Token.StartInOrigin))
                        {
                            return(false);
                        }
                        evalParams.ThirdParam = res.Value;
                    }
                    else if (m > 2)
                    {
                        evalParams.ThirdParam = _Operation.Parameters[2].Item2;
                    }
                }
                else if (m > 3)
                {
                    object obj;

                    evalParams.Parameters = new List <object>(m);
                    for (int i = 0; i < m; i++)
                    {
                        if (i < n && _Elements[i] != null)
                        {
                            if (!_Elements[i].Evaluate(out res, userEvaluate, userAssign) && result.SetError(res.ErrorCode, res.ErrorPos))
                            {
                                return(false);
                            }
                            obj = res.Value;
                            if (!ValidForOperationType(obj, _Operation.Parameters[i].Item1) &&
                                result.SetError(AleTermResult.ERROR_INCOMPATIBLETYPES, _Elements[i].Token.StartInOrigin))
                            {
                                return(false);
                            }
                            evalParams.Parameters.Add(obj);
                        }
                        else
                        {
                            evalParams.Parameters.Add(_Operation.Parameters[i].Item2);
                        }
                    }
                }

                return(_Operation.Evaluator(this, ref evalParams, ref result));
            }
            catch
            {
                result.SetError(AleTermResult.ERROR_EVALUATION, Token.StartInOrigin);
                return(false);
            }
        }
Beispiel #2
0
        public bool Evaluate(out AleTermResult result, TermEvaluate userEvaluate = null, TermAssign userAssign = null)
        {
            result = new AleTermResult();

            if (TypeOfTerm == AleTermType.Atom || TypeOfTerm == AleTermType.Unknown)
            {
                result.Value = _Value;
                return true;
            }

            AleTermResult res = new AleTermResult();

            try
            {
                if (TypeOfTerm == AleTermType.Variable) return UserEvaluate(Value.ToString(), HashCode, null, null, out result, userEvaluate);

                if ((_Operation == null || _Operation.IsClassMethod) && result.SetError(AleTermResult.ERROR_UNKNOWNFUNCTION, Token.StartInOrigin)) return false;

                OperationEvalParameters evalParams = new OperationEvalParameters();
                evalParams.userEvaluate = userEvaluate;
                evalParams.userAssign = userAssign;

                if (_Operation.IsOperator) return _Operation.Evaluator(this, ref evalParams, ref result);

                int m = _Operation.ParametersCount;
                int n = _Elements == null ? 0 : _Elements.Count;
                evalParams.ActualParamsCount = n;

                if (m > 0 && m < 4)
                {
                    if (n > 0 && _Elements[0] != null)
                    {
                        if (!_Elements[0].Evaluate(out res, userEvaluate, userAssign) && result.SetError(res.ErrorCode, res.ErrorPos)) return false;
                        if (!ValidForOperationType(res.Value, _Operation.Parameters[0].Item1) &&
                            result.SetError(AleTermResult.ERROR_INCOMPATIBLETYPES, _Elements[0].Token.StartInOrigin)) return false;
                        evalParams.FirstParam = res.Value;
                    }
                    else evalParams.FirstParam = _Operation.Parameters[0].Item2;

                    if (n > 1 && _Elements[1] != null)
                    {
                        if (!_Elements[1].Evaluate(out res, userEvaluate, userAssign) && result.SetError(res.ErrorCode, res.ErrorPos)) return false;
                        if (!ValidForOperationType(res.Value, _Operation.Parameters[1].Item1) &&
                            result.SetError(AleTermResult.ERROR_INCOMPATIBLETYPES, _Elements[1].Token.StartInOrigin)) return false;
                        evalParams.SecondParam = res.Value;
                    }
                    else if (m > 1) evalParams.SecondParam = _Operation.Parameters[1].Item2;

                    if (n > 2 && _Elements[2] != null)
                    {
                        if (!_Elements[2].Evaluate(out res, userEvaluate, userAssign) && result.SetError(res.ErrorCode, res.ErrorPos)) return false;
                        if (!ValidForOperationType(res.Value, _Operation.Parameters[2].Item1) &&
                            result.SetError(AleTermResult.ERROR_INCOMPATIBLETYPES, _Elements[2].Token.StartInOrigin)) return false;
                        evalParams.ThirdParam = res.Value;
                    }
                    else if (m > 2) evalParams.ThirdParam = _Operation.Parameters[2].Item2;
                }
                else if (m > 3)
                {
                    object obj;

                    evalParams.Parameters = new List<object>(m);
                    for (int i = 0; i < m; i++)
                        if (i < n && _Elements[i] != null)
                        {
                            if (!_Elements[i].Evaluate(out res, userEvaluate, userAssign) && result.SetError(res.ErrorCode, res.ErrorPos)) return false;
                            obj = res.Value;
                            if (!ValidForOperationType(obj, _Operation.Parameters[i].Item1) &&
                                result.SetError(AleTermResult.ERROR_INCOMPATIBLETYPES, _Elements[i].Token.StartInOrigin)) return false;
                            evalParams.Parameters.Add(obj);
                        }
                        else evalParams.Parameters.Add(_Operation.Parameters[i].Item2);
                }

                return _Operation.Evaluator(this, ref evalParams, ref result);

            }
            catch
            {
                result.SetError(AleTermResult.ERROR_EVALUATION, Token.StartInOrigin);
                return false;
            }
        }