@@ -28,6 +28,59 @@ bool isEOF(char c) {
2828 return c == ' \0 ' ;
2929}
3030
31+ // I decided to move the implementation of of the Token class
32+ // from Token.h to Tokenizer.cpp
33+ // IMPLEMENTATION: Token class
34+ Token::Token ()
35+ : type(Token::Eof), image() {}
36+
37+ Token::Token (Token::Type t, const std::string& i)
38+ : type(t), image(i) { }
39+
40+ bool Token::operator ==(const std::string& other) const {
41+ return image == other;
42+ }
43+
44+ bool Token::operator ==(Token::Type other) const {
45+ if (type == Number) {
46+ return other == Number || other == PositiveNumber || other == NegativeNumber;
47+ }
48+ else if (other == Number) {
49+ return type == Number || type == PositiveNumber || type == NegativeNumber;
50+ }
51+
52+ return type == other;
53+ }
54+
55+ bool Token::operator ==(const Token& other) const {
56+ return (*this == other.image ) && (*this == other.type );
57+ }
58+
59+ bool Token::operator !=(const std::string& other) const {
60+ return !(*this == other);
61+ }
62+
63+ bool Token::operator !=(Token::Type other) const {
64+ return !(*this == other);
65+ }
66+
67+ bool Token::operator !=(const Token& other) const {
68+ return !(*this == other);
69+ }
70+
71+ unsigned long long Token::toUInt () const {
72+ return std::stoul (image);
73+ }
74+
75+ long long Token::toInt () const {
76+ return std::stol (image);
77+ }
78+
79+ double Token::toDouble () const {
80+ return std::stod (image);
81+ }
82+ // END OF IMPLEMENTATION Token class
83+
3184Token Tokenizer::getCurrentToken () const {
3285 return currentToken;
3386}
@@ -80,13 +133,13 @@ Token Tokenizer::getNextToken() {
80133 currentChar = getNextChar ();
81134 }
82135 else if (currentChar == ' +' ) {
83- currentToken = Token (Token::Sign , std::string (1 , currentChar));
136+ currentToken = Token (Token::ArithmeticSign , std::string (1 , currentChar));
84137 currentChar = getNextChar ();
85138 }
86139 else if (currentChar == ' -' ) {
87140 currentChar = getNextChar ();
88141 if (!isDigit (currentChar))
89- currentToken = Token (Token::Sign , " -" );
142+ currentToken = Token (Token::ArithmeticSign , " -" );
90143 else { // Negative number
91144 std::string literal = " -" ;
92145 while (isDigit (currentChar) || currentChar == ' .' ) {
@@ -113,7 +166,7 @@ Token Tokenizer::getNextToken() {
113166 std::cout << " Error in literal parsing: reached EOF before literal end" << std::endl;
114167 }
115168
116- currentToken = Token (Token::Literal , literal);
169+ currentToken = Token (Token::StringLiteral , literal);
117170 }
118171 else if (isDigit (currentChar)) {
119172 std::string literal = std::string (1 , currentChar);
@@ -172,8 +225,8 @@ void Tokenizer::skipLine() {
172225void Tokenizer::skipUntil (const std::string& token) {
173226 unsigned long long initLine = lineCount ();
174227
175- while (currentToken.image () != token &&
176- currentToken.type () != Token::Eof) {
228+ while (currentToken.image != token &&
229+ currentToken.type != Token::Eof) {
177230 getNextToken ();
178231 }
179232
0 commit comments