示例#1
0
        /// <inheritdoc/>
        internal override void Run()
        {
            var methodInterpreter = ParentInterpreter.GetParentMethodInterpreter();

            if (ParentInterpreter.IsAborted)
            {
                return;
            }

            var returnValue = ParentInterpreter.RunExpression(Statement.Expression);

            if (ParentInterpreter.IsAborted)
            {
                return;
            }

            methodInterpreter.ReturnedValue    = returnValue;
            ParentInterpreter.State.ExitMethod = true;

            if (BaZicInterpreter.Verbose && !ParentInterpreter.IsAborted)
            {
                var valueString = methodInterpreter.ReturnedValue == null ? L.BaZic.Runtime.Debugger.ValueInfo.Null : $"{returnValue} ({ValueInfo.GetValueInfo(returnValue)})";
                ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Statements.ReturnInterpreter.FormattedReturn(valueString));
            }
        }
示例#2
0
        /// <summary>
        /// Assigns the specified value to a property.
        /// </summary>
        /// <param name="propertyReference">The reference to the property to set.</param>
        /// <param name="value">The value to assign.</param>
        private void AssignProperty(PropertyReferenceExpression propertyReference, object value)
        {
            if (string.IsNullOrWhiteSpace(propertyReference.PropertyName?.Identifier))
            {
                BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.PropertyReferenceInterpreter.UndefinedName), propertyReference);
            }
            var targetObjectValue = ParentInterpreter.RunExpression(propertyReference.TargetObject);

            if (ParentInterpreter.IsAborted)
            {
                return;
            }

            if (targetObjectValue == null)
            {
                BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.PropertyReferenceInterpreter.NullValue), propertyReference);
                return;
            }

            if (propertyReference.TargetObject is ClassReferenceExpression && targetObjectValue is Type)
            {
                BaZicInterpreter.Reflection.SetStaticProperty((Type)targetObjectValue, propertyReference.PropertyName.Identifier, value);
            }
            else if (targetObjectValue is FrameworkElement)
            {
                BaZicInterpreter.ProgramInterpreter.UIDispatcher.Invoke(() =>
                {
                    BaZicInterpreter.Reflection.SetProperty(targetObjectValue, propertyReference.PropertyName.Identifier, value);
                }, System.Windows.Threading.DispatcherPriority.Background);
            }
            else
            {
                BaZicInterpreter.Reflection.SetProperty(targetObjectValue, propertyReference.PropertyName.Identifier, value);
            }
        }
        /// <summary>
        /// Run the interpretation
        /// </summary>
        /// <returns>Returns the result of the interpretation</returns>
        internal override object Execute()
        {
            object     left;
            object     right;
            MethodInfo operatorMethod;

            left = ParentInterpreter.RunExpression(Expression._leftExpression);

            if (ParentInterpreter.FailedOrStop)
            {
                return(null);
            }

            right = ParentInterpreter.RunExpression(Expression._rightExpression);

            if (ParentInterpreter.FailedOrStop)
            {
                return(null);
            }

            if (DebugMode)
            {
                ParentInterpreter.Log(this, $"Doing an operation '{Expression._operator}'");
            }

            if (Expression._operator == AlgorithmBinaryOperatorType.Equals)
            {
                if (left == null)
                {
                    if (right == null)
                    {
                        return(true); // null == null
                    }
                    return(right.Equals(null));
                }
                return(left.Equals(right));
            }

            operatorMethod = OperatorHelperCache.GetOperator(Expression._operator, left.GetType(), right.GetType());
            if (operatorMethod == null)
            {
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new OperatorNotFoundException(Expression._operator.ToString(), $"Operator '{Expression._operator}' cannot be applied to operands of type '{left.GetType().FullName}' and '{right.GetType().FullName}'"), Expression), ParentInterpreter.GetDebugInfo()));
                return(null);
            }

            if (Expression._operator == AlgorithmBinaryOperatorType.Division)
            {
                long num;
                var  convertedToLong = long.TryParse(right.ToString(), out num);
                if (convertedToLong && num == 0)
                {
                    ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new DivideByZeroException("Attempted to divide by zero."), Expression), ParentInterpreter.GetDebugInfo()));
                    return(null);
                }
            }

            return(operatorMethod.Invoke(null, new[] { left, right }));
        }
