Skip to content

Commit

Permalink
[cpp] ast node can now be to_string() and fixed iomanip fidgeting on …
Browse files Browse the repository at this point in the history
…-v output
  • Loading branch information
harrand committed May 5, 2024
1 parent f80a7e4 commit 839eed6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
29 changes: 28 additions & 1 deletion cpp/src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,38 @@

struct ast
{
struct integer_literal
{
std::int64_t val;
constexpr std::string to_string() const
{
return std::format("integer-literal({})", val);
}
};
struct decimal_literal
{
double val;
constexpr std::string to_string() const
{
return std::format("decimal-literal({})", val);
}
};

struct node
{
using payload_t = std::variant<int>;
using payload_t = std::variant<integer_literal, decimal_literal>;
payload_t payload;
srcloc meta;

constexpr std::string to_string() const
{
std::string ret;
std::visit([&ret](auto&& arg)
{
ret = arg.to_string();
}, this->payload);
return ret;
}
};
};

Expand Down
17 changes: 8 additions & 9 deletions cpp/src/psyc_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,21 @@ struct timers
void print()
{
constexpr int width = 12;
std::cout << std::right;
std::cout << std::setw(width) << "lexer:" << std::setw(6) << std::setprecision(3) << (this->lexing / 1000.0f) << " seconds" << std::endl;
std::cout << std::setw(width) << "parser:" << std::setw(6) << std::setprecision(3) << (this->parsing / 1000.0f) << " seconds" << std::endl;
std::cout << std::setw(width) << "buildmeta:" << std::setw(6) << std::setprecision(3) << (this->buildmeta / 1000.0f) << " seconds" << std::endl;
std::cout << std::setw(width) << "semal:" << std::setw(6) << std::setprecision(3) << (this->semal / 1000.0f) << " seconds" << std::endl;
std::cout << std::setw(width) << "codegen:" << std::setw(6) << std::setprecision(3) << (this->codegen / 1000.0f) << " seconds" << std::endl;
std::cout << std::setw(width) << "link:" << std::setw(6) << std::setprecision(3) << (this->link / 1000.0f) << " seconds" << std::endl;
std::cout << std::left << std::setw(width) << "lexer:" << std::setw(6) << std::setprecision(3) << std::right << (this->lexing / 1000.0f) << " seconds" << std::endl;
std::cout << std::left << std::setw(width) << "parser:" << std::setw(6) << std::setprecision(3) << std::right << (this->parsing / 1000.0f) << " seconds" << std::endl;
std::cout << std::left << std::setw(width) << "buildmeta:" << std::setw(6) << std::setprecision(3) << std::right << (this->buildmeta / 1000.0f) << " seconds" << std::endl;
std::cout << std::left << std::setw(width) << "semal:" << std::setw(6) << std::setprecision(3) << std::right << (this->semal / 1000.0f) << " seconds" << std::endl;
std::cout << std::left << std::setw(width) << "codegen:" << std::setw(6) << std::setprecision(3) << std::right << (this->codegen / 1000.0f) << " seconds" << std::endl;
std::cout << std::left << std::setw(width) << "link:" << std::setw(6) << std::setprecision(3) << std::right << (this->link / 1000.0f) << " seconds" << std::endl;
std::uint64_t total =
this->lexing +
this->parsing +
this->buildmeta +
this->semal +
this->codegen +
this->link;
std::cout << std::setw(width + 6 + 9) << std::setfill('=') << "\n" << std::setfill(' ');
std::cout << std::setw(width) << "total:" << std::setw(6) << std::setprecision(3) << (total / 1000.0f) << " seconds" << std::endl;
std::cout << std::right << std::setw(width + 6 + 9) << std::setfill('=') << "\n" << std::setfill(' ');
std::cout << std::left << std::setw(width) << "total:" << std::setw(6) << std::setprecision(3) << std::right << (total / 1000.0f) << " seconds" << std::endl;
}
};

Expand Down

0 comments on commit 839eed6

Please sign in to comment.