202608290010

This commit is contained in:
starcells committed 2026-08-29 00:10:20 +08:00
1 parent a96e7ffa87
commit 5aa4893dc6
3 files changed
+140 -24

No files matched your search

+10 -9
View File
@@ -9,6 +9,8 @@
constexpr size_t CELL_SIZE = 8;
namespace r8asm_maps {}
class r8asm {
using data = int16_t;
using parameter_index = uint8_t;
@@ -29,7 +31,6 @@ private:
DIRECTIVE,
PARAMETER,
BUILTIN,
PREFIX_REMARK,
LPAREN,
RPAREN,
@@ -50,8 +51,11 @@ private:
std::string filename;
std::vector<token_line> lines;
const std::set<std::string> raw_rot8_ins = {"stp", "btp", "ror", "flb",
"biz", "rnz", "out", "inp"};
token_stream(const std::string &filename);
std::vector<token_line> lex(const std::string &in_chars);
std::vector<token_line> lex(const std::string &char_stream);
};
enum class ast_node_t {
@@ -124,7 +128,6 @@ private:
AND,
OR,
NOT,
NEG,
};
const std::map<std::string, operator_t> operator_map = {
@@ -132,11 +135,11 @@ private:
{"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},
{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();
@@ -162,8 +165,6 @@ private:
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();
+129 -15
View File
@@ -1,13 +1,64 @@
#include "r8asm/r8asm.hh"
#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define EMPTY_CHARS (" \t\n\r\f\v")
#define OPERATORS ("+-*/^~\"()")
namespace r8asm_strtools {
bool is_integer(const std::string &str) {
return !str.empty() &&
std::ranges::all_of(str, [](char c) { return c >= '0' && c <= '9'; });
}
void trim(std::string &str) { // Clean empty chars in the string.
auto start = str.find_first_not_of(EMPTY_CHARS);
if (!(start ==
std::string::npos)) { // The string isn't filled with empty chars.
auto tmp_end = str.find_last_not_of(EMPTY_CHARS);
str = str.substr(start, tmp_end - start + 1);
} else
str = "";
}
std::string peel(std::string &str) { // Peel the first word from string.
for (size_t i = 1; i < str.size();
i++) { // insert space before and after operators appear
if (std::string(OPERATORS).find(str[i]) != std::string::npos) {
if (str[i - 1] != ' ') {
str = str.substr(0, i) + ' ' + str.substr(i, str.size());
i++;
}
if (str[i + 1] != ' ') {
str = str.substr(0, i + 1) + ' ' + str.substr(i + 1, str.size());
}
}
}
trim(str);
auto end = str.find_first_of(EMPTY_CHARS);
if (end != std::string::npos) {
std::string tmp_str = str.substr(0, end);
str = str.substr(end);
trim(str);
return (tmp_str);
} else {
std::string tmp_str = str;
str = "";
return (tmp_str);
}
}
} // namespace r8asm_strtools
#undef EMPTY_CHARS
#undef OPERATORS
r8asm::general_data r8asm::ast::expand_node_to_data(const ast_node &node) {
general_data to_return = 0;
switch (node.type) {
@@ -60,9 +111,6 @@ r8asm::general_data r8asm::ast::expand_node_to_data(const ast_node &node) {
case operator_t::NOT:
to_return = ~expand_node_to_data(node.children[0]);
break;
case operator_t::NEG:
to_return = -expand_node_to_data(node.children[0]);
break;
default:;
break;
}
@@ -410,9 +458,6 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
to_return.children.push_back(
{r8asm::ast_node_t::ROT8, it->tokens[0].value, {}});
} break;
case token_t::PREFIX_REMARK:
// ignore it.
break;
default:
break;
@@ -422,11 +467,6 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
}
#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(present_token) \
(it != line.tokens.begin() && \
((present_token->type == token_t::LPAREN || \
@@ -502,7 +542,7 @@ r8asm::ast_node r8asm::parse_single_arg(const token_line &line) {
} break;
case token_t::STRING: {
{
if (is_integer(it->value))
if (r8asm_strtools::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, {}});
@@ -524,7 +564,6 @@ 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(it->value)) {
case ast::operator_t::NEG:
case ast::operator_t::NOT: // unary operators
{
right_assoc_depth++;
@@ -571,3 +610,78 @@ r8asm::ast_node r8asm::parse_single_arg(const token_line &line) {
}
#undef EMPTY_CODEBLOCK
#define OPERATORS ("+-*/^~")
std::vector<r8asm::token_line>
r8asm::token_stream::lex(const std::string &char_stream) {
std::vector<token_line> to_return;
std::istringstream iss(char_stream);
std::string str_buffer;
line_number num = 0;
bool next_will_be_remark = false;
const std::map<std::string, std::string> operator_map = {
// without "-"
{"+", "op_add"}, {"*", "op_mul"}, {"/", "op_div"}, {"^", "op_xor"},
{"&", "op_and"}, {"|", "op_or"}, {"~", "op_not"}, {"-", "op_sub"}};
while (std::getline(iss, str_buffer)) {
std::vector<token> token_buffer = {};
bool start_of_line = true;
bool lquote_appeared = false;
for (std::string token_val = r8asm_strtools::peel(str_buffer);
token_val != "";) {
if (lquote_appeared && token_val[0] != '\"')
token_buffer.push_back({r8asm::token_t::STRING, token_val});
else {
if (start_of_line) { // it's the first token in the line
start_of_line = false;
if (token_val[0] == '.') // builtin
token_buffer.push_back(
{r8asm::token_t::BUILTIN, "builtin_" + token_val.substr(0, 1)});
// e.g. .var -> builtin_var
else if (token_val[0] == '%') // directive
token_buffer.push_back(
{r8asm::token_t::DIRECTIVE, token_val.substr(0, 1)});
// e.g. %macro -> macro
else if (token_val[0] == ';') // remark
next_will_be_remark = true;
else if (raw_rot8_ins.find(token_val) != raw_rot8_ins.end()) // rot8
token_buffer.push_back({r8asm::token_t::ROT8, "rot8_" + token_val});
else // macro
token_buffer.push_back({r8asm::token_t::MACROCALL, token_val});
} else { // it's a part of argument
if (!next_will_be_remark) {
if (token_val[0] == ';') // remark
next_will_be_remark = true;
else if (token_val[0] == '%') // parameter
token_buffer.push_back({r8asm::token_t::PARAMETER, token_val});
else if (token_val[0] == '"') { // quote(filename)
if (!lquote_appeared) {
lquote_appeared = true; // only %include will use quotes, they
// won't appear again
token_buffer.push_back({r8asm::token_t::LQUOTE});
} else
token_buffer.push_back({r8asm::token_t::RQUOTE});
} else if (token_val[0] == '(')
token_buffer.push_back({r8asm::token_t::LPAREN});
else if (token_val[0] == ')')
token_buffer.push_back({r8asm::token_t::RPAREN});
else if (std::string(OPERATORS).find(token_val[0]) !=
std::string::npos) // operator
token_buffer.push_back({r8asm::token_t::OPERATOR, token_val});
else
token_buffer.push_back({r8asm::token_t::STRING, token_val});
}
}
}
}
num++;
if (!token_buffer.empty())
to_return.push_back({num, token_buffer});
next_will_be_remark = false;
}
return to_return;
}
+1
View File
@@ -1 +1,2 @@
int main() { return (0); }