示例#4
0
        /// <summary>
        /// Run the interpretation
        /// </summary>
        /// <returns>Returns the result of the interpretation</returns>
        internal override object Execute()
        {
            if (Expression._targetObject == null)
            {
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NullReferenceException("Unable to invoke a method when the TargetObject of an AlgorithmInvokeMethodExpression is null."), Expression), ParentInterpreter.GetDebugInfo()));
                return(null);
            }

            if (DebugMode)
            {
                ParentInterpreter.Log(this, $"Calling method '{Expression._targetObject}.{Expression._methodName}'");
            }

            var referenceClass = ParentInterpreter.RunExpression(Expression._targetObject) as ClassInterpreter;
            var callerMethod   = ParentInterpreter.ParentMethodInterpreter;

            if (ParentInterpreter.FailedOrStop)
            {
                return(null);
            }

            if (referenceClass == null)
            {
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new ClassNotFoundException("{Unknow}", "It looks like the reference class does not exists."), Expression), ParentInterpreter.GetDebugInfo()));
                return(null);
            }

            if (!referenceClass.IsInstance)
            {
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NoInstanceReferenceException("Unable to invoke a method of a not instancied class."), Expression), ParentInterpreter.GetDebugInfo()));
                return(null);
            }

            var argumentValues = GetArgumentValues(Expression, ParentInterpreter);

            if (!ParentInterpreter.FailedOrStop)
            {
                var callStackService = ParentInterpreter.ParentProgramInterpreter.DebugInfo.CallStackService;
                _result = null;

                if (callStackService.CallCount > Consts.InvokeMethodCountBeforeNewThread)
                {
                    // Make a new thread avoid the stack overflow.
                    callStackService.CallCount = 0;
                    CallMethodNewThread(referenceClass, argumentValues, callerMethod, callStackService).Wait();
                }
                else
                {
                    callStackService.CallCount++;
                    _result = referenceClass.InvokeMethod(ParentInterpreter, Expression, argumentValues, callerMethod, callStackService);
                }

                return(_result);
            }
            return(null);
        }
示例#5
0
        /// <summary>
        /// Returns the corresponding assignable object
        /// </summary>
        /// <returns>the assignable object</returns>
        public object GetAssignableObject()
        {
            if (Expression._targetObject == null)
            {
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NullReferenceException("Unable to access to a property or variable when the TargetObject of an AlgorithmArrayIndexerExpression is null."), Expression), ParentInterpreter.GetDebugInfo()));
                return(null);
            }

            if (DebugMode)
            {
                ParentInterpreter.Log(this, $"Getting the value '{Expression}'");
            }

            var indexableValue = ParentInterpreter.RunExpression(Expression._targetObject);

            if (ParentInterpreter.FailedOrStop)
            {
                return(null);
            }

            if (indexableValue == null)
            {
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NoInstanceReferenceException("Unable to get a value because the array is null."), Expression), ParentInterpreter.GetDebugInfo()));
                return(null);
            }
            // ReSharper disable once UseIsOperator.1
            // ReSharper disable once UseMethodIsInstanceOfType
            if (!typeof(IList).IsAssignableFrom(indexableValue.GetType()))
            {
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new IndexerException(Expression._propertyName.ToString()), Expression), ParentInterpreter.GetDebugInfo()));
                return(null);
            }

            if (ParentInterpreter.FailedOrStop)
            {
                return(null);
            }

            var indiceValue = ParentInterpreter.RunExpression(Expression._indice);

            if (ParentInterpreter.FailedOrStop)
            {
                return(null);
            }

            var indexValue = indiceValue as int?;

            if (indexValue != null)
            {
                IndexValue = indexValue.Value;
                return(indexableValue);
            }

            ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new InvalidCastException("Unable to cast this value to a number."), Expression), ParentInterpreter.GetDebugInfo()));
            return(null);
        }
示例#6
0
        /// <inheritdoc/>
        internal override object Run()
        {
            var array = new ObservableDictionary();

            foreach (var value in Expression.Values)
            {
                array.Add(ParentInterpreter.RunExpression(value));
            }

            return(array);
        }
示例#7
0
        /// <inheritdoc/>
        internal override void Run()
        {
            var expression = ParentInterpreter.RunExpression(Statement.Expression);

            var exception = expression as Exception;

            if (exception == null)
            {
                BaZicInterpreter.ChangeState(this, new BadTypeException(L.BaZic.Runtime.Interpreters.Statements.ThrowInterpreter.FormattedExceptionExpected(typeof(Exception).FullName)));
                return;
            }

            throw exception;
        }
