Skip to content

Instantly share code, notes, and snippets.

@ryanwarsaw
Created March 6, 2018 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanwarsaw/f4eb2a4c5bad684c7c032f395f9258b7 to your computer and use it in GitHub Desktop.
Save ryanwarsaw/f4eb2a4c5bad684c7c032f395f9258b7 to your computer and use it in GitHub Desktop.
#include <sstream>
#include <iostream>
#include "variable_analysis_module.h"
// TODO: Write a comment here.
void VariableAnalysisModule::ParseLine(std::string line, int line_number) {
ArrayList<std::string> result = Split(line, ' ');
std::string output;
// This would follow the format of a variable being defined as: "const <type> <name>;"
if (result.size() == 3 && result[0] == "const") {
std::string variable_name = result[2];
variable_name.pop_back(); // Remove semi-colon from the variable name.
if (!variable_name.empty() && !ispunct(variable_name[variable_name.size() - 1])) { // Make sure the variable isn't in a function call.
output = "Found uninitialized variable: " + variable_name;
} // else, DoNothing(); the variable is within a function call, we don't want it.
}
// This would follow the format of a variable being defined as: "<type> <name>;"
if (result.size() == 2 && result[0] != "return" && result[0] != "#include") {
std::string variable_name = result[1];
variable_name.pop_back(); // Remove semi-colon from the variable name.
if (!variable_name.empty() && !ispunct(variable_name[variable_name.size() - 1])) { // Make sure variable isn't in a function call.
output = "Found uninitialized variable: " + variable_name;
} // else, DoNothing(); the variable is within a function call, we don't want it.
}
if (!output.empty()) {
std::cout << "Line #" << line_number << ": " << output << std::endl;
}
}
// TODO: Write a comment here.
ArrayList<std::string> VariableAnalysisModule::Split(const std::string &line, char deliminator) {
ArrayList<std::string> tokens;
std::stringstream stream(line);
std::string token;
while (getline(stream, token, deliminator)) {
if (!token.empty()) tokens.Add(token);
}
return tokens;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment