202604220037
This commit is contained in:
1 parent
6189b15370
commit
326591317c
6 files changed
+88
-53
No files matched your search
+19
-15
@@ -8,15 +8,16 @@
|
||||
#include <vector>
|
||||
|
||||
typedef uint32_t r8asm_label;
|
||||
typedef uint32_t r8asm_data;
|
||||
typedef int32_t r8asm_data;
|
||||
typedef uint32_t unit_size_type;
|
||||
typedef int32_t unit_offset_type;
|
||||
typedef uint32_t mem_size_type;
|
||||
typedef uint8_t argc_type;
|
||||
typedef uint32_t argc_type;
|
||||
|
||||
#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast<uint8_t>(bc)])
|
||||
#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast<int>(bc)])
|
||||
|
||||
enum class rot8_bytecode { STP,
|
||||
enum class rot8_bytecode {
|
||||
STP,
|
||||
BTP,
|
||||
ROR,
|
||||
FLB,
|
||||
@@ -25,7 +26,7 @@ enum class rot8_bytecode { STP,
|
||||
OUT,
|
||||
INP,
|
||||
};
|
||||
enum class r8asm_macro {
|
||||
enum class r8asm_builtin {
|
||||
LOOP,
|
||||
ENDLOOP,
|
||||
TP,
|
||||
@@ -34,28 +35,31 @@ enum class r8asm_macro {
|
||||
MACRO,
|
||||
ENDMACRO,
|
||||
};
|
||||
|
||||
using R8Operand = std::variant<std::monostate, r8asm_data, std::string>;
|
||||
using R8MetaOp = std::variant<r8asm_macro, rot8_bytecode>;
|
||||
|
||||
struct r8asm_macro {
|
||||
std::string name;
|
||||
std::vector<R8Operand> args;
|
||||
};
|
||||
|
||||
using R8MetaOp = std::variant<r8asm_builtin, r8asm_macro, rot8_bytecode>;
|
||||
|
||||
struct R8Instruction {
|
||||
R8MetaOp op;
|
||||
R8Operand arg = std::monostate {};
|
||||
};
|
||||
|
||||
class R8Macro {
|
||||
argc_type argc;
|
||||
std::vector<R8Instruction> ins;
|
||||
R8Operand arg;
|
||||
R8Instruction(R8MetaOp in_op, R8Operand in_arg = std::monostate {})
|
||||
: op(in_op)
|
||||
, arg(in_arg) {
|
||||
};
|
||||
};
|
||||
|
||||
extern std::map<std::string, r8asm_label> labels;
|
||||
extern std::map<std::string, r8asm_data> datas;
|
||||
extern std::map<std::string, R8Macro> macros;
|
||||
extern mem_size_type dataptr;
|
||||
|
||||
extern unit_size_type UNIT_SIZE;
|
||||
// #define DEFAULT_UNIT_SIZE (sizeof(unit_size_type) * 8)
|
||||
#define DEFAULT_UNIT_SIZE 17 // Just for test.
|
||||
#define DEFAULT_UNIT_SIZE 8 // Just for test.
|
||||
#define UNIT_MAX (((uint64_t)1 << UNIT_SIZE) - 1)
|
||||
|
||||
#endif
|
||||
@@ -2,6 +2,11 @@
|
||||
#define R8ASM_MACRO
|
||||
|
||||
#include "r8asm/r8asm_core.hh"
|
||||
struct R8Macro {
|
||||
argc_type argc;
|
||||
std::vector<R8Instruction> ins;
|
||||
};
|
||||
extern std::map<std::string, R8Macro> macros;
|
||||
|
||||
std::vector<R8Instruction> expand_macro(std::string macro_name, std::vector<R8Operand> argv);
|
||||
|
||||
|
||||
+34
-29
@@ -1,11 +1,11 @@
|
||||
#include "r8asm/r8asm_asm.hh"
|
||||
#include "r8asm/r8asm_core.hh"
|
||||
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
static r8asm_data
|
||||
read_opr(R8Operand arg, r8asm_data default_opr = 0)
|
||||
r8asm_data read_opr(R8Operand arg, r8asm_data default_opr = 0) // Attention,default_opr is 0
|
||||
{
|
||||
if (const std::string *operand = std::get_if<std::string>(&arg)) {
|
||||
if ((*operand) == "$")
|
||||
@@ -16,7 +16,7 @@ read_opr(R8Operand arg, r8asm_data default_opr = 0)
|
||||
return (default_opr);
|
||||
}
|
||||
|
||||
static void expand_xor(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
void expand_xor(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
{
|
||||
r8asm_data opr = read_opr(arg);
|
||||
|
||||
@@ -28,7 +28,7 @@ static void expand_xor(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
}
|
||||
}
|
||||
|
||||
static void expand_loop(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
void expand_loop(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
{
|
||||
r8asm_data opr = read_opr(arg, UNIT_MAX);
|
||||
|
||||
@@ -38,17 +38,15 @@ static void expand_loop(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
tape.push_back(rot8_bytecode::BTP); // >(xor)[<
|
||||
}
|
||||
|
||||
static void expand_endloop(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
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,
|
||||
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());
|
||||
}
|
||||
|
||||
static void expand_tp(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
void expand_tp(std::vector<rot8_bytecode> &tape, R8Operand arg, bool count_offset = true)
|
||||
{
|
||||
r8asm_data opr = read_opr(arg);
|
||||
|
||||
@@ -58,10 +56,11 @@ static void expand_tp(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
for (unit_offset_type i = 0; i < offset; i++) {
|
||||
tape.push_back(stepforward ? rot8_bytecode::STP : rot8_bytecode::BTP);
|
||||
}
|
||||
dataptr = opr;
|
||||
if (count_offset)
|
||||
dataptr = opr;
|
||||
}
|
||||
|
||||
static void expand_anchor(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
void expand_anchor(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
{
|
||||
if (const std::string *name = std::get_if<std::string>(&arg))
|
||||
datas.insert({ *name, dataptr });
|
||||
@@ -69,35 +68,41 @@ static void expand_anchor(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
datas.insert({ std::to_string(*name), dataptr });
|
||||
}
|
||||
|
||||
std::vector<rot8_bytecode>
|
||||
r8asm_tape_out(std::span<const R8Instruction> ops)
|
||||
void expand_bytecode(std::vector<rot8_bytecode> &tape, R8Operand arg, rot8_bytecode op, bool count_offset = true)
|
||||
{
|
||||
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--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)) {
|
||||
r8asm_data repeat = read_opr(it.arg);
|
||||
for (r8asm_data i = 0; i < repeat; i++) {
|
||||
tape.push_back(*bc);
|
||||
if (*bc == rot8_bytecode::STP)
|
||||
dataptr++;
|
||||
else if (*bc == rot8_bytecode::BTP)
|
||||
dataptr--;
|
||||
}
|
||||
} else if (const r8asm_macro *macro = std::get_if<r8asm_macro>(&it.op))
|
||||
switch (*macro) {
|
||||
case r8asm_macro::LOOP:
|
||||
expand_bytecode(tape, it.arg, *bc);
|
||||
} else if (const r8asm_builtin *builtin = std::get_if<r8asm_builtin>(&it.op))
|
||||
switch (*builtin) {
|
||||
case r8asm_builtin::LOOP:
|
||||
expand_loop(tape, it.arg);
|
||||
break;
|
||||
case r8asm_macro::ENDLOOP:
|
||||
case r8asm_builtin::ENDLOOP:
|
||||
expand_endloop(tape, it.arg);
|
||||
break;
|
||||
case r8asm_macro::TP:;
|
||||
case r8asm_builtin::TP:;
|
||||
expand_tp(tape, it.arg);
|
||||
break;
|
||||
case r8asm_macro::XOR:
|
||||
case r8asm_builtin::XOR:
|
||||
expand_xor(tape, it.arg);
|
||||
break;
|
||||
case r8asm_macro::ANCHOR:
|
||||
case r8asm_builtin::ANCHOR:
|
||||
expand_anchor(tape, it.arg);
|
||||
break;
|
||||
default:;
|
||||
|
||||
+3
-2
@@ -3,5 +3,6 @@ unit_size_type UNIT_SIZE = DEFAULT_UNIT_SIZE;
|
||||
mem_size_type dataptr = 0;
|
||||
|
||||
std::map<std::string, r8asm_label> labels = { { std::string("main"), 0 } };
|
||||
std::map<std::string, r8asm_data> datas = { { std::string("_UNIT_MAX"),
|
||||
UNIT_MAX } };
|
||||
std::map<std::string, r8asm_data> datas = {
|
||||
{ std::string("_UNIT_MAX"), UNIT_MAX }
|
||||
};
|
||||
+18
-3
@@ -1,7 +1,22 @@
|
||||
#include "r8asm/r8asm_macro.hh"
|
||||
|
||||
std::vector<R8Instruction> expand_macro(std::string macro_name, std::vector<R8Operand> argv)
|
||||
std::map<std::string, R8Macro> macros;
|
||||
|
||||
std::vector<R8Instruction> expand_macro(std::string macro_name,
|
||||
std::vector<R8Operand> argv)
|
||||
{
|
||||
std::vector<R8Instruction> tape;
|
||||
return (tape);
|
||||
std::vector<R8Instruction> tmp_tape;
|
||||
|
||||
// TODO:explain args
|
||||
|
||||
for (auto i : macros[macro_name].ins) {
|
||||
if (std::get_if<rot8_bytecode>(&i.op) || std::get_if<r8asm_builtin>(&i.op))
|
||||
tmp_tape.push_back(i);
|
||||
else if (r8asm_macro *macro = std::get_if<r8asm_macro>(&i.op)) {
|
||||
std::vector<R8Instruction> expanded_macro = expand_macro(macro->name, macro->args);
|
||||
tmp_tape.insert(tmp_tape.end(), expanded_macro.begin(), expanded_macro.end());
|
||||
}
|
||||
}
|
||||
|
||||
return (tmp_tape);
|
||||
}
|
||||
+9
-4
@@ -1,14 +1,19 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "r8asm/r8asm.hh"
|
||||
#include "r8asm/r8asm_core.hh"
|
||||
|
||||
int main()
|
||||
{
|
||||
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 }
|
||||
R8Instruction { rot8_bytecode::INP }, // To make clang-format distribute all
|
||||
// instructions into diffirent lines.
|
||||
{ r8asm_builtin::LOOP },
|
||||
{ rot8_bytecode::BIZ },
|
||||
{ rot8_bytecode::FLB },
|
||||
{ rot8_bytecode::RNZ },
|
||||
{ rot8_bytecode::ROR },
|
||||
{ r8asm_builtin::ENDLOOP },
|
||||
};
|
||||
std::vector<rot8_bytecode> tape = r8asm_tape_out(ins);
|
||||
|
||||
|
||||
Reference in new issue
Block a user