From c44a4058d16781df4bcf65a9e3b22c872e43dc1c Mon Sep 17 00:00:00 2001 From: StarCells Date: Fri, 28 Aug 2026 01:23:16 +0800 Subject: [PATCH] 202608280123 --- include/r8asm/r8asm.hh | 6 +- src/r8asm.cc | 136 ++++++++++++++++++----------------------- 2 files changed, 64 insertions(+), 78 deletions(-) diff --git a/include/r8asm/r8asm.hh b/include/r8asm/r8asm.hh index 3f85ed3..d6d43cb 100644 --- a/include/r8asm/r8asm.hh +++ b/include/r8asm/r8asm.hh @@ -26,9 +26,9 @@ private: MACROCALL, ROT8, - PREFIX_DIRECTIVE, - PREFIX_PARAMETER, - PREFIX_BUILTIN, + DIRECTIVE, + PARAMETER, + BUILTIN, PREFIX_REMARK, LPAREN, diff --git a/src/r8asm.cc b/src/r8asm.cc index 58e3f77..d64495a 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -264,8 +264,8 @@ bool r8asm::ast::import_macro(const std::string ¯o_name, } #define POINTS_AT_DIRECTIVE(iter, TYPE) \ - (iter->tokens[0].type == token_t::PREFIX_DIRECTIVE && \ - directive_keywords.at(iter->tokens[1].value) == directive_t::TYPE) + (iter->tokens[0].type == token_t::DIRECTIVE && \ + directive_keywords.at(iter->tokens[0].value) == directive_t::TYPE) r8asm::ast_node r8asm::parse(const token_stream &stream) { @@ -279,30 +279,30 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { for (auto it = stream.lines.begin(); it < stream.lines.end(); it++) { switch (it->tokens[0].type) { - case token_t::PREFIX_DIRECTIVE: { + case token_t::DIRECTIVE: { /* directive will be tokenized like this: - % macro raw_add - 0 1 2 + macro raw_add + 0 1 or: - % macro raw_add 2 + 4 - 0 1 2 3 4 5 - (though [3] [4] [5] won't be used) + macro raw_add 2 + 4 + 0 1 2 3 4 + (though [2] [3] [4] won't be used) if it's a rep block: - % rep 1 + 1 - 0 1 2 3 4 + rep 1 + 1 + 0 1 2 3 if it's a include line: - % include filename - 0 1 2 + include filename + 0 1 */ - switch (directive_keywords.at(it->tokens[1].value)) { + switch (directive_keywords.at(it->tokens[0].value)) { // match an entire operation (not a cut for arguments) first case directive_t::MACRO: { - std::string macro_name = it->tokens[2].value; + std::string macro_name = it->tokens[1].value; tmp_codeblock_stream.lines = {}; @@ -323,7 +323,7 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { // build subtree for argument of rep token_line buffer = {it->number, {}}; - for (auto token_it = it->tokens.begin() + 2; // where argument begins + for (auto token_it = it->tokens.begin() + 1; // where argument begins token_it < it->tokens.end(); token_it++) buffer.tokens.push_back(*token_it); /* @@ -334,7 +334,7 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { to_return.children.push_back({r8asm::ast_node_t::REPEAT, "", {}}); to_return.children.back().children.push_back( - parse_args(buffer).children[0]); // count will be pushed here. + parse_single_arg(buffer)); // count will be pushed here. tmp_codeblock_stream.lines = {}; depth_t layer = 0; @@ -355,10 +355,8 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { } 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 + auto tp = it->tokens.begin() + 1; + // now tp points at the LQUOTE for (tp += 1; tp->type != token_t::RQUOTE; tp++) filename += tp->value; @@ -371,27 +369,29 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { break; } } break; - case token_t::PREFIX_BUILTIN: { + case token_t::BUILTIN: { /* BUILTIN value: type children: args */ /* like this: - . var pos1 15 - 0 1 2 3 + builtin_var pos1 15 + 0 1 2 */ to_return.children.push_back( - {r8asm::ast_node_t::BUILTIN, "builtin_" + it->tokens[1].value, {}}); + {r8asm::ast_node_t::BUILTIN, it->tokens[0].value, {}}); // build subtree for argument(s) of builtin token_line buffer = {it->number, {}}; - for (auto token_it = it->tokens.begin() + 2; // where argument begins + for (auto token_it = it->tokens.begin() + 1; // where argument begins token_it < it->tokens.end(); token_it++) buffer.tokens.push_back(*token_it); tmp_codeblock_stream.lines = {buffer}; auto args = parse(tmp_codeblock_stream).children; for (const auto &arg_it : args) - to_return.children.back().children.push_back(arg_it); + to_return.children.back().children.push_back( + arg_it); // though all string args are treated as VARIABLE, I guess + // builtin_var will read children[0].value successfully. } break; case token_t::MACROCALL: { /* like this: @@ -416,7 +416,7 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { } break; case token_t::ROT8: { to_return.children.push_back( - {r8asm::ast_node_t::ROT8, "rot8_" + it->tokens[0].value, {}}); + {r8asm::ast_node_t::ROT8, it->tokens[0].value, {}}); } break; case token_t::PREFIX_REMARK: // ignore it. @@ -488,55 +488,41 @@ r8asm::ast_node r8asm::parse_single_arg(const token_line &line) { layer--; buffer.tokens.push_back(*it); } - if (right_assoc_depth == 0) { - data_stack.push(parse_single_arg(buffer)); - } else { - while (right_assoc_depth) { - auto unary_op = symbol_stack.top(); - symbol_stack.pop(); - unary_op.children.push_back(parse_single_arg(buffer)); - data_stack.push(unary_op); - right_assoc_depth--; - } + data_stack.push(parse_single_arg(buffer)); + while (right_assoc_depth) { + auto unary_op = symbol_stack.top(); + symbol_stack.pop(); + unary_op.children.push_back(data_stack.top()); + data_stack.pop(); + data_stack.push(unary_op); + right_assoc_depth--; } } break; case token_t::STRING: { { - if (right_assoc_depth == 0) { - if (is_integer(it->value)) - data_stack.push({r8asm::ast_node_t::CONSTANT, it->value, {}}); - else - data_stack.push({r8asm::ast_node_t::VARIABLE, it->value, {}}); - } else { - while (right_assoc_depth) { - auto unary_op = symbol_stack.top(); - symbol_stack.pop(); - if (is_integer(it->value)) - unary_op.children.push_back( - {r8asm::ast_node_t::CONSTANT, it->value, {}}); - else - unary_op.children.push_back( - {r8asm::ast_node_t::VARIABLE, it->value, {}}); - data_stack.push(unary_op); - right_assoc_depth--; - } - } + if (is_integer(it->value)) + data_stack.push({r8asm::ast_node_t::CONSTANT, it->value, {}}); + else + data_stack.push({r8asm::ast_node_t::VARIABLE, it->value, {}}); + } + while (right_assoc_depth) { + auto unary_op = symbol_stack.top(); + symbol_stack.pop(); + unary_op.children.push_back(data_stack.top()); + data_stack.pop(); + data_stack.push(unary_op); + right_assoc_depth--; } } break; - case token_t::PREFIX_PARAMETER: { - if (right_assoc_depth == 0) { - data_stack.push( - {r8asm::ast_node_t::PARAMETER, (it->value) + ((++it)->value), {}}); - } else { - while (right_assoc_depth) { - auto unary_op = symbol_stack.top(); - symbol_stack.pop(); - unary_op.children.push_back({r8asm::ast_node_t::PARAMETER, - (it->value) + ((++it)->value), - {}}); - data_stack.push(unary_op); - right_assoc_depth--; - } + case token_t::PARAMETER: { + data_stack.push({r8asm::ast_node_t::PARAMETER, (it->value), {}}); + while (right_assoc_depth) { + auto unary_op = symbol_stack.top(); + symbol_stack.pop(); + unary_op.children.push_back(data_stack.top()); + data_stack.pop(); + data_stack.push(unary_op); + right_assoc_depth--; } } break; case token_t::OPERATOR: @@ -549,18 +535,18 @@ r8asm::ast_node r8asm::parse_single_arg(const token_line &line) { { #define GET_PRECEDENCE(op_key) \ (main_tree.op_precedence_map.at(main_tree.operator_map.at(op_key))) - switch (main_tree.operator_map.at("op_" + it->value)) { + switch (main_tree.operator_map.at(it->value)) { case ast::operator_t::NEG: case ast::operator_t::NOT: // unary operators { right_assoc_depth++; - symbol_stack.push({ast_node_t::OPERATOR, "op_" + it->value, {}}); + symbol_stack.push({ast_node_t::OPERATOR, it->value, {}}); } break; default: // now all of them are binary operators { while ((!symbol_stack.empty()) && - GET_PRECEDENCE("op_" + it->value) >= - GET_PRECEDENCE("op_" + symbol_stack.top().value)) + GET_PRECEDENCE(it->value) >= + GET_PRECEDENCE(symbol_stack.top().value)) // e.g. pushing + while here's already a * in symbol_stack { auto tmp_op = symbol_stack.top(); @@ -572,7 +558,7 @@ r8asm::ast_node r8asm::parse_single_arg(const token_line &line) { tmp_op.children.push_back(tmp_arg); data_stack.push(tmp_op); } - symbol_stack.push({ast_node_t::OPERATOR, "op_" + it->value, {}}); + symbol_stack.push({ast_node_t::OPERATOR, it->value, {}}); } break; } }