예제 #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
        /// <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));
        }
예제 #3
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));
            }
        }
예제 #4
0
        /// <inheritdoc/>
        internal override void Run()
        {
            if (BaZicInterpreter.Verbose)
            {
                ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Statements.ConditionInterpreter.FormattedExecutingCondition(Statement.Condition));
            }

            var conditionResult = RunCondition(BaZicInterpreter, ParentInterpreter, Statement.Condition);

            if (ParentInterpreter.IsAborted || conditionResult == null)
            {
                return;
            }

            IReadOnlyList <Code.AbstractSyntaxTree.Statement> statements;

            if (conditionResult.Value)
            {
                statements = Statement.TrueStatements;
            }
            else
            {
                statements = Statement.FalseStatements;
            }

            if (statements == null || statements.Count == 0)
            {
                return;
            }

            // Execute statements
            var block = new BlockInterpreter(BaZicInterpreter, ParentInterpreter, ExecutionFlowId, ParentInterpreter.State.IsInIteration, ParentInterpreter.CaughtException, statements);

            block.Run();
            ChildBlockState = block.State;

            if (BaZicInterpreter.Verbose)
            {
                ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Statements.ConditionInterpreter.FormattedEndExecutingCondition(Statement.Condition));
            }
        }
예제 #5
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()));
        }
예제 #6
0
        /// <inheritdoc/>
        internal override void Run()
        {
            try
            {
                // Execute statements
                var block = new BlockInterpreter(BaZicInterpreter, ParentInterpreter, ExecutionFlowId, ParentInterpreter.State.IsInIteration, ParentInterpreter.CaughtException, Statement.TryStatements);
                block.Run();
                ChildBlockState = block.State;
            }
            catch (Exception exception)
            {
                if (BaZicInterpreter.Verbose)
                {
                    ParentInterpreter.VerboseLog(L.BaZic.Runtime.Interpreters.Statements.TryCatchInterpreter.FormattedExceptionCaught(exception.GetType().FullName));
                }

                // Execute statements
                var block = new BlockInterpreter(BaZicInterpreter, ParentInterpreter, ExecutionFlowId, ParentInterpreter.State.IsInIteration, exception, Statement.CatchStatements);
                block.Run();
                ChildBlockState = block.State;
            }
        }
예제 #7
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);
        }
예제 #8
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)));
            }
        }