 |
Cerata
A library to generate structural hardware designs
|
15 #include "cerata/vhdl/block.h"
24 static std::string
tab(
int n) {
25 return std::string(
static_cast<uint64_t
>(2 * n),
' ');
29 lhs.
parts.push_back(str);
39 lhs.
parts.back().append(str);
44 std::vector<size_t> ret = {0};
45 for (
const auto &l :
lines) {
46 for (
size_t p = 0; p < l.parts.size(); p++) {
47 if (p >= ret.size()) {
48 ret.push_back(l.parts[p].length());
49 }
else if (l.parts[p].length() > ret[p]) {
50 ret[p] = l.parts[p].length();
58 std::stringstream ret;
60 for (
const auto &l :
lines) {
63 for (
size_t p = 0; p < l.parts.size(); p++) {
65 auto plen = l.parts[p].length();
66 auto diff = align - plen;
68 auto padding = std::string(diff,
' ');
69 m << l.parts[p] + padding;
75 ret << std::regex_replace(m.str(), std::regex(
"\\s+$"), std::string(
"")) +
"\n";
87 [&](
const Line &la,
const Line &lb) ->
bool {
88 auto a = la.ToString();
89 auto b = lb.ToString();
91 return a.substr(0, a.find_first_of(*c)) > b.substr(0, b.find_first_of(*c));
99 Block &Block::AppendBlankLineIfNotEmpty() {
100 if (!lines.empty()) {
101 if (!lines.back().IsBlank()) {
102 lines.emplace_back();
109 lhs.
lines.push_back(line);
120 for (
auto &l : rhs->
lines) {
121 if (!l.parts.empty()) {
122 if (l.parts.front() ==
" : ") {
123 l.parts.insert(l.parts.begin(), lhs);
125 l.parts.front() = lhs + sep + l.parts.front();
136 if (!lhs.
lines.empty()) {
137 for (
auto &l : lhs.
lines) {
138 if (!l.parts.empty()) {
139 l.
parts.back().append(rhs);
151 if (!lhs.
lines.empty()) {
152 for (
size_t i = 0; i < lhs.
lines.size() - 1; i++) {
153 lhs.
lines[i].parts.back().append(rhs);
160 lhs.
blocks.push_back(rhs);
172 for (
const auto &b : rhs.
blocks) {
178 std::string MultiBlock::ToString()
const {
179 std::stringstream ret;
180 for (
const auto &b : blocks) {
186 std::string
ToString(
const std::vector<Block> &blocks) {
187 std::stringstream ret;
188 for (
const auto &b : blocks) {
194 std::string Line::ToString()
const {
195 std::stringstream str;
196 for (
const auto &p : parts) {
std::string tab(uint n)
Return indent string.
std::shared_ptr< Edge > operator<<=(Node *dst, const std::shared_ptr< Node > &src)
Create an edge, connecting the src node to the dst node.
std::vector< std::string > parts
The parts of the line of code.
A structure to hold multiple blocks.
std::vector< Line > lines
Lines in the blocks.
std::string ToString() const
Return this block as a single string.
std::vector< Block > blocks
The blocks in this multiblock.
std::string ToString(Expression::Op operation)
Human-readable expression operator.
Block & Prepend(const std::string &lhs, Block *rhs, const std::string &sep)
Prepend a string to every line of a block.
Line & operator+=(Line &lhs, const std::string &str)
Append a string to the last line part.
std::string ToString() const
Return this multiblock as a single string.
Line & operator<<(Line &lhs, const std::string &str)
Append a part to a line.
Block & Sort(std::optional< char > c=std::nullopt)
Sort the lines in the block. Supply a character to stop sorting per line after encountering the chara...
int indent
Indenting level of the block.
Block & Reverse()
Return the block in reverse.
std::vector< size_t > GetAlignments() const
Return the alignment for each line.
Contains everything related to the VHDL back-end.