Skip to content

Кролевец Никита Андреевич#1

Open
cheeseandmacaroni wants to merge 4 commits into
AleksandrPanov:masterfrom
cheeseandmacaroni:master
Open

Кролевец Никита Андреевич#1
cheeseandmacaroni wants to merge 4 commits into
AleksandrPanov:masterfrom
cheeseandmacaroni:master

Conversation

@cheeseandmacaroni
Copy link
Copy Markdown

No description provided.

Comment thread src/Source.cpp Outdated
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 });
}
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем вам здесь использовать state?
Я говорю про эту часть условия:
state == LexemeState::un_op || state == LexemeState::left_bracket || state == LexemeState::bin_op || state == LexemeState::start
Казалось бы её можно просто удалить

Comment thread src/Source.cpp
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 });
}
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Знать LexemeState state для convertToLexeme в целом необязательно (кроме одного случая). LexemeState state нужен только для того, чтобы отделить унарный минус от бинарного. Во всех остальных случаях, лексема однозначно опредеяется своим символом.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants