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

View File

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