private void ProcessInnerParenthesis(ISymbol <string, string> readed, ISymbolReader <string, string> reader) { var parenthesisStatus = 1; this.currentReadingValues.Add(readed); do { if (reader.IsAtEOF()) { throw new OdmpProblemException("Matriz set is in a wrong format."); } else { readed = reader.Get(); if (this.elementsDelimiterTypes.ContainsKey(readed.SymbolType)) { this.ProcessDelimiteres(readed, reader); } else if (readed.SymbolType == "left_parenthesis") { ++parenthesisStatus; } else if (readed.SymbolType == "right_parenthesis") { --parenthesisStatus; } this.currentReadingValues.Add(readed); } } while (parenthesisStatus > 0); }
/// <summary> /// Ignora todos os símbolos vazios consecutivos. /// </summary> /// <param name="symbolReader">O leito de símbolos.</param> private void IgnoreVoids(ISymbolReader <string, string> symbolReader) { ISymbol <string, string> symbol = symbolReader.Peek(); while (voids.Contains(symbol.SymbolType)) { symbolReader.Get(); symbol = symbolReader.Peek(); } }
private void IgnoreVoids(ISymbolReader <string, string> reader) { var readed = reader.Peek(); while (readed.SymbolType == "blancks" || readed.SymbolType == "carriage_return" || readed.SymbolType == "new_line") { reader.Get(); readed = reader.Peek(); } }
/// <summary> /// Função de transição "enquanto" - estado 2. /// </summary> /// <param name="reader">O leitor de símbolos.</param> /// <returns>O próximo estado.</returns> private IState <string, string> WhileTransition(ISymbolReader <string, string> reader) { this.IgnoreVoids(reader); var readed = reader.Peek(); var topScopeDefinition = this.scopeDefinitionStack.Pop(); if (readed.SymbolType == "eof") { this.currentResult.InterpreterMessageStatus = EMathematicsInterpreterStatus.WAITING; this.currentResult.InterpreterResultMessage = "Incomplete expression."; return(this.stateList[1]); } else if (readed.SymbolType == "left_brace") { reader.Get(); topScopeDefinition.ExecuteInScope = topScopeDefinition.ConditionExpression.AssertCondition(); topScopeDefinition.HasBraces = true; if (topScopeDefinition.ExecuteInScope) { topScopeDefinition.ScopeStartMemento = (reader as MementoSymbolReader <CharSymbolReader <string>, string, string>).SaveToMemento(); this.scopeDefinitionStack.Push(topScopeDefinition); } return(this.stateList[0]); } else if (readed.SymbolType == "semi_colon") { topScopeDefinition.ExecuteInScope = topScopeDefinition.ConditionExpression.AssertCondition(); if (topScopeDefinition.ExecuteInScope) { this.scopeDefinitionStack.Push(topScopeDefinition); return(this.stateList[2]); } else { return(this.stateList[0]); } } else { topScopeDefinition.ExecuteInScope = topScopeDefinition.ConditionExpression.AssertCondition(); if (topScopeDefinition.ExecuteInScope) { topScopeDefinition.ScopeStartMemento = (reader as MementoSymbolReader <CharSymbolReader <string>, string, string>).SaveToMemento(); this.scopeDefinitionStack.Push(topScopeDefinition); return(this.stateList[0]); } else { this.scopeDefinitionStack.Push(topScopeDefinition); return(this.stateList[11]); } } }
/// <summary> /// A transição inicial - estado 0. /// </summary> /// <param name="reader">O leitor de símbolos.</param> /// <returns>O próximo estado.</returns> private IState <string, string> StartState(ISymbolReader <string, string> reader) { this.IgnoreVoids(reader); var readed = reader.Get(); if (reader.IsAtEOF()) { throw new OdmpProblemException("Unexpected end of file."); } if (readed.SymbolType == "left_bracket") { return(this.states[2]); } return(this.states[0]); }
private void ProcessDelimiteres(ISymbol <string, string> readed, ISymbolReader <string, string> reader) { var closeDelimiters = this.elementsDelimiterTypes[readed.SymbolType]; this.currentReadingValues.Add(readed); do { if (reader.IsAtEOF()) { throw new OdmpProblemException("Matriz set is in a wrong format."); } else { readed = reader.Get(); this.currentReadingValues.Add(readed); } } while (!closeDelimiters.Contains(readed.SymbolType)); }
/// <summary> /// Função de transição "enquanto se" - estado 5. /// </summary> /// <param name="reader">O leitor de símbolos.</param> /// <returns>O próximo estado.</returns> private IState <string, string> WhileIfConditionTransition(ISymbolReader <string, string> reader) { this.IgnoreVoids(reader); var readed = reader.Get(); if (readed.SymbolType == "eof") { this.currentResult.InterpreterMessageStatus = EMathematicsInterpreterStatus.WAITING; this.currentResult.InterpreterResultMessage = "Sentence is incomplete."; return(this.stateList[1]); } else if (readed.SymbolType == "left_parenthesis") { return(this.stateList[10]); } else { return(this.SetupErrorAndQuit("Expecting parenthesis after word while.")); } }
/// <summary> /// A função correspondente à transição de vírgula - estado 6. /// </summary> /// <param name="reader">O leitor.</param> /// <returns>O próximo estado.</returns> private IState <string, ELambdaExpressionWordType> CommaTransition( ISymbolReader <string, ELambdaExpressionWordType> reader) { if (reader.IsAtEOF()) { } else { var readed = reader.Get(); switch (readed.SymbolType) { case ELambdaExpressionWordType.ALPHA: break; case ELambdaExpressionWordType.DELIMITER: break; case ELambdaExpressionWordType.CLOSE_PARENTHESIS: break; case ELambdaExpressionWordType.COMMA: break; case ELambdaExpressionWordType.NUMERIC: break; case ELambdaExpressionWordType.OPEN_PARENTHESIS: break; case ELambdaExpressionWordType.OTHER: break; case ELambdaExpressionWordType.SPACE: break; } } throw new NotImplementedException(); }
/// <summary> /// A funçáo correspondente à transição inicial - estado 0. /// </summary> /// <param name="reader">O leitor.</param> /// <returns>O próximo estado.</returns> private IState <string, ELambdaExpressionWordType> StartTransition( ISymbolReader <string, ELambdaExpressionWordType> reader) { if (reader.IsAtEOF()) { // Termina de forma vazia. return(this.stateList[1]); } else { var readed = reader.Get(); switch (readed.SymbolType) { case ELambdaExpressionWordType.ALPHA: break; case ELambdaExpressionWordType.DELIMITER: break; case ELambdaExpressionWordType.CLOSE_PARENTHESIS: break; case ELambdaExpressionWordType.COMMA: break; case ELambdaExpressionWordType.NUMERIC: break; case ELambdaExpressionWordType.OPEN_PARENTHESIS: break; case ELambdaExpressionWordType.OTHER: break; } return(this.stateList[0]); } }
/// <summary> /// Estado de leitura da colecção de matrizes - estado 2. /// </summary> /// <param name="reader">O leitor de símbolos.</param> /// <returns>O próximo estado.</returns> private IState <string, string> InsideBracketsState(ISymbolReader <string, string> reader) { this.IgnoreVoids(reader); var readed = reader.Get(); if (reader.IsAtEOF()) { return(this.states[1]); } if (readed.SymbolType == "left_parenthesis") { return(this.states[3]); } else if (readed.SymbolType == "right_bracket") { return(this.states[1]); } else { throw new OdmpProblemException(string.Format("Unexpected symbol {0}.", readed.SymbolValue)); } }
/// <summary> /// Estado da leitura do valor - estado 4. /// </summary> /// <param name="reader">O leitor de símbolos.</param> /// <returns>O próximo estado.</returns> private IState <string, string> ValueState(ISymbolReader <string, string> reader) { this.IgnoreVoids(reader); var readed = reader.Get(); if (reader.IsAtEOF()) { throw new OdmpProblemException("Unexpected end of file."); } this.currentReadingValues.Clear(); if (this.elementsDelimiterTypes.ContainsKey(readed.SymbolType)) { this.ProcessDelimiteres(readed, reader); } else if (readed.SymbolType == "left_parenthesis") { this.ProcessInnerParenthesis(readed, reader); } else { this.currentReadingValues.Add(readed); } var error = new LogStatus <string, EParseErrorLevel>(); var value = this.objectElementsReader.Parse(this.currentReadingValues.ToArray(), error); if (error.HasLogs(EParseErrorLevel.ERROR)) { throw new OdmpProblemException(string.Format("Can't parse value {0}.", readed.SymbolValue)); } this.SetValueInMatrixSet(this.componentCoord, this.lineCoord, this.columnCoord, value); this.coordState = 0; return(this.states[2]); }
/// <summary> /// Função de transição "enquanto se interior" - estado 10. /// </summary> /// <param name="reader">O leitor de símbolos.</param> /// <returns>O próximo estado.</returns> private IState <string, string> InsideWhileIfConditionTransition(ISymbolReader <string, string> reader) { var readed = reader.Get(); if (readed.SymbolType == "eof") { this.currentResult.InterpreterMessageStatus = EMathematicsInterpreterStatus.WAITING; this.currentResult.InterpreterResultMessage = "Expecting expression."; return(this.stateList[1]); } else if (readed.SymbolType == "left_parenthesis") { this.openParenthesisStack.Push("left_parenthesis"); this.currentItem += readed.SymbolValue; } else if (readed.SymbolType == "right_parenthesis") { if (this.openParenthesisStack.Count == 0) { var topScopeDefinition = this.scopeDefinitionStack.Pop(); var readedExpression = this.expressionReader.Parse(new StringSymbolReader(new StringReader(this.currentItem), false)); this.currentItem = string.Empty; if (readedExpression.MathematicsType == EMathematicsType.CONDITION) { topScopeDefinition.ConditionExpression = readedExpression as BooleanConditionMathematicsObject; this.scopeDefinitionStack.Push(topScopeDefinition); if (topScopeDefinition.ScopeType == EScopeType.WHILE) { return(this.stateList[2]); } else if (topScopeDefinition.ScopeType == EScopeType.IF_ELSE) { return(this.stateList[3]); } } else { this.SetupErrorAndQuit("Expecting a condition expression after while."); return(this.stateList[1]); } } else { var topOperator = this.openParenthesisStack.Pop(); if (topOperator != "left_parenthesis") { this.openParenthesisStack.Push(topOperator); } this.currentItem += readed.SymbolValue; } } else if (readed.SymbolType == "left_bracket") { this.currentItem += readed.SymbolValue; this.openParenthesisStack.Push("left_bracket"); } else if (readed.SymbolType == "right_bracket") { var topOperator = this.openParenthesisStack.Pop(); if (topOperator != "left_bracket") { this.openParenthesisStack.Push(topOperator); } this.currentItem += readed.SymbolValue; } else if (readed.SymbolType == "left_brace") { this.currentItem += readed.SymbolValue; this.openParenthesisStack.Push("left_brace"); } else if (readed.SymbolType == "right_brace") { var topOperator = this.openParenthesisStack.Pop(); if (topOperator != "left_brace") { this.openParenthesisStack.Push(topOperator); } this.currentItem += readed.SymbolValue; } else { this.currentItem += readed.SymbolValue; } return(this.stateList[10]); }
/// <summary> /// Função de transição inicial - estado 0. /// </summary> /// <param name="reader">O leitor de símbolos.</param> /// <returns>O próximo estado.</returns> private IState <string, string> StartTransition(ISymbolReader <string, string> reader) { var readed = reader.Get(); var topScopeDefinition = this.scopeDefinitionStack.Pop(); if (readed.SymbolType == "eof") { if (!string.IsNullOrEmpty(this.currentItem)) { var symbolReader = new StringSymbolReader(new StringReader(this.currentItem), false); this.lastExpressionResult = this.expressionReader.Parse(symbolReader); this.currentItem = string.Empty; if (this.lastExpressionResult.IsFunctionObject) { ((AMathematicsFunctionObject)this.lastExpressionResult).Evaulate(); } } if (topScopeDefinition.ScopeType != EScopeType.MAIN) { this.currentResult.InterpreterMessageStatus = EMathematicsInterpreterStatus.WAITING; this.currentResult.InterpreterResultMessage = "Sentence is incomplete. Scope delimiters don't match."; if (topScopeDefinition.ScopeType == EScopeType.WHILE) { var whileCondition = topScopeDefinition.ConditionExpression; if (whileCondition.AssertCondition()) { (reader as StringSymbolReader).RestoreToMemento(topScopeDefinition.ScopeStartMemento); } } else if (topScopeDefinition.ScopeType == EScopeType.FOR) { var updateExpression = topScopeDefinition.UpdateExpression; updateExpression.Evaulate(); var forCondition = topScopeDefinition.ConditionExpression; if (forCondition.AssertCondition()) { (reader as StringSymbolReader).RestoreToMemento(topScopeDefinition.ScopeStartMemento); } } this.scopeDefinitionStack.Push(topScopeDefinition); return(this.stateList[1]); } else { this.currentResult.InterpreterMessageStatus = EMathematicsInterpreterStatus.COMPLETED; this.currentResult.InterpreterResultMessage = this.lastExpressionResult.ToString(); return(this.stateList[1]); } } else if (readed.SymbolType == "semi_colon") { var symbolReader = new StringSymbolReader(new StringReader(this.currentItem), false); this.lastExpressionResult = this.expressionReader.Parse(symbolReader); this.currentItem = string.Empty; if (this.lastExpressionResult.IsFunctionObject) { ((AMathematicsFunctionObject)this.lastExpressionResult).Evaulate(); } if (topScopeDefinition.ScopeType == EScopeType.WHILE) { var whileCondition = topScopeDefinition.ConditionExpression; if (whileCondition.AssertCondition()) { (reader as StringSymbolReader).RestoreToMemento(topScopeDefinition.ScopeStartMemento); } } else if (topScopeDefinition.ScopeType == EScopeType.FOR) { var updateExpression = topScopeDefinition.UpdateExpression; updateExpression.Evaulate(); var forCondition = topScopeDefinition.ConditionExpression; if (forCondition.AssertCondition()) { (reader as StringSymbolReader).RestoreToMemento(topScopeDefinition.ScopeStartMemento); } } this.scopeDefinitionStack.Push(topScopeDefinition); } else if (readed.SymbolType == "left_brace") { var innerScope = new ScopeDefinition() { ScopeType = EScopeType.INNER }; this.scopeDefinitionStack.Push(topScopeDefinition); this.scopeDefinitionStack.Push(innerScope); } else if (readed.SymbolType == "right_brace") { if (topScopeDefinition.ScopeType == EScopeType.MAIN) { return(this.SetupErrorAndQuit("Scope delimiters don't match.")); } else if (topScopeDefinition.ScopeType == EScopeType.INNER) { return(this.stateList[0]); } else if (topScopeDefinition.ScopeType == EScopeType.WHILE) { var whileCondition = topScopeDefinition.ConditionExpression; if (whileCondition.AssertCondition()) { (reader as StringSymbolReader).RestoreToMemento(topScopeDefinition.ScopeStartMemento); this.scopeDefinitionStack.Push(topScopeDefinition); } } else if (topScopeDefinition.ScopeType == EScopeType.FOR) { var updateExpression = topScopeDefinition.UpdateExpression; updateExpression.Evaulate(); var forCondition = topScopeDefinition.ConditionExpression; if (forCondition.AssertCondition()) { (reader as StringSymbolReader).RestoreToMemento(topScopeDefinition.ScopeStartMemento); this.scopeDefinitionStack.Push(topScopeDefinition); } } } else if (readed.SymbolType == "while") { this.scopeDefinitionStack.Push(topScopeDefinition); this.scopeDefinitionStack.Push(new ScopeDefinition() { ScopeType = EScopeType.WHILE }); return(this.stateList[5]); } else if (readed.SymbolType == "if") { this.scopeDefinitionStack.Push(topScopeDefinition); this.scopeDefinitionStack.Push(new ScopeDefinition() { ScopeType = EScopeType.IF_ELSE }); return(this.stateList[5]); } else if (readed.SymbolType == "for") { this.scopeDefinitionStack.Push(topScopeDefinition); this.scopeDefinitionStack.Push(new ScopeDefinition() { ScopeType = EScopeType.FOR }); return(this.stateList[7]); } else if (readed.SymbolType == "else") { this.scopeDefinitionStack.Push(topScopeDefinition); return(this.SetupErrorAndQuit("Unexpected word else.")); } else { this.scopeDefinitionStack.Push(topScopeDefinition); this.currentItem += readed.SymbolValue; } return(this.stateList[0]); }
/// <summary> /// Estado de leitura das coordenadas - estado 3. /// </summary> /// <param name="reader">O leitor de símbolos.</param> /// <returns>O próximo estado.</returns> private IState <string, string> InsideParenthesisState(ISymbolReader <string, string> reader) { this.IgnoreVoids(reader); var readed = reader.Get(); if (reader.IsAtEOF()) { throw new OdmpProblemException("Unexpected end of file."); } this.currentReadingValues.Clear(); if (readed.SymbolType == "right_parenthesis") { if (this.coordState != 3) { throw new OdmpProblemException(string.Format( "Wrong number, {0}, of coordinates. Expecting 3.", this.coordState)); } this.coordState = 0; return(this.states[4]); } else if (readed.SymbolType == "left_parenthesis") { this.ProcessInnerParenthesis(readed, reader); } else if (this.elementsDelimiterTypes.ContainsKey(readed.SymbolType)) { this.ProcessDelimiteres(readed, reader); } else { this.currentReadingValues.Add(readed); } var error = new LogStatus <string, EParseErrorLevel>(); switch (this.coordState) { case 0: this.componentCoord = this.componentElementsReader.Parse(this.currentReadingValues.ToArray(), error); if (error.HasLogs(EParseErrorLevel.ERROR)) { throw new OdmpProblemException(string.Format("Can't parse component coordinate: {0}.", readed.SymbolValue)); } break; case 1: this.lineCoord = this.lineElementsReader.Parse(this.currentReadingValues.ToArray(), error); if (error.HasLogs(EParseErrorLevel.ERROR)) { throw new OdmpProblemException(string.Format("Can't parse line coordinate: {0}.", readed.SymbolValue)); } break; case 2: this.columnCoord = this.columnElementsReader.Parse(this.currentReadingValues.ToArray(), error); if (error.HasLogs(EParseErrorLevel.ERROR)) { throw new OdmpProblemException(string.Format("Can't parse column coordinate: {0}.", readed.SymbolValue)); } break; default: throw new OdmpProblemException("An internal error has occured."); } ++this.coordState; return(this.states[3]); }