From 169bb3a756e50870d9b8c5bb2c5fbffebc954b7f Mon Sep 17 00:00:00 2001 From: starcells Date: Thu, 7 May 2026 13:35:11 +0800 Subject: [PATCH] [tmp]202605071335 --- include/r8asm/r8asm_parse.hh | 8 ++++++++ src/r8asm_macro.cc | 16 +++++++++------- src/r8asm_parse.cc | 29 +++++++++++++++++++++++++---- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/include/r8asm/r8asm_parse.hh b/include/r8asm/r8asm_parse.hh index 0f3dde9..ac83e4d 100644 --- a/include/r8asm/r8asm_parse.hh +++ b/include/r8asm/r8asm_parse.hh @@ -1,17 +1,25 @@ #ifndef R8ASM_PARSE #define R8ASM_PARSE +#include "r8asm/r8asm_core.hh" +#include #include +#include #include #define EMPTY_CHARS (" \t\n\r\f\v") +typedef size_t ins_type_sign; + struct r8asm_src { std::vector> raw_src; + std::vector> codeblocks; }; r8asm_src read_src(std::string filename); void include_src(r8asm_src); +std::vector r8asm_preprocess(r8asm_src &src); + #endif \ No newline at end of file diff --git a/src/r8asm_macro.cc b/src/r8asm_macro.cc index 34af0b8..4986477 100644 --- a/src/r8asm_macro.cc +++ b/src/r8asm_macro.cc @@ -28,6 +28,7 @@ R8Operand read_arg(std::vector &argv, R8Operand argf, std::vector expand_single_macro(std::string macro_name, std::vector argv) { std::vector tmp_tape; + std::vector macro_args; for (auto i : macros[macro_name].ins) { if (std::get_if(&i.op) || @@ -35,7 +36,7 @@ std::vector expand_single_macro(std::string macro_name, tmp_tape.push_back( {i.op, read_arg(argv, i.arg, macros[macro_name].argc)}); else if (r8asm_macro *macro = std::get_if(&i.op)) { - std::vector macro_args; + macro_args.clear(); for (auto j : macro->args) macro_args.push_back( read_arg(argv, j, macros[macro_name].argc)); @@ -50,14 +51,15 @@ std::vector expand_single_macro(std::string macro_name, } std::vector expand_macros(std::vector &ins) { - std::vector out_tape; + std::vector tape; + std::vector tmp_tape; for (auto i : ins) { + tmp_tape.clear(); if (r8asm_macro *macro = std::get_if(&i.op)) { - std::vector tmp_tape = - (expand_single_macro(macro->name, macro->args)); - out_tape.insert(out_tape.end(), tmp_tape.begin(), tmp_tape.end()); + tmp_tape = (expand_single_macro(macro->name, macro->args)); + tape.insert(tape.end(), tmp_tape.begin(), tmp_tape.end()); } else - out_tape.push_back(i); + tape.push_back(i); } - return (out_tape); + return (tape); } \ No newline at end of file diff --git a/src/r8asm_parse.cc b/src/r8asm_parse.cc index c3e2690..cae3c51 100644 --- a/src/r8asm_parse.cc +++ b/src/r8asm_parse.cc @@ -1,6 +1,10 @@ #include "r8asm/r8asm_parse.hh" +#include "r8asm/r8asm_core.hh" #include +#include #include +#include +#include void r8asm_trim(std::string &str) { auto start = str.find_first_not_of(EMPTY_CHARS); @@ -30,20 +34,37 @@ std::string r8asm_peel(std::string &str) { } r8asm_src read_src(std::string filename) { - r8asm_src out_src; + r8asm_src src; + ins_type_sign line_counter = 0; std::ifstream ifile; std::string line_buffer; ifile.open(filename, std::ios::in); + std::vector ins_buffer; + std::stack matcher; while (std::getline(ifile, line_buffer)) { + ins_buffer.clear(); auto comment = line_buffer.find_first_of(';'); if (comment != std::string::npos) line_buffer = line_buffer.substr(0, comment); - std::vector ins_buffer; + if (line_buffer.empty()) + continue; + line_counter++; while (!line_buffer.empty()) ins_buffer.push_back(r8asm_peel(line_buffer)); - out_src.raw_src.push_back(ins_buffer); + src.raw_src.push_back({ins_buffer}); + if (ins_buffer[0][0] == '%') { + if (ins_buffer[0].substr(1, 3) == "end") { + src.codeblocks.push_back({matcher.top(), line_counter}); + matcher.pop(); + } else + matcher.push(line_counter); + } } ifile.close(); - return (out_src); + return (src); +} + +std::vector r8asm_preprocess(r8asm_src &src) { + std::vector ins; } \ No newline at end of file