commit 4145ce3989cfc5b7934706509e28406c15abb265 Author: StarCells Date: Tue Aug 25 18:09:16 2026 +0800 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..109e128 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build/ +.cache/ +.continue/ +.vscode/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..42da1e3 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.10) +project(R8ASM) + +set(CMAKE_CXX_STANDARD 20) + +include_directories(include) + +add_library(r8asm_core src/r8asm.cc) + +set(CMAKE_CXX_FLAGS "-O3 -march=native -DNDEBUG") + +add_executable(r8asm_test tests/r8_test.cc) +target_link_libraries(r8asm_test r8asm_core) diff --git a/include/r8asm/r8asm.hh b/include/r8asm/r8asm.hh new file mode 100644 index 0000000..214e71e --- /dev/null +++ b/include/r8asm/r8asm.hh @@ -0,0 +1,101 @@ +#pragma once + +#include +#include +#include +#include +#include + +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 children; + }; + class ast { + private: + public: + ast_node root; + addr dataptr = 0; + std::vector> scopes; + std::map> macro_bodies; + + general_data expand_node_to_data(const ast_node &node); + std::vector expand_node_to_ins(const ast_node &node); + + enum class operator_t { + ADD, + SUB, + MUL, + DIV, + XOR, + AND, + OR, + NOT, + }; + std::map 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 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 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 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}}; +}; \ No newline at end of file diff --git a/src/r8asm.cc b/src/r8asm.cc new file mode 100644 index 0000000..c812317 --- /dev/null +++ b/src/r8asm.cc @@ -0,0 +1,175 @@ +#include "r8asm/r8asm.hh" +#include +#include +#include + +r8asm::general_data r8asm::ast::expand_node_to_data(const ast_node &node) { + general_data to_return = 0; + switch (node.type) { + case ast_node_t::CONSTANT: + to_return = std::stoi(node.value); + break; + case ast_node_t::PARAMETER: + to_return = read_parameter(node.value); + break; + case ast_node_t::VARIABLE: + for (auto i = scopes.rbegin(); i != scopes.rend(); i++) { + auto tmp = i->find(node.value); + if (tmp != i->end()) { + to_return = tmp->second; + break; + } + // TODO: VAR NOT FOUND + } + break; + case ast_node_t::OPERATOR: { + auto a = expand_node_to_data(node.children[0]); + auto b = expand_node_to_data(node.children[1]); + switch (operator_map.at(node.value)) { + case operator_t::ADD: + to_return = a + b; + break; + case operator_t::SUB: + to_return = a - b; + break; + case operator_t::MUL: + to_return = a * b; + break; + case operator_t::DIV: + to_return = a / b; + break; + case operator_t::XOR: + to_return = a ^ b; + break; + case operator_t::AND: + to_return = a & b; + break; + case operator_t::OR: + to_return = a | b; + break; + case operator_t::NOT: + to_return = ~a; + break; + default:; + break; + } + break; + }; + + default: + std::cerr << "ERROR: THIS NODE SHOULD NOT BE EXPANDED TO DATA!!!" + << std::endl; + break; + } + return to_return; +} + +r8asm::general_data r8asm::ast::read_parameter(std::string index_str) { + parameter_index i = std::stoi(index_str); + return scopes.back()['%' + index_str]; +} + +// TODO: FILL THE BLANK +#define ROT8_NODE(INS) \ + { \ + r8asm::ast_node_t::ROT8, INS, {} \ + } +std::vector +r8asm::ast::expand_node_to_ins(const ast_node &node) { + std::vector to_return = {}; + switch (node.type) { + case ast_node_t::ROT8: { + to_return = {node}; + if (node.value == "stp") + dataptr++; + if (node.value == "btp") + dataptr--; + } break; + case ast_node_t::MACROCALL: { + /* + MACROCALL + value: macro_name + children: arguments + */ + std::map tmp_scope = {}; + for (auto i = 0; i < node.children.size(); i++) + tmp_scope.insert_or_assign('%' + std::to_string(i + 1), + expand_node_to_data(node.children[i])); + scopes.push_back(tmp_scope); + // %1 in macro means children[0] + for (auto body : macro_bodies.at(node.value)) + for (auto it : expand_node_to_ins(body)) + to_return.push_back(it); + scopes.pop_back(); + + } break; + case ast_node_t::BUILTIN: { + switch (builtin_map.at(node.value)) { + case builtin_t::ANCHOR: { + /* + ANCHOR + value: var_name + */ + scopes.back().insert_or_assign(node.value, dataptr); + } break; + case builtin_t::TP: { + /* + TP + children[0]: destination + */ + addr destination = expand_node_to_data(node.children[0]); + if (destination > dataptr) + for (addr i = 0; i < destination - dataptr; i++) + to_return.push_back(ROT8_NODE("stp")); + else + for (addr i = 0; i < dataptr - destination; i++) + to_return.push_back(ROT8_NODE("btp")); + dataptr = destination; + // stp or btp might be created + // so dataptr must be updated at the same time. + } break; + case builtin_t::XOR: { + /* + XOR + children[0]: argument + */ + data d = expand_node_to_data(node.children[0]); + for (auto i = 0; i < CELL_SIZE; i++) { + if (d & 1) + to_return.push_back(ROT8_NODE("FLB")); + to_return.push_back(ROT8_NODE("ROR")); + d >>= 1; + } + } break; + case builtin_t::LOOP: { + to_return.push_back(ROT8_NODE("stp")); + to_return.push_back(ROT8_NODE("biz")); + to_return.push_back(ROT8_NODE("btp")); + // >[< + } break; + case builtin_t::ENDLOOP: { + to_return.push_back(ROT8_NODE("stp")); + to_return.push_back(ROT8_NODE("flb")); + to_return.push_back(ROT8_NODE("rnz")); + to_return.push_back(ROT8_NODE("btp")); + // >-+]< + } break; + case builtin_t::VAR: { + /* + VAR + value: var_name + children[0]: var_value + */ + scopes.back().insert_or_assign(node.value, + expand_node_to_data(node.children[0])); + } break; + default: + break; + }; + } break; + default:; + break; + } + return to_return; +} +#undef ROT8_NODE \ No newline at end of file diff --git a/tests/r8_test.cc b/tests/r8_test.cc new file mode 100644 index 0000000..9b4b6ae --- /dev/null +++ b/tests/r8_test.cc @@ -0,0 +1 @@ +int main() { return (0); } \ No newline at end of file