示例#8
0
        /// <summary>
        /// Run the interpretation
        /// </summary>
        internal override void Execute()
        {
            object defaultValue = null;

            if (Statement._defaultValue != null)
            {
                defaultValue = ParentInterpreter.RunExpression(Statement._defaultValue);
            }

            if (ParentInterpreter.FailedOrStop)
            {
                return;
            }

            ParentInterpreter.AddVariable((IAlgorithmVariable)Statement, defaultValue);
        }
        /// <inheritdoc/>
        internal override object Run()
        {
            if (BaZicInterpreter.Verbose)
            {
                ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Expressions.PropertyReferenceInterpreter.FormattedGettingProperty(Expression));
            }

            if (Expression.TargetObject == null)
            {
                BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.PropertyReferenceInterpreter.NullValue), Expression);
                return(null);
            }
            else if (string.IsNullOrWhiteSpace(Expression.PropertyName?.Identifier))
            {
                BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.PropertyReferenceInterpreter.UndefinedName), Expression);
                return(null);
            }

            var targetObjectValue = ParentInterpreter.RunExpression(Expression.TargetObject);

            if (ParentInterpreter.IsAborted)
            {
                return(null);
            }

            if (targetObjectValue == null)
            {
                BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.PropertyReferenceInterpreter.NullValue), Expression);
                return(null);
            }

            if (Expression.TargetObject is ClassReferenceExpression && targetObjectValue is Type)
            {
                return(BaZicInterpreter.Reflection.GetStaticPropertyOrEnum((Type)targetObjectValue, Expression.PropertyName.Identifier));
            }

            if (targetObjectValue is FrameworkElement)
            {
                return(BaZicInterpreter.ProgramInterpreter.UIDispatcher.Invoke(() =>
                {
                    return BaZicInterpreter.Reflection.GetProperty(targetObjectValue, Expression.PropertyName.Identifier);
                }, System.Windows.Threading.DispatcherPriority.Background));
            }

            return(BaZicInterpreter.Reflection.GetProperty(targetObjectValue, Expression.PropertyName.Identifier));
        }
示例#10
0
        /// <summary>
        /// Run the interpretation
        /// </summary>
        internal override void Execute()
        {
            var methodInterpreter = ParentInterpreter.ParentMethodInterpreter;

            if (methodInterpreter == null)
            {
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new MethodNotFoundException("{Unknow}", "It looks like the caller/parent's method does not exists."), Statement), ParentInterpreter.GetDebugInfo()));
                return;
            }

            methodInterpreter.ReturnedValue = ParentInterpreter.RunExpression(Statement._expression);

            if (DebugMode && !ParentInterpreter.FailedOrStop)
            {
                ParentInterpreter.Log(this, "({0}) Return : {1}", methodInterpreter.MethodDeclaration._name, methodInterpreter.ReturnedValue == null ? "{null}" : $"'{methodInterpreter.ReturnedValue}' (type:{methodInterpreter.ReturnedValue.GetType().FullName})");
            }
        }
示例#11
0
        /// <inheritdoc/>
        internal override void Run()
        {
            if (BaZicInterpreter.Verbose)
            {
                ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Statements.AssignInterpreter.FormattedAssign(Statement.LeftExpression, Statement.RightExpression));
            }

            if (!typeof(IAssignable).IsAssignableFrom(Statement.LeftExpression.GetType()))
            {
                BaZicInterpreter.ChangeState(this, new NotAssignableException(L.BaZic.Runtime.Interpreters.Statements.AssignInterpreter.NotAssignable), Statement);
                return;
            }

            var rightValue = ParentInterpreter.RunExpression(Statement.RightExpression);

            if (ParentInterpreter.IsAborted)
            {
                return;
            }

            switch (Statement.LeftExpression)
            {
            case ArrayIndexerExpression arrayIndexer:
                AssignArrayValue(arrayIndexer, rightValue);
                break;

            case PropertyReferenceExpression propertyReference:
                AssignProperty(propertyReference, rightValue);
                break;

            case VariableReferenceExpression variableReference:
                ParentInterpreter.SetVariable(variableReference, rightValue);
                break;

            default:
                throw new InternalException(L.BaZic.Runtime.Interpreters.Statements.AssignInterpreter.FormattedNoInterpreter(Statement.LeftExpression.GetType().FullName));
            }

            if (BaZicInterpreter.Verbose && !ParentInterpreter.IsAborted)
            {
                var rightValueString = rightValue == null ? L.BaZic.Runtime.Debugger.ValueInfo.Null : $"'{rightValue}'(type:{ rightValue.GetType().FullName})";
                ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Statements.AssignInterpreter.FormattedNowEqualsTo(Statement.LeftExpression, rightValueString));
            }
        }
示例#12
0
        /// <inheritdoc/>
        internal override object Run()
        {
            if (Expression.CreateType == null)
            {
                BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.InstantiateInterpreter.FormattedCreateTypeNull(nameof(InstantiateExpression.CreateType), nameof(InstantiateExpression))), Expression);
                return(null);
            }

            var createType = ParentInterpreter.RunExpression(Expression.CreateType) as Type;

            if (ParentInterpreter.IsAborted)
            {
                return(null);
            }

            if (BaZicInterpreter.Verbose)
            {
                ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Expressions.InstantiateInterpreter.FormattedCreateInstance(Expression.CreateType));
            }

            // Execute argument's values.
            if (BaZicInterpreter.Verbose)
            {
                ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.MethodInterpreter.ExecutingArguments);
            }
            var argumentValues = new List <object>();

            for (var i = 0; i < Expression.Arguments.Count; i++)
            {
                var argumentValue = ParentInterpreter.RunExpression(Expression.Arguments[i]);
                argumentValues.Add(argumentValue);
            }

            if (ParentInterpreter.IsAborted)
            {
                return(null);
            }

            return(BaZicInterpreter.Reflection.Instantiate(createType, argumentValues.ToArray()));
        }
