PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/SimpleBrainFuck/Controls/BrainFuckInteractiveConsole.cs

http://ironbrainfuck.codeplex.com
C# | 190 lines | 158 code | 32 blank | 0 comment | 12 complexity | 3649476673b3f65a37b335192182f75f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. using ZackFlame.IronBrainFuck;
  12. using ZackFlame.SimpleBrainFuck.TextResources;
  13. namespace ZackFlame.SimpleBrainFuck.Controls
  14. {
  15. public partial class BrainFuckInteractiveConsole : UserControl
  16. {
  17. const string StartText = "IronBrainFuck Interactive Console";
  18. BrainFuckMachine brainFuckMachine = new BrainFuckJIT();
  19. Thread brainFuckThread;
  20. bool running = false;
  21. bool inputEnabled = true;
  22. InteractiveConsoleReader reader;
  23. InteractiveConsoleWriter writer;
  24. public BrainFuckInteractiveConsole()
  25. {
  26. InitializeComponent();
  27. this.Disposed += BrainFuckInteractiveConsole_Disposed;
  28. writer = new InteractiveConsoleWriter(textBoxConsole);
  29. reader = new InteractiveConsoleReader(commandBox);
  30. reader.WaitInput += new EventHandler(reader_WaitInput);
  31. reader.GotInput += new EventHandler(reader_GotInput);
  32. reader.LineReaded += new EventHandler<ReadEventArgs>(reader_LineReaded);
  33. brainFuckMachine.Input = reader;
  34. brainFuckMachine.Output = writer;
  35. Reset();
  36. }
  37. public void Reset()
  38. {
  39. if (running)
  40. {
  41. brainFuckThread.Abort();
  42. running = false;
  43. writer.WriteToTextBox = false;
  44. }
  45. textBoxConsole.Clear();
  46. textBoxConsole.AppendText(StartText + Environment.NewLine);
  47. reader.ClearInput();
  48. EnableCommandBox();
  49. commandBox.Focus();
  50. brainFuckMachine.Reset();
  51. }
  52. private void EnableCommandBox()
  53. {
  54. reader.DoWaitForInput = false;
  55. if (running)
  56. {
  57. if (reader.HasBufferedInput)
  58. return;
  59. reader.DoWaitForInput = true;
  60. commandBox.BackColor = Color.LightGreen;
  61. commandBox.Focus();
  62. }
  63. else
  64. {
  65. commandBox.BackColor = SystemColors.Window;
  66. }
  67. inputEnabled = true;
  68. }
  69. private void DisableCommandBox()
  70. {
  71. reader.DoWaitForInput = false;
  72. commandBox.BackColor = SystemColors.ControlDark;
  73. inputEnabled = false;
  74. }
  75. private void reader_WaitInput(object sender, EventArgs e)
  76. {
  77. EnableCommandBox();
  78. }
  79. private void reader_GotInput(object sender, EventArgs e)
  80. {
  81. DisableCommandBox();
  82. }
  83. private void reader_LineReaded(object sender, ReadEventArgs e)
  84. {
  85. textBoxConsole.AppendText(
  86. e.ReadedText.Replace("\n", Environment.NewLine));
  87. }
  88. private void commandBox_KeyDown(object sender, KeyEventArgs e)
  89. {
  90. if (running)
  91. {
  92. if (!inputEnabled)
  93. e.SuppressKeyPress = true;
  94. return;
  95. }
  96. if (e.KeyData == Keys.Enter)
  97. {
  98. e.Handled = true;
  99. e.SuppressKeyPress = true;
  100. string currentCommand = commandBox.Text;
  101. RunCode(currentCommand);
  102. for (int i = 0; i < commandBox.Items.Count; i++)
  103. {
  104. string command = (string)commandBox.Items[i];
  105. if (string.Equals(command, currentCommand,
  106. StringComparison.Ordinal))
  107. {
  108. commandBox.Items.RemoveAt(i);
  109. break;
  110. }
  111. }
  112. if (currentCommand != string.Empty)
  113. commandBox.Items.Insert(0, currentCommand);
  114. commandBox.Text = string.Empty;
  115. }
  116. }
  117. public void RunCode(string brainFuckCode)
  118. {
  119. if (!running)
  120. {
  121. running = true;
  122. DisableCommandBox();
  123. writer.WriteToTextBox = true;
  124. brainFuckThread = new Thread(() =>
  125. {
  126. try
  127. {
  128. brainFuckMachine.Execute(brainFuckCode);
  129. }
  130. catch (InvalidProgramException ex)
  131. {
  132. writer.WriteLine("Error: " + ex.Message);
  133. }
  134. catch (FormatException)
  135. {
  136. writer.WriteLine(RuntimeErrors.RunCode_InvalidSpecialInput);
  137. }
  138. catch (OverflowException)
  139. {
  140. writer.WriteLine(RuntimeErrors.RunCode_TooBigSpecialInput);
  141. }
  142. this.Invoke(new Action(() =>
  143. {
  144. running = false;
  145. EnableCommandBox();
  146. }));
  147. });
  148. brainFuckThread.IsBackground = true;
  149. brainFuckThread.Start();
  150. }
  151. }
  152. private void BrainFuckInteractiveConsole_Disposed(object sender, EventArgs e)
  153. {
  154. reader.Dispose();
  155. writer.Dispose();
  156. }
  157. }
  158. }