Beispiel #1
0
        public virtual bool UserEvaluate(string name, int nameHash, object instance, List <object> indexes, out AleTermResult result, TermEvaluate userEvaluate)
        {
            result = new AleTermResult();
            AleTermEvaluateArgs e = new AleTermEvaluateArgs(name, nameHash, instance, indexes);

            if (_Parser != null)
            {
                _Parser.EvaluateStandardProperties(this, e);
                if (e.ErrorCode != AleTermResult.ERROR_OK && result.SetError(e.ErrorCode, e.ErrorPos))
                {
                    return(false);
                }
                if (e.HasBeenEvaluated)
                {
                    result.Value = e.Result;
                    return(true);
                }
            }

            if (userEvaluate == null && result.SetError(AleTermResult.ERROR_UNKNOWNELEMENT, _Token.StartInOrigin))
            {
                return(false);
            }

            userEvaluate(this, e);
            if (e.ErrorCode != AleTermResult.ERROR_OK && result.SetError(e.ErrorCode, e.ErrorPos))
            {
                return(false);
            }
            result.Value = e.Result;
            return(true);
        }
Beispiel #2
0
        public bool AssignValue(AleTerm assignTo, ref AleTermResult value, TermEvaluate userEvaluate, TermAssign userAssign)
        {
            switch (assignTo.TypeOfTerm)
            {
            case AleTermType.Variable:
                return(UserAssign(assignTo.Value.ToString(), assignTo.HashCode, null, null, ref value, userAssign));

            case AleTermType.Operation:
                if (assignTo.Operation != null)
                {
                    AleTermResult a = new AleTermResult();
                    AleTermResult b = new AleTermResult();

                    if (assignTo.Operation.IsClassOperator)
                    {
                        if (!assignTo[0].Evaluate(out a, userEvaluate, userAssign) && value.SetError(a.ErrorCode, a.ErrorPos))
                        {
                            return(false);
                        }
                        return(assignTo[1].TypeOfTerm == AleTermType.Variable && UserAssign(assignTo[1].Value.ToString(), assignTo[1].HashCode, a.Value, null, ref value, userAssign));
                    }
                    else if (assignTo.Operation.IsIndexOperator)
                    {
                        List <object> list;

                        if (!assignTo[0].Evaluate(out a, userEvaluate, userAssign) && value.SetError(a.ErrorCode, a.ErrorPos))
                        {
                            return(false);
                        }

                        int n = assignTo[1].Count;
                        list = new List <object>();

                        for (int i = 0; i < n; i++)
                        {
                            if (assignTo[1][i] != null)
                            {
                                if (!assignTo[1][i].Evaluate(out b, userEvaluate, userAssign) && value.SetError(b.ErrorCode, b.ErrorPos))
                                {
                                    return(false);
                                }
                            }
                            else
                            {
                                b.Value = null;
                            }
                            list.Add(b.Value);
                        }

                        return(UserAssign(null, 0, a.Value, list, ref value, userAssign));
                    }
                }
                break;
            }

            value.SetError(AleTermResult.ERROR_GENERAL, assignTo.Token.StartInOrigin);
            return(false);
        }
Beispiel #3
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 #4
0
        public virtual bool UserAssign(string name, int nameHash, object instance, List <object> indexes, ref AleTermResult value, TermAssign userAssign)
        {
            if (userAssign == null && value.SetError(AleTermResult.ERROR_UNKNOWNELEMENT, _Token.StartInOrigin))
            {
                return(false);
            }

            AleTermAssignArgs e = new AleTermAssignArgs(name, nameHash, instance, indexes, value.Value);

            userAssign(this, e);
            if (e.ErrorCode != AleTermResult.ERROR_OK && value.SetError(e.ErrorCode, e.ErrorPos))
            {
                return(false);
            }
            return(true);
        }
Beispiel #5
0
        public virtual bool UserEvaluate(string name, int nameHash, object instance, List<object> indexes, out AleTermResult result, TermEvaluate userEvaluate)
        {
            result = new AleTermResult();
            AleTermEvaluateArgs e = new AleTermEvaluateArgs(name, nameHash, instance, indexes);

            if (_Parser != null)
            {
                _Parser.EvaluateStandardProperties(this, e);
                if (e.ErrorCode != AleTermResult.ERROR_OK && result.SetError(e.ErrorCode, e.ErrorPos)) return false;
                if (e.HasBeenEvaluated)
                {
                    result.Value = e.Result;
                    return true;
                }
            }

            if (userEvaluate == null && result.SetError(AleTermResult.ERROR_UNKNOWNELEMENT, _Token.StartInOrigin)) return false;

            userEvaluate(this, e);
            if (e.ErrorCode != AleTermResult.ERROR_OK && result.SetError(e.ErrorCode, e.ErrorPos)) return false;
            result.Value = e.Result;
            return true;
        }
Beispiel #6
0
        public virtual bool UserAssign(string name, int nameHash, object instance, List<object> indexes, ref AleTermResult value, TermAssign userAssign)
        {
            if (userAssign == null && value.SetError(AleTermResult.ERROR_UNKNOWNELEMENT, _Token.StartInOrigin)) return false;

            AleTermAssignArgs e = new AleTermAssignArgs(name, nameHash, instance, indexes, value.Value);

            userAssign(this, e);
            if (e.ErrorCode != AleTermResult.ERROR_OK && value.SetError(e.ErrorCode, e.ErrorPos)) return false;
            return true;
        }
Beispiel #7
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 #8
0
        public bool AssignValue(AleTerm assignTo, ref AleTermResult value, TermEvaluate userEvaluate, TermAssign userAssign)
        {
            switch (assignTo.TypeOfTerm)
            {
                case AleTermType.Variable:
                    return UserAssign(assignTo.Value.ToString(), assignTo.HashCode, null, null, ref value, userAssign);

                case AleTermType.Operation:
                    if (assignTo.Operation != null)
                    {
                        AleTermResult a = new AleTermResult();
                        AleTermResult b = new AleTermResult();

                        if (assignTo.Operation.IsClassOperator)
                        {
                            if (!assignTo[0].Evaluate(out a, userEvaluate, userAssign) && value.SetError(a.ErrorCode, a.ErrorPos)) return false;
                            return assignTo[1].TypeOfTerm == AleTermType.Variable && UserAssign(assignTo[1].Value.ToString(), assignTo[1].HashCode, a.Value, null, ref value, userAssign);
                        }
                        else if (assignTo.Operation.IsIndexOperator)
                        {
                            List<object> list;

                            if (!assignTo[0].Evaluate(out a, userEvaluate, userAssign) && value.SetError(a.ErrorCode, a.ErrorPos)) return false;

                            int n = assignTo[1].Count;
                            list = new List<object>();

                            for (int i = 0; i < n; i++)
                            {
                                if (assignTo[1][i] != null)
                                {
                                    if (!assignTo[1][i].Evaluate(out b, userEvaluate, userAssign) && value.SetError(b.ErrorCode, b.ErrorPos)) return false;
                                }
                                else b.Value = null;
                                list.Add(b.Value);
                            }

                            return UserAssign(null, 0, a.Value, list, ref value, userAssign);
                        }
                    }
                    break;
            }

            value.SetError(AleTermResult.ERROR_GENERAL, assignTo.Token.StartInOrigin);
            return false;
        }