Beispiel #1
0
        public ReadStateMachine(VariableTable variables, FileManager fileManager)
        {
            CurrentState = ReadMachineState.START;
            transitions  = new Dictionary <ReadStateTransition, ReadMachineState>
            {
                { new ReadStateTransition(ReadMachineState.START, TokenType.READ), ReadMachineState.READ },

                { new ReadStateTransition(ReadMachineState.READ, TokenType.VAR), ReadMachineState.READ_ELEMENT },
                { new ReadStateTransition(ReadMachineState.READ, TokenType.ARRAY), ReadMachineState.READ_ELEMENT },

                { new ReadStateTransition(ReadMachineState.READ_ELEMENT, TokenType.COMMA), ReadMachineState.COMMA },
                { new ReadStateTransition(ReadMachineState.READ_ELEMENT, TokenType.END), ReadMachineState.START },

                { new ReadStateTransition(ReadMachineState.COMMA, TokenType.VAR), ReadMachineState.READ_ELEMENT },
                { new ReadStateTransition(ReadMachineState.COMMA, TokenType.ARRAY), ReadMachineState.READ_ELEMENT },
                { new ReadStateTransition(ReadMachineState.COMMA, TokenType.END), ReadMachineState.START }
            };

            this.variables = variables;
            this.command   = new ReadCommand(variables, fileManager);
        }
Beispiel #2
0
        public ReadMachineState GetNext(Token token)
        {
            ReadMachineState    nextState  = this.CurrentState;
            ReadStateTransition transition = new ReadStateTransition(CurrentState, token.Type);

            if (!transitions.TryGetValue(transition, out nextState))
            {
                throw new Exception("READ: Invalid transition: " + CurrentState + " -> " + nextState + "\n" + token.Text + " " + token.Type);
            }

            if (this.CurrentState == ReadMachineState.START)
            {
                this.variables.ResetReadVariables();
            }
            else if ((this.CurrentState == ReadMachineState.READ || this.CurrentState == ReadMachineState.COMMA) &&
                     (token.Type == TokenType.VAR || token.Type == TokenType.ARRAY))
            {
                this.command.ConsumeToken(token);
            }

            Console.WriteLine("READ: " + this.CurrentState + " -> " + nextState + ": " + token.Text);

            return(nextState);
        }
Beispiel #3
0
 public ReadStateTransition(ReadMachineState currentState, TokenType token)
 {
     this.CurrentState = currentState;
     this.Token        = token;
 }
Beispiel #4
0
        public void MoveToNextState(Token token)
        {
            ReadMachineState nextState = GetNext(token);

            this.CurrentState = nextState;
        }