202608280051

This commit is contained in:
starcells committed 2026-08-28 00:51:40 +08:00
1 parent 10a793e2db
commit 1fc3c6722f
1 file changed
+54 -34
+54 -34
View File
@@ -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<token_line> raw_args;
std::list<token> 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<ast_node> data_stack;
std::stack<ast_node> 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();