diff options
Diffstat (limited to 'dev')
| -rw-r--r-- | dev/ndk/Parser.hxx | 2 | ||||
| -rw-r--r-- | dev/ndk/src/CPlusPlusCompilerAMD64.cxx | 164 | ||||
| -rw-r--r-- | dev/ndk/src/CPlusPlusCompilerPreProcessor.cxx | 267 |
3 files changed, 209 insertions, 224 deletions
diff --git a/dev/ndk/Parser.hxx b/dev/ndk/Parser.hxx index 69326fb..a000b43 100644 --- a/dev/ndk/Parser.hxx +++ b/dev/ndk/Parser.hxx @@ -143,7 +143,7 @@ namespace NDK if (std::isspace(haystack[index]) || std::ispunct(haystack[index])) return true; - if (index < 0 || index >= haystack.size()) + if (index <= 0 || index >= haystack.size()) return true; return false; diff --git a/dev/ndk/src/CPlusPlusCompilerAMD64.cxx b/dev/ndk/src/CPlusPlusCompilerAMD64.cxx index 123bc72..abcf9dc 100644 --- a/dev/ndk/src/CPlusPlusCompilerAMD64.cxx +++ b/dev/ndk/src/CPlusPlusCompilerAMD64.cxx @@ -472,17 +472,9 @@ bool CompilerBackendCPlusPlus::Compile(const std::string text, std::string instr = "mov "; - if (typeFound) + if (typeFound && keyword.first.keyword_kind != NDK::KeywordKind::eKeywordKindVariableInc && + keyword.first.keyword_kind != NDK::KeywordKind::eKeywordKindVariableDec) { - if (keyword.first.keyword_kind == NDK::KeywordKind::eKeywordKindVariableInc) - { - detail::print_error_asm("Can't increment variable when it's being created.", file); - } - else if (keyword.first.keyword_kind == NDK::KeywordKind::eKeywordKindVariableDec) - { - detail::print_error_asm("Can't decrement variable when it's being created.", file); - } - if (kRegisterMap.size() > cRegisters.size()) { ++kLevelFunction; @@ -583,107 +575,107 @@ bool CompilerBackendCPlusPlus::Compile(const std::string text, } kRegisterMap.push_back(varName); + + break; } - else - { - if (kKeywords[keyword.second - 1].keyword_kind == NDK::eKeywordKindType || - kKeywords[keyword.second - 1].keyword_kind == NDK::eKeywordKindTypePtr) - { - syntax_tree.fUserValue = "\n"; - continue; - } - if (keyword.first.keyword_kind == NDK::KeywordKind::eKeywordKindEndInstr) - { - syntax_tree.fUserValue = "\n"; - continue; - } + if (kKeywords[keyword.second - 1].keyword_kind == NDK::eKeywordKindType || + kKeywords[keyword.second - 1].keyword_kind == NDK::eKeywordKindTypePtr) + { + syntax_tree.fUserValue = "\n"; + continue; + } - if (keyword.first.keyword_kind == NDK::KeywordKind::eKeywordKindVariableInc) - { - instr = "add "; - } - else if (keyword.first.keyword_kind == NDK::KeywordKind::eKeywordKindVariableDec) - { - instr = "sub "; - } + if (keyword.first.keyword_kind == NDK::KeywordKind::eKeywordKindEndInstr) + { + syntax_tree.fUserValue = "\n"; + continue; + } - std::string varErrCpy = varName; + if (keyword.first.keyword_kind == NDK::KeywordKind::eKeywordKindVariableInc) + { + instr = "add "; + } + else if (keyword.first.keyword_kind == NDK::KeywordKind::eKeywordKindVariableDec) + { + instr = "sub "; + } - while (varName.find(" ") != std::string::npos) - { - varName.erase(varName.find(" "), 1); - } + std::string varErrCpy = varName; - while (varName.find("\t") != std::string::npos) - { - varName.erase(varName.find("\t"), 1); - } + while (varName.find(" ") != std::string::npos) + { + varName.erase(varName.find(" "), 1); + } - std::size_t indxReg = 0UL; + while (varName.find("\t") != std::string::npos) + { + varName.erase(varName.find("\t"), 1); + } - for (size_t i = 0; !isalnum(valueOfVar[i]); i++) - { - if (i > valueOfVar.size()) - break; + std::size_t indxReg = 0UL; - valueOfVar.erase(i, 1); - } + for (size_t i = 0; !isalnum(valueOfVar[i]); i++) + { + if (i > valueOfVar.size()) + break; - while (valueOfVar.find(" ") != std::string::npos) - { - valueOfVar.erase(valueOfVar.find(" "), 1); - } + valueOfVar.erase(i, 1); + } - while (valueOfVar.find("\t") != std::string::npos) - { - valueOfVar.erase(valueOfVar.find("\t"), 1); - } + while (valueOfVar.find(" ") != std::string::npos) + { + valueOfVar.erase(valueOfVar.find(" "), 1); + } - constexpr auto cTrueVal = "true"; - constexpr auto cFalseVal = "false"; + while (valueOfVar.find("\t") != std::string::npos) + { + valueOfVar.erase(valueOfVar.find("\t"), 1); + } - /// interpet boolean values, since we're on C++ + constexpr auto cTrueVal = "true"; + constexpr auto cFalseVal = "false"; - if (valueOfVar == cTrueVal) - { - valueOfVar = "1"; - } - else if (valueOfVar == cFalseVal) - { - valueOfVar = "0"; - } + /// interpet boolean values, since we're on C++ - for (auto pair : kRegisterMap) - { - ++indxReg; + if (valueOfVar == cTrueVal) + { + valueOfVar = "1"; + } + else if (valueOfVar == cFalseVal) + { + valueOfVar = "0"; + } - if (pair != varName) - continue; + for (auto pair : kRegisterMap) + { + ++indxReg; - std::size_t indexRight = 0ul; + if (pair != varName) + continue; - for (auto pairRight : kRegisterMap) - { - ++indexRight; + std::size_t indexRight = 0ul; - if (pairRight != varName) - { - syntax_tree.fUserValue = instr + cRegisters[kRegisterMap.size()] + ", " + valueOfVar + "\n"; - continue; - } + for (auto pairRight : kRegisterMap) + { + ++indexRight; - syntax_tree.fUserValue = instr + cRegisters[indexRight - 1] + ", " + valueOfVar + "\n"; - break; + if (pairRight != varName) + { + syntax_tree.fUserValue = instr + cRegisters[kRegisterMap.size()] + ", " + valueOfVar + "\n"; + continue; } + syntax_tree.fUserValue = instr + cRegisters[indexRight - 1] + ", " + valueOfVar + "\n"; break; } - if (syntax_tree.fUserValue.empty()) - { - detail::print_error_asm("Variable not declared: " + varErrCpy, file); - } + break; + } + + if (syntax_tree.fUserValue.empty()) + { + detail::print_error_asm("Variable not declared: " + varErrCpy, file); } break; diff --git a/dev/ndk/src/CPlusPlusCompilerPreProcessor.cxx b/dev/ndk/src/CPlusPlusCompilerPreProcessor.cxx index f9d76e5..5f4b502 100644 --- a/dev/ndk/src/CPlusPlusCompilerPreProcessor.cxx +++ b/dev/ndk/src/CPlusPlusCompilerPreProcessor.cxx @@ -15,6 +15,7 @@ #include <filesystem> #include <fstream> #include <iostream> +#include <stdexcept> #include <vector> #define kMacroPrefix '#' @@ -54,6 +55,17 @@ namespace detail std::vector<std::string> fArgs; std::string fName; std::string fValue; + + void Print() + { + std::cout << "name: " << fName << "\n"; + std::cout << "value: " << fValue << "\n"; + + for (auto& arg : fArgs) + { + std::cout << "arg: " << arg << "\n"; + } + } }; class bpp_pragma final @@ -324,10 +336,12 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) hdr_line.erase(hdr_line.find("/*")); } - /// BPP Documentation. - if (hdr_line.find("@bdoc") != std::string::npos) + /// BPP 'brief' documentation. + if (hdr_line.find("@brief") != std::string::npos) { - hdr_line.erase(hdr_line.find("@bdoc")); + hdr_line.erase(hdr_line.find("@brief")); + + // TODO: Write an <file_name>.html or append to it. } if (hdr_line[0] == kMacroPrefix && @@ -356,13 +370,76 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) for (auto macro : kMacros) { - if (NDK::find_word(hdr_line, macro.fName) && - hdr_line.find("#define") == std::string::npos) + if (NDK::find_word(hdr_line, macro.fName)) { - auto value = macro.fValue; + if (hdr_line.substr(hdr_line.find(macro.fName)).find(macro.fName + '(') != NDK::String::npos) + { + if (!macro.fArgs.empty()) + { + NDK::String symbol_val = macro.fValue; + std::vector<NDK::String> args; + + size_t x_arg_indx = 0; + + NDK::String line_after_define = hdr_line; + NDK::String str_arg; + + if (line_after_define.find("(") != NDK::String::npos) + { + line_after_define.erase(0, line_after_define.find("(") + 1); + + for (auto& subc : line_after_define) + { + if (subc == ' ' || subc == '\t') + continue; + + if (subc == ',' || subc == ')') + { + if (str_arg.empty()) + continue; + + args.push_back(str_arg); + + str_arg.clear(); + + continue; + } + + str_arg.push_back(subc); + } + } + + for (auto arg : macro.fArgs) + { + if (symbol_val.find(macro.fArgs[x_arg_indx]) != NDK::String::npos) + { + symbol_val.replace(symbol_val.find(macro.fArgs[x_arg_indx]), macro.fArgs[x_arg_indx].size(), + args[x_arg_indx]); + ++x_arg_indx; + } + else + { + throw std::runtime_error("internal: Internal C++ error. (Please report that bug.)"); + } + } + + auto len = macro.fName.size(); + len += symbol_val.size(); + len += 2; // ( and ) + + hdr_line.erase(hdr_line.find(")"), 1); + + hdr_line.replace(hdr_line.find(hdr_line.substr(hdr_line.find(macro.fName + '('))), len, + symbol_val); + } + else + { + auto value = macro.fValue; - hdr_line.replace(hdr_line.find(macro.fName), macro.fName.size(), - value); + hdr_line.replace(hdr_line.find(macro.fName), macro.fName.size(), + value); + } + } } } @@ -415,49 +492,27 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) macro_key += ch; } - std::vector<std::string> dupls; - std::string str; - - line_after_define.erase(0, line_after_define.find("(") + 1); + std::string str; - for (auto& subc : line_after_define) + if (line_after_define.find("(") != NDK::String::npos) { - if (subc == ',' || subc == ')') + line_after_define.erase(0, line_after_define.find("(") + 1); + + for (auto& subc : line_after_define) { - if (str.empty()) - continue; + if (subc == ',' || subc == ')') + { + if (str.empty()) + continue; - dupls.push_back(str); - args.push_back(str); + args.push_back(str); - str.clear(); + str.clear(); - continue; - } + continue; + } - if (isalnum(subc)) str.push_back(subc); - } - - for (auto& dupl : dupls) - { - std::size_t cnt = 0; - - for (auto& arg : args) - { - if (dupl == arg) - ++cnt; - } - - if (cnt > 1) - { - auto it = std::find(args.begin(), args.end(), dupl); - - while (it != args.end()) - { - args.erase(it); - it = std::find(args.begin(), args.end(), dupl); - } } } @@ -479,97 +534,6 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) continue; } - for (auto& macro : kMacros) - { - if (hdr_line.find(macro.fName) != std::string::npos) - { - std::vector<std::string> arg_values; - - if (macro.fArgs.size() > 0) - { - for (size_t i = 0; i < hdr_line.size(); ++i) - { - if (hdr_line[i] == '(') - { - std::string tmp_arg; - - for (size_t x = i; x < hdr_line.size(); x++) - { - if (hdr_line[x] == ')') - break; - - if (hdr_line[x] == ' ') - continue; - - if (hdr_line[i] == '\\') - continue; - - if (hdr_line[x] == ',') - { - arg_values.push_back(tmp_arg); - tmp_arg.clear(); - continue; - } - - tmp_arg += hdr_line[x]; - } - - break; - } - } - - std::string symbol; - - for (char i : macro.fValue) - { - if (i == '(') - break; - - if (i == '\\') - continue; - - symbol += i; - } - - hdr_line.replace(hdr_line.find(macro.fName), macro.fName.size(), - symbol); - - size_t x_arg_indx = 0; - - for (size_t i = hdr_line.find(macro.fValue); i < hdr_line.size(); - ++i) - { - if (hdr_line.find(macro.fArgs[x_arg_indx]) == i) - { - hdr_line.replace(i, macro.fArgs[x_arg_indx].size(), - arg_values[x_arg_indx]); - ++x_arg_indx; - } - } - } - else - { - std::string symbol; - - for (size_t i = 0; i < macro.fValue.size(); i++) - { - if (macro.fValue[i] == ' ') - continue; - - if (macro.fValue[i] == '\\') - continue; - - symbol += macro.fValue[i]; - } - - hdr_line.replace(hdr_line.find(macro.fName), macro.fName.size(), - symbol); - } - - break; - } - } - pp_out << hdr_line << std::endl; continue; @@ -808,7 +772,7 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) message += ch; } - std::cout << "Warning: " << message << std::endl; + std::cout << "warn: " << message << std::endl; } else if (hdr_line[0] == kMacroPrefix && hdr_line.find("error") != std::string::npos) @@ -827,7 +791,7 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) message += ch; } - throw std::runtime_error("Error: " + message); + throw std::runtime_error("error: " + message); } else if (hdr_line[0] == kMacroPrefix && hdr_line.find("include ") != std::string::npos) @@ -881,6 +845,16 @@ void bpp_parse_file(std::ifstream& hdr_file, std::ofstream& pp_out) { bool open = false; + if (path.ends_with('>')) + { + path.erase(path.find('>')); + } + + if (path.ends_with('"')) + { + path.erase(path.find('"')); + } + for (auto& include : kIncludes) { std::string header_path = include; @@ -942,23 +916,42 @@ NDK_MODULE(CPlusPlusPreprocessorMain) bool double_skip = false; detail::bpp_macro macro_1; + macro_1.fName = "__true"; macro_1.fValue = "1"; kMacros.push_back(macro_1); detail::bpp_macro macro_0; + macro_0.fName = "__false"; macro_0.fValue = "0"; kMacros.push_back(macro_0); detail::bpp_macro macro_zka; - macro_zka.fName = "__ZKA__"; + + macro_zka.fName = "__NDK__"; macro_zka.fValue = "1"; kMacros.push_back(macro_zka); + detail::bpp_macro macro_size_t; + macro_size_t.fName = "__SIZE_TYPE__"; + macro_size_t.fValue = "unsigned long long int"; + + kMacros.push_back(macro_size_t); + + macro_size_t.fName = "__UINT32_TYPE__"; + macro_size_t.fValue = "unsigned int"; + + kMacros.push_back(macro_size_t); + + macro_size_t.fName = "__UINTPTR_TYPE__"; + macro_size_t.fValue = "unsigned int"; + + kMacros.push_back(macro_size_t); + for (auto index = 1UL; index < argc; ++index) { if (skip) |
