PageRenderTime 32ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/BrainFuckOS/BrainFuckOS-TestConsole/BrainFuckInterpreterTEST.cs

#
C# | 157 lines | 142 code | 6 blank | 9 comment | 29 complexity | 07df83dba2629ed3f29f28e6fcb111b2 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace BrainFuckOS
  6. {
  7. class BrainFuckInterpreter
  8. {
  9. string script;
  10. int program_counter;
  11. uint pointer;
  12. uint[] data;
  13. public BrainFuckInterpreter()
  14. {
  15. script = "";
  16. }
  17. public string ProgramCode
  18. {
  19. set
  20. {
  21. script = value;
  22. }
  23. }
  24. public void Run()
  25. {
  26. //Console.WriteLine("Starting BFI...");
  27. if (script != null)
  28. {
  29. //Console.WriteLine("Valid Program...");
  30. data = new uint[6656u];
  31. program_counter = 0;
  32. pointer = 0;
  33. while (program_counter != script.Length)
  34. {
  35. System.Windows.Forms.Application.DoEvents();
  36. char c = script[program_counter];
  37. //Console.WriteLine("Command: {0}", c);
  38. Console.Title = "'" + c + "' / " + program_counter.ToString();
  39. switch (c)
  40. {
  41. case '+':
  42. data[pointer]++;
  43. break;
  44. case '-':
  45. data[pointer]--;
  46. break;
  47. case '>':
  48. pointer++;
  49. break;
  50. case '<':
  51. pointer--;
  52. break;
  53. case '.':
  54. //Console.WriteLine("[{0}] >{1}<",pointer.ToString("X2"),data[pointer].ToString("X2"));
  55. PortForwarder.Write(data[0], data[pointer], this);
  56. break;
  57. case ',':
  58. data[pointer] = PortForwarder.Read(data[0], this);
  59. break;
  60. case '[':
  61. if (data[pointer] == 0)
  62. {
  63. program_counter = FindNextBracket();
  64. //Console.WriteLine("Found ']' at {0}", program_counter);
  65. if (program_counter == -1)
  66. {
  67. //Cosmos.System.Global.Dbg.SendMessage(this.ToString(), "Kein passendes ']'.");
  68. return;
  69. }
  70. }
  71. break;
  72. case ']':
  73. if (data[pointer] != 0)
  74. {
  75. program_counter = FindLastBracket();
  76. //Console.WriteLine("Found '[' at {0}", program_counter);
  77. if (program_counter == -1)
  78. {
  79. //Cosmos.System.Global.Dbg.SendMessage(this.ToString(), "Kein passendes '['.");
  80. return;
  81. }
  82. }
  83. break;
  84. }
  85. program_counter++;
  86. }
  87. }
  88. }
  89. int FindNextBracket()
  90. {
  91. int i = program_counter + 1;
  92. int j = 0;
  93. char c = script[i];
  94. while (i < script.Length)
  95. {
  96. switch (c)
  97. {
  98. case '[':
  99. j++;
  100. break;
  101. case ']':
  102. j--;
  103. break;
  104. }
  105. if (j == -1 && c == ']')
  106. {
  107. break;
  108. }
  109. i++;
  110. if (i < script.Length)
  111. {
  112. c = script[i];
  113. }
  114. }
  115. if (i == script.Length)
  116. {
  117. i = -1;
  118. }
  119. return i;
  120. }
  121. int FindLastBracket()
  122. {
  123. int i = program_counter - 1;
  124. int j = 0;
  125. char c = script[i];
  126. while (i >= 0)
  127. {
  128. //Console.WriteLine("{0} - {1}",c,j);
  129. switch (c)
  130. {
  131. case '[':
  132. j--;
  133. break;
  134. case ']':
  135. j++;
  136. break;
  137. }
  138. if (j == -1 && c == '[')
  139. {
  140. break;
  141. }
  142. i--;
  143. if (i >= 0)
  144. {
  145. c = script[i];
  146. }
  147. }
  148. return i;
  149. }
  150. }
  151. }