initial shared executor

This commit is contained in:
ProgramSnail 2024-09-12 21:41:33 +03:00
parent 331d97ab48
commit 3445a604f7
6 changed files with 145 additions and 32 deletions

View file

@ -11,8 +11,10 @@ int main(int argc, char **argv) {
//
SourcesManager sources_manager;
app::SourcesManager sources_manager(
utils::BuildPrintLog(std::cout),
printers::Printer(std::cout, 2, 80, true));
sources_manager.AddFile(filename);
sources_manager.Print(std::cout);
sources_manager.Print();
}

View file

@ -14,10 +14,28 @@
#include <iostream>
#include <sstream>
namespace app {
using Exprs = nodes::NodeStorage; // nodes::ExprStorage;
using Types = nodes::TypeStorage;
using Names = names::NameTree;
using Positions = core::DependentStorage<utils::Pos>;
using Printer = printers::Printer;
// TODO: partial host ?? (part of params are stored in each executor, use if
// constexpr requires) (for Printer, State, ...)
using ExecutorHost =
core::ExecutorHost<Exprs, Types, Names, Positions,
builders::State /*, type_check::State*/, Printer>;
class SourcesManager {
public:
SourcesManager(Log &&log, Printer &&printer)
: executor_host_(std::move(log), {}, {}, {}, {}, {}, std::move(printer)),
statements_{} {}
void AddFile(const std::string &filename) {
Log::Context logc(executor_.log(), Log::Area::kParse);
Log::Context logc(executor_host_.log(), Log::Area::kParse);
std::ifstream in;
in.open(filename);
@ -37,7 +55,8 @@ public:
{{"There are some parsing errors in file"}} /*,parse_tree.get_root()*/);
}
builders::BuilderTask<nodes::Statements> statements_builder(executor_);
builders::Executor executor(executor_host_);
builders::BuilderTask<nodes::Statements> statements_builder(executor);
auto new_statements = statements_builder(parse_tree.get_root(), {});
@ -49,14 +68,8 @@ public:
new_statements.clear();
}
void Print(std::ostream &out) {
printers::Printer printer(out, 2, 80, true);
// TODO: FIXME: share executor
printers::Executor executor(
utils::BuildPrintLog(std::cout),
printers::Exprs{executor_.state<printers::Exprs>()},
std::move(printer));
void Print() { // TODO: any output
printers::Executor executor(executor_host_);
printers::PrintTask<nodes::Statements>{executor}(statements_, {});
}
@ -69,7 +82,8 @@ public:
}
private:
builders::Executor executor_ =
builders::Executor(utils::BuildPrintLog(std::cout), {}, {}, {}, {}, {});
std::vector<nodes::Statement> statements_ = {};
ExecutorHost executor_host_;
std::vector<nodes::Statement> statements_;
};
} // namespace app

View file

@ -0,0 +1,9 @@
#pragma once
namespace core {
struct None {
static constexpr const char *name = "Executor";
};
} // namespace core

View file

@ -0,0 +1,50 @@
#pragma once
#include "core_utils.hpp"
#include "log.hpp"
namespace core {
template <typename Tag>
requires requires() { Tag::name; }
class CounterElem;
template <typename Tag = core::None>
requires requires() { Tag::name; }
class Counter {
friend class CounterElem<Tag>;
public:
Counter() = default;
~Counter() {
utils::Assert(count_ == 0,
std::format("counter has been failed, count={}, tag={}",
count_, Tag::name));
}
private:
void inc() { ++count_; }
void dec() {
utils::Assert(count_ != 0, "counter internal error, can't dec zero");
--count_;
}
private:
size_t count_ = 0;
};
template <typename Tag = core::None>
requires requires() { Tag::name; }
class CounterElem {
public:
CounterElem(Counter<Tag> &counter) : counter_(counter) { counter_.inc(); }
~CounterElem() { counter_.dec(); }
private:
Counter<Tag> &counter_;
};
} // namespace core

View file

@ -1,25 +1,70 @@
#pragma once
#include "counter.hpp"
#include "log.hpp"
namespace core {
struct None {};
template <typename Exec, typename Ret, typename Args, typename Node> class Task;
template <typename State> class ExecutorState {
//
namespace utils {
struct ExecutorTag {
static constexpr const char *name = "Executor";
};
} // namespace utils
template <typename State> class ExecutorStateHost {
public:
ExecutorState(State &&state) : state_(std::move(state)) {}
ExecutorStateHost(State &&state) : state_(std::move(state)) {}
State &operator*() { return state_; }
const State &operator*() const { return state_; }
protected:
State state_;
};
template <typename... States>
class ExecutorHost : public ExecutorStateHost<States>... {
public:
ExecutorHost(Log &&log, States &&...states)
: ExecutorStateHost<States>(std::move(states))..., log_(std::move(log)) {}
template <typename T> T &state() { return ExecutorStateHost<T>::state_; }
template <typename T> const T &state() const {
return ExecutorStateHost<T>::state_;
}
//
Log &log() { return log_; }
const Log &log() const { return log_; }
Counter<utils::ExecutorTag> &counter() { return counter_; }
private:
Log log_;
Counter<utils::ExecutorTag> counter_;
};
//
template <typename State> class ExecutorState {
public:
ExecutorState(ExecutorStateHost<State> &host) : state_(*host) {}
protected:
State &state_;
};
template <typename... States> class Executor : public ExecutorState<States>... {
public:
Executor(Log &&log, States &&...states)
: ExecutorState<States>(std::move(states))..., log_(std::move(log)) {}
template <typename... OtherStates>
Executor(ExecutorHost<OtherStates...> &host)
: ExecutorState<States>(host)..., log_(host.log()),
counter_(host.counter()) {}
//
@ -38,7 +83,8 @@ public:
const Log &log() const { return log_; }
private:
Log log_;
Log &log_;
CounterElem<utils::ExecutorTag> counter_;
};
template <typename Exec, typename Ret, typename Args, typename Node>
@ -50,14 +96,6 @@ public:
//
// template <typename N, typename NArgs> Ret Run(const N& node, NArgs&& args)
// {
// Task<Exec, Ret, NArgs, N> task(executor);
// return task(node, std::forward<Args>(args));
// }
//
virtual Ret operator()(const Node &node, const Args &args = {}) = 0;
//

View file

@ -49,8 +49,8 @@ template <typename T> class DependentStorage : public Storage<T> {
public:
void ForceInsert(Id id, T elem) {
utils::Assert(Storage<T>::data_.insert({id, std::move(elem)}).second,
std::format("insert failed, id={}", *id));
::utils::Assert(Storage<T>::data_.insert({id, std::move(elem)}).second,
std::format("insert failed, id={}", *id));
}
bool Insert(Id id, T &&elem) {