Skip to content

Commit 519024e

Browse files
committed
added beautify
1 parent d72bae0 commit 519024e

2 files changed

Lines changed: 50 additions & 41 deletions

File tree

BasicCalculator/calculator/internal/parser/parser.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ namespace calculator
3737
break;
3838
}
3939
default:
40-
token->output();
4140
throw std::runtime_error("Received invalid token!");
4241
break;
4342
}
@@ -67,7 +66,7 @@ namespace calculator
6766
}
6867

6968
if (rtn_expr.get() == nullptr)
70-
rtn_expr = parse_primary();
69+
rtn_expr = this->parse_primary();
7170

7271
return rtn_expr;
7372
}
@@ -81,27 +80,27 @@ namespace calculator
8180
if (next_token->value == token_tt::SUB)
8281
{
8382
this->eat_token();
84-
std::shared_ptr<shared::base_expr_t> left_expr = parse_paren();
83+
std::shared_ptr<shared::base_expr_t> left_expr = this->parse_paren();
8584
rtn_expr = std::make_unique<shared::unary_expr_t>(std::move(left_expr));
8685
}
8786
}
8887

8988
if (rtn_expr.get() == nullptr)
90-
rtn_expr = parse_paren();
89+
rtn_expr = this->parse_paren();
9190

9291
return rtn_expr;
9392
}
9493

9594
std::shared_ptr<shared::base_expr_t> parser_t::parse_powmod()
9695
{
9796
std::shared_ptr<shared::base_expr_t> rtn_expr;
98-
std::shared_ptr<shared::base_expr_t> left_expr = parse_unary();
97+
std::shared_ptr<shared::base_expr_t> left_expr = this->parse_unary();
9998

10099
if (auto next_token = this->peak_token())
101100
{
102101
while (next_token)
103102
{
104-
if (next_token->value == token_tt::MOD || next_token->value == token_tt::POW)
103+
if (next_token->value == token_tt::POW || next_token->value == token_tt::MOD)
105104
{
106105
shared::token_t current = *this->eat_token();
107106
left_expr = std::make_unique<shared::binary_expr_t>(std::move(left_expr), this->parse_powmod(), current.value);
@@ -151,7 +150,7 @@ namespace calculator
151150
std::shared_ptr<shared::base_expr_t> parser_t::parse_addsub()
152151
{
153152
std::shared_ptr<shared::base_expr_t> rtn_expr;
154-
std::shared_ptr<shared::base_expr_t> left_expr = parse_muldiv();
153+
std::shared_ptr<shared::base_expr_t> left_expr = this->parse_muldiv();
155154

156155
if (auto next_token = this->peak_token())
157156
{

BasicCalculator/calculator/internal/shared/shared.hpp

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,6 @@ namespace calculator
2727
{
2828
double number = 0;
2929
};
30-
31-
void output() const
32-
{
33-
switch (this->value)
34-
{
35-
case token_tt::NONE:
36-
std::printf("NONE\n");
37-
break;
38-
case token_tt::NUMBER:
39-
std::printf("NUMBER: %.8f\n", number);
40-
break;
41-
case token_tt::ADD:
42-
std::printf("ADD\n");
43-
break;
44-
case token_tt::SUB:
45-
std::printf("SUBTRACT\n");
46-
break;
47-
case token_tt::MUL:
48-
std::printf("MULTIPLY\n");
49-
break;
50-
case token_tt::DIV:
51-
std::printf("DIVIDE\n");
52-
break;
53-
case token_tt::MOD:
54-
std::printf("MODULO\n");
55-
break;
56-
case token_tt::POW:
57-
std::printf("POWER\n");
58-
break;
59-
default:
60-
std::printf("INVALID\n");
61-
break;
62-
}
63-
}
6430
};
6531

6632
// Parser structs
@@ -81,6 +47,11 @@ namespace calculator
8147
{
8248
return this->expr_type;
8349
}
50+
51+
virtual std::string beautify()
52+
{
53+
return "NONE";
54+
}
8455
};
8556

8657
struct unary_expr_t : base_expr_t
@@ -89,6 +60,12 @@ namespace calculator
8960
unary_expr_t(std::shared_ptr<base_expr_t> child) : base_expr_t{ expr_tt::UNARY }, child{ child } {}
9061

9162
std::shared_ptr<base_expr_t> child;
63+
64+
65+
std::string beautify() override
66+
{
67+
return "(-" + this->child->beautify() + ")";
68+
}
9269
};
9370

9471
struct number_expr_t : base_expr_t
@@ -97,6 +74,11 @@ namespace calculator
9774
number_expr_t(double number) : base_expr_t{ expr_tt::NUMBER }, value{ number } {}
9875

9976
double value = 0;
77+
78+
std::string beautify() override
79+
{
80+
return std::to_string(value);
81+
}
10082
};
10183

10284
struct binary_expr_t : base_expr_t
@@ -108,6 +90,34 @@ namespace calculator
10890
token_tt operation = token_tt::NONE;
10991
std::shared_ptr<base_expr_t> left{};
11092
std::shared_ptr<base_expr_t> right{};
93+
94+
std::string beautify() override
95+
{
96+
char op = '&';
97+
switch (operation)
98+
{
99+
case token_tt::ADD:
100+
op = '+';
101+
break;
102+
case token_tt::SUB:
103+
op = '-';
104+
break;
105+
case token_tt::MUL:
106+
op = '*';
107+
break;
108+
case token_tt::DIV:
109+
op = '/';
110+
break;
111+
case token_tt::MOD:
112+
op = '%';
113+
break;
114+
case token_tt::POW:
115+
op = '^';
116+
break;
117+
}
118+
119+
return "(" + this->left->beautify() + " " + op + " " + this->right->beautify() + ")";
120+
}
111121
};
112122
}
113123
}

0 commit comments

Comments
 (0)