示例#13
0
        /// <inheritdoc/>
        internal override object Run()
        {
            if (Expression.Expression == null)
            {
                BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.NotOperatorInterpreter.FormattedExpressionNull(nameof(NotOperatorExpression))), Expression);
                return(null);
            }

            var expressionValue = ParentInterpreter.RunExpression(Expression.Expression);

            if (expressionValue == null)
            {
                BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.NotOperatorInterpreter.ValueNull), Expression);
                return(null);
            }
            else if (expressionValue.GetType() != typeof(bool))
            {
                BaZicInterpreter.ChangeState(this, new BadTypeException(L.BaZic.Runtime.Interpreters.Expressions.NotOperatorInterpreter.FormattedBooleanExpected(expressionValue.GetType().Name)), Expression);
                return(null);
            }

            return(!(bool)expressionValue);
        }
示例#14
0
        /// <summary>
        /// Run the interpretation
        /// </summary>
        internal override void Execute()
        {
            int          indexValue   = -1;
            object       targetObject = null;
            object       leftValue    = null;
            object       rightValue;
            PropertyInfo propertyInfo;
            Variable     propertyVariable;
            IList        propertyVariableList;
            var          leftExpression  = Statement._leftExpression;
            var          rightExpression = Statement._rightExpression;

            if (DebugMode)
            {
                ParentInterpreter.Log(this, $"Assign '{leftExpression}' to '{rightExpression}'");

                if (!typeof(IAlgorithmAssignable).IsAssignableFrom(leftExpression.GetType()))
                {
                    ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NotAssignableException($"The left expression is not assignable."), Statement), ParentInterpreter.GetDebugInfo()));
                    return;
                }
            }

            switch (leftExpression.DomType)
            {
            case AlgorithmDomType.PropertyReferenceExpression:
                var propertyReferenceInterpreter = new PropertyReference(DebugMode, ParentInterpreter, leftExpression);
                leftValue    = propertyReferenceInterpreter.GetAssignableObject();
                targetObject = propertyReferenceInterpreter.TargetObject;
                break;

            case AlgorithmDomType.VariableReferenceExpression:
                leftValue = new VariableReference(DebugMode, ParentInterpreter, leftExpression).GetAssignableObject();
                break;

            case AlgorithmDomType.ArrayIndexerExpression:
                var arrayIndexerInterpreter = new ArrayIndexerExpression(DebugMode, ParentInterpreter, leftExpression);
                leftValue  = arrayIndexerInterpreter.GetAssignableObject();
                indexValue = arrayIndexerInterpreter.IndexValue;
                break;

            default:
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new InvalidCastException($"Unable to find an interpreter for this expression : '{leftExpression.GetType().FullName}'"), Statement), ParentInterpreter.GetDebugInfo()));
                break;
            }

            if (ParentInterpreter.FailedOrStop)
            {
                return;
            }

            rightValue = ParentInterpreter.RunExpression(rightExpression);

            if (ParentInterpreter.FailedOrStop)
            {
                return;
            }

            propertyInfo = leftValue as PropertyInfo;
            if (propertyInfo != null)
            {
                if (!propertyInfo.CanWrite)
                {
                    ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NotAssignableException($"This core property is not assignable."), Statement), ParentInterpreter.GetDebugInfo()));
                    return;
                }
                propertyInfo.SetValue(targetObject, rightValue);
            }

            propertyVariable = leftValue as Variable;
            if (propertyVariable != null)
            {
                if (propertyVariable.IsArray && !(typeof(Array).IsAssignableFrom(rightValue.GetType()) || typeof(IList).IsAssignableFrom(rightValue.GetType())))
                {
                    ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NotAssignableException($"The left expression wait for an array, but the right value is not an array."), Statement), ParentInterpreter.GetDebugInfo()));
                    return;
                }
                if (!propertyVariable.IsArray && (typeof(Array).IsAssignableFrom(rightValue.GetType()) || typeof(IList).IsAssignableFrom(rightValue.GetType())))
                {
                    ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NotAssignableException($"The left expression does not support array value, but the right value is  an array."), Statement), ParentInterpreter.GetDebugInfo()));
                    return;
                }
                propertyVariable.Value = rightValue;
            }

            propertyVariableList = leftValue as IList;
            if (propertyVariableList != null)
            {
                if (indexValue < 0 || indexValue >= propertyVariableList.Count)
                {
                    ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new IndexOutOfRangeException($"Unable to get the item number '{indexValue}' because the limit of the array is '{propertyVariableList.Count - 1}'."), Statement), ParentInterpreter.GetDebugInfo()));
                    return;
                }
                propertyVariableList[indexValue] = rightValue;
            }

            if (DebugMode)
            {
                ParentInterpreter.Log(this, "'{0}' is now equal to {1}", leftExpression.ToString(), rightValue == null ? "{null}" : $"'{rightValue}' (type:{rightValue.GetType().FullName})");
            }
        }
