mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
yul: adds reindent() function to reindent yul source code and makes use of it in IRGenerator.
This function does count curly and round braces and reindents accordingly the beginning of each line. It does consider line-comments (// and ///) but not multiline comments (/* ... */).
This commit is contained in:
committed by
chriseth
parent
5329a84912
commit
0fd1db533e
@@ -26,10 +26,57 @@
|
||||
#include <libdevcore/CommonData.h>
|
||||
#include <libdevcore/FixedHash.h>
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
using namespace dev;
|
||||
using namespace yul;
|
||||
|
||||
using boost::split;
|
||||
using boost::is_any_of;
|
||||
|
||||
string yul::reindent(string const& _code)
|
||||
{
|
||||
auto const static countBraces = [](string const& _s) noexcept -> int
|
||||
{
|
||||
auto const i = _s.find("//");
|
||||
auto const e = i == _s.npos ? end(_s) : next(begin(_s), i);
|
||||
auto const opening = count_if(begin(_s), e, [](auto ch) { return ch == '{' || ch == '('; });
|
||||
auto const closing = count_if(begin(_s), e, [](auto ch) { return ch == '}' || ch == ')'; });
|
||||
return opening - closing;
|
||||
};
|
||||
|
||||
vector<string> lines;
|
||||
split(lines, _code, is_any_of("\n"));
|
||||
for (string& line: lines)
|
||||
boost::trim(line);
|
||||
|
||||
stringstream out;
|
||||
int depth = 0;
|
||||
|
||||
for (string const& line: lines)
|
||||
{
|
||||
int const diff = countBraces(line);
|
||||
if (diff < 0)
|
||||
depth += diff;
|
||||
|
||||
for (int i = 0; i < depth; ++i)
|
||||
out << '\t';
|
||||
|
||||
out << line << '\n';
|
||||
|
||||
if (diff > 0)
|
||||
depth += diff;
|
||||
}
|
||||
|
||||
return out.str();
|
||||
}
|
||||
|
||||
u256 yul::valueOfNumberLiteral(Literal const& _literal)
|
||||
{
|
||||
yulAssert(_literal.kind == LiteralKind::Number, "Expected number literal!");
|
||||
|
||||
Reference in New Issue
Block a user