protected void fireExecutionFinished(bool canceled, RuntimeError error)
 {
     if (ExecutionFinished != null)
     {
         ExecutionFinished(canceled, error);
     }
 }
        protected virtual void Run()
        {
            using (var instructionPointer = GetProgram().GetEnumerator())
            {
                bool         canceled = false;
                RuntimeError error    = null;
                try
                {
                    while (instructionPointer.MoveNext())
                    {
                        switch (State)
                        {
                        case ExecutionState.Running:
                            lock (_locker)
                                if (_breakpoints.Any(bp => bp >= instructionPointer.Current.Index && bp < instructionPointer.Current.Index + Math.Max(instructionPointer.Current.Length, 1)))
                                {
                                    State = ExecutionState.Debugging;
                                    goto case ExecutionState.Debugging;
                                }
                            break;

                        case ExecutionState.Debugging:
                            fireDebuggerBreak(instructionPointer.Current);
                            _resetEvent.Reset();
                            _resetEvent.WaitOne();
                            if (State == ExecutionState.Stop)
                            {
                                goto case ExecutionState.Stop;
                            }
                            break;

                        case ExecutionState.Stop:
                            canceled = true;
                            goto finished;

                        case ExecutionState.Finished:
                            goto finished;

                        default:
                            throw new InvalidOperationException("Execution state has invalid value: " + State);
                        }
                    }
                }
                catch (Exception e)
                {
                    error = new RuntimeError(instructionPointer.Current, e.Message + e.GetType().Apply(type => type != typeof(Exception) ? " (" + type.Name + ")" : ""));
                }

finished:
                fireExecutionFinished(canceled, error);
                State = ExecutionState.Finished;
                _resetEvent.Reset();
            }
        }
        public void Continue(bool blockUntilFinished = false)
        {
            if (State == ExecutionState.Finished)
            {
                _resetEvent.Reset();
                return;
            }

            if (_runner == null)
            {
                _runner = new Thread(() =>
                {
                    if (State == ExecutionState.Finished)
                    {
                        _resetEvent.Reset();
                        return;
                    }

                    try
                    {
                        Run();
                    }
                    catch (Exception e)
                    {
                        var type  = e.GetType();
                        var error = new RuntimeError(null, e.Message + (type != typeof(Exception) ? " (" + type.Name + ")" : ""));
                        fireExecutionFinished(true, error);
                        State = ExecutionState.Finished;
                        _resetEvent.Reset();
                    }

                    _runner = null;
                });
                _runner.Start();
            }

            _resetEvent.Set();
            if (blockUntilFinished)
            {
                while (State != ExecutionState.Finished)
                {
                    _resetEvent.WaitOne();
                }
            }
        }
Exemple #4
0
        private void executionFinished(bool canceled, RuntimeError runtimeError)
        {
            removeRunToCursorBreakpoint();
            tabWatch.Controls.Clear();
            tabWatch.Controls.Add(notRunningLabel);

            var sel = txtSource.SelectionStart;
            var len = txtSource.SelectionLength;

            txtSource.Text = _env.OriginalSource;
            txtOutput.Text = _env.Output.UnifyLineEndings();
            if (canceled && runtimeError == null)
            {
                txtOutput.Text += Environment.NewLine + Environment.NewLine + "Execution stopped.";
            }

            _env             = null;
            _currentPosition = null;

            ctTabs.SelectedTab = tabOutput;
            txtSource.Focus();
            txtSource.SelectionStart  = sel;
            txtSource.SelectionLength = len;
            txtSource.ScrollToCaret();

            // In case the file has changed since the last time we ran the program
            checkFileChanged();

            if (runtimeError != null)
            {
                var msg = "A run-time error occurred:{0}{0}{1}".Fmt(Environment.NewLine, runtimeError.Message);
                txtOutput.Text += Environment.NewLine + Environment.NewLine + msg;
                txtSource.Focus();
                if (runtimeError.Position != null)
                {
                    txtSource.Focus();
                    txtSource.SelectionStart  = runtimeError.Position.Index;
                    txtSource.SelectionLength = runtimeError.Position.Length;
                    txtSource.ScrollToCaret();
                }
                DlgMessage.Show(msg, "Run-time error", DlgType.Error, "&OK");
            }
        }
