chore: major restructuring (again)

This commit is contained in:
TopchetoEU 2023-02-10 16:10:34 +02:00
parent 4a351f8b78
commit f63ad45ccb
No known key found for this signature in database
GPG Key ID: 5ED5FFB2A3F5DB21
13 changed files with 147 additions and 86 deletions

View File

@ -1,10 +1,13 @@
#ifndef PPC_H #ifndef PPC_H
#define PPC_H 1 #define PPC_H 1
#include "compiler.h" #include "lang/version.hh"
namespace ppc {
static const int VERSION_MAJOR = PPC_VERSION_MAJOR; static const version_t VERSION(
static const int VERSION_MINOR = PPC_VERSION_MINOR; PPC_VERSION_MAJOR,
static const int VERSION_BUILD = PPC_VERSION_BUILD; PPC_VERSION_MINOR,
PPC_VERSION_BUILD
);
}
#endif #endif

View File

@ -1,8 +1,8 @@
#pragma once #pragma once
#include "utils/message.hh"
#include "lang/common.hh" #include "lang/common.hh"
#include "treeifier/tokenizer.hh" #include "treeifier/tokenizer.hh"
#include "utils/data.hh"
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <memory> #include <memory>
@ -12,54 +12,29 @@
#include <unordered_set> #include <unordered_set>
using namespace std::string_literals; using namespace std::string_literals;
namespace ppc::tree {
using namespace ppc; using namespace ppc;
using namespace ppc::lang; using namespace ppc::lang;
using namespace ppc::messages; using namespace ppc::messages;
namespace ppc::tree::constr { struct parse_ctx_t {
struct ast_ctx_t;
struct glob_t {
loc_nmsp_t nmsp;
std::vector<loc_nmsp_t> 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 ParserT, class GlobT = glob_t>
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 {
public: public:
msg_stack_t &messages; msg_stack_t &messages;
std::vector<token_t> &tokens; std::vector<token_t> &tokens;
loc_nmsp_t nmsp; loc_nmsp_t nmsp;
ast_ctx_t &operator=(const ast_ctx_t &other) = delete; parse_ctx_t &operator=(const parse_ctx_t &other) = delete;
ast_ctx_t(msg_stack_t &messages, std::vector<token_t> &tokens); parse_ctx_t(msg_stack_t &messages, std::vector<token_t> &tokens);
}; };
bool parse_identifier(ast_ctx_t &ctx, size_t &res_i, located_t<std::string> &out); namespace parse {
bool parse_nmsp(ast_ctx_t &ctx, size_t &res_i, loc_nmsp_t &out); bool parse_identifier(parse_ctx_t &ctx, size_t &res_i, located_t<std::string> &out);
bool parse_nmsp_id(ast_ctx_t &ctx, size_t &res_i, glob_t glob, nmsp_t nmsp); 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<nmsp_t> 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_glob, parse_nmsp, parse_identifier, parse_type, parse_exp, parse_stat_exp;
// parser_func_t parse_func, parse_field, parse_export, parse_struct; // parser_func_t parse_func, parse_field, parse_export, parse_struct;

View File

@ -0,0 +1,40 @@
#pragma once
#include <vector>
#include <memory>
#include "lang/common.hh"
#include "utils/message.hh"
#ifdef PROFILE_debug
#include <iostream>
#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<std::unique_ptr<definition_t>> defs;
std::vector<loc_nmsp_t> 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
};
}

View File

@ -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;
};
}

View File

@ -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;
};
}

View File

@ -1,15 +1,11 @@
#pragma once
#include "treeifier/constr.hh" #include "treeifier/constr.hh"
using namespace ppc; namespace ppc::tree::parse {
using namespace ppc::lang;
using namespace ppc::data;
using namespace ppc::tree;
using namespace ppc::tree::constr;
namespace ppc::tree::constr {
struct helper_t { struct helper_t {
private: private:
ast_ctx_t &ctx; parse_ctx_t &ctx;
size_t &res_i; size_t &res_i;
public: 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; this->i = i;
} }
}; };

View File

