Example #1
0
        /// <summary>
        /// Execute an expression
        /// </summary>
        /// <param name="expression">The expression to interpret</param>
        /// <returns>Returns the returned value of the expression</returns>
        internal object RunExpression(Code.AbstractSyntaxTree.Expression expression)
        {
            if (expression == null)
            {
                return(null);
            }

            if (BaZicInterpreter.Verbose)
            {
                VerboseLog(L.BaZic.Runtime.Interpreters.Interpreter.FormattedExecutingExpression(expression.GetType().Name));
            }

            object expressionResult = null;

            switch (expression)
            {
            case ArrayCreationExpression arrayCreation:
                expressionResult = new ArrayCreationInterpreter(BaZicInterpreter, this, arrayCreation).Run();
                break;

            case ArrayIndexerExpression arrayIndexer:
                expressionResult = new ArrayIndexerInterpreter(BaZicInterpreter, this, arrayIndexer).Run();
                break;

            case BinaryOperatorExpression binaryOperator:
                expressionResult = new BinaryOperatorInterpreter(BaZicInterpreter, this, binaryOperator).Run();
                break;

            case ClassReferenceExpression classReference:
                expressionResult = new ClassReferenceInterpreter(BaZicInterpreter, this, classReference).Run();
                break;

            case ExceptionReferenceExpression exceptionReference:
                expressionResult = new ExceptionInterpreter(BaZicInterpreter, this, exceptionReference).Run();
                break;

            case InstantiateExpression instantiate:
                expressionResult = new InstantiateInterpreter(BaZicInterpreter, this, instantiate).Run();
                break;

            case InvokeCoreMethodExpression invokeCoreMethod:
                expressionResult = new InvokeCoreMethodInterpreter(BaZicInterpreter, this, invokeCoreMethod, ExecutionFlowId).Run();
                break;

            case InvokeMethodExpression invokeMethod:
                expressionResult = new InvokeMethodInterpreter(BaZicInterpreter, this, invokeMethod, ExecutionFlowId).Run();
                break;

            case NotOperatorExpression notOperator:
                expressionResult = new NotOperatorInterpreter(BaZicInterpreter, this, notOperator).Run();
                break;

            case PrimitiveExpression primitive:
                expressionResult = new PrimitiveInterpreter(BaZicInterpreter, this, primitive).Run();
                break;

            case PropertyReferenceExpression propertyReference:
                expressionResult = new PropertyReferenceInterpreter(BaZicInterpreter, this, propertyReference).Run();
                break;

            case VariableReferenceExpression variableReference:
                expressionResult = new VariableReferenceInterpreter(BaZicInterpreter, this, variableReference).Run();
                break;

            default:
                BaZicInterpreter.ChangeState(this, new InternalException(L.BaZic.Runtime.Interpreters.Interpreter.FormattedInterpreterNotFound(expression.GetType().Name)), expression);
                break;
            }

            if (IsAborted)
            {
                return(null);
            }

            if (BaZicInterpreter.Verbose)
            {
                VerboseLog(L.BaZic.Runtime.Interpreters.Interpreter.FormattedExpressionReturnedValue(expressionResult, ValueInfo.GetValueInfo(expressionResult)));
            }

            return(expressionResult);
        }
Example #2
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);
        }