From fb99662ae8da4cd00b70d481bb2d2ed0482f1eda Mon Sep 17 00:00:00 2001 From: Andrea Bontempi Date: Thu, 6 Aug 2015 22:40:48 +0200 Subject: [PATCH] Change name and architecture --- .gitignore | 11 +++- CMakeLists.txt | 2 +- RamInstruction.cpp | 85 ++++++++++++++++++++++++++++ ramInterpreter.h => RamInstruction.h | 12 ++-- main.cpp | 4 +- ramInterpreter.cpp | 69 ---------------------- 6 files changed, 102 insertions(+), 81 deletions(-) create mode 100644 RamInstruction.cpp rename ramInterpreter.h => RamInstruction.h (56%) delete mode 100644 ramInterpreter.cpp diff --git a/.gitignore b/.gitignore index 7027c8c..4cb9fb2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,9 @@ -build/* -.kdev4/* -nbproject/* *.kdev4 +*.tar.xz +*.deb +*.rpm +.kdev4/* +build/* +src/* +pkg/* +nbproject/* diff --git a/CMakeLists.txt b/CMakeLists.txt index aa1bdbd..20a6b0b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(BOOST_LIBS program_options system) 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}) diff --git a/RamInstruction.cpp b/RamInstruction.cpp new file mode 100644 index 0000000..d97b40d --- /dev/null +++ b/RamInstruction.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#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 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]; +} + diff --git a/ramInterpreter.h b/RamInstruction.h similarity index 56% rename from ramInterpreter.h rename to RamInstruction.h index 4c4c911..a877633 100644 --- a/ramInterpreter.h +++ b/RamInstruction.h @@ -9,19 +9,19 @@ struct instruction_t { std::string noun; }; -class RamInterpreter { +class RamInstruction { private: std::vector code; std::map label; - std::vector input; - std::vector output; - std::map registers; - int program_counter = 0; + int lineNumber; public: - RamInterpreter(std::string filename); + RamInstruction(std::string filename); + int getLineNumber(); + int getLabelPosition(std::string name); + instruction_t getInstruction(int line); }; diff --git a/main.cpp b/main.cpp index bdf041d..a2063b0 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ #include -#include "ramInterpreter.h" +#include "RamInstruction.h" int main(int argc, char **argv) { - RamInterpreter a("text.ram"); + RamInstruction a("text.ram"); return 0; } diff --git a/ramInterpreter.cpp b/ramInterpreter.cpp deleted file mode 100644 index 902e70c..0000000 --- a/ramInterpreter.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include -#include -#include -#include -#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 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(); -}