From f63ad45ccbf86ae1a9817c20a653ca5b92915115 Mon Sep 17 00:00:00 2001 From: topchetoeu <36534413+TopchetoEU@users.noreply.github.com> Date: Fri, 10 Feb 2023 16:10:34 +0200 Subject: [PATCH] chore: major restructuring (again) --- include/ppc.hh | 13 +++-- include/treeifier/constr.hh | 53 +++++-------------- include/treeifier/constructs/glob.hh | 40 ++++++++++++++ include/treeifier/parsers/definition.hh | 13 +++++ include/treeifier/parsers/glob.hh | 16 ++++++ .../treeifier/{constr => parsers}/helper.hh | 14 ++--- .../treeifier/{constr => parsers}/inspoint.hh | 23 ++++---- src/lsinc.cc | 19 ++++++- src/main/main.cc | 10 ++-- src/treeifier/ast.cc | 8 ++- src/treeifier/constr.cc | 12 ++--- src/treeifier/constr/glob.cc | 8 +-- src/treeifier/lexer.cc | 4 +- 13 files changed, 147 insertions(+), 86 deletions(-) create mode 100644 include/treeifier/constructs/glob.hh create mode 100644 include/treeifier/parsers/definition.hh create mode 100644 include/treeifier/parsers/glob.hh rename include/treeifier/{constr => parsers}/helper.hh (92%) rename include/treeifier/{constr => parsers}/inspoint.hh (75%) diff --git a/include/ppc.hh b/include/ppc.hh index e8c22b9..7ebad24 100644 --- a/include/ppc.hh +++ b/include/ppc.hh @@ -1,10 +1,13 @@ #ifndef PPC_H #define PPC_H 1 -#include "compiler.h" - -static const int VERSION_MAJOR = PPC_VERSION_MAJOR; -static const int VERSION_MINOR = PPC_VERSION_MINOR; -static const int VERSION_BUILD = PPC_VERSION_BUILD; +#include "lang/version.hh" +namespace ppc { + static const version_t VERSION( + PPC_VERSION_MAJOR, + PPC_VERSION_MINOR, + PPC_VERSION_BUILD + ); +} #endif \ No newline at end of file diff --git a/include/treeifier/constr.hh b/include/treeifier/constr.hh index 5a8d2fa..627aed5 100644 --- a/include/treeifier/constr.hh +++ b/include/treeifier/constr.hh @@ -1,8 +1,8 @@ #pragma once +#include "utils/message.hh" #include "lang/common.hh" #include "treeifier/tokenizer.hh" -#include "utils/data.hh" #include #include #include @@ -12,54 +12,29 @@ #include using namespace std::string_literals; -using namespace ppc; -using namespace ppc::lang; -using namespace ppc::messages; -namespace ppc::tree::constr { - struct ast_ctx_t; +namespace ppc::tree { + using namespace ppc; + using namespace ppc::lang; + using namespace ppc::messages; - struct glob_t { - loc_nmsp_t nmsp; - std::vector imports; - - #ifdef PROFILE_debug - void print() const { - std::cout << "Namespace: " << nmsp.to_string() << "\n"; - std::cout << "Imports:\n"; - for (auto &imp : imports) { - std::cout << " - " << imp.to_string() << "\n"; - } - } - #endif - }; - - template - class parser_t { - public: - virtual bool operator()(ast_ctx_t &ctx, size_t &res_i, ParserT &out) const = 0; - virtual bool simplify(ast_ctx_t &ctx, GlobT &glob, ParserT &val) const = 0; - }; - - - struct ast_ctx_t { + struct parse_ctx_t { public: msg_stack_t &messages; std::vector &tokens; loc_nmsp_t nmsp; - ast_ctx_t &operator=(const ast_ctx_t &other) = delete; - ast_ctx_t(msg_stack_t &messages, std::vector &tokens); + parse_ctx_t &operator=(const parse_ctx_t &other) = delete; + parse_ctx_t(msg_stack_t &messages, std::vector &tokens); }; - bool parse_identifier(ast_ctx_t &ctx, size_t &res_i, located_t &out); - bool parse_nmsp(ast_ctx_t &ctx, size_t &res_i, loc_nmsp_t &out); - bool parse_nmsp_id(ast_ctx_t &ctx, size_t &res_i, glob_t glob, nmsp_t nmsp); + namespace parse { + bool parse_identifier(parse_ctx_t &ctx, size_t &res_i, located_t &out); + bool parse_nmsp(parse_ctx_t &ctx, size_t &res_i, loc_nmsp_t &out); + bool parse_nmsp_id(parse_ctx_t &ctx, size_t &res_i, std::set imports, nmsp_t nmsp); + } + - class glob_parser_t { - public: - bool operator()(ast_ctx_t &ctx, glob_t &out) const; - }; // parser_func_t parse_glob, parse_nmsp, parse_identifier, parse_type, parse_exp, parse_stat_exp; // parser_func_t parse_func, parse_field, parse_export, parse_struct; diff --git a/include/treeifier/constructs/glob.hh b/include/treeifier/constructs/glob.hh new file mode 100644 index 0000000..16962ad --- /dev/null +++ b/include/treeifier/constructs/glob.hh @@ -0,0 +1,40 @@ +#pragma once + +#include +#include +#include "lang/common.hh" +#include "utils/message.hh" + +#ifdef PROFILE_debug +#include +#endif + +namespace ppc::tree::constr { + using namespace ppc::lang; + using namespace ppc::messages; + + struct global_t; + + struct definition_t { + virtual std::string name() const = 0; + + virtual bool verify(msg_stack_t &msg_stack, global_t &glob) const { return true; } + virtual bool simplify(global_t &glob) const { return false; } + }; + + struct global_t { + loc_nmsp_t nmsp; + std::vector> defs; + std::vector imports; + + #ifdef PROFILE_debug + void print() const { + std::cout << "Namespace: " << nmsp.to_string() << "\n"; + std::cout << "Imports:\n"; + for (auto &imp : imports) { + std::cout << " - " << imp.to_string() << "\n"; + } + } + #endif + }; +} \ No newline at end of file diff --git a/include/treeifier/parsers/definition.hh b/include/treeifier/parsers/definition.hh new file mode 100644 index 0000000..575da5b --- /dev/null +++ b/include/treeifier/parsers/definition.hh @@ -0,0 +1,13 @@ +#pragma once + +#include "lang/common.hh" +#include "treeifier/tokenizer.hh" +#include "treeifier/constr.hh" +#include "treeifier/constructs/glob.hh" + +namespace ppc::tree::parse { + class definition_parser_t { + public: + virtual bool operator()(parse_ctx_t &ctx, size_t &res_i, constr::definition_t &res) const = 0; + }; +} \ No newline at end of file diff --git a/include/treeifier/parsers/glob.hh b/include/treeifier/parsers/glob.hh new file mode 100644 index 0000000..479b07f --- /dev/null +++ b/include/treeifier/parsers/glob.hh @@ -0,0 +1,16 @@ +#pragma once + +#include "lang/common.hh" +#include "treeifier/tokenizer.hh" +#include "treeifier/constr.hh" +#include "treeifier/constructs/glob.hh" +#include "treeifier/parsers/inspoint.hh" + +namespace ppc::tree::parse { + using namespace constr; + + class glob_parser_t { + public: + bool operator()(parse_ctx_t &ctx, global_t &out) const; + }; +} \ No newline at end of file diff --git a/include/treeifier/constr/helper.hh b/include/treeifier/parsers/helper.hh similarity index 92% rename from include/treeifier/constr/helper.hh rename to include/treeifier/parsers/helper.hh index bfb7186..f0dc831 100644 --- a/include/treeifier/constr/helper.hh +++ b/include/treeifier/parsers/helper.hh @@ -1,15 +1,11 @@ +#pragma once + #include "treeifier/constr.hh" -using namespace ppc; -using namespace ppc::lang; -using namespace ppc::data; -using namespace ppc::tree; -using namespace ppc::tree::constr; - -namespace ppc::tree::constr { +namespace ppc::tree::parse { struct helper_t { private: - ast_ctx_t &ctx; + parse_ctx_t &ctx; size_t &res_i; public: @@ -122,7 +118,7 @@ namespace ppc::tree::constr { } } - helper_t(ast_ctx_t &ctx, size_t &i): ctx(ctx), res_i(i) { + helper_t(parse_ctx_t &ctx, size_t &i): ctx(ctx), res_i(i) { this->i = i; } }; diff --git a/include/treeifier/constr/inspoint.hh b/include/treeifier/parsers/inspoint.hh similarity index 75% rename from include/treeifier/constr/inspoint.hh rename to include/treeifier/parsers/inspoint.hh index 96bb632..de04114 100644 --- a/include/treeifier/constr/inspoint.hh +++ b/include/treeifier/parsers/inspoint.hh @@ -1,8 +1,14 @@ -#include "treeifier/constr.hh" -#include "treeifier/constr/helper.hh" -#include "treeifier/constr/nmsp.hh" +#pragma once + +#include "treeifier/constr.hh" +#include "treeifier/parsers/helper.hh" + +#ifdef PROFILE_debug +#include +#endif + +namespace ppc::tree::parse { -namespace ppc::tree::constr { struct named_t { virtual std::string name() = 0; virtual ~named_t() = default; @@ -31,18 +37,13 @@ namespace ppc::tree::constr { return *this; } - bool operator()(ast_ctx_t &ctx, size_t &i, std::unique_ptr &out) const override { + bool operator()(ppc::tree::parse_ctx_t &ctx, size_t &i, std::unique_ptr &out) const override { helper_t h(ctx, i); if (h.ended()) return false; for (std::pair> &pair : parsers) { - ResT res; - - if (pair.second(ctx, i, res)) { - out = std::make_unique(res); - return true; - } + if (pair.second(ctx, i, out)) return true; } return false; diff --git a/src/lsinc.cc b/src/lsinc.cc index 003a3ea..3410a82 100644 --- a/src/lsinc.cc +++ b/src/lsinc.cc @@ -60,10 +60,26 @@ bool get_import(std::string line, fs::path root, fs::path &import) { void get_imports(fs::path file, fs::path include, std::set &res) { static std::set parents = { }; + static std::vector tmp = { }; if (!parents.emplace(file).second) { - throw "Circular dependency encountered."s; + std::stringstream ss; + ss << "Circular dependency encountered ("; + auto it = tmp.rbegin(); + + for (; it != tmp.rend(); it++) { + if (*it == file) break; + ss << *it << "<-"; + } + + ss << *it << ")"; + + parents.clear(); + tmp.clear(); + + throw ss.str(); } + tmp.push_back(file); std::ifstream f(file.string()); if (f.is_open() && !f.eof()) { @@ -80,6 +96,7 @@ void get_imports(fs::path file, fs::path include, std::set &res) { } parents.erase(file); + tmp.pop_back(); } int main(int argc, char **argv) { diff --git a/src/main/main.cc b/src/main/main.cc index aa491e2..ae1d204 100644 --- a/src/main/main.cc +++ b/src/main/main.cc @@ -17,9 +17,10 @@ #endif #include "./opions.hh" -#include "treeifier/constr.hh" #include "treeifier/lexer.hh" #include "treeifier/tokenizer.hh" +#include "treeifier/constructs/glob.hh" +#include "treeifier/parsers/glob.hh" #include "utils/json.hh" #include "utils/strings.hh" #include "utils/threading.hh" @@ -32,7 +33,6 @@ using std::cout; using std::size_t; using namespace ppc; using namespace ppc::tree; -using namespace ppc::tree::constr; void add_flags(options::parser_t &parser) { parser.add_flag({ @@ -162,9 +162,9 @@ int main(int argc, const char *argv[]) { std::ifstream f { file, std::ios_base::in }; if (!f.is_open()) throw message_t::error("The file doesn't exist.", { file }); auto tokens = token_t::parse_many(msg_stack, lex::token_t::parse_file(msg_stack, file, f)); - glob_t glob; - auto ctx = ast_ctx_t(msg_stack, tokens); - glob_parser_t()(ctx, glob); + constr::global_t glob; + auto ctx = parse_ctx_t(msg_stack, tokens); + parse::glob_parser_t()(ctx, glob); #ifdef PROFILE_debug glob.print(); #endif diff --git a/src/treeifier/ast.cc b/src/treeifier/ast.cc index 6063987..029f15c 100644 --- a/src/treeifier/ast.cc +++ b/src/treeifier/ast.cc @@ -2,17 +2,15 @@ #include using namespace ppc; -using namespace ppc::data; using namespace ppc::lang; -using namespace ppc::tree::constr; +using namespace ppc::tree; -// ppc::tree::constr::inspoint_t &ast_ctx_t::group(const std::string &name) { +// ppc::tree::constr::inspoint_t &parse_ctx_t::group(const std::string &name) { // if (groups.find(name) == groups.end()) return groups[name] = {}; // else return groups[name]; // } -ast_ctx_t::ast_ctx_t(msg_stack_t &messages, std::vector &tokens): - messages(messages), tokens(tokens) { +parse_ctx_t::parse_ctx_t(msg_stack_t &messages, std::vector &tokens): messages(messages), tokens(tokens) { // group("$_exp_val") // .add("$_var", parse_exp_var) // .add("$_int", parse_exp_int_lit) diff --git a/src/treeifier/constr.cc b/src/treeifier/constr.cc index ce7ff52..fbc0f58 100644 --- a/src/treeifier/constr.cc +++ b/src/treeifier/constr.cc @@ -1,10 +1,10 @@ #include "treeifier/constr.hh" -#include "treeifier/constr/helper.hh" +#include "treeifier/parsers/helper.hh" #include "lang/common.hh" -using namespace ppc::tree::constr; +using namespace ppc::tree; -bool ppc::tree::constr::parse_identifier(ast_ctx_t &ctx, size_t &res_i, located_t &out) { +bool parse::parse_identifier(parse_ctx_t &ctx, size_t &res_i, located_t &out) { helper_t h(ctx, res_i); if (h.ended()) return false; @@ -16,7 +16,7 @@ bool ppc::tree::constr::parse_identifier(ast_ctx_t &ctx, size_t &res_i, located_ else return false; } -bool ppc::tree::constr::parse_nmsp(ast_ctx_t &ctx, size_t &res_i, loc_nmsp_t &out) { +bool parse::parse_nmsp(parse_ctx_t &ctx, size_t &res_i, loc_nmsp_t &out) { helper_t h(ctx, res_i); if (h.ended()) return false; @@ -38,11 +38,11 @@ bool ppc::tree::constr::parse_nmsp(ast_ctx_t &ctx, size_t &res_i, loc_nmsp_t &ou return h.submit(false); } -bool ppc::tree::constr::parse_nmsp_id(ast_ctx_t &ctx, size_t &res_i, glob_t glob, nmsp_t nmsp) { +bool parse::parse_nmsp_id(parse_ctx_t &ctx, size_t &res_i, std::set glob, nmsp_t nmsp) { helper_t h(ctx, res_i); loc_nmsp_t src; if (!h.parse(parse_nmsp, src)) return false; - if (resolve_name(glob.imports, src, nmsp)) return h.submit(false); + if (resolve_name(glob, src, nmsp)) return h.submit(false); else return false; } diff --git a/src/treeifier/constr/glob.cc b/src/treeifier/constr/glob.cc index d150cf2..9e751b0 100644 --- a/src/treeifier/constr/glob.cc +++ b/src/treeifier/constr/glob.cc @@ -1,9 +1,11 @@ #include "treeifier/constr.hh" -#include "treeifier/constr/helper.hh" +#include "treeifier/parsers/helper.hh" +#include "treeifier/parsers/glob.hh" +#include "treeifier/constructs/glob.hh" -using namespace ppc::tree::constr; +using namespace ppc::tree; -bool ppc::tree::constr::glob_parser_t::operator()(ast_ctx_t &ctx, glob_t &out) const { +bool parse::glob_parser_t::operator()(parse_ctx_t &ctx, constr::global_t &out) const { size_t res_i = 0; helper_t h(ctx, res_i); out = {}; diff --git a/src/treeifier/lexer.cc b/src/treeifier/lexer.cc index d17352f..358b1d2 100644 --- a/src/treeifier/lexer.cc +++ b/src/treeifier/lexer.cc @@ -26,7 +26,7 @@ struct res_t { }; -static inline bool isoct(char c) { +static inline bool is_oct(char c) { return c >= '0' && c <= '7'; } static inline bool is_any(char c, std::string chars) { @@ -70,7 +70,7 @@ static res_t lexlet_bin(char c, std::vector &tok) { else return lexer_end(token_t::BIN_LITERAL); }; static res_t lexlet_oct(char c, std::vector &tok) { - if (isoct(c)) return lexer_none(); + if (is_oct(c)) return lexer_none(); else if (isdigit(c)) throw message_t::error("An octal literal may only contain octal digits."); else return lexer_end(token_t::OCT_LITERAL); };