From 1fc3c6722f92f0227d236c8cfd93bf9e9e6f8bf2 Mon Sep 17 00:00:00 2001 From: StarCells Date: Fri, 28 Aug 2026 00:51:40 +0800 Subject: [PATCH] 202608280051 --- src/r8asm.cc | 88 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 34 deletions(-) diff --git a/src/r8asm.cc b/src/r8asm.cc index 22be86f..58e3f77 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -435,19 +435,23 @@ bool r8asm::is_integer(const std::string &str) { std::ranges::all_of(str, [](char c) { return c >= '0' && c <= '9'; }); } -#define UNRELATED_TO_LEFT(token) \ - (token.type == token_t::LPAREN) || (token.type == token_t::STRING) || \ - (token.type == token_t::PREFIX_PARAMETER) || \ - ((token.type == token_t::OPERATOR) && \ - ((token.value == "not") || (token.value == "neg"))) - +#define UNRELATED_TO_LEFT(present_token) \ + (it == line.tokens.begin() || \ + ((present_token->type == token_t::LPAREN || \ + present_token->type == token_t::STRING || \ + (present_token->type == token_t::OPERATOR && \ + (present_token->value == "neg" || present_token->value == "not"))) && \ + (!((present_token - 1)->type == token_t::OPERATOR || \ + (present_token - 1)->type == token_t::LPAREN)))) r8asm::ast_node r8asm::parse_args(const token_line &line) { ast_node to_return = EMPTY_CODEBLOCK; // arguments will be storaged in to_return.children std::vector raw_args; std::list buffer; - for (const auto &it : line.tokens) { + for (auto it = line.tokens.begin(); it != line.tokens.end(); it++) { + buffer.push_back(*it); + if (UNRELATED_TO_LEFT(it)) { raw_args.push_back({line.number, {}}); while (!buffer.empty()) { @@ -455,7 +459,7 @@ r8asm::ast_node r8asm::parse_args(const token_line &line) { buffer.pop_front(); } } - buffer.push_back(it); + buffer.push_back(*it); } for (const auto &it : raw_args) to_return.children.push_back(parse_single_arg(it)); @@ -469,7 +473,7 @@ r8asm::ast_node r8asm::parse_single_arg(const token_line &line) { std::stack data_stack; std::stack symbol_stack; - bool right_assoc_flag = false; + depth_t right_assoc_depth = 0; for (auto it = line.tokens.begin(); it < line.tokens.end(); it++) { switch (it->type) { @@ -484,41 +488,55 @@ r8asm::ast_node r8asm::parse_single_arg(const token_line &line) { layer--; buffer.tokens.push_back(*it); } - if (!right_assoc_flag) { + if (right_assoc_depth == 0) { data_stack.push(parse_single_arg(buffer)); } else { - 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_flag = false; + 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--; + } } } break; case token_t::STRING: { { - if (!right_assoc_flag) { - data_stack.push({r8asm::ast_node_t::VARIABLE, it->value, {}}); + 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 { - auto unary_op = symbol_stack.top(); - symbol_stack.pop(); - unary_op.children.push_back( - {r8asm::ast_node_t::VARIABLE, it->value, {}}); - data_stack.push(unary_op); - right_assoc_flag = false; + 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--; + } } } } break; case token_t::PREFIX_PARAMETER: { - if (!right_assoc_flag) + if (right_assoc_depth == 0) { data_stack.push( {r8asm::ast_node_t::PARAMETER, (it->value) + ((++it)->value), {}}); - else { - 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_flag = false; + } 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--; + } } } break; case token_t::OPERATOR: @@ -535,13 +553,14 @@ r8asm::ast_node r8asm::parse_single_arg(const token_line &line) { case ast::operator_t::NEG: case ast::operator_t::NOT: // unary operators { - right_assoc_flag = true; + right_assoc_depth++; symbol_stack.push({ast_node_t::OPERATOR, "op_" + it->value, {}}); } break; default: // now all of them are binary operators { - if (GET_PRECEDENCE("op_" + it->value) > - GET_PRECEDENCE("op_" + symbol_stack.top().value)) + while ((!symbol_stack.empty()) && + GET_PRECEDENCE("op_" + it->value) >= + GET_PRECEDENCE("op_" + symbol_stack.top().value)) // e.g. pushing + while here's already a * in symbol_stack { auto tmp_op = symbol_stack.top(); @@ -562,6 +581,7 @@ r8asm::ast_node r8asm::parse_single_arg(const token_line &line) { break; } } + while (!symbol_stack.empty()) { auto tmp_op = symbol_stack.top(); symbol_stack.pop();