From 8e8051b11d01a9971b39bc350ed778137eaaf516 Mon Sep 17 00:00:00 2001 From: starcells Date: Sat, 18 Apr 2026 21:19:59 +0800 Subject: [PATCH] FIRST WORKABLE VERSION --- .clang-format | 5 ++-- README.md | 7 ++--- include/r8asm/r8asm.hh | 3 +- src/r8asm.cc | 68 ++++++++++++++++++++++++++++-------------- tests/r8_test.cc | 11 ++++--- 5 files changed, 59 insertions(+), 35 deletions(-) diff --git a/.clang-format b/.clang-format index 67c0c8b..67d549f 100644 --- a/.clang-format +++ b/.clang-format @@ -1,3 +1,4 @@ -BasedOnStyle: LLVM +BasedOnStyle: Google IndentWidth: 4 -UseTab: Always \ No newline at end of file +UseTab: Always +PointerAlignment: Right \ No newline at end of file diff --git a/README.md b/README.md index 4e23e04..24e5176 100644 --- a/README.md +++ b/README.md @@ -11,14 +11,11 @@ section .text %macro erase 1 .tp %1 - stp - .xor UNIT_MAX - btp - .loop + .loop UNIT_MAX biz flb - ror rnz + ror .endloop %endmacro diff --git a/include/r8asm/r8asm.hh b/include/r8asm/r8asm.hh index 2c64502..b66d677 100644 --- a/include/r8asm/r8asm.hh +++ b/include/r8asm/r8asm.hh @@ -8,7 +8,7 @@ #include #include -#define ROT8_BC_TO_CHAR(bc) "><+-[].,"[static_cast(bc)] +#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast(bc)]) enum class rot8_bytecode { STP, BTP, ROR, FLB, BIZ, RNZ, OUT, INP }; enum class r8asm_macro { @@ -38,5 +38,6 @@ extern std::map datas; extern std::map> macros; extern uint8_t UNIT_SIZE; +#define UNIT_MAX ((1 << UNIT_SIZE) - 1) #endif \ No newline at end of file diff --git a/src/r8asm.cc b/src/r8asm.cc index 2c4f386..92ce36e 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -1,45 +1,67 @@ #include "r8asm/r8asm.hh" + #include +#include uint8_t UNIT_SIZE = 8; std::map labels = {{std::string("main"), 0}}; -std::map datas = {{std::string("_UNIT_SIZE"), UNIT_SIZE}}; +std::map datas = {{std::string("_UNIT_MAX"), UNIT_MAX}}; + +int read_opr(R8Operand arg, int default_opr = 1) { + if (const std::string *operand = std::get_if(&arg)) + return (datas[*operand]); + else if (const int *operand = std::get_if(&arg)) + return (*operand); + return (default_opr); +} void expand_xor(std::vector &tape, R8Operand arg) { - if (const std::string *operand = std::get_if(&arg)) { - } // TODO + int opr = read_opr(arg); + + for (int i = 0; i < UNIT_SIZE; i++) { + if ((opr & 1) == 1) tape.push_back(rot8_bytecode::FLB); + tape.push_back(rot8_bytecode::ROR); + opr >>= 1; + } } void expand_loop(std::vector &tape, R8Operand arg) { - tape.insert(tape.end(), { - rot8_bytecode::STP, - }); + int opr = read_opr(arg, UNIT_MAX); + + tape.push_back(rot8_bytecode::STP); + expand_xor(tape, opr); + tape.push_back(rot8_bytecode::BIZ); + tape.push_back(rot8_bytecode::BTP); // >(xor)[< +} + +void expand_endloop(std::vector &tape, R8Operand arg) { + std::vector tmp = {rot8_bytecode::STP, rot8_bytecode::FLB, + rot8_bytecode::ROR, rot8_bytecode::RNZ, + rot8_bytecode::BTP}; // >-+]< + tape.insert(tape.end(), tmp.begin(), tmp.end()); } std::vector r8asm_tape_out(std::span ops) { std::vector tape; for (const auto it : ops) { - int repeat = 1; if (const rot8_bytecode *bc = std::get_if(&it.op)) { - if (const std::string *operand = std::get_if(&it.arg)) - repeat = datas[*operand]; - else if (const int *operand = std::get_if(&it.arg)) - repeat = *operand; - for (int i = 0; i < repeat; i++) - tape.push_back(*bc); + int repeat = read_opr(it.arg); + for (int i = 0; i < repeat; i++) tape.push_back(*bc); } else if (const r8asm_macro *macro = std::get_if(&it.op)) switch (*macro) { - case r8asm_macro::LOOP: - expand_loop(tape, it.arg); - break; - case r8asm_macro::ENDLOOP:; - break; - case r8asm_macro::TP:; - break; - case r8asm_macro::XOR:; - break; - default:; + case r8asm_macro::LOOP: + expand_loop(tape, it.arg); + break; + case r8asm_macro::ENDLOOP:; + expand_endloop(tape, it.arg); + break; + case r8asm_macro::TP:; + break; + case r8asm_macro::XOR:; + expand_xor(tape, it.arg); + break; + default:; } } return (tape); diff --git a/tests/r8_test.cc b/tests/r8_test.cc index 49ed3da..3eedca3 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -1,9 +1,12 @@ -#include "r8asm/r8asm.hh" #include + +#include "r8asm/r8asm.hh" int main() { - std::vector ins = {{rot8_bytecode::STP, 3}}; + std::vector ins = { + {rot8_bytecode::INP}, {r8asm_macro::LOOP}, {rot8_bytecode::BIZ}, + {rot8_bytecode::FLB}, {rot8_bytecode::RNZ}, {rot8_bytecode::ROR}, + {r8asm_macro::ENDLOOP}, {rot8_bytecode::OUT}}; std::vector tape = r8asm_tape_out(ins); - for (const auto i : tape) - std::cout << ROT8_BC_TO_CHAR(i); + for (const auto i : tape) std::cout << ROT8_BC_TO_CHAR(i); return (0); }