From c41f0b02ffc99ad02bab86c345cd8ba6ae572932 Mon Sep 17 00:00:00 2001 From: StarCells Date: Thu, 27 Aug 2026 13:38:59 +0800 Subject: [PATCH] 202608271338 --- include/r8asm/r8asm.hh | 1 + src/r8asm.cc | 90 +++++++++++++++++++++++++++--------------- 2 files changed, 59 insertions(+), 32 deletions(-) diff --git a/include/r8asm/r8asm.hh b/include/r8asm/r8asm.hh index 8e6cbe8..f843b6f 100644 --- a/include/r8asm/r8asm.hh +++ b/include/r8asm/r8asm.hh @@ -128,6 +128,7 @@ private: public: ast_node expand_node_to_ins(const ast_node &node); std::string expand_root(); + bool import_macro(const std::string ¯o_name,const ast_node &node); }; ast main_tree; diff --git a/src/r8asm.cc b/src/r8asm.cc index 40a9a8e..22eec2e 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -1,8 +1,5 @@ #include "r8asm/r8asm.hh" -#include #include -#include -#include #include #include @@ -250,27 +247,25 @@ std::string r8asm::ast::expand_root() { } #undef ROT8_BC_TO_CHAR -// TODO: ERROR std::string r8asm::export_rot8() { return main_tree.expand_root(); } +bool r8asm::ast::import_macro(const std::string ¯o_name, + const ast_node &node) { + return macros.insert_or_assign(macro_name, node).second; +} + +#define POINTS_AT(iter, TYPE) \ + (iter->tokens[0].type == token_t::PREFIX_DIRECTIVE && \ + directive_keywords.at(iter->tokens[1].value) == directive_t::TYPE) r8asm::ast_node r8asm::parse(const token_stream &stream) { ast_node to_return = EMPTY_CODEBLOCK; token_stream tmp_codeblock_stream = {stream.filename}; + /* ast_node tmp_codeblock_node; - enum class stat_t { - NOTHING, - IS_CREATING_MACRO, - IS_CREATING_REPEAT, - IS_INCLUDING_FILE, - }; - stat_t stat = stat_t::NOTHING; - - std::string macro_name; - std::vector *destination = &to_return.children; - std::vector *last_destination = nullptr; + std::vector *last_destination = nullptr;*/ for (auto it = stream.lines.begin(); it < stream.lines.end(); it++) { switch (it->tokens[0].type) { @@ -295,36 +290,67 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { */ switch (directive_keywords.at(it->tokens[1].value)) { case directive_t::MACRO: { - stat = stat_t::IS_CREATING_MACRO; - tmp_codeblock_node = EMPTY_CODEBLOCK; - last_destination = destination; - destination = &tmp_codeblock_node.children; - macro_name = it->tokens[2].value; + + std::string macro_name = it->tokens[2].value; tmp_codeblock_stream.lines = {}; + + bool tmp = false; + // now it points at %macro line + for (it += 1; !(POINTS_AT(it, ENDMACRO) && tmp); it++) { + if (POINTS_AT(it, MACRO) || POINTS_AT(it, ENDMACRO)) + tmp = !tmp; + tmp_codeblock_stream.lines.push_back(*it); + } + // now it points at %endmacro line + main_tree.import_macro(macro_name, parse(tmp_codeblock_stream)); } break; case directive_t::REP: { - stat = stat_t::IS_CREATING_REPEAT; - destination->push_back(EMPTY_CODEBLOCK); - destination->back().type = ast_node_t::REPEAT; - - last_destination = destination; - - destination = &(destination->back().children); // build subtree for argument of rep token_line buffer = {it->number, {}}; for (auto token_it = it->tokens.begin() + 2; // where argument begins - token_it < it->tokens.end(); it++) + token_it < it->tokens.end(); token_it++) buffer.tokens.push_back(*token_it); tmp_codeblock_stream.lines = {buffer}; - // REPEAT's children[0] is the argument - destination->push_back(parse(tmp_codeblock_stream)); + /* + REPEAT + children[0]: count + children[1]: codeblock + */ + + to_return.children.push_back({r8asm::ast_node_t::REPEAT, "", {}}); + to_return.children.back().children.push_back( + parse(tmp_codeblock_stream)); // count will be pushed here. tmp_codeblock_stream.lines = {}; + + bool tmp = false; + // now it points at %rep line + for (it += 1; !(POINTS_AT(it, ENDREP) && tmp); it++) { + if (POINTS_AT(it, REP) || POINTS_AT(it, ENDREP)) + tmp = !tmp; + tmp_codeblock_stream.lines.push_back(*it); + } + // now it points at %endrep line + + to_return.children.push_back( + parse(tmp_codeblock_stream)); // codeblock will be pushed here. + } break; case directive_t::INCLUDE: { - - } + std::string filename = ""; + auto tp = it->tokens.begin() + 2; + while (tp->type != token_t::LQUOTE) + tp++; + // now tp points at LQUOTE + + for (tp += 1; tp->type != token_t::RQUOTE; tp++) + filename += tp->value; + // now tp points at RQUOTE + + import_file( + filename); // TODO: check whether the file is open successfully + } break; default: break; }