Beispiel #1
0
        public ArithmeticStateMachine(VariableTable variables, FileManager fileManager)
        {
            CurrentState = ArithmeticMachineState.START;
            transitions  = new Dictionary <ArithmeticStateTransition, ArithmeticMachineState>
            {
                { new ArithmeticStateTransition(ArithmeticMachineState.START, TokenType.END), ArithmeticMachineState.START },
                { new ArithmeticStateTransition(ArithmeticMachineState.START, TokenType.INT), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.START, TokenType.VAR), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.START, TokenType.ARRAY), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.START, TokenType.PLUS), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.START, TokenType.MINUS), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.START, TokenType.MULT), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.START, TokenType.DIV), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.START, TokenType.POWER), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.START, TokenType.OPENING_BRACES), ArithmeticMachineState.EXP },

                { new ArithmeticStateTransition(ArithmeticMachineState.EXP, TokenType.INT), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.EXP, TokenType.VAR), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.EXP, TokenType.ARRAY), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.EXP, TokenType.PLUS), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.EXP, TokenType.MINUS), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.EXP, TokenType.MULT), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.EXP, TokenType.DIV), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.EXP, TokenType.POWER), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.EXP, TokenType.OPENING_BRACES), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.EXP, TokenType.CLOSING_BRACES), ArithmeticMachineState.EXP },
                { new ArithmeticStateTransition(ArithmeticMachineState.EXP, TokenType.END), ArithmeticMachineState.START },
            };

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

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

            if (token.Type != TokenType.END)
            {
                this.command.ConsumeToken(token);
            }
            else
            {
                this.command.EndOfExpression();
            }

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

            return(nextState);
        }
Beispiel #3
0
        public void MoveToNextState(Token token)
        {
            ArithmeticMachineState nextState = GetNext(token);

            this.CurrentState = nextState;
        }
Beispiel #4
0
 public void Reset()
 {
     this.CurrentState = ArithmeticMachineState.START;
     this.command.EndOfExpression();
 }
Beispiel #5
0
 public ArithmeticStateTransition(ArithmeticMachineState currentState, TokenType token)
 {
     this.CurrentState = currentState;
     this.Token        = token;
 }