Skip to content

Commit

Permalink
add wf to debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
fhackett committed Aug 13, 2024
1 parent 971e0a6 commit 01a59a5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 22 deletions.
79 changes: 58 additions & 21 deletions include/trieste/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "pass.h"
#include "wf.h"
#include "wf_meta.h"

#include <chrono>
#include <filesystem>
Expand Down Expand Up @@ -161,7 +162,8 @@ namespace trieste

bool check_well_formed{true};

std::function<bool(Node&, std::string, size_t index, PassStatistics&)>
std::function<bool(
Node&, std::string, const wf::Wellformed&, size_t index, PassStatistics&)>
pass_complete;

std::function<Nodes(Nodes&, std::string)> error_pass;
Expand All @@ -174,12 +176,27 @@ namespace trieste
* AST and details of the pass that has just completed.
*/
Process& set_pass_complete(
std::function<bool(Node&, std::string, size_t, PassStatistics&)> f)
std::function<bool(
Node&, std::string, const wf::Wellformed&, size_t, PassStatistics&)> f)
{
pass_complete = f;
return *this;
}

Process& set_pass_complete(
std::function<bool(Node&, std::string, size_t, PassStatistics&)> f)
{
pass_complete = [f](
Node& ast,
std::string pass_name,
const wf::Wellformed&,
size_t index,
PassStatistics& stats) {
return f(ast, pass_name, index, stats);
};
return *this;
}

Process& set_default_pass_complete(
logging::Log& summary,
const std::string& language_name = "",
Expand All @@ -188,6 +205,7 @@ namespace trieste
pass_complete = [output_directory, language_name, &summary](
Node& ast,
std::string pass_name,
const wf::Wellformed& wf,
size_t index,
PassStatistics& stats) {
auto [count, changes, duration] = stats;
Expand All @@ -214,28 +232,44 @@ namespace trieste
}
}

std::filesystem::path output;
if (index < 10)
{
output = output_directory /
("0" + std::to_string(index) + "_" + pass_name + ".trieste");
}
else
{
output = output_directory /
(std::to_string(index) + "_" + pass_name + ".trieste");
}
auto open_dbg_file =
[&](const std::string& ext) -> std::pair<std::ofstream, bool> {
std::filesystem::path output;
if (index < 10)
{
output = output_directory /
("0" + std::to_string(index) + "_" + pass_name + ext);
}
else
{
output = output_directory /
(std::to_string(index) + "_" + pass_name + ext);
}

std::ofstream f(output, std::ios::binary | std::ios::out);
std::ofstream f(output, std::ios::binary | std::ios::out);
bool ok = true;
if (!f)
{
logging::Error() << "Could not open " << output << " for writing.";
ok = false;
}
return {std::move(f), ok};
};

if (!f)
{
logging::Error() << "Could not open " << output << " for writing.";
auto [f, ok] = open_dbg_file(".trieste");
if (!ok)
return false;
}

// Write the AST to the output file.
f << language_name << std::endl << pass_name << std::endl << ast;

std::tie(f, ok) = open_dbg_file(".trieste_wf");
if (!ok)
return false;
// Write the well-formedness definition to a neighboring output file.
f << language_name << std::endl
<< pass_name << std::endl
<< wf::meta::wf_to_node(wf);

return true;
};

Expand Down Expand Up @@ -285,7 +319,10 @@ namespace trieste

PassStatistics stats;
std::string last_pass = pass_range.entry_pass_name();
ok = pass_complete(ast, pass_range.entry_pass_name(), 0, stats) && ok;
ok =
pass_complete(
ast, pass_range.entry_pass_name(), pass_range.input_wf(), 0, stats) &&
ok;

for (; ok && pass_range.has_next(); index++)
{
Expand All @@ -309,7 +346,7 @@ namespace trieste
changes,
std::chrono::duration_cast<std::chrono::microseconds>(then - now)};

ok = pass_complete(ast, pass->name(), index, stats) && ok;
ok = pass_complete(ast, pass->name(), pass->wf(), index, stats) && ok;

last_pass = pass->name();
}
Expand Down
7 changes: 6 additions & 1 deletion include/trieste/wf_meta.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#pragma once

#include "rewrite.h"
#include "token.h"
#include "wf.h"

#include <algorithm>
#include <initializer_list>
#include <sstream>
#include <stdexcept>
#include <trieste/trieste.h>
#include <unordered_set>
#include <variant>

namespace trieste::wf::meta
{
using namespace wf::ops;

inline const auto WfMeta = TokenDef("wf-meta-meta");

inline const auto WfNone = TokenDef("wf-meta-none");
Expand Down

0 comments on commit 01a59a5

Please sign in to comment.