202608271338

This commit is contained in:
starcells committed 2026-08-27 13:38:59 +08:00
1 parent 0bc249e35a
commit c41f0b02ff
2 files changed
+59 -32

No files matched your search

+1
View File
@@ -128,6 +128,7 @@ private:
public:
ast_node expand_node_to_ins(const ast_node &node);
std::string expand_root();
bool import_macro(const std::string &macro_name,const ast_node &node);
};
ast main_tree;
+58 -32
View File
@@ -1,8 +1,5 @@
#include "r8asm/r8asm.hh"
#include <filesystem>
#include <iostream>
#include <memory>
#include <stop_token>
#include <string>
#include <vector>
@@ -250,27 +247,25 @@ std::string r8asm::ast::expand_root() {
}
#undef ROT8_BC_TO_CHAR
// TODO: ERROR
std::string r8asm::export_rot8() { return main_tree.expand_root(); }
bool r8asm::ast::import_macro(const std::string &macro_name,
const ast_node &node) {
return macros.insert_or_assign(macro_name, node).second;
}
#define POINTS_AT(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;
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;
std::vector<ast_node> *last_destination = nullptr;*/
for (auto it = stream.lines.begin(); it < stream.lines.end(); it++) {
switch (it->tokens[0].type) {
@@ -295,36 +290,67 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
*/
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;
std::string macro_name = it->tokens[2].value;
tmp_codeblock_stream.lines = {};
bool tmp = false;
// now it points at %macro line
for (it += 1; !(POINTS_AT(it, ENDMACRO) && tmp); it++) {
if (POINTS_AT(it, MACRO) || POINTS_AT(it, ENDMACRO))
tmp = !tmp;
tmp_codeblock_stream.lines.push_back(*it);
}
// now it points at %endmacro line
main_tree.import_macro(macro_name, parse(tmp_codeblock_stream));
} 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++)
token_it < it->tokens.end(); token_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));
/*
REPEAT
children[0]: count
children[1]: codeblock
*/
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.
tmp_codeblock_stream.lines = {};
bool tmp = false;
// now it points at %rep line
for (it += 1; !(POINTS_AT(it, ENDREP) && tmp); it++) {
if (POINTS_AT(it, REP) || POINTS_AT(it, ENDREP))
tmp = !tmp;
tmp_codeblock_stream.lines.push_back(*it);
}
// now it points at %endrep line
to_return.children.push_back(
parse(tmp_codeblock_stream)); // codeblock will be pushed here.
} 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
for (tp += 1; tp->type != token_t::RQUOTE; tp++)
filename += tp->value;
// now tp points at RQUOTE
import_file(
filename); // TODO: check whether the file is open successfully
} break;
default:
break;
}