示例#15
0
        /// <summary>
        /// Returns the corresponding assignable object
        /// </summary>
        /// <returns>the assignable object</returns>
        public object GetAssignableObject()
        {
            if (Expression._targetObject == null)
            {
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NullReferenceException("Unable to access to a property when the TargetObject of an AlgorithmPropertyReferenceExpression is null."), Expression), ParentInterpreter.GetDebugInfo()));
                return(null);
            }

            object           property;
            object           value = null;
            PropertyInfo     propertyInfo;
            ClassInterpreter classTargetObject;
            Variable         propertyVariable;

            if (DebugMode)
            {
                ParentInterpreter.Log(this, $"Getting the property '{Expression}'");
            }

            TargetObject = ParentInterpreter.RunExpression(Expression._targetObject);

            if (ParentInterpreter.FailedOrStop)
            {
                return(null);
            }

            if (TargetObject == null)
            {
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new ClassNotFoundException("{Unknow}", "It looks like the reference object does not exists."), Expression), ParentInterpreter.GetDebugInfo()));
                return(null);
            }

            classTargetObject = TargetObject as ClassInterpreter;
            if (classTargetObject != null)
            {
                propertyVariable = classTargetObject.FindVariableInTheCurrentInterpreter(Expression._propertyName.ToString());

                if (propertyVariable == null)
                {
                    ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new PropertyNotFoundException(Expression._propertyName.ToString()), Expression), ParentInterpreter.GetDebugInfo()));
                    return(null);
                }

                property = propertyVariable;
                if (DebugMode)
                {
                    value = propertyVariable.Value;
                }
            }
            else
            {
                propertyInfo = TargetObject.GetType().GetProperty(Expression._propertyName.ToString());

                if (propertyInfo == null)
                {
                    ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new PropertyNotFoundException(Expression._propertyName.ToString()), Expression), ParentInterpreter.GetDebugInfo()));
                    return(null);
                }

                property = propertyInfo;
                if (DebugMode)
                {
                    value = propertyInfo.GetValue(TargetObject);
                }
            }

            if (DebugMode)
            {
                ParentInterpreter.Log(this, "Value of the property '{0}' is {1}", Expression._propertyName.ToString(), value == null ? "{null}" : $"'{value}' (type:{value.GetType().FullName})");
            }
            return(property);
        }
示例#16
0
        /// <inheritdoc/>
        internal override object Run()
        {
            var leftValue = ParentInterpreter.RunExpression(Expression.LeftExpression);

            if (ParentInterpreter.IsAborted)
            {
                return(null);
            }

            var rightValue = ParentInterpreter.RunExpression(Expression.RightExpression);

            if (ParentInterpreter.IsAborted)
            {
                return(null);
            }

            if (BaZicInterpreter.Verbose)
            {
                ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Expressions.BinaryOperatorInterpreter.FormattedPerformOperation(Expression.Operator));
            }

            dynamic dynamicLeftValue  = leftValue;
            dynamic dynamicRightValue = rightValue;

            switch (Expression.Operator)
            {
            case BinaryOperatorType.Equality:
                return(dynamicLeftValue == dynamicRightValue);

            case BinaryOperatorType.BitwiseOr:
                return(dynamicLeftValue | dynamicRightValue);

            case BinaryOperatorType.BitwiseAnd:
                return(dynamicLeftValue & dynamicRightValue);

            case BinaryOperatorType.LogicalOr:
                return(dynamicLeftValue || dynamicRightValue);

            case BinaryOperatorType.LogicalAnd:
                return(dynamicLeftValue && dynamicRightValue);

            case BinaryOperatorType.LessThan:
                return(dynamicLeftValue < dynamicRightValue);

            case BinaryOperatorType.LessThanOrEqual:
                return(dynamicLeftValue <= dynamicRightValue);

            case BinaryOperatorType.GreaterThan:
                return(dynamicLeftValue > dynamicRightValue);

            case BinaryOperatorType.GreaterThanOrEqual:
                return(dynamicLeftValue >= dynamicRightValue);

            case BinaryOperatorType.Addition:
                return(dynamicLeftValue + dynamicRightValue);

            case BinaryOperatorType.Subtraction:
                return(dynamicLeftValue - dynamicRightValue);

            case BinaryOperatorType.Multiply:
                return(dynamicLeftValue * dynamicRightValue);

            case BinaryOperatorType.Division:
                var convertedToLong = long.TryParse(rightValue.ToString(), out long num);
                if (convertedToLong && num == 0)
                {
                    BaZicInterpreter.ChangeState(this, new DivideByZeroException(L.BaZic.Runtime.Interpreters.Expressions.BinaryOperatorInterpreter.DivideByZero), Expression.RightExpression);
                    return(null);
                }

                return(dynamicLeftValue / dynamicRightValue);

            case BinaryOperatorType.Modulus:
                return(dynamicLeftValue % dynamicRightValue);

            default:
                throw new InternalException(L.BaZic.Runtime.Interpreters.Expressions.BinaryOperatorInterpreter.FormattedOperatorNotImplemented(Expression.Operator, nameof(BinaryOperatorInterpreter)));
            }
        }
