202608280013
This commit is contained in:
1 parent
27cf3e2051
commit
10a793e2db
2 files changed
+213
-41
No files matched your search
+33
-19
@@ -3,6 +3,7 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -14,6 +15,8 @@ class r8asm {
|
||||
using addr = uint32_t;
|
||||
using general_data = int32_t;
|
||||
using line_number = unsigned long long;
|
||||
using depth_t = unsigned long long;
|
||||
using precedence_t = uint8_t;
|
||||
|
||||
private:
|
||||
enum class token_t {
|
||||
@@ -76,23 +79,6 @@ private:
|
||||
std::vector<std::map<std::string, general_data>> scopes = {{}};
|
||||
std::map<std::string, ast_node> macros;
|
||||
|
||||
enum class operator_t {
|
||||
ADD,
|
||||
SUB,
|
||||
MUL,
|
||||
DIV,
|
||||
XOR,
|
||||
AND,
|
||||
OR,
|
||||
NOT,
|
||||
NEG,
|
||||
};
|
||||
std::map<std::string, operator_t> operator_map = {
|
||||
{"op_add", operator_t::ADD}, {"op_sub", operator_t::SUB},
|
||||
{"op_mul", operator_t::MUL}, {"op_div", operator_t::DIV},
|
||||
{"op_xor", operator_t::XOR}, {"op_and", operator_t::AND},
|
||||
{"op_or", operator_t::OR}, {"op_not", operator_t::NOT},
|
||||
{"op_neg", operator_t::NEG}};
|
||||
enum class builtin_t {
|
||||
ANCHOR,
|
||||
TP,
|
||||
@@ -101,7 +87,7 @@ private:
|
||||
ENDLOOP,
|
||||
VAR,
|
||||
};
|
||||
std::map<std::string, builtin_t> builtin_map = {
|
||||
const std::map<std::string, builtin_t> builtin_map = {
|
||||
{"builtin_anchor", builtin_t::ANCHOR},
|
||||
{"builtin_tp", builtin_t::TP},
|
||||
{"builtin_xor", builtin_t::XOR},
|
||||
@@ -119,7 +105,7 @@ private:
|
||||
OUT, // OUTput byte,'.'
|
||||
INP, // INPut byte,','
|
||||
};
|
||||
std::map<std::string, rot8_t> rot8_map = {
|
||||
const std::map<std::string, rot8_t> rot8_map = {
|
||||
{"rot8_stp", rot8_t::STP}, {"rot8_btp", rot8_t::BTP},
|
||||
{"rot8_ror", rot8_t::ROR}, {"rot8_flb", rot8_t::FLB},
|
||||
{"rot8_biz", rot8_t::BIZ}, {"rot8_rnz", rot8_t::RNZ},
|
||||
@@ -129,6 +115,29 @@ private:
|
||||
std::string expand_node_to_string(const ast_node &node);
|
||||
|
||||
public:
|
||||
enum class operator_t {
|
||||
ADD,
|
||||
SUB,
|
||||
MUL,
|
||||
DIV,
|
||||
XOR,
|
||||
AND,
|
||||
OR,
|
||||
NOT,
|
||||
NEG,
|
||||
};
|
||||
|
||||
const std::map<std::string, operator_t> operator_map = {
|
||||
{"op_add", operator_t::ADD}, {"op_sub", operator_t::SUB},
|
||||
{"op_mul", operator_t::MUL}, {"op_div", operator_t::DIV},
|
||||
{"op_xor", operator_t::XOR}, {"op_and", operator_t::AND},
|
||||
{"op_or", operator_t::OR}, {"op_not", operator_t::NOT},
|
||||
{"op_neg", operator_t::NEG}};
|
||||
const std::map<operator_t, precedence_t> op_precedence_map = {
|
||||
{operator_t::NEG, 0}, {operator_t::NOT, 0}, {operator_t::MUL, 1},
|
||||
{operator_t::DIV, 1}, {operator_t::ADD, 2}, {operator_t::SUB, 2},
|
||||
{operator_t::AND, 3}, {operator_t::XOR, 4}, {operator_t::OR, 5},
|
||||
};
|
||||
ast_node expand_node_to_ins(const ast_node &node);
|
||||
std::string expand_root();
|
||||
bool import_macro(const std::string ¯o_name, const ast_node &node);
|
||||
@@ -149,7 +158,12 @@ private:
|
||||
{"rep", directive_t::REP},
|
||||
{"endrep", directive_t::ENDREP},
|
||||
{"include", directive_t::INCLUDE}};
|
||||
|
||||
ast_node parse(const token_stream &stream);
|
||||
ast_node parse_args(const token_line &line);
|
||||
ast_node parse_single_arg(const token_line &line);
|
||||
token_stream lex(const std::string &char_stream);
|
||||
bool is_integer(const std::string &str);
|
||||
|
||||
public:
|
||||
std::string export_rot8();
|
||||
|
||||
+180
-22
@@ -1,5 +1,9 @@
|
||||
#include "r8asm/r8asm.hh"
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -154,6 +158,11 @@ r8asm::ast_node r8asm::ast::expand_node_to_ins(const ast_node &node) {
|
||||
} break;
|
||||
|
||||
case ast_node_t::BUILTIN: {
|
||||
/*
|
||||
BUILTIN
|
||||
value: type
|
||||
children: args
|
||||
*/
|
||||
switch (builtin_map.at(node.value)) {
|
||||
// WARNING: node.value HAS BEEN USED!!! DO NOT CONSUME IT AGAIN!!!
|
||||
case builtin_t::ANCHOR: {
|
||||
@@ -254,9 +263,10 @@ bool r8asm::ast::import_macro(const std::string ¯o_name,
|
||||
return macros.insert_or_assign(macro_name, node).second;
|
||||
}
|
||||
|
||||
#define POINTS_AT(iter, TYPE) \
|
||||
#define POINTS_AT_DIRECTIVE(iter, TYPE) \
|
||||
(iter->tokens[0].type == token_t::PREFIX_DIRECTIVE && \
|
||||
directive_keywords.at(iter->tokens[1].value) == directive_t::TYPE)
|
||||
|
||||
r8asm::ast_node r8asm::parse(const token_stream &stream) {
|
||||
|
||||
ast_node to_return = EMPTY_CODEBLOCK;
|
||||
@@ -296,12 +306,13 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
|
||||
|
||||
tmp_codeblock_stream.lines = {};
|
||||
|
||||
line_number layer = 0;
|
||||
depth_t layer = 0;
|
||||
// now it points at %macro line
|
||||
for (it += 1; !(POINTS_AT(it, ENDMACRO) && (layer == 0)); it++) {
|
||||
if (POINTS_AT(it, MACRO))
|
||||
for (it += 1; !(POINTS_AT_DIRECTIVE(it, ENDMACRO) && (layer == 0));
|
||||
it++) {
|
||||
if (POINTS_AT_DIRECTIVE(it, MACRO))
|
||||
layer++;
|
||||
else if (POINTS_AT(it, ENDMACRO))
|
||||
else if (POINTS_AT_DIRECTIVE(it, ENDMACRO))
|
||||
layer--;
|
||||
tmp_codeblock_stream.lines.push_back(*it);
|
||||
}
|
||||
@@ -315,7 +326,6 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
|
||||
for (auto token_it = it->tokens.begin() + 2; // where argument begins
|
||||
token_it < it->tokens.end(); token_it++)
|
||||
buffer.tokens.push_back(*token_it);
|
||||
tmp_codeblock_stream.lines = {buffer};
|
||||
/*
|
||||
REPEAT
|
||||
children[0]: count
|
||||
@@ -324,15 +334,16 @@ 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(tmp_codeblock_stream)); // count will be pushed here.
|
||||
parse_args(buffer).children[0]); // count will be pushed here.
|
||||
tmp_codeblock_stream.lines = {};
|
||||
|
||||
line_number layer = 0;
|
||||
depth_t layer = 0;
|
||||
// now it points at %rep line
|
||||
for (it += 1; !(POINTS_AT(it, ENDREP) && (layer == 0)); it++) {
|
||||
if (POINTS_AT(it, REP))
|
||||
for (it += 1; !(POINTS_AT_DIRECTIVE(it, ENDREP) && (layer == 0));
|
||||
it++) {
|
||||
if (POINTS_AT_DIRECTIVE(it, REP))
|
||||
layer++;
|
||||
else if (POINTS_AT(it, ENDREP))
|
||||
else if (POINTS_AT_DIRECTIVE(it, ENDREP))
|
||||
layer--;
|
||||
tmp_codeblock_stream.lines.push_back(*it);
|
||||
}
|
||||
@@ -361,6 +372,11 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
|
||||
}
|
||||
} break;
|
||||
case token_t::PREFIX_BUILTIN: {
|
||||
/*
|
||||
BUILTIN
|
||||
value: type
|
||||
children: args
|
||||
*/
|
||||
/* like this:
|
||||
. var pos1 15
|
||||
0 1 2 3
|
||||
@@ -373,11 +389,6 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
|
||||
token_it < it->tokens.end(); token_it++)
|
||||
buffer.tokens.push_back(*token_it);
|
||||
tmp_codeblock_stream.lines = {buffer};
|
||||
/*
|
||||
REPEAT
|
||||
children[0]: count
|
||||
children[1]: codeblock
|
||||
*/
|
||||
auto args = parse(tmp_codeblock_stream).children;
|
||||
for (const auto &arg_it : args)
|
||||
to_return.children.back().children.push_back(arg_it);
|
||||
@@ -394,13 +405,12 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
|
||||
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};
|
||||
/*
|
||||
REPEAT
|
||||
children[0]: count
|
||||
children[1]: codeblock
|
||||
MACROCALL
|
||||
value: macro_name
|
||||
children: arguments
|
||||
*/
|
||||
auto args = parse(tmp_codeblock_stream).children;
|
||||
auto args = parse_args(buffer).children;
|
||||
for (const auto &arg_it : args)
|
||||
to_return.children.back().children.push_back(arg_it);
|
||||
} break;
|
||||
@@ -411,11 +421,159 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
|
||||
case token_t::PREFIX_REMARK:
|
||||
// ignore it.
|
||||
break;
|
||||
default: // Now it's surely a line of one argument or arguments.
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return to_return;
|
||||
}
|
||||
#undef POINTS_AT_DIRECTIVE
|
||||
|
||||
bool r8asm::is_integer(const std::string &str) {
|
||||
return !str.empty() &&
|
||||
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")))
|
||||
|
||||
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) {
|
||||
if (UNRELATED_TO_LEFT(it)) {
|
||||
raw_args.push_back({line.number, {}});
|
||||
while (!buffer.empty()) {
|
||||
raw_args.back().tokens.push_back(buffer.front());
|
||||
buffer.pop_front();
|
||||
}
|
||||
}
|
||||
buffer.push_back(it);
|
||||
}
|
||||
for (const auto &it : raw_args)
|
||||
to_return.children.push_back(parse_single_arg(it));
|
||||
return to_return;
|
||||
}
|
||||
|
||||
#undef UNRELATED_TO_LEFT
|
||||
|
||||
r8asm::ast_node r8asm::parse_single_arg(const token_line &line) {
|
||||
ast_node to_return;
|
||||
std::stack<ast_node> data_stack;
|
||||
std::stack<ast_node> symbol_stack;
|
||||
|
||||
bool right_assoc_flag = false;
|
||||
|
||||
for (auto it = line.tokens.begin(); it < line.tokens.end(); it++) {
|
||||
switch (it->type) {
|
||||
case token_t::LPAREN: {
|
||||
depth_t layer = 0;
|
||||
token_line buffer = {line.number, {}};
|
||||
|
||||
for (it += 1; !((it->type == token_t::RPAREN) && (layer == 0)); it++) {
|
||||
if (it->type == token_t::LPAREN)
|
||||
layer++;
|
||||
else if (it->type == token_t::RPAREN)
|
||||
layer--;
|
||||
buffer.tokens.push_back(*it);
|
||||
}
|
||||
if (!right_assoc_flag) {
|
||||
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;
|
||||
}
|
||||
} break;
|
||||
case token_t::STRING: {
|
||||
{
|
||||
if (!right_assoc_flag) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
} break;
|
||||
case token_t::PREFIX_PARAMETER: {
|
||||
if (!right_assoc_flag)
|
||||
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;
|
||||
}
|
||||
} break;
|
||||
case token_t::OPERATOR:
|
||||
// precedence: NEG = NOT
|
||||
// > MUL = DIV
|
||||
// > ADD = SUB
|
||||
// > AND
|
||||
// > XOR
|
||||
// > OR
|
||||
{
|
||||
#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)) {
|
||||
case ast::operator_t::NEG:
|
||||
case ast::operator_t::NOT: // unary operators
|
||||
{
|
||||
right_assoc_flag = true;
|
||||
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))
|
||||
// e.g. pushing + while here's already a * in symbol_stack
|
||||
{
|
||||
auto tmp_op = symbol_stack.top();
|
||||
symbol_stack.pop();
|
||||
auto tmp_arg = data_stack.top(); // 2nd arg got
|
||||
data_stack.pop();
|
||||
tmp_op.children.push_back(data_stack.top()); // 1st arg pushed
|
||||
data_stack.pop();
|
||||
tmp_op.children.push_back(tmp_arg);
|
||||
data_stack.push(tmp_op);
|
||||
}
|
||||
symbol_stack.push({ast_node_t::OPERATOR, "op_" + it->value, {}});
|
||||
} break;
|
||||
}
|
||||
}
|
||||
#undef GET_PRECEDENCE
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (!symbol_stack.empty()) {
|
||||
auto tmp_op = symbol_stack.top();
|
||||
symbol_stack.pop();
|
||||
auto tmp_arg = data_stack.top(); // 2nd arg got
|
||||
data_stack.pop();
|
||||
tmp_op.children.push_back(data_stack.top()); // 1st arg pushed
|
||||
data_stack.pop();
|
||||
tmp_op.children.push_back(tmp_arg);
|
||||
data_stack.push(tmp_op);
|
||||
}
|
||||
to_return = data_stack.top();
|
||||
return to_return;
|
||||
}
|
||||
|
||||
#undef EMPTY_CODEBLOCK
|
||||
Reference in new issue
Block a user