Files
2026-09-14 21:01:48 +08:00

182 lines
4.5 KiB
C++

#pragma once
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
class r8asm {
using data = int16_t;
using parameter_index = uint8_t;
using addr = uint32_t;
using general_data = int32_t;
using line_number = unsigned long long;
using depth_t = unsigned long long;
using precedence_t = uint8_t;
private:
enum class token_t {
STRING,
OPERATOR,
MACROCALL,
ROT8,
DIRECTIVE,
PARAMETER,
BUILTIN,
LPAREN,
RPAREN,
LQUOTE,
RQUOTE,
ENDL,
};
struct token {
token_t type;
std::string value;
};
struct token_line {
line_number number;
std::vector<token> tokens;
};
struct token_stream {
std::string filename;
std::vector<token_line> lines;
const std::set<std::string> raw_rot8_ins = {"stp", "btp", "ror", "flb",
"biz", "rnz", "out", "inp"};
token_stream(const std::string &filename) {
this->filename = filename;
std::ifstream file(filename); // TODO: when open failed
std::stringstream buf;
buf << file.rdbuf();
lines = lex(buf.str());
}
std::vector<token_line> lex(const std::string &char_stream);
};
enum class ast_node_t {
CONSTANT,
PARAMETER,
VARIABLE,
OPERATOR,
BUILTIN,
MACROCALL,
REPEAT,
ROT8,
CODEBLOCK,
STRING,
};
struct ast_node {
ast_node_t type;
std::string value;
std::vector<ast_node> children;
};
class ast {
public:
ast_node root = {r8asm::ast_node_t::CODEBLOCK, "", {}};
size_t CELL_SIZE;
// I know template class is the most elegant solution of CELL_SIZE
// but I can't be bother to add that.
private:
addr dataptr = 0;
std::vector<std::map<std::string, general_data>> scopes;
std::map<std::string, ast_node> macros;
enum class builtin_t {
ANCHOR,
TP,
XOR,
LOOP,
ENDLOOP,
VAR,
};
const std::map<std::string, builtin_t> builtin_map = {
{"builtin_anchor", builtin_t::ANCHOR},
{"builtin_tp", builtin_t::TP},
{"builtin_xor", builtin_t::XOR},
{"builtin_loop", builtin_t::LOOP},
{"builtin_endloop", builtin_t::ENDLOOP},
{"builtin_var", builtin_t::VAR},
};
enum class rot8_t {
STP, // Step Tape Pointer,'>'
BTP, // Back Tape Pointer,'<'
ROR, // ROtate Right,'-'
FLB, // Flip Last Bit,'+'
BIZ, // Branch If Zero,'['
RNZ, // Return Not Zero,']'
OUT, // OUTput byte,'.'
INP, // INPut byte,','
};
const std::map<std::string, rot8_t> rot8_map = {
{"rot8_stp", rot8_t::STP}, {"rot8_btp", rot8_t::BTP},
{"rot8_ror", rot8_t::ROR}, {"rot8_flb", rot8_t::FLB},
{"rot8_biz", rot8_t::BIZ}, {"rot8_rnz", rot8_t::RNZ},
{"rot8_out", rot8_t::OUT}, {"rot8_inp", rot8_t::INP},
};
general_data expand_node_to_data(const ast_node &node);
std::string expand_node_to_string(const ast_node &node);
public:
enum class operator_t {
ADD,
SUB,
MUL,
DIV,
XOR,
AND,
OR,
NOT,
};
const std::map<std::string, operator_t> operator_map = {
{"op_add", operator_t::ADD}, {"op_sub", operator_t::SUB},
{"op_mul", operator_t::MUL}, {"op_div", operator_t::DIV},
{"op_xor", operator_t::XOR}, {"op_and", operator_t::AND},
{"op_or", operator_t::OR}, {"op_not", operator_t::NOT},
};
const std::map<operator_t, precedence_t> op_precedence_map = {
{operator_t::NOT, 0}, {operator_t::MUL, 1}, {operator_t::DIV, 1},
{operator_t::ADD, 2}, {operator_t::SUB, 2}, {operator_t::AND, 3},
{operator_t::XOR, 4}, {operator_t::OR, 5},
};
ast_node expand_node_to_ins(const ast_node &node);
std::string expand_root();
bool import_macro(const std::string &macro_name, const ast_node &node);
void set_cell_size(size_t cell_size);
};
ast main_tree;
enum class directive_t {
MACRO,
ENDMACRO,
REP,
ENDREP,
INCLUDE,
};
std::map<std::string, directive_t> directive_keywords = {
{"macro", directive_t::MACRO},
{"endmacro", directive_t::ENDMACRO},
{"rep", directive_t::REP},
{"endrep", directive_t::ENDREP},
{"include", directive_t::INCLUDE}};
ast_node parse(const token_stream &stream);
ast_node parse_args(const token_line &line);
ast_node parse_single_arg(const token_line &line);
public:
std::string export_rot8();
bool import_file(const std::string &filename, size_t cell_size);
};