#include "r8asm/r8asm.hh" #include #include #include #include #include #include #include #include #define EMPTY_CHARS (" \t\n\r\f\v") #define OPERATORS ("+-*/^~\"()") namespace r8asm_strtools { bool is_integer(const std::string &str) { return !str.empty() && std::ranges::all_of(str, [](char c) { return c >= '0' && c <= '9'; }); } void trim(std::string &str) { // Clean empty chars in the string. auto start = str.find_first_not_of(EMPTY_CHARS); if (!(start == std::string::npos)) { // The string isn't filled with empty chars. auto tmp_end = str.find_last_not_of(EMPTY_CHARS); str = str.substr(start, tmp_end - start + 1); } else str = ""; } std::string peel(std::string &str) { // Peel the first word from string. for (size_t i = 1; i < str.size(); i++) { // insert space before and after operators appear if (std::string(OPERATORS).find(str[i]) != std::string::npos) { if (str[i - 1] != ' ') { str = str.substr(0, i) + ' ' + str.substr(i, str.size()); i++; } if (str[i + 1] != ' ') { str = str.substr(0, i + 1) + ' ' + str.substr(i + 1, str.size()); } } } trim(str); auto end = str.find_first_of(EMPTY_CHARS); if (end != std::string::npos) { std::string tmp_str = str.substr(0, end); str = str.substr(end); trim(str); return (tmp_str); } else { std::string tmp_str = str; str = ""; return (tmp_str); } } } // namespace r8asm_strtools #undef EMPTY_CHARS #undef OPERATORS 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 = scopes.back().at('%' + 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: { switch (operator_map.at(node.value)) { case operator_t::ADD: to_return = expand_node_to_data(node.children[0]) + expand_node_to_data(node.children[1]); break; case operator_t::SUB: to_return = expand_node_to_data(node.children[0]) - expand_node_to_data(node.children[1]); break; case operator_t::MUL: to_return = expand_node_to_data(node.children[0]) * expand_node_to_data(node.children[1]); break; case operator_t::DIV: to_return = expand_node_to_data(node.children[0]) / expand_node_to_data(node.children[1]); break; case operator_t::XOR: to_return = expand_node_to_data(node.children[0]) ^ expand_node_to_data(node.children[1]); break; case operator_t::AND: to_return = expand_node_to_data(node.children[0]) & expand_node_to_data(node.children[1]); break; case operator_t::OR: to_return = expand_node_to_data(node.children[0]) | expand_node_to_data(node.children[1]); break; case operator_t::NOT: to_return = ~expand_node_to_data(node.children[0]); break; default:; break; } break; }; default: std::cerr << "ERROR: THIS NODE SHOULD NOT BE EXPANDED TO DATA!!!" << std::endl; break; } return to_return; } std::string r8asm::ast::expand_node_to_string(const ast_node &node) { return node.value; } #define EMPTY_CODEBLOCK \ { \ r8asm::ast_node_t::CODEBLOCK, "", {} \ } // TODO: FILL THE BLANK #define ROT8_NODE(INS) \ { \ r8asm::ast_node_t::ROT8, std::string("rot8_") + INS, {} \ } r8asm::ast_node r8asm::ast::expand_node_to_ins(const ast_node &node) { ast_node to_return = EMPTY_CODEBLOCK; switch (node.type) { case ast_node_t::ROT8: { /* ROT8 value: rot8_ins */ to_return.children = {node}; switch (rot8_map.at(node.value)) { case rot8_t::STP: dataptr++; break; case rot8_t::BTP: dataptr--; break; default: break; } } break; case ast_node_t::REPEAT: { /* REPEAT children[0]: count children[1]: codeblock */ for (data count = expand_node_to_data(node.children[0]); count > 0; count--) for (const auto &i : expand_node_to_ins(node.children[1]).children) to_return.children.push_back(i); } 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), // %1 in macro means children[0] expand_node_to_data(node.children[i])); scopes.push_back(tmp_scope); for (const auto &it : expand_node_to_ins(macros.at(node.value)).children) to_return.children.push_back(it); scopes.pop_back(); } break; case ast_node_t::CODEBLOCK: { /* CODEBLOCK children: ins */ for (const auto &i : node.children) for (const auto &ins : expand_node_to_ins(i).children) to_return.children.push_back(ins); } break; case ast_node_t::BUILTIN: { /* BUILTIN value: type children: args */ switch (builtin_map.at(node.value)) { // WARNING: node.value HAS BEEN USED!!! DO NOT CONSUME IT AGAIN!!! case builtin_t::ANCHOR: { /* ANCHOR children[0]: var_name */ scopes.back().insert_or_assign(expand_node_to_string(node.children[0]), 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.children.push_back(ROT8_NODE("stp")); else for (addr i = 0; i < dataptr - destination; i++) to_return.children.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 WARNING: argument bigger than ((1< 5(0b101) when define CELL_SIZE 2, only the last 2 bit will be noticed, and 5 means 0b01(1) here. then the node will be expanded like this: flb ror ror */ data d = expand_node_to_data(node.children[0]); for (auto i = 0; i < CELL_SIZE; i++) { if (d & 1) to_return.children.push_back(ROT8_NODE("flb")); to_return.children.push_back(ROT8_NODE("ror")); d >>= 1; } } break; case builtin_t::LOOP: { to_return.children.push_back(ROT8_NODE("stp")); to_return.children.push_back(ROT8_NODE("biz")); to_return.children.push_back(ROT8_NODE("btp")); // >[< } break; case builtin_t::ENDLOOP: { to_return.children.push_back(ROT8_NODE("stp")); to_return.children.push_back(ROT8_NODE("flb")); to_return.children.push_back(ROT8_NODE("rnz")); to_return.children.push_back(ROT8_NODE("btp")); // >-+]< } break; case builtin_t::VAR: { /* VAR children[0]: var_name children[1]: var_value */ scopes.back().insert_or_assign(expand_node_to_string(node.children[0]), expand_node_to_data(node.children[1])); } break; default: break; }; } break; default:; break; } return to_return; } #undef ROT8_NODE #define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast(bc)]) std::string r8asm::ast::expand_root() { auto rot8_ins = expand_node_to_ins(root); std::string rot8_tape = ""; for (const auto &i : rot8_ins.children) rot8_tape += ROT8_BC_TO_CHAR(rot8_map.at(i.value)); return rot8_tape; } #undef ROT8_BC_TO_CHAR std::string r8asm::export_rot8() { return main_tree.expand_root(); } bool r8asm::ast::import_macro(const std::string ¯o_name, const ast_node &node) { return macros.insert_or_assign(macro_name, node).second; } #define POINTS_AT_DIRECTIVE(iter, TYPE) \ (iter->tokens[0].type == token_t::DIRECTIVE && \ directive_keywords.at(iter->tokens[0].value) == directive_t::TYPE) r8asm::ast_node r8asm::parse(const token_stream &stream) { ast_node to_return = EMPTY_CODEBLOCK; token_stream tmp_codeblock_stream = {stream.filename}; /* ast_node tmp_codeblock_node; std::vector *destination = &to_return.children; std::vector *last_destination = nullptr;*/ for (auto it = stream.lines.begin(); it < stream.lines.end(); it++) { switch (it->tokens[0].type) { case token_t::DIRECTIVE: { /* directive will be tokenized like this: macro raw_add 0 1 or: macro raw_add 2 + 4 0 1 2 3 4 (though [2] [3] [4] won't be used) if it's a rep block: rep 1 + 1 0 1 2 3 if it's a include line: include filename 0 1 */ switch (directive_keywords.at(it->tokens[0].value)) { // match an entire operation (not a cut for arguments) first case directive_t::MACRO: { std::string macro_name = it->tokens[1].value; tmp_codeblock_stream.lines = {}; depth_t layer = 0; // now it points at %macro line for (it += 1; !(POINTS_AT_DIRECTIVE(it, ENDMACRO) && (layer == 0)); it++) { if (POINTS_AT_DIRECTIVE(it, MACRO)) layer++; else if (POINTS_AT_DIRECTIVE(it, ENDMACRO)) layer--; tmp_codeblock_stream.lines.push_back(*it); } // now it points at %endmacro line main_tree.import_macro(macro_name, parse(tmp_codeblock_stream)); } break; case directive_t::REP: { // build subtree for argument of rep token_line buffer = {it->number, {}}; for (auto token_it = it->tokens.begin() + 1; // where argument begins token_it < it->tokens.end(); token_it++) buffer.tokens.push_back(*token_it); /* REPEAT children[0]: count children[1]: codeblock */ to_return.children.push_back({r8asm::ast_node_t::REPEAT, "", {}}); to_return.children.back().children.push_back( parse_single_arg(buffer)); // count will be pushed here. tmp_codeblock_stream.lines = {}; depth_t layer = 0; // now it points at %rep line for (it += 1; !(POINTS_AT_DIRECTIVE(it, ENDREP) && (layer == 0)); it++) { if (POINTS_AT_DIRECTIVE(it, REP)) layer++; else if (POINTS_AT_DIRECTIVE(it, ENDREP)) layer--; tmp_codeblock_stream.lines.push_back(*it); } // now it points at %endrep line to_return.children.back().children.push_back( parse(tmp_codeblock_stream)); // codeblock will be pushed here. } break; case directive_t::INCLUDE: { std::string filename = ""; auto tp = it->tokens.begin() + 1; // now tp points at the LQUOTE for (tp += 1; tp->type != token_t::RQUOTE; tp++) filename += tp->value; // now tp points at RQUOTE import_file( filename); // TODO: check whether the file is open successfully } break; default: break; } } break; case token_t::BUILTIN: { /* BUILTIN value: type children: args */ /* like this: builtin_var pos1 15 0 1 2 */ to_return.children.push_back( {r8asm::ast_node_t::BUILTIN, it->tokens[0].value, {}}); // build subtree for argument(s) of builtin token_line buffer = {it->number, {}}; for (auto token_it = it->tokens.begin() + 1; // where argument begins token_it < it->tokens.end(); token_it++) buffer.tokens.push_back(*token_it); auto args = parse_args(buffer).children; for (const auto &arg_it : args) to_return.children.back().children.push_back( arg_it); // though all string args are treated as VARIABLE, I guess // builtin_var will read children[0].value successfully. } break; case token_t::MACROCALL: { /* like this: raw_add 2 4 4 + 2 ( 4 + 4 ) pos1 pos1 + 2 0 1 ... */ to_return.children.push_back( {r8asm::ast_node_t::MACROCALL, it->tokens[0].value, {}}); // build subtree for argument(s) of macro token_line buffer = {it->number, {}}; for (auto token_it = it->tokens.begin() + 1; // where argument begins token_it < it->tokens.end(); token_it++) buffer.tokens.push_back(*token_it); /* MACROCALL value: macro_name children: arguments */ auto args = parse_args(buffer).children; for (const auto &arg_it : args) to_return.children.back().children.push_back(arg_it); } break; case token_t::ROT8: { to_return.children.push_back( {r8asm::ast_node_t::ROT8, it->tokens[0].value, {}}); } break; default: break; } } return to_return; } #undef POINTS_AT_DIRECTIVE #define UNRELATED_TO_LEFT(present_token) \ (it != line.tokens.begin() && \ ((present_token->type == token_t::LPAREN || \ present_token->type == token_t::STRING || \ (present_token->type == token_t::OPERATOR && \ (present_token->value == "op_neg" || \ present_token->value == "op_not"))) && \ (!((present_token - 1)->type == token_t::OPERATOR || \ (present_token - 1)->type == token_t::LPAREN)))) r8asm::ast_node r8asm::parse_args(const token_line &line) { ast_node to_return = EMPTY_CODEBLOCK; // arguments will be storaged in to_return.children std::vector raw_args; std::list buffer; for (auto it = line.tokens.begin(); it != line.tokens.end(); it++) { if (UNRELATED_TO_LEFT(it)) { raw_args.push_back({line.number, {}}); while (!buffer.empty()) { raw_args.back().tokens.push_back(buffer.front()); buffer.pop_front(); } } buffer.push_back(*it); } if (!buffer.empty()) { raw_args.push_back({line.number, {}}); while (!buffer.empty()) { raw_args.back().tokens.push_back(buffer.front()); buffer.pop_front(); } } for (const auto &it : raw_args) to_return.children.push_back(parse_single_arg(it)); return to_return; } #undef UNRELATED_TO_LEFT r8asm::ast_node r8asm::parse_single_arg(const token_line &line) { ast_node to_return; std::stack data_stack; std::stack symbol_stack; depth_t right_assoc_depth = 0; auto deal_with_assoc = [&data_stack, &symbol_stack, &right_assoc_depth] { while (right_assoc_depth) { auto unary_op = symbol_stack.top(); symbol_stack.pop(); unary_op.children.push_back(data_stack.top()); data_stack.pop(); data_stack.push(unary_op); right_assoc_depth--; } }; for (auto it = line.tokens.begin(); it < line.tokens.end(); it++) { switch (it->type) { case token_t::LPAREN: { depth_t layer = 0; token_line buffer = {line.number, {}}; for (it += 1; !((it->type == token_t::RPAREN) && (layer == 0)); it++) { if (it->type == token_t::LPAREN) layer++; else if (it->type == token_t::RPAREN) layer--; buffer.tokens.push_back(*it); } data_stack.push(parse_single_arg(buffer)); deal_with_assoc(); } break; case token_t::STRING: { { if (r8asm_strtools::is_integer(it->value)) data_stack.push({r8asm::ast_node_t::CONSTANT, it->value, {}}); else data_stack.push({r8asm::ast_node_t::VARIABLE, it->value, {}}); } deal_with_assoc(); } break; case token_t::PARAMETER: { data_stack.push({r8asm::ast_node_t::PARAMETER, (it->value), {}}); deal_with_assoc(); } break; case token_t::OPERATOR: // precedence: NEG = NOT // > MUL = DIV // > ADD = SUB // > AND // > XOR // > OR { #define GET_PRECEDENCE(op_key) \ (main_tree.op_precedence_map.at(main_tree.operator_map.at(op_key))) switch (main_tree.operator_map.at(it->value)) { case ast::operator_t::NOT: // unary operators { right_assoc_depth++; symbol_stack.push({ast_node_t::OPERATOR, it->value, {}}); } break; default: // now all of them are binary operators { while ((!symbol_stack.empty()) && GET_PRECEDENCE(it->value) >= GET_PRECEDENCE(symbol_stack.top().value)) // e.g. pushing + while here's already a * in symbol_stack { auto tmp_op = symbol_stack.top(); symbol_stack.pop(); auto tmp_arg = data_stack.top(); // 2nd arg got data_stack.pop(); tmp_op.children.push_back(data_stack.top()); // 1st arg pushed data_stack.pop(); tmp_op.children.push_back(tmp_arg); data_stack.push(tmp_op); } symbol_stack.push({ast_node_t::OPERATOR, it->value, {}}); } break; } } #undef GET_PRECEDENCE default: break; } } while (!symbol_stack.empty()) { auto tmp_op = symbol_stack.top(); symbol_stack.pop(); auto tmp_arg = data_stack.top(); // 2nd arg got data_stack.pop(); tmp_op.children.push_back(data_stack.top()); // 1st arg pushed data_stack.pop(); tmp_op.children.push_back(tmp_arg); data_stack.push(tmp_op); } to_return = data_stack.top(); return to_return; } #undef EMPTY_CODEBLOCK #define OPERATORS ("+-*/^~") std::vector r8asm::token_stream::lex(const std::string &char_stream) { std::vector to_return; std::istringstream iss(char_stream); std::string str_buffer; line_number num = 0; bool next_will_be_remark = false; const std::map operator_map = { // without "-" {"+", "op_add"}, {"*", "op_mul"}, {"/", "op_div"}, {"^", "op_xor"}, {"&", "op_and"}, {"|", "op_or"}, {"~", "op_not"}, {"-", "op_sub"}}; while (std::getline(iss, str_buffer)) { std::vector token_buffer = {}; bool start_of_line = true; bool lquote_appeared = false; for (std::string token_val = r8asm_strtools::peel(str_buffer); token_val != "";) { if (lquote_appeared && token_val[0] != '\"') token_buffer.push_back({r8asm::token_t::STRING, token_val}); else { if (start_of_line) { // it's the first token in the line start_of_line = false; if (token_val[0] == '.') // builtin token_buffer.push_back( {r8asm::token_t::BUILTIN, "builtin_" + token_val.substr(0, 1)}); // e.g. .var -> builtin_var else if (token_val[0] == '%') // directive token_buffer.push_back( {r8asm::token_t::DIRECTIVE, token_val.substr(0, 1)}); // e.g. %macro -> macro else if (token_val[0] == ';') // remark next_will_be_remark = true; else if (raw_rot8_ins.find(token_val) != raw_rot8_ins.end()) // rot8 token_buffer.push_back({r8asm::token_t::ROT8, "rot8_" + token_val}); else // macro token_buffer.push_back({r8asm::token_t::MACROCALL, token_val}); } else { // it's a part of argument if (!next_will_be_remark) { if (token_val[0] == ';') // remark next_will_be_remark = true; else if (token_val[0] == '%') // parameter token_buffer.push_back({r8asm::token_t::PARAMETER, token_val}); else if (token_val[0] == '"') { // quote(filename) if (!lquote_appeared) { lquote_appeared = true; // only %include will use quotes, they // won't appear again token_buffer.push_back({r8asm::token_t::LQUOTE}); } else token_buffer.push_back({r8asm::token_t::RQUOTE}); } else if (token_val[0] == '(') token_buffer.push_back({r8asm::token_t::LPAREN}); else if (token_val[0] == ')') token_buffer.push_back({r8asm::token_t::RPAREN}); else if (std::string(OPERATORS).find(token_val[0]) != std::string::npos) // operator token_buffer.push_back({r8asm::token_t::OPERATOR, token_val}); else token_buffer.push_back({r8asm::token_t::STRING, token_val}); } } } } num++; if (!token_buffer.empty()) to_return.push_back({num, token_buffer}); next_will_be_remark = false; } return to_return; }