PageRenderTime 44ms CodeModel.GetById 20ms app.highlight 4ms RepoModel.GetById 18ms app.codeStats 0ms

/TuringMachine/CompilerModule/Compiler.cs

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