Finished include parse

This commit is contained in:
starcells committed 2026-07-02 19:00:38 +08:00
1 parent c92f8b7da6
commit 3ecca2cfa8
10 files changed
+56 -39

No files matched your search

+5
View File
@@ -1,3 +1,8 @@
%include asm/lib.r8asm
%include asm/and.r8asm
%include asm/or.r8asm
%include asm/xor.r8asm
; Macro bit_add
; %1: UnitBit A
; %2: UnitBit B
+2
View File
@@ -1,3 +1,5 @@
%include asm/lib.r8asm
; Macro bit_unsafe_and
; %1: CellBit arg1
; %2: CellBit arg2
+1
View File
@@ -1,3 +1,4 @@
%include asm/add.r8asm
.xor 123
.tp 2
+1
View File
@@ -1,3 +1,4 @@
%include asm/lib.r8asm
; Macro bit_unsafe_or
; %1: CellBit arg1
; %2: CellBit arg2
+1
View File
@@ -1,3 +1,4 @@
%include asm/lib.r8asm
; Macro bit_unsafe_xor
; %1: CellBit arg1
; %2: CellBit arg2
+6 -1
View File
@@ -33,11 +33,15 @@ enum class r8asm_builtin {
XOR,
ANCHOR,
ERASE,
};
enum class r8asm_parse {
MACRO,
ENDMACRO,
REPEAT,
ENDREPEAT,
INCLUDE,
VAR,
};
using R8Operand = std::variant<std::monostate, r8asm_data, std::string>;
@@ -47,7 +51,8 @@ struct r8asm_macrocall {
std::vector<R8Operand> actual_args;
};
using R8MetaOp = std::variant<r8asm_builtin, r8asm_macrocall, rot8_bytecode>;
using R8MetaOp =
std::variant<r8asm_builtin, r8asm_macrocall, rot8_bytecode, r8asm_parse>;
struct R8Instruction {
R8MetaOp op;
+4 -4
View File
@@ -3,11 +3,10 @@
#include "r8asm/r8asm_core.hh"
#include <list>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <utility>
#include <queue>
#include <vector>
#define EMPTY_CHARS (" \t\n\r\f\v")
@@ -17,11 +16,12 @@ typedef std::list<std::vector<std::string>>::iterator r8asm_src_point;
struct R8Src {
std::list<std::vector<std::string>> raw_src;
std::queue<std::pair<r8asm_src_point, r8asm_src_point>> codeblocks;
void preprocess();
void preprocess_blocks();
void preprocess_lines();
};
R8Src read_src(std::string filename);
std::vector<R8Instruction> r8asm_preprocess(R8Src &src);
extern std::map<std::string, R8MetaOp> r8asm_parsemap;
extern std::unordered_map<r8asm_parse, std::string> r8asm_parsemap;
#endif
+5 -17
View File
@@ -63,27 +63,15 @@ void expand_anchor(std::vector<rot8_bytecode> &tape, R8Operand arg) {
datas.insert_or_assign(std::to_string(*name), dataptr);
}
/*
void expand_erase(std::vector<rot8_bytecode> &tape, R8Operand arg) {
std::vector<rot8_bytecode> tmp = {rot8_bytecode::BIZ, rot8_bytecode::FLB,
rot8_bytecode::RNZ,
rot8_bytecode::ROR}; // [-]+
for (cell_size_type i = 0; i < cell_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) {
rot8_bytecode op) {
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--;
}
if (op == rot8_bytecode::STP)
dataptr++;
else if (op == rot8_bytecode::BTP)
dataptr--;
}
}
+30 -11
View File
@@ -11,12 +11,10 @@
#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},
{"%include", r8asm_builtin::INCLUDE}, // TODO:Expand include
std::unordered_map<r8asm_parse, std::string> r8asm_parsemap = {
{r8asm_parse::MACRO, "%macro"}, {r8asm_parse::ENDMACRO, "%endmacro"},
{r8asm_parse::REPEAT, "%repeat"}, {r8asm_parse::ENDREPEAT, "%endrepeat"},
{r8asm_parse::INCLUDE, "%include"}, {r8asm_parse::VAR, "%var"},
};
void r8asm_trim(std::string &str) { // Clean empty chars in the string.
@@ -66,7 +64,8 @@ R8Src read_src(std::string filename) {
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") {
if (ins_buffer[0] == r8asm_parsemap[r8asm_parse::ENDMACRO] ||
ins_buffer[0] == r8asm_parsemap[r8asm_parse::ENDREPEAT]) {
src.codeblocks.push(
{matcher.top(), std::prev(src.raw_src.end())});
matcher.pop();
@@ -107,7 +106,25 @@ r8asm_translate_single_ins(std::vector<std::string> const raw_ins) {
return (R8Instruction{});
}
void R8Src::preprocess() {
void R8Src::preprocess_lines() {
R8Src tmp_src;
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(
(*i)[1], std::get<r8asm_data>(r8asm_arg_stoi((*i)[1])));
i = this->raw_src.erase(i);
} else if ((*i)[0] == r8asm_parsemap[r8asm_parse::INCLUDE]) {
tmp_src = read_src((*i)[1]);
tmp_src.preprocess_lines();
tmp_src.preprocess_blocks();
i = this->raw_src.erase(i);
} else
++i;
}
}
void R8Src::preprocess_blocks() {
std::vector<R8Instruction> tmp_codeblock;
while (!this->codeblocks.empty()) {
auto i = this->codeblocks.front();
@@ -122,12 +139,12 @@ void R8Src::preprocess() {
*/
for (auto j : raw_codeblock)
tmp_codeblock.push_back(r8asm_translate_single_ins(j));
if ((*i.first)[0] == "%macro") {
if ((*i.first)[0] == r8asm_parsemap[r8asm_parse::MACRO]) {
r8asm_macromap.insert_or_assign(
(*i.first)[1], R8Macro{(argc_type)std::get<r8asm_data>(
r8asm_arg_stoi((*i.first)[2])),
tmp_codeblock});
} else if ((*i.first)[0] == "%repeat") {
} else if ((*i.first)[0] == r8asm_parsemap[r8asm_parse::REPEAT]) {
for (auto count =
std::get<r8asm_data>(r8asm_arg_stoi((*i.first)[1]));
count > 0; count--)
@@ -140,7 +157,9 @@ void R8Src::preprocess() {
std::vector<R8Instruction> r8asm_preprocess(R8Src &src) {
std::vector<R8Instruction> tmp_ins;
src.preprocess();
src.preprocess_lines();
src.preprocess_blocks();
for (auto i : src.raw_src) {
tmp_ins.push_back(r8asm_translate_single_ins(i));
}
+1 -6
View File
@@ -4,12 +4,7 @@
#include "r8asm/r8asm_parse.hh"
int main(int argc, char *argv[]) {
std::string src_filename;
src_filename = argv[argc - 1];
for (auto i = 1; i < argc - 1; i++) {
auto r8lib = read_src(argv[i]);
r8lib.preprocess();
}
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)));
for (auto i : out_tape)