First workable version with preprocessing ability!
This commit is contained in:
1 parent
169bb3a756
commit
725b2ae2ce
9 files changed
+165
-80
No files matched your search
@@ -0,0 +1,12 @@
|
||||
%macro test_erase 1
|
||||
.tp %1
|
||||
.loop _UNIT_MAX
|
||||
biz
|
||||
flb
|
||||
rnz
|
||||
ror
|
||||
.endloop
|
||||
%endmacro
|
||||
|
||||
.xor 15
|
||||
test_erase $
|
||||
@@ -35,27 +35,30 @@ enum class r8asm_builtin {
|
||||
ERASE,
|
||||
MACRO,
|
||||
ENDMACRO,
|
||||
REPEAT,
|
||||
ENDREPEAT,
|
||||
};
|
||||
|
||||
using R8Operand = std::variant<std::monostate, r8asm_data, std::string,
|
||||
std::vector<std::string>>;
|
||||
using R8Operand = std::variant<std::monostate, r8asm_data, std::string>;
|
||||
|
||||
struct r8asm_macro {
|
||||
struct r8asm_macrocall {
|
||||
std::string name;
|
||||
std::vector<R8Operand> args;
|
||||
std::vector<R8Operand> actual_args;
|
||||
};
|
||||
|
||||
using R8MetaOp = std::variant<r8asm_builtin, r8asm_macro, rot8_bytecode>;
|
||||
using R8MetaOp = std::variant<r8asm_builtin, r8asm_macrocall, rot8_bytecode>;
|
||||
|
||||
struct R8Instruction {
|
||||
R8MetaOp op;
|
||||
R8Operand arg;
|
||||
R8Operand formal_para;
|
||||
};
|
||||
|
||||
extern std::map<std::string, r8asm_label> labels;
|
||||
extern std::map<std::string, r8asm_data> datas;
|
||||
extern mem_size_type dataptr;
|
||||
|
||||
extern std::map<std::string, R8MetaOp> r8asm_insmap;
|
||||
|
||||
extern unit_size_type UNIT_SIZE;
|
||||
// #define DEFAULT_UNIT_SIZE (sizeof(unit_size_type) * 8)
|
||||
#define DEFAULT_UNIT_SIZE 8 // Just for test.
|
||||
|
||||
@@ -7,11 +7,11 @@ struct R8Macro {
|
||||
argc_type argc = 0;
|
||||
std::vector<R8Instruction> ins;
|
||||
};
|
||||
extern std::map<std::string, R8Macro> macros;
|
||||
extern std::map<std::string, R8Macro> r8asm_macromap;
|
||||
|
||||
std::vector<R8Instruction> expand_single_macro(std::string macro_name,
|
||||
std::vector<R8Operand> argv);
|
||||
std::vector<R8Operand> argv);
|
||||
|
||||
std::vector<R8Instruction> expand_macros(std::vector<R8Instruction> &ins);
|
||||
std::vector<R8Instruction> expand_macros(std::vector<R8Instruction> ins);
|
||||
|
||||
#endif
|
||||
@@ -2,24 +2,25 @@
|
||||
#define R8ASM_PARSE
|
||||
|
||||
#include "r8asm/r8asm_core.hh"
|
||||
#include <cstddef>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#define EMPTY_CHARS (" \t\n\r\f\v")
|
||||
|
||||
typedef size_t ins_type_sign;
|
||||
typedef std::list<std::vector<std::string>>::iterator r8asm_src_point;
|
||||
|
||||
struct r8asm_src {
|
||||
std::vector<std::vector<std::string>> raw_src;
|
||||
std::vector<std::pair<ins_type_sign, ins_type_sign>> codeblocks;
|
||||
struct R8Src {
|
||||
std::list<std::vector<std::string>> raw_src;
|
||||
std::stack<std::pair<r8asm_src_point, r8asm_src_point>> codeblocks;
|
||||
void preprocess();
|
||||
};
|
||||
|
||||
r8asm_src read_src(std::string filename);
|
||||
|
||||
void include_src(r8asm_src);
|
||||
|
||||
std::vector<R8Instruction> r8asm_preprocess(r8asm_src &src);
|
||||
R8Src read_src(std::string filename);
|
||||
std::vector<R8Instruction> r8asm_preprocess(R8Src &src);
|
||||
extern std::map<std::string, R8MetaOp> r8asm_parsemap;
|
||||
|
||||
#endif
|
||||
+7
-7
@@ -90,27 +90,27 @@ 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);
|
||||
expand_bytecode(tape, it.formal_para, *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);
|
||||
expand_loop(tape, it.formal_para);
|
||||
break;
|
||||
case r8asm_builtin::ENDLOOP:
|
||||
expand_endloop(tape, it.arg);
|
||||
expand_endloop(tape, it.formal_para);
|
||||
break;
|
||||
case r8asm_builtin::TP:;
|
||||
expand_tp(tape, it.arg);
|
||||
expand_tp(tape, it.formal_para);
|
||||
break;
|
||||
case r8asm_builtin::XOR:
|
||||
expand_xor(tape, it.arg);
|
||||
expand_xor(tape, it.formal_para);
|
||||
break;
|
||||
case r8asm_builtin::ANCHOR:
|
||||
expand_anchor(tape, it.arg);
|
||||
expand_anchor(tape, it.formal_para);
|
||||
case r8asm_builtin::ERASE:
|
||||
break;
|
||||
expand_erase(tape, it.arg);
|
||||
expand_erase(tape, it.formal_para);
|
||||
break;
|
||||
default:;
|
||||
}
|
||||
|
||||
+12
-3
@@ -2,7 +2,16 @@
|
||||
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_label> labels = {{std::string("main"), 0}};
|
||||
std::map<std::string, r8asm_data> datas = {
|
||||
{ std::string("_UNIT_MAX"), UNIT_MAX }
|
||||
};
|
||||
{std::string("_UNIT_MAX"), UNIT_MAX}};
|
||||
|
||||
std::map<std::string, R8MetaOp> r8asm_insmap = {
|
||||
{"stp", rot8_bytecode::STP}, {"btp", rot8_bytecode::BTP},
|
||||
{"ror", rot8_bytecode::ROR}, {"flb", rot8_bytecode::FLB},
|
||||
{"biz", rot8_bytecode::BIZ}, {"rnz", rot8_bytecode::RNZ},
|
||||
{"out", rot8_bytecode::OUT}, {"inp", rot8_bytecode::INP},
|
||||
{".loop", r8asm_builtin::LOOP}, {".endloop", r8asm_builtin::ENDLOOP},
|
||||
{".anchor", r8asm_builtin::ANCHOR}, {".tp", r8asm_builtin::TP},
|
||||
{".xor", r8asm_builtin::XOR}, {".erase", r8asm_builtin::ERASE},
|
||||
};
|
||||
+21
-22
@@ -1,9 +1,8 @@
|
||||
#include "r8asm/r8asm_macro.hh"
|
||||
#include "r8asm/r8asm_core.hh"
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
std::map<std::string, R8Macro> macros = {
|
||||
std::map<std::string, R8Macro> r8asm_macromap = {
|
||||
{std::string("STEP"),
|
||||
{1,
|
||||
{{rot8_bytecode::STP, std::string("%1")},
|
||||
@@ -13,35 +12,36 @@ std::map<std::string, R8Macro> macros = {
|
||||
{{rot8_bytecode::BTP, std::string("%1")},
|
||||
{rot8_bytecode::BTP, std::string("%1")}}}}};
|
||||
|
||||
R8Operand read_arg(std::vector<R8Operand> &argv, R8Operand argf,
|
||||
R8Operand read_arg(std::vector<R8Operand> &actual_args, R8Operand formal_para,
|
||||
argc_type argc) {
|
||||
if (std::string *str = std::get_if<std::string>(&argf)) {
|
||||
if (std::string *str = std::get_if<std::string>(&formal_para)) {
|
||||
if ((*str) == "%0")
|
||||
return ((r8asm_data)argc);
|
||||
else if ((*str)[0] == '%')
|
||||
return (argv[std::stoi(str->substr(1)) - 1]);
|
||||
return (actual_args[std::stoi(str->substr(1)) - 1]);
|
||||
// e.g. "%15" -> "15" -> 14 -> argv[14]
|
||||
}
|
||||
return (argf);
|
||||
return (formal_para);
|
||||
}
|
||||
|
||||
std::vector<R8Instruction> expand_single_macro(std::string macro_name,
|
||||
std::vector<R8Operand> argv) {
|
||||
std::vector<R8Instruction>
|
||||
expand_single_macro(std::string macro_name,
|
||||
std::vector<R8Operand> actual_args) {
|
||||
std::vector<R8Instruction> tmp_tape;
|
||||
std::vector<R8Operand> macro_args;
|
||||
R8Macro this_macro = r8asm_macromap[macro_name];
|
||||
|
||||
for (auto i : macros[macro_name].ins) {
|
||||
for (auto i : this_macro.ins) {
|
||||
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)) {
|
||||
macro_args.clear();
|
||||
for (auto j : macro->args)
|
||||
macro_args.push_back(
|
||||
read_arg(argv, j, macros[macro_name].argc));
|
||||
{i.op, read_arg(actual_args, i.formal_para, this_macro.argc)});
|
||||
else if (r8asm_macrocall *tmp_macro =
|
||||
std::get_if<r8asm_macrocall>(&i.op)) {
|
||||
std::vector<R8Operand> macro_args;
|
||||
for (auto j : tmp_macro->actual_args)
|
||||
macro_args.push_back(read_arg(actual_args, j, this_macro.argc));
|
||||
std::vector<R8Instruction> macro_body =
|
||||
expand_single_macro(macro->name, macro_args);
|
||||
expand_single_macro(tmp_macro->name, macro_args);
|
||||
tmp_tape.insert(tmp_tape.end(), macro_body.begin(),
|
||||
macro_body.end());
|
||||
}
|
||||
@@ -50,13 +50,12 @@ std::vector<R8Instruction> expand_single_macro(std::string macro_name,
|
||||
return (tmp_tape);
|
||||
}
|
||||
|
||||
std::vector<R8Instruction> expand_macros(std::vector<R8Instruction> &ins) {
|
||||
std::vector<R8Instruction> expand_macros(std::vector<R8Instruction> ins) {
|
||||
std::vector<R8Instruction> tape;
|
||||
std::vector<R8Instruction> tmp_tape;
|
||||
for (auto i : ins) {
|
||||
tmp_tape.clear();
|
||||
if (r8asm_macro *macro = std::get_if<r8asm_macro>(&i.op)) {
|
||||
tmp_tape = (expand_single_macro(macro->name, macro->args));
|
||||
if (r8asm_macrocall *tmp_macro = std::get_if<r8asm_macrocall>(&i.op)) {
|
||||
std::vector<R8Instruction> tmp_tape =
|
||||
(expand_single_macro(tmp_macro->name, tmp_macro->actual_args));
|
||||
tape.insert(tape.end(), tmp_tape.begin(), tmp_tape.end());
|
||||
} else
|
||||
tape.push_back(i);
|
||||
|
||||
+87
-11
@@ -1,11 +1,22 @@
|
||||
#include "r8asm/r8asm_parse.hh"
|
||||
#include "r8asm/r8asm_core.hh"
|
||||
#include "r8asm/r8asm_macro.hh"
|
||||
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <ranges>
|
||||
#include <stack>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
std::map<std::string, R8MetaOp> r8asm_parsemap = {
|
||||
{"%macro", r8asm_builtin::MACRO},
|
||||
{"%endmacro", r8asm_builtin::ENDMACRO},
|
||||
{"%repeat", r8asm_builtin::REPEAT},
|
||||
{"%endrepeat", r8asm_builtin::ENDREPEAT},
|
||||
};
|
||||
|
||||
void r8asm_trim(std::string &str) {
|
||||
auto start = str.find_first_not_of(EMPTY_CHARS);
|
||||
|
||||
@@ -33,15 +44,15 @@ std::string r8asm_peel(std::string &str) {
|
||||
}
|
||||
}
|
||||
|
||||
r8asm_src read_src(std::string filename) {
|
||||
r8asm_src src;
|
||||
R8Src read_src(std::string filename) {
|
||||
R8Src src;
|
||||
|
||||
ins_type_sign line_counter = 0;
|
||||
std::ifstream ifile;
|
||||
std::string line_buffer;
|
||||
ifile.open(filename, std::ios::in);
|
||||
|
||||
std::string line_buffer;
|
||||
std::vector<std::string> ins_buffer;
|
||||
std::stack<ins_type_sign> matcher;
|
||||
std::stack<r8asm_src_point> matcher;
|
||||
while (std::getline(ifile, line_buffer)) {
|
||||
ins_buffer.clear();
|
||||
auto comment = line_buffer.find_first_of(';');
|
||||
@@ -49,22 +60,87 @@ r8asm_src read_src(std::string filename) {
|
||||
line_buffer = line_buffer.substr(0, comment);
|
||||
if (line_buffer.empty())
|
||||
continue;
|
||||
line_counter++;
|
||||
while (!line_buffer.empty())
|
||||
ins_buffer.push_back(r8asm_peel(line_buffer));
|
||||
src.raw_src.push_back({ins_buffer});
|
||||
if (ins_buffer[0][0] == '%') {
|
||||
if (ins_buffer[0].substr(1, 3) == "end") {
|
||||
src.codeblocks.push_back({matcher.top(), line_counter});
|
||||
src.codeblocks.push(
|
||||
{matcher.top(), std::prev(src.raw_src.end())});
|
||||
matcher.pop();
|
||||
} else
|
||||
matcher.push(line_counter);
|
||||
matcher.push(--src.raw_src.end());
|
||||
}
|
||||
}
|
||||
ifile.close();
|
||||
return (src);
|
||||
}
|
||||
|
||||
std::vector<R8Instruction> r8asm_preprocess(r8asm_src &src) {
|
||||
std::vector<R8Instruction> ins;
|
||||
R8Operand r8asm_arg_stoi(std::string raw_arg) {
|
||||
try {
|
||||
r8asm_data arg = std::stoi(raw_arg);
|
||||
return (arg);
|
||||
} catch (std::invalid_argument) {
|
||||
if (datas.find(raw_arg) != datas.end())
|
||||
return (datas[raw_arg]);
|
||||
return (raw_arg);
|
||||
}
|
||||
}
|
||||
|
||||
R8Instruction
|
||||
r8asm_translate_single_ins(std::vector<std::string> const raw_ins) {
|
||||
R8MetaOp tmp_op;
|
||||
if (r8asm_insmap.find(raw_ins[0]) != r8asm_insmap.end())
|
||||
if (raw_ins.size() == 1)
|
||||
return (R8Instruction{r8asm_insmap[raw_ins[0]]});
|
||||
else
|
||||
return (R8Instruction{r8asm_insmap[raw_ins[0]],
|
||||
r8asm_arg_stoi(raw_ins[1])});
|
||||
else if (r8asm_macromap.find(raw_ins[0]) != r8asm_macromap.end()) {
|
||||
std::vector<R8Operand> tmp_operands;
|
||||
for (auto i : raw_ins | std::views::drop(1))
|
||||
tmp_operands.push_back(r8asm_arg_stoi(i));
|
||||
return (R8Instruction{r8asm_macrocall{raw_ins[0], tmp_operands}});
|
||||
}
|
||||
return (R8Instruction{});
|
||||
}
|
||||
|
||||
void R8Src::preprocess() {
|
||||
std::vector<R8Instruction> codeblock;
|
||||
while (!this->codeblocks.empty()) {
|
||||
auto i = this->codeblocks.top();
|
||||
this->codeblocks.pop();
|
||||
codeblock.clear();
|
||||
auto raw_codeblock =
|
||||
std::ranges::subrange(std::next(i.first), i.second);
|
||||
/*
|
||||
src.raw_src: 0 1 2 3(%macro) 4 5 6 7(%endmacro) 8
|
||||
i:-----------------^first----------^second-------
|
||||
subrange:--------------------4 5 6---------------
|
||||
*/
|
||||
for (auto j : raw_codeblock)
|
||||
codeblock.push_back(r8asm_translate_single_ins(j));
|
||||
if ((*i.first)[0] == "%macro") {
|
||||
r8asm_macromap.insert_or_assign(
|
||||
(*i.first)[1], R8Macro{(argc_type)std::get<r8asm_data>(
|
||||
r8asm_arg_stoi((*i.first)[2])),
|
||||
codeblock});
|
||||
} else if ((*i.first)[0] == "%repeat") {
|
||||
for (auto count =
|
||||
std::get<r8asm_data>(r8asm_arg_stoi((*i.first)[1]));
|
||||
count > 0; count--)
|
||||
this->raw_src.insert(std::next(i.second), std::next(i.first),
|
||||
i.second);
|
||||
}
|
||||
this->raw_src.erase(i.first, std::next(i.second));
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<R8Instruction> r8asm_preprocess(R8Src &src) {
|
||||
std::vector<R8Instruction> tmp_ins;
|
||||
src.preprocess();
|
||||
for (auto i : src.raw_src) {
|
||||
tmp_ins.push_back(r8asm_translate_single_ins(i));
|
||||
}
|
||||
return (tmp_ins);
|
||||
}
|
||||
+3
-18
@@ -2,24 +2,9 @@
|
||||
|
||||
#include "r8asm/r8asm.hh"
|
||||
|
||||
int main() {
|
||||
std::vector<R8Instruction> ins = {
|
||||
R8Instruction{rot8_bytecode::INP},
|
||||
// To make clang-format distribute all instructions into diffirent
|
||||
// lines.
|
||||
{rot8_bytecode::STP},
|
||||
{r8asm_builtin::XOR, 255},
|
||||
{rot8_bytecode::BTP},
|
||||
{r8asm_builtin::LOOP},
|
||||
{rot8_bytecode::BIZ},
|
||||
{rot8_bytecode::FLB},
|
||||
{rot8_bytecode::RNZ},
|
||||
{rot8_bytecode::ROR},
|
||||
{rot8_bytecode::OUT},
|
||||
{r8asm_builtin::ENDLOOP},
|
||||
};
|
||||
auto out_tape = r8asm_tape_out(expand_macros(ins));
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
auto src = read_src(argc > 1 ? argv[1] : "asm/asm.r8asm");
|
||||
auto out_tape = r8asm_tape_out(expand_macros(r8asm_preprocess(src)));
|
||||
for (auto i : out_tape)
|
||||
std::cout << ROT8_BC_TO_CHAR(i);
|
||||
return (0);
|
||||
|
||||
Reference in new issue
Block a user