Skip to content

Commit df9756d

Browse files
author
Avi SZYCHTER
committed
More improvment with what can be done with a Token
1 parent 0f0c39c commit df9756d

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

include/Token.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,32 @@ struct Token {
1515
Separator,
1616
PositiveNumber,
1717
NegativeNumber,
18+
FloatingPointNumber,
1819
Number,
1920
Identifier,
2021
ArithmeticSign,
2122
Eof
2223
};
2324

25+
/**
26+
* @return A Token representing an arithmetic sign
27+
*/
28+
static Token createArithmeticSign(char src);
29+
30+
/**
31+
* @return A Token representing a separator
32+
*/
33+
static Token createSeparator(char src);
34+
35+
/**
36+
* The Token's type returned here depends on is_positive and is_float
37+
* @param number The image of the number
38+
* @param is_positive true if number represents a positive number
39+
* @param is_float true if the number represents a floating-point number
40+
* @return A Token representing a number
41+
*/
42+
static Token createNumber(const std::string& number, bool is_positive, bool is_float);
43+
2444
/**
2545
* @brief Constructs an EOF token.
2646
*/
@@ -50,7 +70,7 @@ struct Token {
5070

5171
/**
5272
* If the token's type is Number or if other is Number,
53-
* then it also matches PositiveNumber and NegativeNumber
73+
* then it also matches PositiveNumber, NegativeNumber and FloatingPointNumber
5474
*
5575
* @return true if the token's type is equal to other
5676
* @param other Type to compare

src/parsing/Tokenizer.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ bool Token::operator==(const std::string& other) const {
4343

4444
bool Token::operator==(Token::Type other) const {
4545
if(type == Number) {
46-
return other == Number || other == PositiveNumber || other == NegativeNumber;
46+
return other == Number || other == PositiveNumber ||
47+
other == NegativeNumber || other == FloatingPointNumber;
4748
}
4849
else if(other == Number) {
49-
return type == Number || type == PositiveNumber || type == NegativeNumber;
50+
return type == Number || type == PositiveNumber ||
51+
type == NegativeNumber || type == FloatingPointNumber;
5052
}
5153

5254
return type == other;
@@ -79,6 +81,25 @@ long long Token::toInt() const {
7981
double Token::toDouble() const {
8082
return std::stod(image);
8183
}
84+
85+
Token Token::createArithmeticSign(char src) {
86+
return Token(ArithmeticSign, std::string(1, src));
87+
}
88+
89+
Token Token::createSeparator(char src) {
90+
return Token(Separator, std::string(1, src));
91+
}
92+
93+
Token Token::createNumber(const std::string& number, bool is_positive, bool is_float) {
94+
if(is_float) {
95+
return Token(FloatingPointNumber, number);
96+
}
97+
else if(is_positive) {
98+
return Token(PositiveNumber, number);
99+
}
100+
101+
return Token(NegativeNumber, number);
102+
}
82103
// END OF IMPLEMENTATION Token class
83104

84105
Token Tokenizer::getCurrentToken() const {

0 commit comments

Comments
 (0)