202608271235

This commit is contained in:
starcells committed 2026-08-27 12:35:20 +08:00
1 parent 16cabba2ee
commit 0bc249e35a
2 files changed
+127 -16

No files matched your search

+31 -14
View File
@@ -13,21 +13,9 @@ class r8asm {
using parameter_index = uint8_t;
using addr = uint32_t;
using general_data = int32_t;
using line_number = unsigned long long;
private:
enum class directive_t {
MACRO,
ENDMACRO,
REP,
ENDREP,
INCLUDE,
};
std::map<std::string, directive_t> directive_map = {
{"dir_macro", r8asm::directive_t::MACRO},
{"dir_endmacro", r8asm::directive_t::ENDMACRO},
{"dir_rep", r8asm::directive_t::REP},
{"dir_endrep", r8asm::directive_t::ENDREP},
{"dir_include", r8asm::directive_t::INCLUDE}};
enum class token_t {
STRING,
OPERATOR,
@@ -43,12 +31,23 @@ private:
RQUOTE,
ENDL,
ENDF,
};
struct token {
token_t type;
std::string value;
};
struct token_line {
line_number number;
std::vector<token> tokens;
};
struct token_stream {
std::string filename;
std::vector<token_line> lines;
token_stream(const std::string &filename);
std::vector<token_line> lex(const std::string &in_chars);
};
enum class ast_node_t {
CONSTANT,
PARAMETER,
@@ -66,6 +65,7 @@ private:
std::string value;
std::vector<ast_node> children;
};
class ast {
private:
ast_node root = {r8asm::ast_node_t::CODEBLOCK, "", {}};
@@ -131,6 +131,23 @@ private:
};
ast main_tree;
enum class directive_t {
MACRO,
ENDMACRO,
REP,
ENDREP,
INCLUDE,
};
std::map<std::string, directive_t> directive_keywords = {
{"macro", directive_t::MACRO},
{"endmacro", directive_t::ENDMACRO},
{"rep", directive_t::REP},
{"endrep", directive_t::ENDREP},
{"include", directive_t::INCLUDE}};
ast_node parse(const token_stream &stream);
public:
std::string export_rot8();
bool import_file(std::string filename);
};
+96 -2
View File
@@ -1,5 +1,8 @@
#include "r8asm/r8asm.hh"
#include <filesystem>
#include <iostream>
#include <memory>
#include <stop_token>
#include <string>
#include <vector>
@@ -84,13 +87,18 @@ std::string r8asm::ast::expand_node_to_string(const ast_node &node) {
return to_return;
}
#define EMPTY_CODEBLOCK \
{ \
r8asm::ast_node_t::CODEBLOCK, "", {} \
}
// TODO: FILL THE BLANK
#define ROT8_NODE(INS) \
{ \
r8asm::ast_node_t::ROT8, std::string("rot8_") + INS, {} \
}
r8asm::ast_node r8asm::ast::expand_node_to_ins(const ast_node &node) {
ast_node to_return = {ast_node_t::CODEBLOCK, "", {}};
ast_node to_return = EMPTY_CODEBLOCK;
switch (node.type) {
case ast_node_t::ROT8: {
/*
@@ -242,4 +250,90 @@ std::string r8asm::ast::expand_root() {
}
#undef ROT8_BC_TO_CHAR
std::string r8asm::export_rot8() { return main_tree.expand_root(); }
// TODO: ERROR
std::string r8asm::export_rot8() { return main_tree.expand_root(); }
r8asm::ast_node r8asm::parse(const token_stream &stream) {
ast_node to_return = EMPTY_CODEBLOCK;
token_stream tmp_codeblock_stream = {stream.filename};
ast_node tmp_codeblock_node;
enum class stat_t {
NOTHING,
IS_CREATING_MACRO,
IS_CREATING_REPEAT,
IS_INCLUDING_FILE,
};
stat_t stat = stat_t::NOTHING;
std::string macro_name;
std::vector<ast_node> *destination = &to_return.children;
std::vector<ast_node> *last_destination = nullptr;
for (auto it = stream.lines.begin(); it < stream.lines.end(); it++) {
switch (it->tokens[0].type) {
case token_t::PREFIX_DIRECTIVE: {
/*
directive will be tokenized like this:
% macro raw_add
0 1 2
or:
% macro raw_add 2 + 4
0 1 2 3 4 5
(though [3] [4] [5] won't be used)
if it's a rep block:
% rep 1 + 1
0 1 2 3 4
if it's a include line:
% include filename
0 1 2
*/
switch (directive_keywords.at(it->tokens[1].value)) {
case directive_t::MACRO: {
stat = stat_t::IS_CREATING_MACRO;
tmp_codeblock_node = EMPTY_CODEBLOCK;
last_destination = destination;
destination = &tmp_codeblock_node.children;
macro_name = it->tokens[2].value;
tmp_codeblock_stream.lines = {};
} break;
case directive_t::REP: {
stat = stat_t::IS_CREATING_REPEAT;
destination->push_back(EMPTY_CODEBLOCK);
destination->back().type = ast_node_t::REPEAT;
last_destination = destination;
destination = &(destination->back().children);
// build subtree for argument of rep
token_line buffer = {it->number, {}};
for (auto token_it = it->tokens.begin() + 2; // where argument begins
token_it < it->tokens.end(); it++)
buffer.tokens.push_back(*token_it);
tmp_codeblock_stream.lines = {buffer};
// REPEAT's children[0] is the argument
destination->push_back(parse(tmp_codeblock_stream));
tmp_codeblock_stream.lines = {};
} break;
case directive_t::INCLUDE: {
}
default:
break;
}
} break;
default:
break;
}
}
return to_return;
}
#undef EMPTY_CODEBLOCK