示例#17
0
 /// <inheritdoc/>
 internal override void Run()
 {
     ParentInterpreter.RunExpression(Statement.Expression);
 }
示例#18
0
        /// <summary>
        /// Run the interpretation
        /// </summary>
        /// <returns>Returns the result of the interpretation</returns>
        internal override object Execute()
        {
            Collection <object> argumentValues;
            var createType = Expression._createType;
            var reference  = ParentInterpreter.RunExpression(createType);

            if (ParentInterpreter.FailedOrStop)
            {
                return(null);
            }

            if (DebugMode)
            {
                ParentInterpreter.Log(this, $"Creating a new instance of '{createType}'");
            }

            var classInterpreter = reference as ClassInterpreter;

            if (classInterpreter != null)
            {
                var program       = ParentInterpreter.ParentProgramInterpreter;
                var classInstance = classInterpreter.CreateNewInstance();

                classInstance.StateChanged           += ParentInterpreter.ChangeState;
                classInstance.OnGetParentInterpreter += new Func <ProgramInterpreter>(() => program);
                classInstance.OnDone += new Action <ClassInterpreter>((cl) =>
                {
                    cl.StateChanged -= ParentInterpreter.ChangeState;
                });
                classInstance.Initialize();

                argumentValues = InvokeMethod.GetArgumentValues(Expression, ParentInterpreter);

                if (ParentInterpreter.FailedOrStop)
                {
                    return(null);
                }

                classInstance.CreateNewInstanceCallConstructors(argumentValues);
                return(classInstance);
            }

            var type = reference as Type;

            if (type != null)
            {
                object classInstance = null;
                argumentValues = InvokeMethod.GetArgumentValues(Expression, ParentInterpreter);

                if (ParentInterpreter.FailedOrStop)
                {
                    return(null);
                }

                try
                {
                    classInstance = Activator.CreateInstance(type, argumentValues.ToArray());
                }
                catch (Exception ex)
                {
                    if (ex is ArgumentException)
                    {
                        ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new BadArgumentException("{Unknow}", ex.Message), Expression), ParentInterpreter.GetDebugInfo()));
                    }
                    else if (ex is TargetParameterCountException)
                    {
                        ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new MethodNotFoundException("ctor", $"There is no constructor with {argumentValues.Count} argument(s) in the class '{Expression._createType}'."), Expression), ParentInterpreter.GetDebugInfo()));
                    }
                    else
                    {
                        ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(ex, Expression), ParentInterpreter.GetDebugInfo()));
                    }
                    return(null);
                }
                return(classInstance);
            }
            return(null);
        }
示例#19
0
        /// <inheritdoc/>
        internal override object Run()
        {
            var expressionValue = ParentInterpreter.RunExpression(Expression.TargetObject);

            if (ParentInterpreter.IsAborted)
            {
                return(null);
            }

            if (expressionValue == null)
            {
                BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.NullValue), Expression);
                return(null);
            }

            var valueInfo = ValueInfo.GetValueInfo(expressionValue);

            if (!valueInfo.IsArray)
            {
                BaZicInterpreter.ChangeState(this, new BadTypeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.FormattedIndexerForbidden(valueInfo.Type.Name)), Expression);
                return(null);
            }

            if (Expression.Indexes.Length != 1)
            {
                BaZicInterpreter.ChangeState(this, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.OneIndexerAllowed), Expression);
                return(null);
            }

            var index = ParentInterpreter.RunExpression(Expression.Indexes[0]);

            if (ParentInterpreter.IsAborted)
            {
                return(null);
            }

            if (index == null)
            {
                BaZicInterpreter.ChangeState(this, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.IndexMustNotBeNull), Expression);
                return(null);
            }

            if (valueInfo.Type == typeof(ObservableDictionary))
            {
                var    dictionary = (ObservableDictionary)expressionValue;
                object val        = null;
                if (dictionary.TryGetValue(index, out val))
                {
                    return(val);
                }

                BaZicInterpreter.ChangeState(this, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.FormattedKeyDoesNotExist(index?.ToString())), Expression);
                return(null);
            }
            else if (typeof(IDictionary).IsAssignableFrom(valueInfo.Type))
            {
                return(((IDictionary)expressionValue)[index]);
            }
            else
            {
                var indexValue = index as int?;

                if (indexValue == null)
                {
                    BaZicInterpreter.ChangeState(this, new BadArgumentException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.CastToNumber), Expression);
                    return(null);
                }

                if (indexValue < 0 || indexValue >= valueInfo.Length)
                {
                    BaZicInterpreter.ChangeState(this, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.FormattedOutOfRange(indexValue.ToString(), (valueInfo.Length - 1).ToString())), Expression);
                    return(null);
                }

                if (valueInfo.Type.IsArray || valueInfo.Type == typeof(Array))
                {
                    return(((Array)expressionValue).GetValue(indexValue.Value));
                }
                else if (typeof(IList).IsAssignableFrom(valueInfo.Type))
                {
                    return(((IList)expressionValue)[indexValue.Value]);
                }
            }

            BaZicInterpreter.ChangeState(this, new InternalException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.FormattedUnsupportedArray(valueInfo.Type.FullName)), Expression);
            return(null);
        }
