PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/TuringMachine/Controller.cs

#
C# | 158 lines | 132 code | 20 blank | 6 comment | 6 complexity | ff907b750f399caaa4977e28563fa533 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using TuringMachine.StateAutomat;
  5. using TuringMachine.CompilerModule;
  6. namespace TuringMachine
  7. {
  8. public class Controller
  9. {
  10. private Tape tape = null;
  11. private Automat stateAutomat = null;
  12. private TransactionTable transactionTable = null;
  13. private Compiler compiler = null;
  14. private string input;
  15. public Controller()
  16. {
  17. tape = new Tape("#",0);
  18. stateAutomat = new Automat();
  19. transactionTable = new TransactionTable();
  20. compiler = new Compiler(ref stateAutomat, ref transactionTable);
  21. }
  22. public string GetTapeString()
  23. {
  24. return tape.TapeInput;
  25. }
  26. public int GetTapeIndex()
  27. {
  28. return tape.Position;
  29. }
  30. public void SetTapeString(string input)
  31. {
  32. tape.TapeInput = input;
  33. this.input = input;
  34. }
  35. public string GetInput()
  36. {
  37. return this.input;
  38. }
  39. public void SetTapeIndex(UInt16 index)
  40. {
  41. tape.Position = index;
  42. }
  43. public void GetStatesList(ref int stepsCounter, ref List<string> stateNames)
  44. {
  45. }
  46. public string GetAutomatState()
  47. {
  48. return stateAutomat.CurrentState.Name;
  49. }
  50. public void MoveAutomatToStart()
  51. {
  52. stateAutomat.CurrentState = stateAutomat.GetStartState();
  53. this.SetTapeIndex(0);
  54. }
  55. public void Compile(string script)
  56. {
  57. string[] commandsArray = null;
  58. string[] delim = {"\n","\r","\r\n"};
  59. commandsArray = script.Split(delim, StringSplitOptions.RemoveEmptyEntries);
  60. compiler.CompileScript(commandsArray);
  61. }
  62. public string PerformStep()
  63. {
  64. char inputChar = tape.CurrentChar;
  65. StringBuilder _ret = new StringBuilder();
  66. //Gets current state in automat
  67. State fromState = stateAutomat.CurrentState;
  68. if (inputChar == ' ')
  69. {
  70. inputChar = '_';
  71. }
  72. _ret.AppendFormat("Automat in state '{0}'. Read '{1}'.", fromState.Name, inputChar);
  73. //Gets transaction
  74. //FromConfiguration fromConfiguration = new FromConfiguration(fromState, inputChar);
  75. FromConfiguration fromConfiguration = transactionTable.GetFromConfigurationInstance(fromState, inputChar);
  76. if (fromConfiguration == null)
  77. {
  78. ///TODO: Add stop
  79. throw new Exception ("There are no step from state " + fromState.Name + " for input "+inputChar.ToString());
  80. }
  81. ToConfiguration toConfiguration = transactionTable.GetToConfiguration(fromConfiguration);
  82. State toState = toConfiguration.PairState;
  83. char tapeAction = toConfiguration.PairChar;
  84. //Move automat
  85. stateAutomat.CurrentState = toState;
  86. _ret.AppendFormat("Move to state '{0}'.", toState.Name);
  87. //Perform tape action
  88. switch (tapeAction)
  89. {
  90. case('>'):
  91. tape.MoveRight();
  92. _ret.Append("Move right on tape");
  93. break;
  94. case('<'):
  95. tape.MoveLeft();
  96. _ret.Append("Move left on tape");
  97. break;
  98. case('_'):
  99. tape.CurrentChar = ' ';
  100. _ret.AppendFormat("Write '{0}' to tape",tapeAction);
  101. break;
  102. default:
  103. tape.CurrentChar = tapeAction;
  104. _ret.AppendFormat("Write '{0}' to tape", tapeAction);
  105. break;
  106. }
  107. return _ret.ToString();
  108. }
  109. public bool IsStop(ref string reason)
  110. {
  111. bool _ret = true;
  112. switch (stateAutomat.CurrentState.Type)
  113. {
  114. case StateType.StartState:
  115. _ret = false;
  116. break;
  117. case StateType.MiddleState:
  118. _ret = false;
  119. break;
  120. case StateType.HaltState:
  121. reason = "Automat entered to Halting state";
  122. break;
  123. case StateType.YesState:
  124. reason = "Automat entered to Yes state";
  125. break;
  126. case StateType.NoState:
  127. reason = "Automat entered to No state";
  128. break;
  129. case StateType.UnknownState:
  130. reason = "Automat entered to Unknown state";
  131. break;
  132. default:
  133. break;
  134. }
  135. return _ret;
  136. }
  137. }
  138. }