Some fix
This commit is contained in:
parent
c8aced2805
commit
f35ef667bc
4 changed files with 28 additions and 10 deletions
|
@ -1,6 +1,13 @@
|
|||
cmake_minimum_required(VERSION 2.6)
|
||||
project(simram)
|
||||
|
||||
add_executable(simram main.cpp)
|
||||
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)
|
||||
|
||||
target_link_libraries(simram ${Boost_LIBRARIES})
|
||||
|
||||
install(TARGETS simram RUNTIME DESTINATION bin)
|
||||
|
|
3
main.cpp
3
main.cpp
|
@ -1,6 +1,7 @@
|
|||
#include <iostream>
|
||||
#include "ramInterpreter.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
std::cout << "Hello, world!" << std::endl;
|
||||
RamInterpreter a("text.ram");
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -7,15 +7,14 @@
|
|||
|
||||
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("#"));
|
||||
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())) {
|
||||
|
@ -28,7 +27,7 @@ RamInterpreter::RamInterpreter(std::string filename) {
|
|||
}
|
||||
|
||||
// Check if there are a label
|
||||
int find = buffer.find(":");
|
||||
int find = buffer.find(':');
|
||||
if(find != std::string::npos) {
|
||||
std::string label_name = buffer.substr(0, find);
|
||||
label[label_name] = count;
|
||||
|
@ -41,10 +40,21 @@ RamInterpreter::RamInterpreter(std::string filename) {
|
|||
|
||||
}
|
||||
|
||||
// Skip if empty line
|
||||
if(buffer.empty()) {
|
||||
count++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Save instruction
|
||||
std::vector<int> temp;
|
||||
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];
|
||||
|
|
|
@ -25,4 +25,4 @@ public:
|
|||
|
||||
};
|
||||
|
||||
#endif //RAMINT_H
|
||||
#endif //RAMINT_H
|
||||
|
|
Loading…
Reference in a new issue