Кролевец Никита Андреевич#1
Open
cheeseandmacaroni wants to merge 4 commits into
Open
Conversation
Comment on lines
+104
to
+118
| else if ((expression.substr(index, 3) == "sin" || expression.substr(index, 3) == "cos") && (state == LexemeState::un_op || state == LexemeState::left_bracket || state == LexemeState::bin_op || state == LexemeState::start)) | ||
| { | ||
| if (expression.substr(index, 3) == "sin") | ||
| { | ||
| value = "sin"; | ||
| index += 3; | ||
| return Lexeme({ TypeLexeme::un_op, value, Priority::high }); | ||
| } | ||
| else if (expression.substr(index, 3) == "cos") | ||
| { | ||
| value = "cos"; | ||
| index += 3; | ||
| return Lexeme({ TypeLexeme::un_op, value, Priority::high }); | ||
| } | ||
| } |
Owner
There was a problem hiding this comment.
Зачем вам здесь использовать state?
Я говорю про эту часть условия:
state == LexemeState::un_op || state == LexemeState::left_bracket || state == LexemeState::bin_op || state == LexemeState::start
Казалось бы её можно просто удалить
Comment on lines
+50
to
+118
| Lexeme convertToLexeme(const std::string &expression, size_t &index, LexemeState state) | ||
| { | ||
| std::string value; | ||
| if (expression[index] >= '0' && expression[index] <= '9') | ||
| { | ||
| if (expression[index + 1] == '0') | ||
| { | ||
| value.push_back('0'); | ||
| ++index; | ||
| return Lexeme({ TypeLexeme::number, value, Priority::number }); | ||
| } | ||
| while (expression[index] >= '0' && expression[index] <= '9') | ||
| { | ||
| value.push_back(expression[index]); | ||
| ++index; | ||
| } | ||
| return Lexeme({ TypeLexeme::number, value, Priority::number }); | ||
| } | ||
| else if (expression[index] == '*' || expression[index] == '/') | ||
| { | ||
| value.push_back(expression[index]); | ||
| index++; | ||
| return Lexeme({ TypeLexeme::bin_op, value, Priority::mid }); | ||
| } | ||
| else if (expression[index] == '^') | ||
| { | ||
| value.push_back(expression[index]); | ||
| index++; | ||
| return Lexeme({ TypeLexeme::bin_op, value, Priority::high }); | ||
| } | ||
| else if (expression[index] == '+' || (expression[index] == '-' && (state == LexemeState::variable || state == LexemeState::number || state == LexemeState::right_bracket))) | ||
| { | ||
| value.push_back(expression[index]); | ||
| index++; | ||
| return Lexeme({ TypeLexeme::bin_op, value, Priority::low }); | ||
| } | ||
| else if (expression[index] == '-' && (state == LexemeState::left_bracket || state == LexemeState::start)) | ||
| { | ||
| value.push_back(expression[index]); | ||
| index++; | ||
| return Lexeme({ TypeLexeme::un_op, value, Priority::high }); | ||
| } | ||
| else if (expression[index] == '(' && (state == LexemeState::bin_op || state == LexemeState::start || state == LexemeState::un_op)) | ||
| { | ||
| value.push_back(expression[index]); | ||
| index++; | ||
| return Lexeme({ TypeLexeme::left_bracket, value, Priority::bracket }); | ||
| } | ||
| else if (expression[index] == ')' && (state == LexemeState::variable || state == LexemeState::number || state == LexemeState::right_bracket)) | ||
| { | ||
| value.push_back(expression[index]); | ||
| index++; | ||
| return Lexeme({ TypeLexeme::right_bracket, value, Priority::bracket }); | ||
| } | ||
| else if ((expression.substr(index, 3) == "sin" || expression.substr(index, 3) == "cos") && (state == LexemeState::un_op || state == LexemeState::left_bracket || state == LexemeState::bin_op || state == LexemeState::start)) | ||
| { | ||
| if (expression.substr(index, 3) == "sin") | ||
| { | ||
| value = "sin"; | ||
| index += 3; | ||
| return Lexeme({ TypeLexeme::un_op, value, Priority::high }); | ||
| } | ||
| else if (expression.substr(index, 3) == "cos") | ||
| { | ||
| value = "cos"; | ||
| index += 3; | ||
| return Lexeme({ TypeLexeme::un_op, value, Priority::high }); | ||
| } | ||
| } |
Owner
There was a problem hiding this comment.
Знать LexemeState state для convertToLexeme в целом необязательно (кроме одного случая). LexemeState state нужен только для того, чтобы отделить унарный минус от бинарного. Во всех остальных случаях, лексема однозначно опредеяется своим символом.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.