101 lines
2.5 KiB
C++
101 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
constexpr size_t CELL_SIZE = 8;
|
|
|
|
class r8asm {
|
|
using data = int16_t;
|
|
using parameter_index = uint8_t;
|
|
using addr = uint32_t;
|
|
using general_data = int32_t;
|
|
|
|
private:
|
|
public:
|
|
enum class ast_node_t {
|
|
CONSTANT,
|
|
PARAMETER,
|
|
VARIABLE,
|
|
OPERATOR,
|
|
BUILTIN,
|
|
MACROCALL,
|
|
ROT8,
|
|
};
|
|
struct ast_node {
|
|
ast_node_t type;
|
|
std::string value;
|
|
std::vector<ast_node> children;
|
|
};
|
|
class ast {
|
|
private:
|
|
public:
|
|
ast_node root;
|
|
addr dataptr = 0;
|
|
std::vector<std::map<std::string, general_data>> scopes;
|
|
std::map<std::string, std::vector<ast_node>> macro_bodies;
|
|
|
|
general_data expand_node_to_data(const ast_node &node);
|
|
std::vector<ast_node> expand_node_to_ins(const ast_node &node);
|
|
|
|
enum class operator_t {
|
|
ADD,
|
|
SUB,
|
|
MUL,
|
|
DIV,
|
|
XOR,
|
|
AND,
|
|
OR,
|
|
NOT,
|
|
};
|
|
std::map<std::string, operator_t> operator_map = {
|
|
{"+", operator_t::ADD}, {"-", operator_t::SUB}, {"*", operator_t::MUL},
|
|
{"/", operator_t::DIV}, {"^", operator_t::XOR}, {"&", operator_t::AND},
|
|
{"|", operator_t::OR}, {"~", operator_t::NOT}};
|
|
enum class builtin_t {
|
|
ANCHOR,
|
|
TP,
|
|
XOR,
|
|
LOOP,
|
|
ENDLOOP,
|
|
VAR,
|
|
};
|
|
std::map<std::string, builtin_t> builtin_map = {
|
|
{"anchor", builtin_t::ANCHOR}, {"tp", builtin_t::TP},
|
|
{"xor", builtin_t::XOR}, {"loop", builtin_t::LOOP},
|
|
{"endloop", builtin_t::ENDLOOP}, {"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,','
|
|
};
|
|
std::map<std::string, rot8_t> rot8_map = {
|
|
{"stp", rot8_t::STP}, {"btp", rot8_t::BTP}, {"ror", rot8_t::ROR},
|
|
{"flb", rot8_t::FLB}, {"biz", rot8_t::BIZ}, {"rnz", rot8_t::RNZ},
|
|
{"out", rot8_t::OUT}, {"inp", rot8_t::INP},
|
|
};
|
|
general_data read_parameter(std::string index_str);
|
|
};
|
|
enum class directive_t {
|
|
MACRO,
|
|
ENDMACRO,
|
|
REP,
|
|
ENDREP,
|
|
INCLUDE,
|
|
};
|
|
std::map<std::string, directive_t> directive_map = {
|
|
{"macro", r8asm::directive_t::MACRO},
|
|
{"endmacro", r8asm::directive_t::ENDMACRO},
|
|
{"rep", r8asm::directive_t::REP},
|
|
{"endrep", r8asm::directive_t::ENDREP},
|
|
{"include", r8asm::directive_t::INCLUDE}};
|
|
}; |