Standarized variable types.
This commit is contained in:
1 parent
8e8051b11d
commit
d4bbf1b0a0
2 files changed
+23
-16
No files matched your search
+12
-6
@@ -8,7 +8,11 @@
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast<int>(bc)])
|
||||
typedef uint32_t r8asm_label;
|
||||
typedef uint32_t r8asm_data;
|
||||
typedef uint32_t unit_size_type;
|
||||
|
||||
#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast<uint8_t>(bc)])
|
||||
|
||||
enum class rot8_bytecode { STP, BTP, ROR, FLB, BIZ, RNZ, OUT, INP };
|
||||
enum class r8asm_macro {
|
||||
@@ -18,7 +22,7 @@ enum class r8asm_macro {
|
||||
XOR,
|
||||
};
|
||||
|
||||
using R8Operand = std::variant<std::monostate, int, std::string>;
|
||||
using R8Operand = std::variant<std::monostate, r8asm_data, std::string>;
|
||||
using R8MetaOp = std::variant<r8asm_macro, rot8_bytecode>;
|
||||
|
||||
struct R8Instruction {
|
||||
@@ -33,11 +37,13 @@ class R8Macro {
|
||||
|
||||
std::vector<rot8_bytecode> r8asm_tape_out(std::span<const R8Instruction> ops);
|
||||
|
||||
extern std::map<std::string, int> labels;
|
||||
extern std::map<std::string, int> datas;
|
||||
extern std::map<std::string, r8asm_label> labels;
|
||||
extern std::map<std::string, r8asm_data> datas;
|
||||
extern std::map<std::string, std::vector<R8Macro>> macros;
|
||||
|
||||
extern uint8_t UNIT_SIZE;
|
||||
#define UNIT_MAX ((1 << UNIT_SIZE) - 1)
|
||||
extern unit_size_type UNIT_SIZE;
|
||||
// #define DEFAULT_UNIT_SIZE (sizeof(unit_size_type) * 8)
|
||||
#define DEFAULT_UNIT_SIZE 17 // Just for test.
|
||||
#define UNIT_MAX (((uint64_t)1 << UNIT_SIZE) - 1)
|
||||
|
||||
#endif
|
||||
+11
-10
@@ -3,23 +3,24 @@
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
uint8_t UNIT_SIZE = 8;
|
||||
unit_size_type UNIT_SIZE = DEFAULT_UNIT_SIZE;
|
||||
|
||||
std::map<std::string, int> labels = {{std::string("main"), 0}};
|
||||
std::map<std::string, int> datas = {{std::string("_UNIT_MAX"), UNIT_MAX}};
|
||||
std::map<std::string, r8asm_label> labels = {{std::string("main"), 0}};
|
||||
std::map<std::string, r8asm_data> datas = {
|
||||
{std::string("_UNIT_MAX"), UNIT_MAX}};
|
||||
|
||||
int read_opr(R8Operand arg, int default_opr = 1) {
|
||||
r8asm_data read_opr(R8Operand arg, r8asm_data 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))
|
||||
else if (const r8asm_data *operand = std::get_if<r8asm_data>(&arg))
|
||||
return (*operand);
|
||||
return (default_opr);
|
||||
}
|
||||
|
||||
void expand_xor(std::vector<rot8_bytecode> &tape, R8Operand arg) {
|
||||
int opr = read_opr(arg);
|
||||
r8asm_data opr = read_opr(arg);
|
||||
|
||||
for (int i = 0; i < UNIT_SIZE; i++) {
|
||||
for (unit_size_type i = 0; i < UNIT_SIZE; i++) {
|
||||
if ((opr & 1) == 1) tape.push_back(rot8_bytecode::FLB);
|
||||
tape.push_back(rot8_bytecode::ROR);
|
||||
opr >>= 1;
|
||||
@@ -27,7 +28,7 @@ void expand_xor(std::vector<rot8_bytecode> &tape, R8Operand arg) {
|
||||
}
|
||||
|
||||
void expand_loop(std::vector<rot8_bytecode> &tape, R8Operand arg) {
|
||||
int opr = read_opr(arg, UNIT_MAX);
|
||||
r8asm_data opr = read_opr(arg, UNIT_MAX);
|
||||
|
||||
tape.push_back(rot8_bytecode::STP);
|
||||
expand_xor(tape, opr);
|
||||
@@ -46,8 +47,8 @@ std::vector<rot8_bytecode> r8asm_tape_out(std::span<const R8Instruction> ops) {
|
||||
std::vector<rot8_bytecode> tape;
|
||||
for (const auto it : ops) {
|
||||
if (const rot8_bytecode *bc = std::get_if<rot8_bytecode>(&it.op)) {
|
||||
int repeat = read_opr(it.arg);
|
||||
for (int i = 0; i < repeat; i++) tape.push_back(*bc);
|
||||
r8asm_data repeat = read_opr(it.arg);
|
||||
for (r8asm_data 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:
|
||||
|
||||
Reference in new issue
Block a user