示例#20
0
        /// <inheritdoc/>
        internal override object Run()
        {
            if (Expression.TargetObject == null)
            {
                BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.InvokeCoreMethodInterpreter.TargetObjectNull), Expression);
                return(null);
            }
            else if (string.IsNullOrWhiteSpace(Expression.MethodName?.Identifier))
            {
                BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.InvokeCoreMethodInterpreter.UndefinedMethodName), Expression);
                return(null);
            }

            var targetObjectValue = ParentInterpreter.RunExpression(Expression.TargetObject);

            if (ParentInterpreter.IsAborted)
            {
                return(null);
            }

            if (targetObjectValue == null)
            {
                BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Expressions.InvokeCoreMethodInterpreter.TargetObjectNull), Expression);
                return(null);
            }


            // Execute argument's values.
            if (BaZicInterpreter.Verbose)
            {
                ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.MethodInterpreter.ExecutingArguments);
            }
            var argumentValues = new List <object>();

            for (var i = 0; i < Expression.Arguments.Count; i++)
            {
                var argumentValue = ParentInterpreter.RunExpression(Expression.Arguments[i]);
                argumentValues.Add(argumentValue);
            }

            if (ParentInterpreter.IsAborted)
            {
                return(null);
            }

            object result = null;

            if (Expression.TargetObject is ClassReferenceExpression && targetObjectValue is Type)
            {
                result = BaZicInterpreter.Reflection.InvokeStaticMethod((Type)targetObjectValue, Expression.MethodName.Identifier, argumentValues.ToArray());
            }
            else if (targetObjectValue is FrameworkElement)
            {
                BaZicInterpreter.ProgramInterpreter.UIDispatcher.Invoke(() =>
                {
                    result = BaZicInterpreter.Reflection.InvokeMethod(targetObjectValue, Expression.MethodName.Identifier, argumentValues.ToArray());
                }, System.Windows.Threading.DispatcherPriority.Background);
            }
            else
            {
                result = BaZicInterpreter.Reflection.InvokeMethod(targetObjectValue, Expression.MethodName.Identifier, argumentValues.ToArray());
            }

            if (Expression.Await)
            {
                if (result == null || !typeof(Task).IsAssignableFrom(result.GetType()))
                {
                    BaZicInterpreter.ChangeState(this, new MethodNotAwaitableException(Expression.MethodName.Identifier), Expression);
                    return(null);
                }
                else
                {
                    var task = (Task)result;
                    task.Wait();
                    var type = task.GetType();
                    if (!type.IsGenericType)
                    {
                        result = null;
                    }
                    else
                    {
                        result = type.GetProperty(nameof(Task <object> .Result)).GetValue(task);
                    }
                    task.Dispose();
                }
            }
            else if (result != null && typeof(Task).IsAssignableFrom(result.GetType()))
            {
                var task = (Task)result;
                BaZicInterpreter.RunningStateManager.AddUnwaitedMethodInvocation(_executionFlowId, task);
            }

            return(result);
        }
