mirror of
https://github.com/avinal/The-VM-to-HACK-Translator.git
synced 2026-01-10 23:28:33 +05:30
93 lines
2.2 KiB
C++
93 lines
2.2 KiB
C++
#include "parser.hpp"
|
|
|
|
c_type parser::command_type(std::string com)
|
|
{
|
|
const std::unordered_map<std::string, c_type> type_map({
|
|
{"add", C_ARITHMETIC},
|
|
{"sub", C_ARITHMETIC},
|
|
{"neg", C_ARITHMETIC},
|
|
{"eq", C_ARITHMETIC},
|
|
{"gt", C_ARITHMETIC},
|
|
{"lt", C_ARITHMETIC},
|
|
{"and", C_ARITHMETIC},
|
|
{"or", C_ARITHMETIC},
|
|
{"not", C_ARITHMETIC},
|
|
{"push", C_PUSH},
|
|
{"pop", C_POP},
|
|
{"label", C_LABEL},
|
|
{"goto", C_GOTO},
|
|
{"function", C_FUNCTION},
|
|
{"call", C_CALL},
|
|
{"return", C_RETURN},
|
|
{"if-goto", C_IF},
|
|
});
|
|
|
|
return type_map.at(com);
|
|
}
|
|
|
|
void parser::parse()
|
|
{
|
|
std::ifstream infile(input_file);
|
|
std::string vline;
|
|
if (infile.is_open())
|
|
{
|
|
while (std::getline(infile, vline))
|
|
{
|
|
vline = vutility::trim(vline);
|
|
if (!vline.empty())
|
|
{
|
|
std::vector<std::string> tokens = vutility::split(vline, ' ');
|
|
c_type c = command_type(tokens[0]);
|
|
|
|
switch (tokens.size())
|
|
{
|
|
case 1:
|
|
{
|
|
if (c == C_ARITHMETIC)
|
|
{
|
|
tokens.push_back(tokens[0]);
|
|
}
|
|
else
|
|
{
|
|
tokens.push_back("null");
|
|
}
|
|
tokens.push_back("0");
|
|
break;
|
|
}
|
|
case 2:
|
|
{
|
|
tokens.push_back("0");
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
if (tokens.size() == 3)
|
|
{
|
|
this->parsed.emplace_back(c, tokens[1], std::stoi(tokens[2]));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
infile.close();
|
|
}
|
|
|
|
std::vector<vmcommand> parser::get_commands()
|
|
{
|
|
return this->parsed;
|
|
}
|
|
|
|
bool parser::change_file(std::string name)
|
|
{
|
|
this->parsed.clear();
|
|
this->input_file = name;
|
|
|
|
if (this->parsed.size() == 0 && input_file == name)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
} |