@ -1,8 +1,14 @@
#include "treeifier/constr.hh" #pragma once
#include "treeifier/constr/helper.hh"
#include "treeifier/constr/nmsp.hh" #include "treeifier/constr.hh"
#include "treeifier/parsers/helper.hh"
#ifdef PROFILE_debug
#include <iostream>
#endif
namespace ppc::tree::parse {
namespace ppc::tree::constr {
struct named_t { struct named_t {
virtual std::string name() = 0; virtual std::string name() = 0;
virtual ~named_t() = default; virtual ~named_t() = default;
@ -31,18 +37,13 @@ namespace ppc::tree::constr {
return *this; return *this;
} }
bool operator()(ast_ctx_t &ctx, size_t &i, std::unique_ptr<ResT> &out) const override { bool operator()(ppc::tree::parse_ctx_t &ctx, size_t &i, std::unique_ptr<ResT> &out) const override {
helper_t h(ctx, i); helper_t h(ctx, i);
if (h.ended()) return false; if (h.ended()) return false;
for (std::pair<std::string, std::unique_ptr<ParserT>> &pair : parsers) { for (std::pair<std::string, std::unique_ptr<ParserT>> &pair : parsers) {
ResT res; if (pair.second(ctx, i, out)) return true;
if (pair.second(ctx, i, res)) {
out = std::make_unique<ResT>(res);
return true;
}
} }
return false; return false;

View File

@ -60,11 +60,27 @@ bool get_import(std::string line, fs::path root, fs::path &import) {
void get_imports(fs::path file, fs::path include, std::set<fs::path> &res) { void get_imports(fs::path file, fs::path include, std::set<fs::path> &res) {
static std::set<fs::path> parents = { }; static std::set<fs::path> parents = { };
static std::vector<fs::path> tmp = { };
if (!parents.emplace(file).second) { 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()); std::ifstream f(file.string());
if (f.is_open() && !f.eof()) { if (f.is_open() && !f.eof()) {
std::string line; std::string line;
@ -80,6 +96,7 @@ void get_imports(fs::path file, fs::path include, std::set<fs::path> &res) {
} }
parents.erase(file); parents.erase(file);
tmp.pop_back();
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {

View File

@ -17,9 +17,10 @@
#endif #endif
#include "./opions.hh" #include "./opions.hh"
#include "treeifier/constr.hh"
#include "treeifier/lexer.hh" #include "treeifier/lexer.hh"
#include "treeifier/tokenizer.hh" #include "treeifier/tokenizer.hh"
#include "treeifier/constructs/glob.hh"
#include "treeifier/parsers/glob.hh"
#include "utils/json.hh" #include "utils/json.hh"
#include "utils/strings.hh" #include "utils/strings.hh"
#include "utils/threading.hh" #include "utils/threading.hh"
@ -32,7 +33,6 @@ using std::cout;
using std::size_t; using std::size_t;
using namespace ppc; using namespace ppc;
using namespace ppc::tree; using namespace ppc::tree;
using namespace ppc::tree::constr;
void add_flags(options::parser_t &parser) { void add_flags(options::parser_t &parser) {
parser.add_flag({ parser.add_flag({
@ -162,9 +162,9 @@ int main(int argc, const char *argv[]) {
std::ifstream f { file, std::ios_base::in }; std::ifstream f { file, std::ios_base::in };
if (!f.is_open()) throw message_t::error("The file doesn't exist.", { file }); 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)); auto tokens = token_t::parse_many(msg_stack, lex::token_t::parse_file(msg_stack, file, f));
glob_t glob; constr::global_t glob;
auto ctx = ast_ctx_t(msg_stack, tokens); auto ctx = parse_ctx_t(msg_stack, tokens);
glob_parser_t()(ctx, glob); parse::glob_parser_t()(ctx, glob);
#ifdef PROFILE_debug #ifdef PROFILE_debug
glob.print(); glob.print();
#endif #endif

View File

@ -2,17 +2,15 @@
#include <iostream> #include <iostream>
using namespace ppc; using namespace ppc;
using namespace ppc::data;
using namespace ppc::lang; 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] = {}; // if (groups.find(name) == groups.end()) return groups[name] = {};
// else return groups[name]; // else return groups[name];
// } // }
ast_ctx_t::ast_ctx_t(msg_stack_t &messages, std::vector<token_t> &tokens): parse_ctx_t::parse_ctx_t(msg_stack_t &messages, std::vector<token_t> &tokens): messages(messages), tokens(tokens) {
messages(messages), tokens(tokens) {
// group("$_exp_val") // group("$_exp_val")
// .add("$_var", parse_exp_var) // .add("$_var", parse_exp_var)
// .add("$_int", parse_exp_int_lit) // .add("$_int", parse_exp_int_lit)

View File

@ -1,10 +1,10 @@
#include "treeifier/constr.hh" #include "treeifier/constr.hh"
#include "treeifier/constr/helper.hh" #include "treeifier/parsers/helper.hh"
#include "lang/common.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<std::string> &out) { bool parse::parse_identifier(parse_ctx_t &ctx, size_t &res_i, located_t<std::string> &out) {
helper_t h(ctx, res_i); helper_t h(ctx, res_i);
if (h.ended()) return false; 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; 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); helper_t h(ctx, res_i);
if (h.ended()) return false; 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); 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<nmsp_t> glob, nmsp_t nmsp) {
helper_t h(ctx, res_i); helper_t h(ctx, res_i);
loc_nmsp_t src; loc_nmsp_t src;
if (!h.parse(parse_nmsp, src)) return false; 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; else return false;
} }

View File

@ -1,9 +1,11 @@
#include "treeifier/constr.hh" #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; size_t res_i = 0;
helper_t h(ctx, res_i); helper_t h(ctx, res_i);
out = {}; out = {};

View File

@ -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'; return c >= '0' && c <= '7';
} }
static inline bool is_any(char c, std::string chars) { static inline bool is_any(char c, std::string chars) {
@ -70,7 +70,7 @@ static res_t lexlet_bin(char c, std::vector<char> &tok) {
else return lexer_end(token_t::BIN_LITERAL); else return lexer_end(token_t::BIN_LITERAL);
}; };
static res_t lexlet_oct(char c, std::vector<char> &tok) { static res_t lexlet_oct(char c, std::vector<char> &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 if (isdigit(c)) throw message_t::error("An octal literal may only contain octal digits.");
else return lexer_end(token_t::OCT_LITERAL); else return lexer_end(token_t::OCT_LITERAL);
}; };