mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2025-12-05 22:48:43 +00:00
initial shared executor
This commit is contained in:
parent
331d97ab48
commit
3445a604f7
6 changed files with 145 additions and 32 deletions
|
|
@ -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.AddFile(filename);
|
||||||
sources_manager.Print(std::cout);
|
sources_manager.Print();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,10 +14,28 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#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 {
|
class SourcesManager {
|
||||||
public:
|
public:
|
||||||
|
SourcesManager(Log &&log, Printer &&printer)
|
||||||
|
: executor_host_(std::move(log), {}, {}, {}, {}, {}, std::move(printer)),
|
||||||
|
statements_{} {}
|
||||||
|
|
||||||
void AddFile(const std::string &filename) {
|
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;
|
std::ifstream in;
|
||||||
in.open(filename);
|
in.open(filename);
|
||||||
|
|
@ -37,7 +55,8 @@ public:
|
||||||
{{"There are some parsing errors in file"}} /*,parse_tree.get_root()*/);
|
{{"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(), {});
|
auto new_statements = statements_builder(parse_tree.get_root(), {});
|
||||||
|
|
||||||
|
|
@ -49,14 +68,8 @@ public:
|
||||||
new_statements.clear();
|
new_statements.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Print(std::ostream &out) {
|
void Print() { // TODO: any output
|
||||||
printers::Printer printer(out, 2, 80, true);
|
printers::Executor executor(executor_host_);
|
||||||
|
|
||||||
// TODO: FIXME: share executor
|
|
||||||
printers::Executor executor(
|
|
||||||
utils::BuildPrintLog(std::cout),
|
|
||||||
printers::Exprs{executor_.state<printers::Exprs>()},
|
|
||||||
std::move(printer));
|
|
||||||
printers::PrintTask<nodes::Statements>{executor}(statements_, {});
|
printers::PrintTask<nodes::Statements>{executor}(statements_, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -69,7 +82,8 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
builders::Executor executor_ =
|
ExecutorHost executor_host_;
|
||||||
builders::Executor(utils::BuildPrintLog(std::cout), {}, {}, {}, {}, {});
|
std::vector<nodes::Statement> statements_;
|
||||||
std::vector<nodes::Statement> statements_ = {};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace app
|
||||||
|
|
|
||||||
9
lang/utils/include/core_utils.hpp
Normal file
9
lang/utils/include/core_utils.hpp
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace core {
|
||||||
|
|
||||||
|
struct None {
|
||||||
|
static constexpr const char *name = "Executor";
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace core
|
||||||
50
lang/utils/include/counter.hpp
Normal file
50
lang/utils/include/counter.hpp
Normal 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
|
||||||
|
|
@ -1,25 +1,70 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "counter.hpp"
|
||||||
#include "log.hpp"
|
#include "log.hpp"
|
||||||
|
|
||||||
namespace core {
|
namespace core {
|
||||||
|
|
||||||
struct None {};
|
|
||||||
|
|
||||||
template <typename Exec, typename Ret, typename Args, typename Node> class Task;
|
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:
|
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:
|
protected:
|
||||||
State state_;
|
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>... {
|
template <typename... States> class Executor : public ExecutorState<States>... {
|
||||||
public:
|
public:
|
||||||
Executor(Log &&log, States &&...states)
|
template <typename... OtherStates>
|
||||||
: ExecutorState<States>(std::move(states))..., log_(std::move(log)) {}
|
Executor(ExecutorHost<OtherStates...> &host)
|
||||||
|
: ExecutorState<States>(host)..., log_(host.log()),
|
||||||
|
counter_(host.counter()) {}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
@ -38,7 +83,8 @@ public:
|
||||||
const Log &log() const { return log_; }
|
const Log &log() const { return log_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Log log_;
|
Log &log_;
|
||||||
|
CounterElem<utils::ExecutorTag> counter_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Exec, typename Ret, typename Args, typename Node>
|
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;
|
virtual Ret operator()(const Node &node, const Args &args = {}) = 0;
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
||||||
|
|
@ -49,8 +49,8 @@ template <typename T> class DependentStorage : public Storage<T> {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void ForceInsert(Id id, T elem) {
|
void ForceInsert(Id id, T elem) {
|
||||||
utils::Assert(Storage<T>::data_.insert({id, std::move(elem)}).second,
|
::utils::Assert(Storage<T>::data_.insert({id, std::move(elem)}).second,
|
||||||
std::format("insert failed, id={}", *id));
|
std::format("insert failed, id={}", *id));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Insert(Id id, T &&elem) {
|
bool Insert(Id id, T &&elem) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue