diff --git a/include/r8asm/r8asm.hh b/include/r8asm/r8asm.hh index 556b22e..8e6cbe8 100644 --- a/include/r8asm/r8asm.hh +++ b/include/r8asm/r8asm.hh @@ -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 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 tokens; + }; + struct token_stream { + std::string filename; + std::vector lines; + + token_stream(const std::string &filename); + std::vector lex(const std::string &in_chars); + }; + enum class ast_node_t { CONSTANT, PARAMETER, @@ -66,6 +65,7 @@ private: std::string value; std::vector 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 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); }; \ No newline at end of file diff --git a/src/r8asm.cc b/src/r8asm.cc index e4fd05a..40a9a8e 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -1,5 +1,8 @@ #include "r8asm/r8asm.hh" +#include #include +#include +#include #include #include @@ -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(); } \ No newline at end of file +// 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 *destination = &to_return.children; + std::vector *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 \ No newline at end of file