mirror of
https://github.com/avinal/The-Hack-Assembler.git
synced 2026-01-10 07:08:32 +05:30
Minor changes
This commit is contained in:
@@ -1,7 +1,43 @@
|
|||||||
#include "../include/parser.hpp"
|
#include "../include/parser.hpp"
|
||||||
|
|
||||||
|
inline std::string trim(std::string str)
|
||||||
|
{
|
||||||
|
str.erase(0, str.find_first_not_of(" \r\t\v\n")); //prefixing spaces
|
||||||
|
str.erase(std::min(str.find_first_of(" /\r\t\v\n"), str.size())); //surfixing spaces
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
void parser::create_table()
|
||||||
|
{
|
||||||
|
std::ifstream firstpass(filename);
|
||||||
|
std::string instruction;
|
||||||
|
int count = -1;
|
||||||
|
if (firstpass.is_open())
|
||||||
|
{
|
||||||
|
while (std::getline(firstpass, instruction))
|
||||||
|
{
|
||||||
|
instruction = trim(instruction);
|
||||||
|
if (!instruction.empty())
|
||||||
|
{
|
||||||
|
if (command_type(instruction) == 'L')
|
||||||
|
{
|
||||||
|
instruction.erase(0, 1);
|
||||||
|
instruction.erase(instruction.size() - 1);
|
||||||
|
table.add_entry(instruction, count + 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
firstpass.close();
|
||||||
|
}
|
||||||
|
|
||||||
void parser::assemble()
|
void parser::assemble()
|
||||||
{
|
{
|
||||||
|
create_table();
|
||||||
std::ifstream asmfile(filename);
|
std::ifstream asmfile(filename);
|
||||||
int pos = filename.find(".");
|
int pos = filename.find(".");
|
||||||
filename.erase(pos);
|
filename.erase(pos);
|
||||||
@@ -14,46 +50,47 @@ void parser::assemble()
|
|||||||
{
|
{
|
||||||
while (std::getline(asmfile, instruction))
|
while (std::getline(asmfile, instruction))
|
||||||
{
|
{
|
||||||
|
instruction = trim(instruction);
|
||||||
std::string converted = "";
|
std::string converted = "";
|
||||||
std::string binary;
|
std::string binary;
|
||||||
instruction.erase(instruction.size() - 1);
|
if (!instruction.empty())
|
||||||
if (instruction.empty() ||
|
|
||||||
instruction.substr(0, 2) == "//")
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
switch (command_type(instruction))
|
switch (command_type(instruction))
|
||||||
{
|
{
|
||||||
case 'A':
|
case 'A':
|
||||||
{
|
{
|
||||||
count_ins++;
|
|
||||||
converted += "0";
|
converted += "0";
|
||||||
int address;
|
int address;
|
||||||
instruction = instruction.substr(1);
|
instruction = instruction.substr(1);
|
||||||
if (std::all_of(instruction.begin(),instruction.end(),::isdigit)){
|
if (std::all_of(instruction.begin(), instruction.end(), ::isdigit))
|
||||||
address = std::stoi(instruction);
|
|
||||||
}else
|
|
||||||
{
|
{
|
||||||
|
address = std::stoi(instruction);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
binary = std::bitset<15>(std::stoi(instruction.substr(1))).to_string();
|
{
|
||||||
|
if (!table.contains(instruction))
|
||||||
|
{
|
||||||
|
address = table.available();
|
||||||
|
table.add_entry(instruction, address);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
address = table.get_address(instruction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
binary = std::bitset<15>(address).to_string();
|
||||||
converted += binary;
|
converted += binary;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'C':
|
case 'C':
|
||||||
{
|
{
|
||||||
count_ins++;
|
|
||||||
auto token = split(instruction);
|
auto token = split(instruction);
|
||||||
binary = comp(token[1]) + dest(token[0]) + jump(token[2]);
|
binary = comp(token[1]) + dest(token[0]) + jump(token[2]);
|
||||||
converted += "111" + binary;
|
converted += "111" + binary;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'L':
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
|
continue;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (genfile.is_open())
|
if (genfile.is_open())
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
#include "../include/symbol_table.hpp"
|
#include "../include/symbol_table.hpp"
|
||||||
|
|
||||||
|
int symbol_table::available()
|
||||||
|
{
|
||||||
|
this->next++;
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
void symbol_table::add_entry(std::string symbol, int address)
|
void symbol_table::add_entry(std::string symbol, int address)
|
||||||
{
|
{
|
||||||
symbols.insert(std::make_pair(symbol, address));
|
symbols.insert(std::make_pair(symbol, address));
|
||||||
@@ -15,7 +21,8 @@ int symbol_table::get_address(std::string symbol)
|
|||||||
if (contains(symbol))
|
if (contains(symbol))
|
||||||
{
|
{
|
||||||
return symbols[symbol];
|
return symbols[symbol];
|
||||||
}else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user