FIRST WORKABLE VERSION
This commit is contained in:
1 parent
14d4bc5517
commit
8e8051b11d
5 files changed
+59
-35
No files matched your search
+3
-2
@@ -1,3 +1,4 @@
|
||||
BasedOnStyle: LLVM
|
||||
BasedOnStyle: Google
|
||||
IndentWidth: 4
|
||||
UseTab: Always
|
||||
UseTab: Always
|
||||
PointerAlignment: Right
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#define ROT8_BC_TO_CHAR(bc) "><+-[].,"[static_cast<int>(bc)]
|
||||
#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast<int>(bc)])
|
||||
|
||||
enum class rot8_bytecode { STP, BTP, ROR, FLB, BIZ, RNZ, OUT, INP };
|
||||
enum class r8asm_macro {
|
||||
@@ -38,5 +38,6 @@ extern std::map<std::string, int> datas;
|
||||
extern std::map<std::string, std::vector<R8Macro>> macros;
|
||||
|
||||
extern uint8_t UNIT_SIZE;
|
||||
#define UNIT_MAX ((1 << UNIT_SIZE) - 1)
|
||||
|
||||
#endif
|
||||
+45
-23
@@ -1,45 +1,67 @@
|
||||
#include "r8asm/r8asm.hh"
|
||||
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
uint8_t UNIT_SIZE = 8;
|
||||
|
||||
std::map<std::string, int> labels = {{std::string("main"), 0}};
|
||||
std::map<std::string, int> datas = {{std::string("_UNIT_SIZE"), UNIT_SIZE}};
|
||||
std::map<std::string, int> datas = {{std::string("_UNIT_MAX"), UNIT_MAX}};
|
||||
|
||||
int read_opr(R8Operand arg, int default_opr = 1) {
|
||||
if (const std::string *operand = std::get_if<std::string>(&arg))
|
||||
return (datas[*operand]);
|
||||
else if (const int *operand = std::get_if<int>(&arg))
|
||||
return (*operand);
|
||||
return (default_opr);
|
||||
}
|
||||
|
||||
void expand_xor(std::vector<rot8_bytecode> &tape, R8Operand arg) {
|
||||
if (const std::string *operand = std::get_if<std::string>(&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<rot8_bytecode> &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<rot8_bytecode> &tape, R8Operand arg) {
|
||||
std::vector<rot8_bytecode> 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<rot8_bytecode> r8asm_tape_out(std::span<const R8Instruction> ops) {
|
||||
std::vector<rot8_bytecode> tape;
|
||||
for (const auto it : ops) {
|
||||
int repeat = 1;
|
||||
if (const rot8_bytecode *bc = std::get_if<rot8_bytecode>(&it.op)) {
|
||||
if (const std::string *operand = std::get_if<std::string>(&it.arg))
|
||||
repeat = datas[*operand];
|
||||
else if (const int *operand = std::get_if<int>(&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<r8asm_macro>(&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);
|
||||
|
||||
+7
-4
@@ -1,9 +1,12 @@
|
||||
#include "r8asm/r8asm.hh"
|
||||
#include <iostream>
|
||||
|
||||
#include "r8asm/r8asm.hh"
|
||||
int main() {
|
||||
std::vector<R8Instruction> ins = {{rot8_bytecode::STP, 3}};
|
||||
std::vector<R8Instruction> 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<rot8_bytecode> 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);
|
||||
}
|
||||
Reference in new issue
Block a user