PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/TuringMachine/CompilerModule/Compiler.cs

#
C# | 78 lines | 65 code | 9 blank | 4 comment | 4 complexity | 9882cdae9e7afb657979eed3dcfde5c1 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using TuringMachine.StateAutomat;
  5. namespace TuringMachine.CompilerModule
  6. {
  7. class Compiler
  8. {
  9. private Automat _automat = null;
  10. private TransactionTable _transactionTable = null;
  11. private Parser _parser = null;
  12. public Compiler()
  13. {
  14. }
  15. public Compiler(ref Automat automat, ref TransactionTable transactionTable)
  16. {
  17. _automat = automat;
  18. _transactionTable = transactionTable;
  19. _parser = new Parser();
  20. }
  21. public void CompileScript(string[] commandsArray)
  22. {
  23. State fromState = null;
  24. char inputChar;
  25. State toState = null;
  26. char tapeAction;
  27. foreach (string command in commandsArray)
  28. {
  29. _parser.Parse(command);
  30. //Get all elements of command
  31. fromState = _parser.StartState();
  32. inputChar = _parser.InputChar();
  33. toState = _parser.NextState();
  34. tapeAction = _parser.TapeAction();
  35. //Check if such states already in automat, if not add them
  36. if (_automat.IsStateInAutomat(fromState))
  37. {
  38. fromState = _automat.GetStateInstance(fromState);
  39. }
  40. else
  41. {
  42. _automat.AddState(fromState);
  43. }
  44. if (_automat.IsStateInAutomat(toState))
  45. {
  46. toState = _automat.GetStateInstance(toState);
  47. }
  48. else
  49. {
  50. _automat.AddState(toState);
  51. }
  52. ///TODO: Add deternemistic check
  53. //Add transaction to table
  54. if (!_transactionTable.IsFromConfigurationExist(fromState,inputChar))
  55. {
  56. _transactionTable.AddTransaction(fromState, inputChar, toState, tapeAction);
  57. }
  58. }
  59. if (_automat.GetStartState()==null)
  60. {
  61. throw new Exception("No start state exist in automat");
  62. }
  63. else
  64. {
  65. _automat.CurrentState = _automat.GetStartState();
  66. }
  67. }
  68. }
  69. }