I have the following code: ~ enum class TokenType { Variable, Assign, Number, Plus,…
- User
I have the following code: ~ enum class TokenType { Variable, Assign, Number, Plus, Minus, Multiply, Divide, Sin, PI, T, t, OpenParen, CloseParen, Semicolon };
struct Token { TokenType type; std::string value; };
std::vector tokenize(const std::string &code) { std::vector tokens; std::string token; std::stringstream ss(code);
while (std::getline(ss, token, ';')) { std::stringstream line(token); std::string variable, value; if (std::getline(line, variable, '=')) { if (std::getline(line, value)) { variable.erase(variable.find_last_not_of(' ') + 1); // remove trailing spaces value.erase(0, value.find_first_not_of(' ')); // remove leading spaces tokens.push_back({TokenType::Variable, variable}); tokens.push_back({TokenType::Assign, "="}); std::stringstream valueStream(value); std::string token; while (valueStream >> token) { std::cout << "token: " << token << std::endl; if (token == "+") { tokens.push_back({TokenType::Plus, "+"}); } else if (token == "-") { tokens.push_back({TokenType::Minus, "-"}); } else if (token == "*") { tokens.push_back({TokenType::Multiply, "*"}); } else if (token == "/") { tokens.push_back({TokenType::Divide, "/"}); } else if (token == "sin") { tokens.push_back({TokenType::Sin, "sin"}); } else if (token == "PI") { tokens.push_back({TokenType::PI, "PI"}); } else if (token == "T") { tokens.push_back({TokenType::T, "T"}); } else if (token == "t") { tokens.push_back({TokenType::t, "t"}); } else if (token == "(") { tokens.push_back({TokenType::OpenParen, "("}); } else if (token == ")") { tokens.push_back({TokenType::CloseParen, ")"}); } else { tokens.push_back({TokenType::Number, token}); } } } } } return tokens;} ~ and I would like that the following string freq = 440 + sin(2pit); will return the following vector of items: freq, =, 440, +, sin, (, 2, *, pi, *, t, ). Do you know how I have to modify my code to obtain it?
- Assistant