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

+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;
}