202608280123
This commit is contained in:
1 parent
1fc3c6722f
commit
c44a4058d1
2 files changed
+64
-78
No files matched your search
@@ -26,9 +26,9 @@ private:
|
||||
MACROCALL,
|
||||
ROT8,
|
||||
|
||||
PREFIX_DIRECTIVE,
|
||||
PREFIX_PARAMETER,
|
||||
PREFIX_BUILTIN,
|
||||
DIRECTIVE,
|
||||
PARAMETER,
|
||||
BUILTIN,
|
||||
PREFIX_REMARK,
|
||||
|
||||
LPAREN,
|
||||
|
||||
+61
-75
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user