add new tokens for the lexer and optimize the code

This commit is contained in:
Chris Cromer 2022-11-04 11:19:30 -03:00
parent 01e19adee7
commit 6592f208d9
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
2 changed files with 25 additions and 10 deletions

View File

@ -8,7 +8,7 @@ int obelisk::Lexer::getToken()
while (isspace(lastChar)) while (isspace(lastChar))
{ {
lastChar = getchar(); lastChar = std::getc(stdin);
} }
if (isalpha(lastChar)) if (isalpha(lastChar))
@ -20,6 +20,21 @@ int obelisk::Lexer::getToken()
appendIdentifier(lastChar); appendIdentifier(lastChar);
} }
if (getIdentifier() == "fact")
{
return Token::kTokenFact;
}
if (getIdentifier() == "rule")
{
return Token::kTokenFact;
}
if (getIdentifier() == "action")
{
return Token::kTokenAction;
}
if (getIdentifier() == "def") if (getIdentifier() == "def")
{ {
return Token::kTokenDef; return Token::kTokenDef;
@ -90,12 +105,12 @@ void obelisk::Lexer::commentLine(int* lastChar)
while (*lastChar != EOF && *lastChar != '\n' && *lastChar != '\r'); while (*lastChar != EOF && *lastChar != '\n' && *lastChar != '\r');
} }
std::string obelisk::Lexer::getIdentifier() const std::string& obelisk::Lexer::getIdentifier()
{ {
return identifier_; return identifier_;
} }
void obelisk::Lexer::setIdentifier(const std::string identifier) void obelisk::Lexer::setIdentifier(const std::string& identifier)
{ {
identifier_ = identifier; identifier_ = identifier;
} }

View File

@ -3,16 +3,16 @@
#include <string> #include <string>
// TODO: add error handling
namespace obelisk namespace obelisk
{ {
class Lexer class Lexer
{ {
private: private:
std::string identifier_; std::string identifier_;
double numberValue_; double numberValue_;
void setIdentifier(const std::string identifier); void setIdentifier(const std::string& identifier);
void eraseIdentifier(); void eraseIdentifier();
void appendIdentifier(int lastChar); void appendIdentifier(int lastChar);
void setNumberValue(double numberValue); void setNumberValue(double numberValue);
@ -21,12 +21,12 @@ namespace obelisk
public: public:
enum Token enum Token
{ {
kTokenInvalid = -1, kTokenEof = -1,
kTokenEof = -2,
// commands // commands
kTokenFact = -3, kTokenFact = -2,
kTokenRule = -4, kTokenRule = -3,
kTokenAction = -4,
kTokenDef = -5, kTokenDef = -5,
kTokenExtern = -6, kTokenExtern = -6,
@ -38,7 +38,7 @@ namespace obelisk
int getToken(); int getToken();
std::string getIdentifier(); const std::string& getIdentifier();
double getNumberValue(); double getNumberValue();
}; };
} // namespace obelisk } // namespace obelisk