From 0c6b494f22d9e87b7ac745d2dbf82d70c975a094 Mon Sep 17 00:00:00 2001 From: StarCells Date: Tue, 25 Aug 2026 18:36:13 +0800 Subject: [PATCH] 202608251836 --- include/r8asm/r8asm.hh | 7 ++++--- src/r8asm.cc | 21 ++++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/include/r8asm/r8asm.hh b/include/r8asm/r8asm.hh index 214e71e..b14af55 100644 --- a/include/r8asm/r8asm.hh +++ b/include/r8asm/r8asm.hh @@ -24,6 +24,7 @@ public: BUILTIN, MACROCALL, ROT8, + CODEBLOCK, }; struct ast_node { ast_node_t type; @@ -33,10 +34,10 @@ public: class ast { private: public: - ast_node root; + ast_node root = {r8asm::ast_node_t::CODEBLOCK, "", {}}; addr dataptr = 0; - std::vector> scopes; - std::map> macro_bodies; + std::vector> scopes = {{}}; + std::map macros; general_data expand_node_to_data(const ast_node &node); std::vector expand_node_to_ins(const ast_node &node); diff --git a/src/r8asm.cc b/src/r8asm.cc index c812317..fff76fa 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -66,7 +66,7 @@ r8asm::general_data r8asm::ast::expand_node_to_data(const ast_node &node) { r8asm::general_data r8asm::ast::read_parameter(std::string index_str) { parameter_index i = std::stoi(index_str); - return scopes.back()['%' + index_str]; + return scopes.back().at('%' + index_str); } // TODO: FILL THE BLANK @@ -91,18 +91,29 @@ r8asm::ast::expand_node_to_ins(const ast_node &node) { value: macro_name children: arguments */ + std::map tmp_scope = {}; for (auto i = 0; i < node.children.size(); i++) tmp_scope.insert_or_assign('%' + std::to_string(i + 1), + // %1 in macro means children[0] expand_node_to_data(node.children[i])); scopes.push_back(tmp_scope); - // %1 in macro means children[0] - for (auto body : macro_bodies.at(node.value)) - for (auto it : expand_node_to_ins(body)) - to_return.push_back(it); + for (const auto &it : expand_node_to_ins(macros.at(node.value))) + to_return.push_back(it); scopes.pop_back(); } break; + + case ast_node_t::CODEBLOCK: { + /* + CODEBLOCK + children: ins + */ + for (const auto &child : node.children) + for (const auto &ins : expand_node_to_ins(child)) + to_return.push_back(ins); + } break; + case ast_node_t::BUILTIN: { switch (builtin_map.at(node.value)) { case builtin_t::ANCHOR: {