Skip to content

Commit 402fef1

Browse files
committed
Refactoring: some more refactoring for CPPValue
1 parent ef4bac0 commit 402fef1

File tree

3 files changed

+43
-40
lines changed

3 files changed

+43
-40
lines changed

src/constexp.y

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ constant_expression: logical_or_expression
8585
TOK_QUESTIONMARK logical_or_expression
8686
TOK_COLON logical_or_expression
8787
{
88-
bool c = ($1.isInt() ? ((long)$1 != 0) : ((double)$1 != 0.0));
88+
bool c = ($1.type()==CPPValue::Type::Int ? ((long)$1 != 0) : ((double)$1 != 0.0));
8989
$$ = c ? $3 : $5;
9090
}
9191
;
@@ -181,7 +181,7 @@ additive_expression: multiplicative_expression
181181
{ $$ = $1; }
182182
| additive_expression TOK_PLUS multiplicative_expression
183183
{
184-
if (!$1.isInt() || !$3.isInt())
184+
if ($1.type()!=CPPValue::Type::Int || $3.type()!=CPPValue::Type::Int)
185185
{
186186
$$ = CPPValue( (double)$1 + (double)$3 );
187187
}
@@ -192,7 +192,7 @@ additive_expression: multiplicative_expression
192192
}
193193
| additive_expression TOK_MINUS multiplicative_expression
194194
{
195-
if (!$1.isInt() || !$3.isInt())
195+
if ($1.type()!=CPPValue::Type::Int || $3.type()!=CPPValue::Type::Int)
196196
{
197197
$$ = CPPValue( (double)$1 - (double)$3 );
198198
}
@@ -207,7 +207,7 @@ multiplicative_expression: unary_expression
207207
{ $$ = $1; }
208208
| multiplicative_expression TOK_STAR unary_expression
209209
{
210-
if (!$1.isInt() || !$3.isInt())
210+
if ($1.type()!=CPPValue::Type::Int || $3.type()!=CPPValue::Type::Int)
211211
{
212212
$$ = CPPValue( (double)$1 * (double)$3 );
213213
}
@@ -218,7 +218,7 @@ multiplicative_expression: unary_expression
218218
}
219219
| multiplicative_expression TOK_DIVIDE unary_expression
220220
{
221-
if (!$1.isInt() || !$3.isInt())
221+
if ($1.type()!=CPPValue::Type::Int || $3.type()!=CPPValue::Type::Int)
222222
{
223223
$$ = CPPValue( (double)$1 / (double)$3 );
224224
}
@@ -243,7 +243,7 @@ unary_expression: primary_expression
243243
{ $$ = $1; }
244244
| TOK_MINUS unary_expression
245245
{
246-
if ($2.isInt())
246+
if ($2.type()==CPPValue::Type::Int)
247247
$$ = CPPValue(-(long)$2);
248248
else
249249
$$ = CPPValue(-(double)$2);

src/cppvalue.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,58 +13,61 @@
1313
*
1414
*/
1515

16-
#include <stdlib.h>
16+
#include <cstdlib>
17+
#include <cassert>
1718

1819
#include "cppvalue.h"
1920
#include "constexp.h"
2021

21-
CPPValue CPPValue::parseOctal(const std::string& token)
22+
CPPValue CPPValue::parseOctal(const std::string &token)
2223
{
2324
long val = 0;
24-
for (const char *p = token.c_str(); *p != 0; p++)
25+
for (const char c : token)
2526
{
26-
if (*p >= '0' && *p <= '7') val = val * 8 + *p - '0';
27+
if (c >= '0' && c <= '7') val = val * 8 + c - '0';
2728
}
2829
return CPPValue(val);
2930
}
3031

31-
CPPValue CPPValue::parseDecimal(const std::string& token)
32+
CPPValue CPPValue::parseDecimal(const std::string &token)
3233
{
3334
long val = 0;
34-
for (const char *p = token.c_str(); *p != 0; p++)
35+
for (const char c : token)
3536
{
36-
if (*p >= '0' && *p <= '9') val = val * 10 + *p - '0';
37+
if (c >= '0' && c <= '9') val = val * 10 + c - '0';
3738
}
3839
return CPPValue(val);
3940
}
4041

41-
CPPValue CPPValue::parseHexadecimal(const std::string& token)
42+
CPPValue CPPValue::parseHexadecimal(const std::string &token)
4243
{
4344
long val = 0;
44-
for (const char *p = token.c_str(); *p != 0; p++)
45+
for (const char c : token)
4546
{
46-
if (*p >= '0' && *p <= '9') val = val * 16 + *p - '0';
47-
else if (*p >= 'a' && *p <= 'f') val = val * 16 + *p - 'a' + 10;
48-
else if (*p >= 'A' && *p <= 'F') val = val * 16 + *p - 'A' + 10;
47+
if (c >= '0' && c <= '9') val = val * 16 + c - '0';
48+
else if (c >= 'a' && c <= 'f') val = val * 16 + c - 'a' + 10;
49+
else if (c >= 'A' && c <= 'F') val = val * 16 + c - 'A' + 10;
4950
}
5051
//printf("parseHexadecimal %s->%x\n",qPrint(token),val);
5152
return CPPValue(val);
5253
}
5354

54-
CPPValue CPPValue::parseBinary(const std::string& token)
55+
CPPValue CPPValue::parseBinary(const std::string &token)
5556
{
5657
long val = 0;
57-
for (const char *p = token.c_str(); *p != 0; p++)
58+
for (const char c : token)
5859
{
59-
if (*p >= '0' && *p <= '1') val = val * 2 + *p - '0';
60+
if (c >= '0' && c <= '1') val = val * 2 + c - '0';
6061
}
6162
return CPPValue(val);
6263
}
6364

64-
CPPValue CPPValue::parseCharacter(const std::string& token) // does not work for '\n' and the alike
65+
CPPValue CPPValue::parseCharacter(const std::string &token) // does not work for '\n' and the alike
6566
{
67+
assert(token.length()>0);
6668
if (token[1]=='\\')
6769
{
70+
assert(token.length()>1);
6871
switch(token[2])
6972
{
7073
case 'n': return CPPValue('\n');
@@ -89,14 +92,14 @@ CPPValue CPPValue::parseCharacter(const std::string& token) // does not work for
8992
return parseOctal(token);
9093
case 'x':
9194
case 'X': return parseHexadecimal(token);
92-
default: printf("Invalid escape sequence %s found!\n",token.c_str());
95+
default: printf("Invalid escape sequence %s found!\n",std::string(token).c_str());
9396
return CPPValue(0L);
9497
}
9598
}
9699
return CPPValue(token[1]);
97100
}
98101

99-
CPPValue CPPValue::parseFloat(const std::string& token)
102+
CPPValue CPPValue::parseFloat(const std::string &token)
100103
{
101104
return CPPValue(std::stod(token));
102105
}

src/cppvalue.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,35 @@
2323
class CPPValue
2424
{
2525
public:
26-
enum Type { Int, Float };
26+
enum class Type { Int, Float };
2727

28-
explicit constexpr CPPValue(char c) noexcept : m_type(Int) { m_l = c; }
29-
explicit constexpr CPPValue(long val=0) noexcept : m_type(Int) { m_l = val; }
30-
explicit constexpr CPPValue(double val) noexcept : m_type(Float) { m_d = val; }
28+
explicit constexpr CPPValue(char c) noexcept : m_type(Type::Int), m_d(0.0), m_l(c) {}
29+
explicit constexpr CPPValue(long val=0) noexcept : m_type(Type::Int), m_d(0.0), m_l(val) {}
30+
explicit constexpr CPPValue(double val) noexcept : m_type(Type::Float), m_d(val), m_l(0) {}
3131

32-
constexpr operator double () const noexcept { return m_type==Int ? static_cast<double>(m_l) : m_d; }
33-
constexpr operator long () const noexcept { return m_type==Int ? m_l : static_cast<long>(m_d); }
32+
constexpr operator double () const noexcept { return m_type==Type::Int ? static_cast<double>(m_l) : m_d; }
33+
constexpr operator long () const noexcept { return m_type==Type::Int ? m_l : static_cast<long>(m_d); }
3434

35-
constexpr bool isInt() const noexcept { return m_type == Int; }
35+
constexpr Type type() const noexcept { return m_type; }
3636

3737
void print() const
3838
{
39-
if (m_type==Int)
39+
if (m_type==Type::Int)
4040
printf("(%ld)\n",m_l);
4141
else
4242
printf("(%f)\n",m_d);
4343
}
44-
static CPPValue parseOctal(const std::string& token);
45-
static CPPValue parseDecimal(const std::string& token);
46-
static CPPValue parseHexadecimal(const std::string& token);
47-
static CPPValue parseBinary(const std::string& token);
48-
static CPPValue parseCharacter(const std::string& token);
49-
static CPPValue parseFloat(const std::string& token);
44+
static CPPValue parseOctal (const std::string &token);
45+
static CPPValue parseDecimal (const std::string &token);
46+
static CPPValue parseHexadecimal(const std::string &token);
47+
static CPPValue parseBinary (const std::string &token);
48+
static CPPValue parseCharacter (const std::string &token);
49+
static CPPValue parseFloat (const std::string &token);
5050

5151
private:
5252
Type m_type;
53-
double m_d = 0.0;
54-
long m_l = 0;
53+
double m_d;
54+
long m_l;
5555
};
5656

5757

0 commit comments

Comments
 (0)