SECOND
This commit is contained in:
parent
53341b8e4e
commit
c8aced2805
6 changed files with 104 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
build/*
|
||||
.kdev4/*
|
||||
nbproject/*
|
||||
*.kdev4
|
6
CMakeLists.txt
Normal file
6
CMakeLists.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
cmake_minimum_required(VERSION 2.6)
|
||||
project(simram)
|
||||
|
||||
add_executable(simram main.cpp)
|
||||
|
||||
install(TARGETS simram RUNTIME DESTINATION bin)
|
1
README.md
Normal file
1
README.md
Normal file
|
@ -0,0 +1 @@
|
|||
|
6
main.cpp
Normal file
6
main.cpp
Normal file
|
@ -0,0 +1,6 @@
|
|||
#include <iostream>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
std::cout << "Hello, world!" << std::endl;
|
||||
return 0;
|
||||
}
|
59
ramInterpreter.cpp
Normal file
59
ramInterpreter.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
#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.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
source_file.open(filename.c_str());
|
||||
|
||||
int count = 0;
|
||||
std::string buffer;
|
||||
while (!source_file.eof() && 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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Save instruction
|
||||
std::vector<int> temp;
|
||||
boost::algorithm::split(temp, buffer, boost::algorithm::is_any_of(" "));
|
||||
|
||||
instruction_t instruction;
|
||||
instruction.verb = temp[0];
|
||||
instruction.noun = temp[1];
|
||||
|
||||
code.push_back(instruction);
|
||||
|
||||
count++;
|
||||
|
||||
}
|
||||
|
||||
source_file.close();
|
||||
}
|
28
ramInterpreter.h
Normal file
28
ramInterpreter.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
#ifndef RAMINT_H
|
||||
#define RAMINT_H
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
struct instruction_t {
|
||||
std::string verb;
|
||||
std::string noun;
|
||||
};
|
||||
|
||||
class RamInterpreter {
|
||||
|
||||
private:
|
||||
std::vector<instruction_t> code;
|
||||
std::map<std::string, int> label;
|
||||
std::vector<int> input;
|
||||
std::vector<int> output;
|
||||
std::map<int,int> registers;
|
||||
int program_counter = 0;
|
||||
|
||||
public:
|
||||
|
||||
RamInterpreter(std::string filename);
|
||||
|
||||
};
|
||||
|
||||
#endif //RAMINT_H
|
Loading…
Reference in a new issue