202604230104
This commit is contained in:
1 parent
326591317c
commit
4d842917c2
4 files changed
+64
-38
No files matched your search
+1
-1
@@ -1,4 +1,4 @@
|
||||
BasedOnStyle: Webkit
|
||||
BasedOnStyle: LLVM
|
||||
IndentWidth: 4
|
||||
UseTab: Always
|
||||
PointerAlignment: Right
|
||||
@@ -32,10 +32,12 @@ enum class r8asm_builtin {
|
||||
TP,
|
||||
XOR,
|
||||
ANCHOR,
|
||||
ERASE,
|
||||
MACRO,
|
||||
ENDMACRO,
|
||||
};
|
||||
using R8Operand = std::variant<std::monostate, r8asm_data, std::string>;
|
||||
|
||||
using R8Operand = std::variant<std::monostate, r8asm_data, std::string, std::vector<std::string>>;
|
||||
|
||||
struct r8asm_macro {
|
||||
std::string name;
|
||||
|
||||
+34
-28
@@ -5,7 +5,8 @@
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
r8asm_data read_opr(R8Operand arg, r8asm_data default_opr = 0) // Attention,default_opr is 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,8 +17,7 @@ r8asm_data read_opr(R8Operand arg, r8asm_data default_opr = 0) // Attention,defa
|
||||
return (default_opr);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
for (unit_size_type i = 0; i < UNIT_SIZE; i++) {
|
||||
@@ -28,26 +28,22 @@ void expand_xor(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);
|
||||
void expand_loop(std::vector<rot8_bytecode> &tape, R8Operand arg) {
|
||||
std::vector<rot8_bytecode> tmp = {rot8_bytecode::STP, rot8_bytecode::BIZ,
|
||||
rot8_bytecode::BTP}; // >[<
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
void expand_tp(std::vector<rot8_bytecode> &tape, R8Operand arg, bool count_offset = true)
|
||||
{
|
||||
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());
|
||||
}
|
||||
|
||||
void expand_tp(std::vector<rot8_bytecode> &tape, R8Operand arg,
|
||||
bool count_offset = true) {
|
||||
r8asm_data opr = read_opr(arg);
|
||||
|
||||
unit_offset_type offset = (unit_offset_type)opr - (unit_offset_type)dataptr;
|
||||
@@ -60,16 +56,24 @@ void expand_tp(std::vector<rot8_bytecode> &tape, R8Operand arg, bool count_offse
|
||||
dataptr = opr;
|
||||
}
|
||||
|
||||
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 });
|
||||
datas.insert({*name, dataptr});
|
||||
else if (const r8asm_data *name = std::get_if<r8asm_data>(&arg))
|
||||
datas.insert({ std::to_string(*name), dataptr });
|
||||
datas.insert({std::to_string(*name), dataptr});
|
||||
}
|
||||
|
||||
void expand_bytecode(std::vector<rot8_bytecode> &tape, R8Operand arg, rot8_bytecode op, bool count_offset = true)
|
||||
{
|
||||
void expand_erase(std::vector<rot8_bytecode> &tape, R8Operand arg) {
|
||||
std::vector<rot8_bytecode> tmp = {rot8_bytecode::BIZ, rot8_bytecode::FLB,
|
||||
rot8_bytecode::ROR,
|
||||
rot8_bytecode::RNZ}; // [-+]
|
||||
for (unit_size_type i = 0; i < UNIT_SIZE; i++)
|
||||
tape.insert(tape.end(), tmp.begin(), tmp.end());
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -82,13 +86,13 @@ void expand_bytecode(std::vector<rot8_bytecode> &tape, R8Operand arg, rot8_bytec
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<rot8_bytecode> r8asm_tape_out(std::span<const R8Instruction> ops)
|
||||
{
|
||||
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)) {
|
||||
expand_bytecode(tape, it.arg, *bc);
|
||||
} else if (const r8asm_builtin *builtin = std::get_if<r8asm_builtin>(&it.op))
|
||||
} else if (const r8asm_builtin *builtin =
|
||||
std::get_if<r8asm_builtin>(&it.op))
|
||||
switch (*builtin) {
|
||||
case r8asm_builtin::LOOP:
|
||||
expand_loop(tape, it.arg);
|
||||
@@ -104,6 +108,8 @@ std::vector<rot8_bytecode> r8asm_tape_out(std::span<const R8Instruction> ops)
|
||||
break;
|
||||
case r8asm_builtin::ANCHOR:
|
||||
expand_anchor(tape, it.arg);
|
||||
case r8asm_builtin::ERASE:
|
||||
expand_erase(tape, it.arg);
|
||||
break;
|
||||
default:;
|
||||
}
|
||||
|
||||
+26
-8
@@ -1,20 +1,38 @@
|
||||
#include "r8asm/r8asm_macro.hh"
|
||||
#include "r8asm/r8asm_core.hh"
|
||||
|
||||
std::map<std::string, R8Macro> macros;
|
||||
|
||||
R8Operand read_arg(std::vector<R8Operand> &argv, R8Operand argf,
|
||||
argc_type argc) {
|
||||
if (std::string *str = std::get_if<std::string>(&argf)) {
|
||||
if ((*str) == "%0")
|
||||
return ((r8asm_data)argc);
|
||||
else if ((*str)[0] == '%')
|
||||
return (argv[std::stoi(str->substr(1)) - 1]);
|
||||
// e.g. "%15" -> "15" -> 14 -> argv[14]
|
||||
}
|
||||
return (argf);
|
||||
}
|
||||
|
||||
std::vector<R8Instruction> expand_macro(std::string macro_name,
|
||||
std::vector<R8Operand> argv)
|
||||
{
|
||||
std::vector<R8Operand> argv) {
|
||||
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);
|
||||
if (std::get_if<rot8_bytecode>(&i.op) ||
|
||||
std::get_if<r8asm_builtin>(&i.op))
|
||||
tmp_tape.push_back(
|
||||
{i.op, read_arg(argv, i.arg, macros[macro_name].argc)});
|
||||
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());
|
||||
std::vector<R8Operand> macro_args;
|
||||
for (auto j : macro->args)
|
||||
macro_args.push_back(
|
||||
read_arg(argv, j, macros[macro_name].argc));
|
||||
std::vector<R8Instruction> macro_body =
|
||||
expand_macro(macro->name, macro_args);
|
||||
tmp_tape.insert(tmp_tape.end(), macro_body.begin(),
|
||||
macro_body.end());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user