Change name and architecture

This commit is contained in:
Andrea Bontempi 2015-08-06 22:40:48 +02:00
parent f35ef667bc
commit fb99662ae8
6 changed files with 102 additions and 81 deletions

11
.gitignore vendored
View file

@ -1,4 +1,9 @@
build/*
.kdev4/*
nbproject/*
*.kdev4 *.kdev4
*.tar.xz
*.deb
*.rpm
.kdev4/*
build/*
src/*
pkg/*
nbproject/*

View file

@ -6,7 +6,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(BOOST_LIBS program_options system) set(BOOST_LIBS program_options system)
find_package(Boost COMPONENTS ${BOOST_LIBS} REQUIRED) find_package(Boost COMPONENTS ${BOOST_LIBS} REQUIRED)
add_executable(simram main.cpp ramInterpreter.h ramInterpreter.cpp) add_executable(simram main.cpp RamInstruction.h RamInstruction.cpp)
target_link_libraries(simram ${Boost_LIBRARIES}) target_link_libraries(simram ${Boost_LIBRARIES})

85
RamInstruction.cpp Normal file
View file

@ -0,0 +1,85 @@
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <fstream>
#include <vector>
#include <map>
#include "RamInstruction.h"
RamInstruction::RamInstruction(std::string filename) {
std::ifstream source_file;
source_file.open(filename.c_str());
this->lineNumber = 0;
std::string line;
while (std::getline(source_file, line)) {
// Remove all char after #
line = line.substr(0, line.find('#'));
// Remove all spaces preceding the instruction
while(std::isspace(*line.begin())) {
line.erase(line.begin());
}
// Remove all spaces that follow the instruction
while(std::isspace(*line.rbegin())) {
line.erase(line.length()-1);
}
// Check if there are a label
int find = line.find(':');
if(find != std::string::npos) {
std::string label_name = line.substr(0, find);
this->label[label_name] = this->lineNumber;
line = line.substr(find, std::string::npos);
// Remove all spaces preceding the instruction
while(std::isspace(*line.begin())) {
line.erase(line.begin());
}
}
// Skip if empty line
if(line.empty()) {
this->lineNumber++;
continue;
}
// Save instruction
std::vector<std::string> temp;
boost::algorithm::split(temp, line, boost::algorithm::is_any_of(" "));
if(temp.size() != 2) {
std::cerr << "Syntax error on line " << this->lineNumber+1 << ": " << line << std::endl;
std::abort();
}
instruction_t instruction;
instruction.verb = temp[0];
instruction.noun = temp[1];
this->code.push_back(instruction);
this->lineNumber++;
}
source_file.close();
}
int RamInstruction::getLineNumber() {
return this->lineNumber;
}
instruction_t RamInstruction::getInstruction(int line) {
return this->code[line];
}
int RamInstruction::getLabelPosition(std::string name) {
return this->label[name];
}

View file

@ -9,19 +9,19 @@ struct instruction_t {
std::string noun; std::string noun;
}; };
class RamInterpreter { class RamInstruction {
private: private:
std::vector<instruction_t> code; std::vector<instruction_t> code;
std::map<std::string, int> label; std::map<std::string, int> label;
std::vector<int> input; int lineNumber;
std::vector<int> output;
std::map<int,int> registers;
int program_counter = 0;
public: public:
RamInterpreter(std::string filename); RamInstruction(std::string filename);
int getLineNumber();
int getLabelPosition(std::string name);
instruction_t getInstruction(int line);
}; };

View file

@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include "ramInterpreter.h" #include "RamInstruction.h"
int main(int argc, char **argv) { int main(int argc, char **argv) {
RamInterpreter a("text.ram"); RamInstruction a("text.ram");
return 0; return 0;
} }

View file

@ -1,69 +0,0 @@
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <fstream>
#include <vector>
#include <map>
#include "ramInterpreter.h"
RamInterpreter::RamInterpreter(std::string filename) {
std::ifstream source_file;
source_file.open(filename.c_str());
int count = 0;
std::string buffer;
while (std::getline(source_file, buffer)) {
// Remove all char after #
buffer = buffer.substr(0, buffer.find('#'));
// Remove all spaces preceding the instruction
while(std::isspace(*buffer.begin())) {
buffer.erase(buffer.begin());
}
// Remove all spaces that follow the instruction
while(std::isspace(*buffer.rbegin())) {
buffer.erase(buffer.length()-1);
}
// Check if there are a label
int find = buffer.find(':');
if(find != std::string::npos) {
std::string label_name = buffer.substr(0, find);
label[label_name] = count;
buffer = buffer.substr(find, std::string::npos);
// Remove all spaces preceding the instruction
while(std::isspace(*buffer.begin())) {
buffer.erase(buffer.begin());
}
}
// Skip if empty line
if(buffer.empty()) {
count++;
continue;
}
// Save instruction
std::vector<std::string> temp;
boost::algorithm::split(temp, buffer, boost::algorithm::is_any_of(" "));
if(temp.size() != 2) {
std::cerr << "Syntax error on line " << count+1 << ": " << buffer << std::endl;
std::abort();
}
instruction_t instruction;
instruction.verb = temp[0];
instruction.noun = temp[1];
code.push_back(instruction);
count++;
}
source_file.close();
}