diff --git a/asm/add.r8asm b/asm/add.r8asm index be5ecad..2ad60a2 100644 --- a/asm/add.r8asm +++ b/asm/add.r8asm @@ -1,3 +1,8 @@ +%include asm/lib.r8asm +%include asm/and.r8asm +%include asm/or.r8asm +%include asm/xor.r8asm + ; Macro bit_add ; %1: UnitBit A ; %2: UnitBit B diff --git a/asm/and.r8asm b/asm/and.r8asm index ec65c49..239df84 100644 --- a/asm/and.r8asm +++ b/asm/and.r8asm @@ -1,3 +1,5 @@ +%include asm/lib.r8asm + ; Macro bit_unsafe_and ; %1: CellBit arg1 ; %2: CellBit arg2 diff --git a/asm/asm.r8asm b/asm/asm.r8asm index a2f2879..6d4dc5d 100644 --- a/asm/asm.r8asm +++ b/asm/asm.r8asm @@ -1,3 +1,4 @@ +%include asm/add.r8asm .xor 123 .tp 2 diff --git a/asm/or.r8asm b/asm/or.r8asm index c719e5a..2adab33 100644 --- a/asm/or.r8asm +++ b/asm/or.r8asm @@ -1,3 +1,4 @@ +%include asm/lib.r8asm ; Macro bit_unsafe_or ; %1: CellBit arg1 ; %2: CellBit arg2 diff --git a/asm/xor.r8asm b/asm/xor.r8asm index e5a5698..dc95cd3 100644 --- a/asm/xor.r8asm +++ b/asm/xor.r8asm @@ -1,3 +1,4 @@ +%include asm/lib.r8asm ; Macro bit_unsafe_xor ; %1: CellBit arg1 ; %2: CellBit arg2 diff --git a/include/r8asm/r8asm_core.hh b/include/r8asm/r8asm_core.hh index 4700db2..a2c3f4e 100644 --- a/include/r8asm/r8asm_core.hh +++ b/include/r8asm/r8asm_core.hh @@ -33,11 +33,15 @@ enum class r8asm_builtin { XOR, ANCHOR, ERASE, +}; + +enum class r8asm_parse { MACRO, ENDMACRO, REPEAT, ENDREPEAT, INCLUDE, + VAR, }; using R8Operand = std::variant; @@ -47,7 +51,8 @@ struct r8asm_macrocall { std::vector actual_args; }; -using R8MetaOp = std::variant; +using R8MetaOp = + std::variant; struct R8Instruction { R8MetaOp op; diff --git a/include/r8asm/r8asm_parse.hh b/include/r8asm/r8asm_parse.hh index f60a761..f5b7d2d 100644 --- a/include/r8asm/r8asm_parse.hh +++ b/include/r8asm/r8asm_parse.hh @@ -3,11 +3,10 @@ #include "r8asm/r8asm_core.hh" #include -#include #include #include +#include #include -#include #include #define EMPTY_CHARS (" \t\n\r\f\v") @@ -17,11 +16,12 @@ typedef std::list>::iterator r8asm_src_point; struct R8Src { std::list> raw_src; std::queue> codeblocks; - void preprocess(); + void preprocess_blocks(); + void preprocess_lines(); }; R8Src read_src(std::string filename); std::vector r8asm_preprocess(R8Src &src); -extern std::map r8asm_parsemap; +extern std::unordered_map r8asm_parsemap; #endif \ No newline at end of file diff --git a/src/r8asm_asm.cc b/src/r8asm_asm.cc index 2c1fb07..5d26810 100644 --- a/src/r8asm_asm.cc +++ b/src/r8asm_asm.cc @@ -63,27 +63,15 @@ void expand_anchor(std::vector &tape, R8Operand arg) { datas.insert_or_assign(std::to_string(*name), dataptr); } -/* -void expand_erase(std::vector &tape, R8Operand arg) { - std::vector tmp = {rot8_bytecode::BIZ, rot8_bytecode::FLB, - rot8_bytecode::RNZ, - rot8_bytecode::ROR}; // [-]+ - for (cell_size_type i = 0; i < cell_SIZE; i++) - tape.insert(tape.end(), tmp.begin(), tmp.end()); - return; -}*/ - void expand_bytecode(std::vector &tape, R8Operand arg, - rot8_bytecode op, bool count_offset = true) { + rot8_bytecode op) { r8asm_data repeat = read_opr(arg, 1); for (r8asm_data i = 0; i < repeat; i++) { tape.push_back(op); - if (count_offset) { - if (op == rot8_bytecode::STP) - dataptr++; - else if (op == rot8_bytecode::BTP) - dataptr--; - } + if (op == rot8_bytecode::STP) + dataptr++; + else if (op == rot8_bytecode::BTP) + dataptr--; } } diff --git a/src/r8asm_parse.cc b/src/r8asm_parse.cc index 62c0682..e9d06e6 100644 --- a/src/r8asm_parse.cc +++ b/src/r8asm_parse.cc @@ -11,12 +11,10 @@ #include #include -std::map r8asm_parsemap = { - {"%macro", r8asm_builtin::MACRO}, - {"%endmacro", r8asm_builtin::ENDMACRO}, - {"%repeat", r8asm_builtin::REPEAT}, - {"%endrepeat", r8asm_builtin::ENDREPEAT}, - {"%include", r8asm_builtin::INCLUDE}, // TODO:Expand include +std::unordered_map r8asm_parsemap = { + {r8asm_parse::MACRO, "%macro"}, {r8asm_parse::ENDMACRO, "%endmacro"}, + {r8asm_parse::REPEAT, "%repeat"}, {r8asm_parse::ENDREPEAT, "%endrepeat"}, + {r8asm_parse::INCLUDE, "%include"}, {r8asm_parse::VAR, "%var"}, }; void r8asm_trim(std::string &str) { // Clean empty chars in the string. @@ -66,7 +64,8 @@ R8Src read_src(std::string filename) { ins_buffer.push_back(r8asm_peel(line_buffer)); src.raw_src.push_back({ins_buffer}); if (ins_buffer[0][0] == '%') { - if (ins_buffer[0].substr(1, 3) == "end") { + if (ins_buffer[0] == r8asm_parsemap[r8asm_parse::ENDMACRO] || + ins_buffer[0] == r8asm_parsemap[r8asm_parse::ENDREPEAT]) { src.codeblocks.push( {matcher.top(), std::prev(src.raw_src.end())}); matcher.pop(); @@ -107,7 +106,25 @@ r8asm_translate_single_ins(std::vector const raw_ins) { return (R8Instruction{}); } -void R8Src::preprocess() { +void R8Src::preprocess_lines() { + R8Src tmp_src; + for (auto i = (this->raw_src).begin(); i != (this->raw_src).end();) { + auto tmp_i = i; + if ((*i)[0] == r8asm_parsemap[r8asm_parse::VAR]) { + datas.insert_or_assign( + (*i)[1], std::get(r8asm_arg_stoi((*i)[1]))); + i = this->raw_src.erase(i); + } else if ((*i)[0] == r8asm_parsemap[r8asm_parse::INCLUDE]) { + tmp_src = read_src((*i)[1]); + tmp_src.preprocess_lines(); + tmp_src.preprocess_blocks(); + i = this->raw_src.erase(i); + } else + ++i; + } +} + +void R8Src::preprocess_blocks() { std::vector tmp_codeblock; while (!this->codeblocks.empty()) { auto i = this->codeblocks.front(); @@ -122,12 +139,12 @@ void R8Src::preprocess() { */ for (auto j : raw_codeblock) tmp_codeblock.push_back(r8asm_translate_single_ins(j)); - if ((*i.first)[0] == "%macro") { + if ((*i.first)[0] == r8asm_parsemap[r8asm_parse::MACRO]) { r8asm_macromap.insert_or_assign( (*i.first)[1], R8Macro{(argc_type)std::get( r8asm_arg_stoi((*i.first)[2])), tmp_codeblock}); - } else if ((*i.first)[0] == "%repeat") { + } else if ((*i.first)[0] == r8asm_parsemap[r8asm_parse::REPEAT]) { for (auto count = std::get(r8asm_arg_stoi((*i.first)[1])); count > 0; count--) @@ -140,7 +157,9 @@ void R8Src::preprocess() { std::vector r8asm_preprocess(R8Src &src) { std::vector tmp_ins; - src.preprocess(); + + src.preprocess_lines(); + src.preprocess_blocks(); for (auto i : src.raw_src) { tmp_ins.push_back(r8asm_translate_single_ins(i)); } diff --git a/tests/r8_test.cc b/tests/r8_test.cc index 05a5155..2b65d24 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -4,12 +4,7 @@ #include "r8asm/r8asm_parse.hh" int main(int argc, char *argv[]) { - std::string src_filename; - src_filename = argv[argc - 1]; - for (auto i = 1; i < argc - 1; i++) { - auto r8lib = read_src(argv[i]); - r8lib.preprocess(); - } + std::string src_filename = argv[1]; auto src = read_src(src_filename.c_str()); auto out_tape = r8asm_tape_out(expand_macros(r8asm_preprocess(src))); for (auto i : out_tape)