示例#21
0
        /// <summary>
        /// Assigns the specified value to an array.
        /// </summary>
        /// <param name="arrayIndexer">The reference to the position in the array to set.</param>
        /// <param name="value">The value to assign.</param>
        private void AssignArrayValue(ArrayIndexerExpression arrayIndexer, object value)
        {
            var expressionValue = ParentInterpreter.RunExpression(arrayIndexer.TargetObject);

            if (ParentInterpreter.IsAborted)
            {
                return;
            }

            if (expressionValue == null)
            {
                BaZicInterpreter.ChangeState(this, new NullValueException(L.BaZic.Runtime.Interpreters.Statements.AssignInterpreter.TargetObjectNull), arrayIndexer);
                return;
            }

            var valueInfo = ValueInfo.GetValueInfo(expressionValue);

            if (!valueInfo.IsArray)
            {
                BaZicInterpreter.ChangeState(this, new BadTypeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.FormattedIndexerForbidden(valueInfo.Type.Name)), arrayIndexer);
                return;
            }

            if (arrayIndexer.Indexes.Length != 1)
            {
                BaZicInterpreter.ChangeState(this, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.OneIndexerAllowed), arrayIndexer);
                return;
            }

            var index = ParentInterpreter.RunExpression(arrayIndexer.Indexes[0]);

            if (ParentInterpreter.IsAborted)
            {
                return;
            }

            if (index == null)
            {
                BaZicInterpreter.ChangeState(this, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.IndexMustNotBeNull), arrayIndexer.Indexes[0]);
                return;
            }

            if (valueInfo.Type == typeof(ObservableDictionary))
            {
                ((ObservableDictionary)expressionValue)[index] = value;
                return;
            }
            else if (typeof(IDictionary).IsAssignableFrom(valueInfo.Type))
            {
                ((IDictionary)expressionValue)[index] = value;
                return;
            }
            else
            {
                var indexValue = index as int?;

                if (indexValue == null)
                {
                    BaZicInterpreter.ChangeState(this, new BadArgumentException(L.BaZic.Runtime.Interpreters.Expressions.ArrayIndexerInterpreter.CastToNumber), arrayIndexer);
                    return;
                }

                if (indexValue < 0 || indexValue >= valueInfo.Length)
                {
                    BaZicInterpreter.ChangeState(this, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Statements.AssignInterpreter.FormattedOutOfRange(indexValue, valueInfo.Length - 1)), arrayIndexer);
                    return;
                }

                if (valueInfo.Type.IsArray || valueInfo.Type == typeof(Array))
                {
                    ((Array)expressionValue).SetValue(value, indexValue.Value);
                    return;
                }
                else if (typeof(IList).IsAssignableFrom(valueInfo.Type))
                {
                    ((IList)expressionValue)[indexValue.Value] = value;
                    return;
                }
            }

            BaZicInterpreter.ChangeState(this, new InternalException(L.BaZic.Runtime.Interpreters.Statements.AssignInterpreter.FormattedUnsupportedArray(valueInfo.Type.FullName)), arrayIndexer);
        }
示例#22
0
 /// <summary>
 /// Run the interpretation
 /// </summary>
 internal override void Execute()
 {
     ParentInterpreter.RunExpression(Statement._expression);
 }
示例#23
0
        /// <summary>
        /// Run the interpretation
        /// </summary>
        /// <returns>Returns the result of the interpretation</returns>
        internal override object Execute()
        {
            if (Expression._targetObject == null)
            {
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new NullReferenceException("Unable to invoke a core method when the TargetObject of an AlgorithmInvokeCoreMethodExpression is null."), Expression), ParentInterpreter.GetDebugInfo()));
                return(null);
            }

            object referenceClass;
            object returnedValue;
            Type   type;
            Task   task;

            if (DebugMode)
            {
                ParentInterpreter.Log(this, $"Calling core method '{Expression._targetObject}.{Expression._methodName}'");
            }

            referenceClass = ParentInterpreter.RunExpression(Expression._targetObject);

            if (ParentInterpreter.FailedOrStop)
            {
                return(null);
            }

            if (referenceClass == null)
            {
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new ClassNotFoundException("{Unknow}", "It looks like the reference object does not exists."), Expression), ParentInterpreter.GetDebugInfo()));
                return(null);
            }

            if (referenceClass is ClassInterpreter)
            {
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new ClassNotFoundException("{Unknow}", "Unable to call a core method from a class made with AlgorithmDOM."), Expression), ParentInterpreter.GetDebugInfo()));
                return(null);
            }

            type = referenceClass as Type;
            if (type != null)
            {
                returnedValue = InvokeMethod(type, null);
            }
            else
            {
                returnedValue = InvokeMethod(referenceClass.GetType(), referenceClass);
            }

            if (ParentInterpreter.FailedOrStop)
            {
                return(null);
            }

            if (Expression._await)
            {
                task = returnedValue as Task;
                if (task != null)
                {
                    task.Wait();
                    var resultPropertyInfo = task.GetType().GetProperty("Result");
                    if (resultPropertyInfo != null && resultPropertyInfo.PropertyType.Name != "VoidTaskResult")
                    {
                        return(resultPropertyInfo.GetValue(task));
                    }
                    return(null);
                }
                ParentInterpreter.ChangeState(this, new AlgorithmInterpreterStateEventArgs(new Error(new MethodNotAwaitableException($"{Expression._targetObject}.{Expression._methodName}"), Expression), ParentInterpreter.GetDebugInfo()));
                return(null);
            }

            return(returnedValue);
        }