diff --git a/CMakeLists.txt b/CMakeLists.txt index 6673f11..c1cf292 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 20) include_directories(include) add_library(r8asm_core src/r8asm_core.cc src/r8asm_asm.cc - src/r8asm_macro.cc) + src/r8asm_macro.cc src/r8asm_parse.cc) add_executable(r8asm_test tests/r8_test.cc) target_link_libraries(r8asm_test r8asm_core) diff --git a/include/r8asm/r8asm.hh b/include/r8asm/r8asm.hh index 9400301..c051533 100644 --- a/include/r8asm/r8asm.hh +++ b/include/r8asm/r8asm.hh @@ -2,6 +2,7 @@ #define R8ASM #include "r8asm/r8asm_asm.hh" // IWYU pragma: export -#include "r8asm_macro.hh" // IWYU pragma: export +#include "r8asm_macro.hh" // IWYU pragma: export +#include "r8asm_parse.hh" // IWYU pragma: export #endif \ No newline at end of file diff --git a/include/r8asm/r8asm_parse.hh b/include/r8asm/r8asm_parse.hh new file mode 100644 index 0000000..0f3dde9 --- /dev/null +++ b/include/r8asm/r8asm_parse.hh @@ -0,0 +1,17 @@ +#ifndef R8ASM_PARSE +#define R8ASM_PARSE + +#include +#include + +#define EMPTY_CHARS (" \t\n\r\f\v") + +struct r8asm_src { + std::vector> raw_src; +}; + +r8asm_src read_src(std::string filename); + +void include_src(r8asm_src); + +#endif \ No newline at end of file diff --git a/src/r8asm_parse.cc b/src/r8asm_parse.cc new file mode 100644 index 0000000..c3e2690 --- /dev/null +++ b/src/r8asm_parse.cc @@ -0,0 +1,49 @@ +#include "r8asm/r8asm_parse.hh" +#include +#include + +void r8asm_trim(std::string &str) { + auto start = str.find_first_not_of(EMPTY_CHARS); + + if (!(start == + std::string::npos)) { // The string isn't filled with empty chars. + auto tmp_end = str.find_last_not_of(EMPTY_CHARS); + str = str.substr(start, tmp_end - start + 1); + } else + str = ""; +} + +std::string r8asm_peel(std::string &str) { + r8asm_trim(str); + auto end = str.find_first_of(EMPTY_CHARS); + + if (end != std::string::npos) { + std::string tmp_str = str.substr(0, end); + str = str.substr(end); + r8asm_trim(str); + return (tmp_str); + } else { + std::string tmp_str = str; + str = ""; + return (tmp_str); + } +} + +r8asm_src read_src(std::string filename) { + r8asm_src out_src; + + std::ifstream ifile; + std::string line_buffer; + ifile.open(filename, std::ios::in); + while (std::getline(ifile, line_buffer)) { + auto comment = line_buffer.find_first_of(';'); + if (comment != std::string::npos) + line_buffer = line_buffer.substr(0, comment); + std::vector ins_buffer; + while (!line_buffer.empty()) + ins_buffer.push_back(r8asm_peel(line_buffer)); + out_src.raw_src.push_back(ins_buffer); + } + ifile.close(); + return (out_src); +} \ No newline at end of file diff --git a/tests/r8_test.cc b/tests/r8_test.cc index bfc942c..cd9b264 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -1,9 +1,6 @@ #include #include "r8asm/r8asm.hh" -#include "r8asm/r8asm_asm.hh" -#include "r8asm/r8asm_core.hh" -#include "r8asm/r8asm_macro.hh" int main() { std::vector ins = {