Exemple #5
0
        private void executionFinished(bool canceled, RuntimeError runtimeError)
        {
            removeRunToCursorBreakpoint();
            tabWatch.Controls.Clear();
            tabWatch.Controls.Add(notRunningLabel);

            var sel = txtSource.SelectionStart;
            var len = txtSource.SelectionLength;
            txtSource.Text = _env.OriginalSource;
            txtOutput.Text = _env.Output.UnifyLineEndings();
            if (canceled && runtimeError == null)
                txtOutput.Text += Environment.NewLine + Environment.NewLine + "Execution stopped.";

            _env = null;
            _currentPosition = null;

            ctTabs.SelectedTab = tabOutput;
            txtSource.Focus();
            txtSource.SelectionStart = sel;
            txtSource.SelectionLength = len;
            txtSource.ScrollToCaret();

            // In case the file has changed since the last time we ran the program
            checkFileChanged();

            if (runtimeError != null)
            {
                var msg = "A run-time error occurred:{0}{0}{1}".Fmt(Environment.NewLine, runtimeError.Message);
                txtOutput.Text += Environment.NewLine + Environment.NewLine + msg;
                txtSource.Focus();
                if (runtimeError.Position != null)
                {
                    txtSource.Focus();
                    txtSource.SelectionStart = runtimeError.Position.Index;
                    txtSource.SelectionLength = runtimeError.Position.Length;
                    txtSource.ScrollToCaret();
                }
                DlgMessage.Show(msg, "Run-time error", DlgType.Error, "&OK");
            }
        }
 protected void fireExecutionFinished(bool canceled, RuntimeError error)
 {
     ExecutionFinished?.Invoke(canceled, error);
 }
        protected virtual void Run()
        {
            using (var instructionPointer = GetProgram().GetEnumerator())
            {
                bool canceled = false;
                RuntimeError error = null;
                try
                {
                    while (instructionPointer.MoveNext())
                    {
                        switch (State)
                        {
                            case ExecutionState.Running:
                                lock (_locker)
                                    if (_breakpoints.Any(bp => bp >= instructionPointer.Current.Index && bp < instructionPointer.Current.Index + Math.Max(instructionPointer.Current.Length, 1)))
                                    {
                                        State = ExecutionState.Debugging;
                                        goto case ExecutionState.Debugging;
                                    }
                                break;
                            case ExecutionState.Debugging:
                                fireDebuggerBreak(instructionPointer.Current);
                                _resetEvent.Reset();
                                _resetEvent.WaitOne();
                                if (State == ExecutionState.Stop)
                                    goto case ExecutionState.Stop;
                                break;
                            case ExecutionState.Stop:
                                canceled = true;
                                goto finished;
                            case ExecutionState.Finished:
                                goto finished;
                            default:
                                throw new InvalidOperationException("Execution state has invalid value: " + State);
                        }
                    }
                }
                catch (Exception e)
                {
                    error = new RuntimeError(instructionPointer.Current, e.Message + e.GetType().Apply(type => type != typeof(Exception) ? " (" + type.Name + ")" : ""));
                }

                finished:
                fireExecutionFinished(canceled, error);
                State = ExecutionState.Finished;
                _resetEvent.Reset();
            }
        }
 protected void fireExecutionFinished(bool canceled, RuntimeError error)
 {
     if (ExecutionFinished != null) ExecutionFinished(canceled, error);
 }
        public void Continue(bool blockUntilFinished = false)
        {
            if (State == ExecutionState.Finished)
            {
                _resetEvent.Reset();
                return;
            }

            if (_runner == null)
            {
                _runner = new Thread(() =>
                {
                    if (State == ExecutionState.Finished)
                    {
                        _resetEvent.Reset();
                        return;
                    }

                    try
                    {
                        Run();
                    }
                    catch (Exception e)
                    {
                        var type = e.GetType();
                        var error = new RuntimeError(null, e.Message + (type != typeof(Exception) ? " (" + type.Name + ")" : ""));
                        fireExecutionFinished(true, error);
                        State = ExecutionState.Finished;
                        _resetEvent.Reset();
                    }

                    _runner = null;
                });
                _runner.Start();
            }

            _resetEvent.Set();
            if (blockUntilFinished)
                while (State != ExecutionState.Finished)
                    _resetEvent.WaitOne();
        }