/// <summary> /// Execute the statements depending on the result of the condition /// </summary> /// <param name="conditionResult">The condition result</param> private void RunResultStatements(bool conditionResult) { AlgorithmStatementCollection statements; if (conditionResult) { statements = Statement._trueStatements; } else { statements = Statement._falseStatements; } if (statements == null || statements.Count == 0) { return; } var block = new BlockInterpreter(statements, DebugMode, ParentInterpreter.ParentProgramInterpreter, ParentInterpreter.ParentMethodInterpreter, ParentInterpreter.ParentBlockInterpreter, ParentInterpreter.ParentClassInterpreter); block.OnGetParentInterpreter += new Func <BlockInterpreter>(() => ParentInterpreter); block.StateChanged += ParentInterpreter.ChangeState; block.Initialize(); ReturnOccured = block.Run(); block.StateChanged -= ParentInterpreter.ChangeState; block.Dispose(); }
/// <summary> /// If the interpreter is requested to be in pause, then pause the thread. /// </summary> /// <param name="blockInterpreter">The current block interpreter that check whether it must pause the thread.</param> internal void AttemptPauseIfRequired(BlockInterpreter blockInterpreter) { if (_pauseModeWaiter != null) { ChangeState(this, new BaZicInterpreterStateChangeEventArgs(BaZicInterpreterState.Pause)); DebugInfos = blockInterpreter.GetDebugInfo(); _pauseModeWaiter.WaitOne(); } }
/// <summary> /// Run the interpretation /// </summary> internal override void Execute() { var conditionResult = false; if (Statement._initializationStatement != null) { ParentInterpreter.RunStatement(Statement._initializationStatement); if (ParentInterpreter.FailedOrStop) { return; } } _IterationLoop: if (!Statement._conditionAfterBody) { conditionResult = RunCondition(); if (!conditionResult) { return; } } var block = new BlockInterpreter(Statement._statements, DebugMode, ParentInterpreter.ParentProgramInterpreter, ParentInterpreter.ParentMethodInterpreter, ParentInterpreter.ParentBlockInterpreter, ParentInterpreter.ParentClassInterpreter); block.OnGetParentInterpreter += new Func<BlockInterpreter>(() => ParentInterpreter); block.StateChanged += ParentInterpreter.ChangeState; block.Initialize(); ReturnOccured = block.Run(); block.StateChanged -= ParentInterpreter.ChangeState; block.Dispose(); if (ReturnOccured || ParentInterpreter.FailedOrStop) { return; } if (Statement._incrementStatement != null) { ParentInterpreter.RunStatement(Statement._incrementStatement); } if (ParentInterpreter.FailedOrStop) { return; } if (Statement._conditionAfterBody) { conditionResult = RunCondition(); } if (conditionResult) { goto _IterationLoop; } }
/// <summary> /// Initializes a new instance of the <see cref="StatementInterpreter"/> class. /// </summary> /// <param name="baZicInterpreter">The main interpreter.</param> /// <param name="parentInterpreter">The parent interpreter.</param> /// <param name="executionFlowId">A GUID that defines in which callstack is linked.</param> /// <param name="statement">The statement to interpret.</param> protected StatementInterpreter(BaZicInterpreterCore baZicInterpreter, BlockInterpreter parentInterpreter, Guid executionFlowId, T statement) { Requires.NotNull(baZicInterpreter, nameof(baZicInterpreter)); Requires.NotNull(parentInterpreter, nameof(parentInterpreter)); Requires.NotNull(statement, nameof(statement)); BaZicInterpreter = baZicInterpreter; ParentInterpreter = parentInterpreter; Statement = statement; ExecutionFlowId = executionFlowId; }
/// <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); }
/// <summary> /// Execute the condition /// </summary> /// <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(BlockInterpreter parentInterpreter, AlgorithmExpression condition) { if (condition == null) { parentInterpreter.ChangeState(parentInterpreter, new AlgorithmInterpreterStateEventArgs(new Error(new NullReferenceException("A conditional expression is missing.")), parentInterpreter.GetDebugInfo())); return(null); } var conditionResult = parentInterpreter.RunExpression(condition); if (parentInterpreter.FailedOrStop) { 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: parentInterpreter.ChangeState(parentInterpreter, new AlgorithmInterpreterStateEventArgs(new Error(new InvalidCastException("Unable to cast this number to a boolean."), condition), parentInterpreter.GetDebugInfo())); return(null); } } parentInterpreter.ChangeState(parentInterpreter, new AlgorithmInterpreterStateEventArgs(new Error(new InvalidCastException("Unable to perform a condition statement without a boolean value as conditional expression result."), condition), parentInterpreter.GetDebugInfo())); return(null); }
/// <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)); } }
/// <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; } }
/// <inheritdoc/> internal override void Run() { var conditionResult = false; _IterationLoop: if (!Statement.ConditionAfterBody) { conditionResult = RunCondition(); if (!conditionResult) { return; } } // Execute statements var block = new BlockInterpreter(BaZicInterpreter, ParentInterpreter, ExecutionFlowId, true, ParentInterpreter.CaughtException, Statement.Statements); block.Run(); ChildBlockState = block.State; if (ChildBlockState.ExitMethod || ChildBlockState.ExitIteration || ChildBlockState.ExitBlockBecauseOfLabelJump || ParentInterpreter.IsAborted) { ChildBlockState.ExitIteration = false; // prevent to exit the parent iteration, we want to exit just the current one. return; } if (Statement.ConditionAfterBody) { conditionResult = RunCondition(); } if (conditionResult) { goto _IterationLoop; } }
/// <summary> /// Initialize a new instance of <see cref="Return"/> /// </summary> /// <param name="debugMode">Defines if the debug mode is enabled</param> /// <param name="parentInterpreter">The parent block interpreter</param> /// <param name="statement">The algorithm statement</param> public Return(bool debugMode, BlockInterpreter parentInterpreter, AlgorithmStatement statement) : base(debugMode, parentInterpreter, statement) { }
/// <summary> /// Initialize a new instance of <see cref="InterpretExpression"/> /// </summary> /// <param name="debugMode">Defines if the debug mode is enabled</param> /// <param name="parentInterpreter">the parent <see cref="BlockInterpreter"/></param> /// <param name="expression">the algorithm expression</param> internal InterpretExpression(bool debugMode, BlockInterpreter parentInterpreter, AlgorithmExpression expression) : base(debugMode) { ParentInterpreter = parentInterpreter; Expression = expression; }
/// <summary> /// Returns the arguments values /// </summary> /// <returns>Returns a collection of object which represents the argument value</returns> internal static Collection <object> GetArgumentValues(AlgorithmExpression expression, BlockInterpreter parentInterpreter) { var argumentValues = new Collection <object>(); foreach (var arg in expression._argumentsExpression) { if (!parentInterpreter.FailedOrStop) { argumentValues.Add(parentInterpreter.RunExpression(arg)); } } return(argumentValues); }
/// <summary> /// Initialize a new instance of <see cref="InvokeMethod"/> /// </summary> /// <param name="debugMode">Defines if the debug mode is enabled</param> /// <param name="parentInterpreter">The parent block interpreter</param> /// <param name="expression">The algorithm expression</param> internal InvokeMethod(bool debugMode, BlockInterpreter parentInterpreter, AlgorithmExpression expression) : base(debugMode, parentInterpreter, expression) { }
/// <summary> /// Initialize a new instance of <see cref="InvokeCoreMethod"/> /// </summary> /// <param name="debugMode">Defines if the debug mode is enabled</param> /// <param name="parentInterpreter">The parent block interpreter</param> /// <param name="expression">The algorithm expression</param> internal InvokeCoreMethod(bool debugMode, BlockInterpreter parentInterpreter, AlgorithmExpression expression) : base(debugMode, parentInterpreter, expression) { _dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher; }
/// <summary> /// Initialize a new instance of <see cref="Breakpoint"/> /// </summary> /// <param name="debugMode">Defines if the debug mode is enabled</param> /// <param name="parentInterpreter">The parent block interpreter</param> internal Breakpoint(bool debugMode, BlockInterpreter parentInterpreter) : base(debugMode, parentInterpreter, null) { }
/// <summary> /// Initialize a new instance of <see cref="ArrayIndexerExpression"/> /// </summary> /// <param name="debugMode">Defines if the debug mode is enabled</param> /// <param name="parentInterpreter">The parent block interpreter</param> /// <param name="expression">The algorithm expression</param> internal ArrayIndexerExpression(bool debugMode, BlockInterpreter parentInterpreter, AlgorithmExpression expression) : base(debugMode, parentInterpreter, expression) { }
/// <summary> /// Initialize a new instance of <see cref="PrimitiveValue"/> /// </summary> /// <param name="debugMode">Defines if the debug mode is enabled</param> /// <param name="parentInterpreter">The parent block interpreter</param> /// <param name="expression">The algorithm expression</param> internal PrimitiveValue(bool debugMode, BlockInterpreter parentInterpreter, AlgorithmExpression expression) : base(debugMode, parentInterpreter, expression) { }
/// <summary> /// Initialize a new instance of <see cref="PropertyReference"/> /// </summary> /// <param name="debugMode">Defines if the debug mode is enabled</param> /// <param name="parentInterpreter">The parent block interpreter</param> /// <param name="expression">The algorithm expression</param> internal PropertyReference(bool debugMode, BlockInterpreter parentInterpreter, AlgorithmExpression expression) : base(debugMode, parentInterpreter, expression) { }
/// <summary> /// Initialize a new instance of <see cref="BinaryOperator"/> /// </summary> /// <param name="debugMode">Defines if the debug mode is enabled</param> /// <param name="parentInterpreter">The parent block interpreter</param> /// <param name="expression">The algorithm expression</param> internal BinaryOperator(bool debugMode, BlockInterpreter parentInterpreter, AlgorithmExpression expression) : base(debugMode, parentInterpreter, expression) { }
/// <summary> /// Initialize a new instance of <see cref="Instanciate"/> /// </summary> /// <param name="debugMode">Defines if the debug mode is enabled</param> /// <param name="parentInterpreter">The parent block interpreter</param> /// <param name="expression">The algorithm expression</param> internal Instanciate(bool debugMode, BlockInterpreter parentInterpreter, AlgorithmExpression expression) : base(debugMode, parentInterpreter, expression) { }
/// <summary> /// Initialize a new instance of <see cref="InterpretStatement"/> /// </summary> /// <param name="debugMode">Defines if the debug mode is enabled</param> /// <param name="parentInterpreter">the parent <see cref="BlockInterpreter"/></param> /// <param name="statement">the algorithm statement</param> internal InterpretStatement(bool debugMode, BlockInterpreter parentInterpreter, AlgorithmStatement statement) : base(debugMode) { ParentInterpreter = parentInterpreter; Statement = statement; }
internal TryCatchInterpreter(BaZicInterpreterCore baZicInterpreter, BlockInterpreter parentInterpreter, Guid executionFlowId, TryCatchStatement statement) : base(baZicInterpreter, parentInterpreter, executionFlowId, statement) { }
internal ExpressionStatementInterpreter(BaZicInterpreterCore baZicInterpreter, BlockInterpreter parentInterpreter, Guid executionFlowId, ExpressionStatement statement) : base(baZicInterpreter, parentInterpreter, executionFlowId, statement) { }
internal LabelConditionInterpreter(BaZicInterpreterCore baZicInterpreter, BlockInterpreter parentInterpreter, Guid executionFlowId, LabelConditionStatement statement) : base(baZicInterpreter, parentInterpreter, executionFlowId, statement) { }
/// <summary> /// Initialize a new instance of <see cref="VariableDeclaration"/> /// </summary> /// <param name="debugMode">Defines if the debug mode is enabled</param> /// <param name="parentInterpreter">The parent block interpreter</param> /// <param name="statement">The algorithm statement</param> public VariableDeclaration(bool debugMode, BlockInterpreter parentInterpreter, AlgorithmStatement statement) : base(debugMode, parentInterpreter, statement) { }
/// <summary> /// Initialize a new instance of <see cref="ExpressionStatement"/> /// </summary> /// <param name="debugMode">Defines if the debug mode is enabled</param> /// <param name="parentInterpreter">The parent block interpreter</param> /// <param name="statement">The algorithm statement</param> public ExpressionStatement(bool debugMode, BlockInterpreter parentInterpreter, AlgorithmStatement statement) : base(debugMode, parentInterpreter, statement) { }
/// <summary> /// Initialize a new instance of <see cref="Assign"/> /// </summary> /// <param name="debugMode">Defines if the debug mode is enabled</param> /// <param name="parentInterpreter">The parent block interpreter</param> /// <param name="statement">The algorithm statement</param> internal Assign(bool debugMode, BlockInterpreter parentInterpreter, AlgorithmStatement statement) : base(debugMode, parentInterpreter, statement) { }
internal VariableDeclarationInterpreter(BaZicInterpreterCore baZicInterpreter, BlockInterpreter parentInterpreter, Guid executionFlowId, VariableDeclaration statement) : base(baZicInterpreter, parentInterpreter, executionFlowId, statement) { }