Example #1
0
        /// <summary>
        /// Execute the condition
        /// </summary>
        /// <param name="baZicInterpreter">The <see cref="BaZicInterpreterCore"/>.</param>
        /// <param name="parentInterpreter">The parent block interpreter</param>
        /// <param name="condition">The condition expression</param>
        /// <returns>Return true, false, or null in case of error</returns>
        internal static bool?RunCondition(BaZicInterpreterCore baZicInterpreter, BlockInterpreter parentInterpreter, Code.AbstractSyntaxTree.Expression condition)
        {
            if (condition == null)
            {
                baZicInterpreter.ChangeState(parentInterpreter, new NullValueException(L.BaZic.Runtime.Interpreters.Statements.ConditionInterpreter.MissingCondition));
                return(null);
            }

            var conditionResult = parentInterpreter.RunExpression(condition);

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

            var boolResult = conditionResult as bool?;

            if (boolResult != null)
            {
                return(boolResult.Value);
            }

            var intResult = conditionResult as int?;

            if (intResult != null)
            {
                switch (intResult.Value)
                {
                case 1:
                    return(true);

                case 0:
                    return(false);

                default:
                    baZicInterpreter.ChangeState(parentInterpreter, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Statements.ConditionInterpreter.CastToBool), condition);
                    return(null);
                }
            }

            baZicInterpreter.ChangeState(parentInterpreter, new OutOfRangeException(L.BaZic.Runtime.Interpreters.Statements.ConditionInterpreter.BooleanExpected), condition);
            return(null);
        }
Example #2
0
        /// <summary>
        /// Determines whether the interpreter must goes in Idle or Running state and set it if possible.
        /// </summary>
        internal void UpdateState()
        {
            if (_baZicInterpreterCore.State == BaZicInterpreterState.Stopped || _baZicInterpreterCore.State == BaZicInterpreterState.StoppedWithError)
            {
                return;
            }

            var newState = BaZicInterpreterState.Running;

            lock (_unwaitedMethodInvocation)
            {
                if (!_isRunningMainFunction && _externMethodRunningCount == 0 && _unwaitedMethodInvocation.All(tasks => tasks.Value.Count == 0))
                {
                    newState = BaZicInterpreterState.Idle;
                }
            }

            _baZicInterpreterCore.ChangeState(this, new BaZicInterpreterStateChangeEventArgs(newState));
        }