Add .clang-format and apply it

This commit is contained in:
2021-12-02 07:18:16 +00:00
parent e58dede1b6
commit cd5e2538df
103 changed files with 2714 additions and 2132 deletions

View File

@@ -9,46 +9,46 @@
#include <string>
#include <tuple>
enum class Token : char {
Eof,
Number,
LParens = '(',
RParens = ')',
Add = '+',
Multiply = '*'
};
enum class Token : char { Eof, Number, LParens = '(', RParens = ')', Add = '+', Multiply = '*' };
using Value = unsigned long;
struct Parser {
Parser(std::string const &s) : expr_(s), pos_(0) { skip_whitespace(); }
struct Parser
{
Parser(std::string const& s) : expr_(s), pos_(0) { skip_whitespace(); }
Value evaluate() { return binop(); }
private:
Value binop() {
Value binop()
{
auto value = primary();
do {
if (peek() == Token::Add) {
chew(Token::Add);
value += primary();
} else if (peek() == Token::Multiply) {
}
else if (peek() == Token::Multiply) {
chew(Token::Multiply);
value *= primary();
} else {
}
else {
return value;
}
} while (true);
}
Value primary() {
Value primary()
{
if (peek() == Token::LParens) {
chew(Token::LParens);
Value value = binop();
chew(Token::RParens);
return value;
} else if (peek() == Token::Number) {
}
else if (peek() == Token::Number) {
return chew_number();
} else {
}
else {
std::cout << "expr_ = " << expr_ << "\n";
std::cout << "pos_ = " << pos_ << "\n";
std::cout << "End = " << expr_.substr(pos_) << "\n";
@@ -56,7 +56,8 @@ private:
}
}
Token peek() {
Token peek()
{
if (pos_ == expr_.size()) {
return Token::Eof;
}
@@ -89,7 +90,8 @@ private:
}
}
void chew(Token tok) {
void chew(Token tok)
{
assert(peek() == tok);
switch (tok) {
case Token::LParens:
@@ -104,13 +106,15 @@ private:
}
}
void skip_whitespace() {
void skip_whitespace()
{
while (pos_ < expr_.size() && expr_[pos_] == ' ') {
++pos_;
}
}
Value chew_number() {
Value chew_number()
{
assert(peek() == Token::Number);
std::size_t len = 0;
@@ -124,7 +128,8 @@ private:
std::string::size_type pos_;
};
int main(void) {
int main(void)
{
std::string line;
Value result = 0;
while (std::getline(std::cin, line)) {