// Event Handler for StepByStep ToolStripMenuItem, executes one line of code per click private void StepByStepToolStripMenuItem_Click(object sender, EventArgs e) { if (!waitingForInput) { StepByStep = true; try { // Don't compile twice if code is already compiled if (!compiled) { errorBox.Text = ""; rM = new RegisterMachine(textEditorBox.Text.Split('\n').ToList(), ifMode); compiled = true; } Highlight(); // Check if there are more steps available or end of code is reached RegisterMachine.ReturnType T = rM.ExecuteOneStep(ref OutputString); switch (T) { case RegisterMachine.ReturnType.OUTPUT: errorBox.Text = OutputString; break; case RegisterMachine.ReturnType.INPUT: UpdateLabels(); UpdateDataGridView(); waitingForInput = true; inputTextBox.ReadOnly = false; inputTextBox.Enabled = true; inputTextBox.Text = "Waiting for Input"; return; case RegisterMachine.ReturnType.END: UpdateLabels(); UpdateDataGridView(); errorBox.Text = "Program execution finished!"; rM.ResetState(); break; default: UpdateLabels(); UpdateDataGridView(); break; } } catch (Exception exc) { errorBox.Text = exc.Message; } } }
//Event Handler for the "Run" item in the MenuStrip, compiles and runs the program private void RunToolStripMenuItemClick(object sender, EventArgs e) { waitingForInput = false; StepByStep = false; UpdateCodelines(); errorBox.Text = ""; try { rM = new RegisterMachine(textEditorBox.Text.Split('\n').ToList(), ifMode); compiled = true; RegisterMachine.ReturnType T = RegisterMachine.ReturnType.RUNNING; // Execute code until end or input/output command while (T != RegisterMachine.ReturnType.END) { T = rM.ExecuteRegisterMachine(ref OutputString); // Show output if asked for if (T == RegisterMachine.ReturnType.OUTPUT) { errorBox.Text = OutputString; } // Set register machine to "wait"-state and request input if (T == RegisterMachine.ReturnType.INPUT) { UpdateLabels(); UpdateDataGridView(); waitingForInput = true; inputTextBox.ReadOnly = false; inputTextBox.Enabled = true; inputTextBox.Text = "Waiting for Input"; return; } } } catch (Exception exception) { errorBox.Text = exception.Message; } UpdateLabels(); UpdateDataGridView(); rM.ResetState(); }
//manages input/output prompt private void InputTextBox_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { if (waitingForInput) { waitingForInput = false; InputString = inputTextBox.Text; inputTextBox.ReadOnly = false; inputTextBox.Enabled = true; textEditorBox.Focus(); inputTextBox.Text = "No input required"; if (StepByStep) { try { Highlight(); RegisterMachine.ReturnType T = rM.ExecuteRegisterMachine(ref InputString); if (T == RegisterMachine.ReturnType.OUTPUT) { errorBox.Text = InputString; } if (T == RegisterMachine.ReturnType.INPUT) { UpdateLabels(); UpdateDataGridView(); waitingForInput = true; inputTextBox.Text = "Waiting for Input"; return; } if (T == RegisterMachine.ReturnType.END) { UpdateLabels(); UpdateDataGridView(); errorBox.Text = "Program execution finished!"; rM.ResetState(); return; } } catch (Exception exc) { errorBox.Text = exc.Message; } } else { try { RegisterMachine.ReturnType T = rM.ExecuteRegisterMachine(ref InputString); if (T == RegisterMachine.ReturnType.OUTPUT) { errorBox.Text = InputString; } if (T == RegisterMachine.ReturnType.INPUT) { UpdateLabels(); UpdateDataGridView(); waitingForInput = true; inputTextBox.Text = "Waiting for Input"; return; } while (T != RegisterMachine.ReturnType.END) { T = rM.ExecuteRegisterMachine(ref OutputString); if (T == RegisterMachine.ReturnType.OUTPUT) { errorBox.Text = OutputString; } if (T == RegisterMachine.ReturnType.INPUT) { waitingForInput = true; inputTextBox.Text = "Waiting for Input"; UpdateLabels(); UpdateDataGridView(); return; } if (T == RegisterMachine.ReturnType.END) { UpdateLabels(); UpdateDataGridView(); errorBox.Text = "Program execution finished!"; rM.ResetState(); return; } } } catch (Exception exception) { errorBox.Text = exception.Message; } UpdateLabels(); UpdateDataGridView(); rM.ResetState(); } } } }