Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre and post that are run only once, even if the pass is repeated #58

Merged
merged 1 commit into from
Aug 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions include/trieste/pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ namespace trieste
class PassDef
{
public:
using PreF = std::function<size_t(Node)>;
using PostF = std::function<size_t(Node)>;
using F = std::function<size_t(Node)>;

private:
std::map<Token, PreF> pre_;
std::map<Token, PostF> post_;
F pre_once;
F post_once;
std::map<Token, F> pre_;
std::map<Token, F> post_;
dir::flag direction_;
std::vector<detail::PatternEffect<Node>> rules_;

Expand All @@ -45,12 +46,22 @@ namespace trieste
return std::make_shared<PassDef>(std::move(*this));
}

void pre(const Token& type, PreF f)
void pre(F f)
{
pre_once = f;
}

void post(F f)
{
post_once = f;
}

void pre(const Token& type, F f)
{
pre_[type] = f;
}

void post(const Token& type, PostF f)
void post(const Token& type, F f)
{
post_[type] = f;
}
Expand All @@ -73,6 +84,9 @@ namespace trieste
size_t changes_sum = 0;
size_t count = 0;

if (pre_once)
changes += pre_once(node);

// Because apply runs over child nodes, the top node is never visited.
do
{
Expand All @@ -89,6 +103,9 @@ namespace trieste
break;
} while (changes > 0);

if (post_once)
changes += post_once(node);

return {node, count, changes_sum};
}

Expand Down