202607040922

This commit is contained in:
starcells committed 2026-07-04 09:22:42 +08:00
1 parent e7537727f4
commit 4593d7055f
8 files changed
+47 -45

No files matched your search

+12 -11
View File
@@ -7,12 +7,13 @@
#include <variant>
#include <vector>
typedef uint32_t r8asm_label;
typedef int32_t r8asm_data;
typedef uint8_t cell_size_type;
typedef int32_t cell_offset_type;
typedef uint32_t mem_size_type;
typedef uint32_t argc_type;
using r8asm_label = uint32_t;
using r8asm_cell_size = uint8_t;
using r8asm_cell_offset = int32_t;
using r8asm_mem_size = uint32_t;
using r8asm_argc = r8asm_mem_size;
using r8asm_data = int32_t;
#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast<int>(bc)])
@@ -59,16 +60,16 @@ struct R8Instruction {
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, r8asm_label>
r8asm_labelmap; // TODO:sections support
extern std::map<std::string, r8asm_data> r8asm_datamap;
extern r8asm_mem_size rot8_dataptr;
extern std::map<std::string, R8MetaOp> r8asm_insmap;
extern cell_size_type CELL_SIZE;
extern r8asm_cell_size CELL_SIZE;
#define CELL_MAX (((uint64_t)1 << CELL_SIZE) - 1)
// #define DEFAULT_cell_SIZE (sizeof(cell_size_type) * 8)
#define DEFAULT_CELL_SIZE 8 // Just for test.
#define DEFAULT_CELL_MAX (((uint64_t)1 << DEFAULT_CELL_SIZE) - 1)
+4 -4
View File
@@ -4,14 +4,14 @@
#include "r8asm/r8asm_core.hh"
#include <vector>
struct R8Macro {
argc_type argc = 0;
r8asm_argc argc = 0;
std::vector<R8Instruction> ins;
};
extern std::map<std::string, R8Macro> r8asm_macromap;
std::vector<R8Instruction> expand_single_macro(std::string macro_name,
std::vector<R8Operand> argv);
std::vector<R8Instruction>
r8asm_expand_single_macro(std::string macro_name, std::vector<R8Operand> argv);
std::vector<R8Instruction> expand_macros(std::vector<R8Instruction> ins);
std::vector<R8Instruction> r8asm_expand_macros(std::vector<R8Instruction> ins);
#endif
+1 -1
View File
@@ -20,7 +20,7 @@ struct R8Src {
void preprocess_lines();
};
R8Src read_src(std::string filename);
R8Src r8asm_read_src(std::string filename);
std::vector<R8Instruction> r8asm_preprocess(R8Src &src);
extern std::unordered_map<r8asm_parse, std::string> r8asm_parsemap;
+11 -10
View File
@@ -10,8 +10,8 @@ r8asm_data read_opr(R8Operand arg,
{
if (const std::string *operand = std::get_if<std::string>(&arg)) {
if ((*operand) == "$")
return dataptr;
return (datas[*operand]);
return rot8_dataptr;
return (r8asm_datamap[*operand]);
} else if (const r8asm_data *operand = std::get_if<r8asm_data>(&arg))
return (*operand);
return (default_opr);
@@ -20,7 +20,7 @@ r8asm_data read_opr(R8Operand arg,
void expand_xor(std::vector<rot8_bytecode> &tape, R8Operand arg) {
r8asm_data real_arg = read_opr(arg);
for (cell_size_type i = 0; i < CELL_SIZE; i++) {
for (r8asm_cell_size i = 0; i < CELL_SIZE; i++) {
if ((real_arg & 1) == 1)
tape.push_back(rot8_bytecode::FLB);
tape.push_back(rot8_bytecode::ROR);
@@ -46,21 +46,22 @@ void expand_tp(std::vector<rot8_bytecode> &tape, R8Operand arg,
bool count_offset = true) {
r8asm_data opr = read_opr(arg);
cell_offset_type offset = (cell_offset_type)opr - (cell_offset_type)dataptr;
r8asm_cell_offset offset =
(r8asm_cell_offset)opr - (r8asm_cell_offset)rot8_dataptr;
bool stepforward = (offset > 0);
offset = abs(offset);
for (cell_offset_type i = 0; i < offset; i++) {
for (r8asm_cell_offset i = 0; i < offset; i++) {
tape.push_back(stepforward ? rot8_bytecode::STP : rot8_bytecode::BTP);
}
if (count_offset)
dataptr = opr;
rot8_dataptr = opr;
}
void expand_anchor(std::vector<rot8_bytecode> &tape, R8Operand arg) {
if (const std::string *name = std::get_if<std::string>(&arg))
datas.insert_or_assign(*name, dataptr);
r8asm_datamap.insert_or_assign(*name, rot8_dataptr);
else if (const r8asm_data *name = std::get_if<r8asm_data>(&arg))
datas.insert_or_assign(std::to_string(*name), dataptr);
r8asm_datamap.insert_or_assign(std::to_string(*name), rot8_dataptr);
}
void expand_bytecode(std::vector<rot8_bytecode> &tape, R8Operand arg,
@@ -69,9 +70,9 @@ void expand_bytecode(std::vector<rot8_bytecode> &tape, R8Operand arg,
for (r8asm_data i = 0; i < repeat; i++) {
tape.push_back(op);
if (op == rot8_bytecode::STP)
dataptr++;
rot8_dataptr++;
else if (op == rot8_bytecode::BTP)
dataptr--;
rot8_dataptr--;
}
}
+4 -4
View File
@@ -1,9 +1,9 @@
#include "r8asm/r8asm_core.hh"
cell_size_type CELL_SIZE = DEFAULT_CELL_SIZE;
mem_size_type dataptr = 0;
r8asm_cell_size CELL_SIZE = DEFAULT_CELL_SIZE;
r8asm_mem_size rot8_dataptr = 0;
std::map<std::string, r8asm_label> labels = {{std::string("main"), 0}};
std::map<std::string, r8asm_data> datas = {
std::map<std::string, r8asm_label> r8asm_labelmap = {{std::string("main"), 0}};
std::map<std::string, r8asm_data> r8asm_datamap = {
{std::string("_CELL_MAX"), DEFAULT_CELL_MAX},
{std::string{"_CELL_SIZE"}, DEFAULT_CELL_SIZE}};
+7 -7
View File
@@ -5,7 +5,7 @@
std::map<std::string, R8Macro> r8asm_macromap;
R8Operand read_arg(std::vector<R8Operand> &actual_args, R8Operand formal_para,
argc_type argc) {
r8asm_argc argc) {
if (std::string *str = std::get_if<std::string>(&formal_para)) {
if ((*str) == "%0")
return ((r8asm_data)argc);
@@ -17,8 +17,8 @@ R8Operand read_arg(std::vector<R8Operand> &actual_args, R8Operand formal_para,
}
std::vector<R8Instruction>
expand_single_macro(std::string macro_name,
std::vector<R8Operand> actual_args) {
r8asm_expand_single_macro(std::string macro_name,
std::vector<R8Operand> actual_args) {
std::vector<R8Instruction> tmp_tape;
R8Macro this_macro = r8asm_macromap[macro_name];
@@ -33,7 +33,7 @@ expand_single_macro(std::string macro_name,
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(tmp_macro->name, macro_args);
r8asm_expand_single_macro(tmp_macro->name, macro_args);
tmp_tape.insert(tmp_tape.end(), macro_body.begin(),
macro_body.end());
}
@@ -42,12 +42,12 @@ expand_single_macro(std::string macro_name,
return (tmp_tape);
}
std::vector<R8Instruction> expand_macros(std::vector<R8Instruction> ins) {
std::vector<R8Instruction> r8asm_expand_macros(std::vector<R8Instruction> ins) {
std::vector<R8Instruction> tape;
for (auto i : ins) {
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));
std::vector<R8Instruction> tmp_tape = (r8asm_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);
+6 -6
View File
@@ -44,7 +44,7 @@ std::string r8asm_peel(std::string &str) { // Peel the first word from string.
}
}
R8Src read_src(std::string filename) {
R8Src r8asm_read_src(std::string filename) {
R8Src src;
std::ifstream ifile;
@@ -82,8 +82,8 @@ R8Operand r8asm_arg_stoi(std::string raw_arg) { // TODO:expression evaluating
r8asm_data arg = std::stoi(raw_arg);
return (arg);
} catch (std::invalid_argument) {
if (datas.find(raw_arg) != datas.end())
return (datas[raw_arg]);
if (r8asm_datamap.find(raw_arg) != r8asm_datamap.end())
return (r8asm_datamap[raw_arg]);
return (raw_arg);
}
}
@@ -111,11 +111,11 @@ void R8Src::preprocess_lines() {
for (auto i = (this->raw_src).begin(); i != (this->raw_src).end();) {
auto tmp_i = i;
if ((*i)[0] == r8asm_parsemap[r8asm_parse::VAR]) {
datas.insert_or_assign(
r8asm_datamap.insert_or_assign(
(*i)[1], std::get<r8asm_data>(r8asm_arg_stoi((*i)[2])));
i = this->raw_src.erase(i);
} else if ((*i)[0] == r8asm_parsemap[r8asm_parse::INCLUDE]) {
tmp_src = read_src((*i)[1]);
tmp_src = r8asm_read_src((*i)[1]);
tmp_src.preprocess_lines();
tmp_src.preprocess_blocks();
i = this->raw_src.erase(i);
@@ -141,7 +141,7 @@ void R8Src::preprocess_blocks() {
tmp_codeblock.push_back(r8asm_translate_single_ins(j));
if ((*i.first)[0] == r8asm_parsemap[r8asm_parse::MACRO]) {
r8asm_macromap.insert_or_assign(
(*i.first)[1], R8Macro{(argc_type)std::get<r8asm_data>(
(*i.first)[1], R8Macro{(r8asm_argc)std::get<r8asm_data>(
r8asm_arg_stoi((*i.first)[2])),
tmp_codeblock});
} else if ((*i.first)[0] == r8asm_parsemap[r8asm_parse::REPEAT]) {
+2 -2
View File
@@ -4,8 +4,8 @@
int main(int argc, char *argv[]) {
std::string src_filename = argv[1];
auto src = read_src(src_filename.c_str());
auto out_tape = r8asm_tape_out(expand_macros(r8asm_preprocess(src)));
auto src = r8asm_read_src(src_filename.c_str());
auto out_tape = r8asm_tape_out(r8asm_expand_macros(r8asm_preprocess(src)));
for (auto i : out_tape)
std::cout << ROT8_BC_TO_CHAR(i);
std::cout << std::endl;