public LetMachineState GetNext(Token token) { LetMachineState nextState = this.CurrentState; LetStateTransition transition = new LetStateTransition(CurrentState, token.Type); if (this.CurrentState == LetMachineState.LET && (token.Type == TokenType.VAR || token.Type == TokenType.ARRAY_ELEMENT)) { this.variable = token; } if (!(this.CurrentState == LetMachineState.EQUALS && token.Type != TokenType.END) && !transitions.TryGetValue(transition, out nextState)) { throw new Exception("LET: Invalid transition: " + CurrentState + " -> " + nextState + "\n" + token.Text + " " + token.Type); } if (this.CurrentState == LetMachineState.EQUALS && token.Type == TokenType.END && nextState == LetMachineState.START) { this.exp.Reset(); this.variables.variableToIndex[this.variable.Text] = this.variables.variableCounter++; this.command.GenerateStoreInMemInstructions(this.variables.variableToIndex[this.variable.Text]); } else if (this.CurrentState == LetMachineState.EQUALS) { this.exp.MoveToNextState(token); } Console.WriteLine("LET: " + this.CurrentState + " -> " + nextState + ": " + token.Text); return(nextState); }
public LetStateMachine(VariableTable variables, FileManager fileManager) { CurrentState = LetMachineState.START; transitions = new Dictionary <LetStateTransition, LetMachineState> { { new LetStateTransition(LetMachineState.START, TokenType.END), LetMachineState.START }, { new LetStateTransition(LetMachineState.START, TokenType.LET), LetMachineState.LET }, { new LetStateTransition(LetMachineState.LET, TokenType.VAR), LetMachineState.VAR_FROM_INDEX }, { new LetStateTransition(LetMachineState.LET, TokenType.ARRAY_ELEMENT), LetMachineState.VAR_FROM_INDEX }, { new LetStateTransition(LetMachineState.VAR_FROM_INDEX, TokenType.EQUALS), LetMachineState.EQUALS }, { new LetStateTransition(LetMachineState.EQUALS, TokenType.END), LetMachineState.START }, }; this.variables = variables; this.command = new LetCommand(fileManager); this.exp = new ArithmeticStateMachine(variables, fileManager); }
public LetStateTransition(LetMachineState currentState, TokenType token) { this.CurrentState = currentState; this.Token = token; }
public void MoveToNextState(Token token) { LetMachineState nextState = GetNext(token); this.CurrentState = nextState; }