optimize the parser code

This commit is contained in:
Chris Cromer 2022-11-04 11:19:53 -03:00
parent 6592f208d9
commit 257df0fc99
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
2 changed files with 7 additions and 24 deletions

View File

@ -3,29 +3,14 @@
#include "ast/variable_expression_ast.h" #include "ast/variable_expression_ast.h"
#include "parser.h" #include "parser.h"
#include <memory>
obelisk::Parser::Parser() obelisk::Parser::Parser()
{ {
lexer_ = new obelisk::Lexer(); lexer_ = std::unique_ptr<obelisk::Lexer> {new obelisk::Lexer()};
} }
obelisk::Parser::Parser(obelisk::Lexer* lexer) std::unique_ptr<obelisk::Lexer>& obelisk::Parser::getLexer()
{
if (lexer != nullptr)
{
lexer_ = lexer;
}
else
{
Parser();
}
}
obelisk::Parser::~Parser()
{
delete lexer_;
}
obelisk::Lexer* obelisk::Parser::getLexer()
{ {
return lexer_; return lexer_;
} }
@ -91,7 +76,7 @@ std::unique_ptr<obelisk::ExpressionAST> obelisk::Parser::parseNumberExpression()
auto result = std::make_unique<obelisk::NumberExpressionAST>( auto result = std::make_unique<obelisk::NumberExpressionAST>(
getLexer()->getNumberValue()); getLexer()->getNumberValue());
getNextToken(); getNextToken();
return std::move(result); return result;
} }
std::unique_ptr<obelisk::ExpressionAST> std::unique_ptr<obelisk::ExpressionAST>

View File

@ -13,7 +13,7 @@ namespace obelisk
class Parser class Parser
{ {
private: private:
obelisk::Lexer* lexer_; std::unique_ptr<obelisk::Lexer> lexer_;
int currentToken_; int currentToken_;
void setCurrentToken(int currentToken); void setCurrentToken(int currentToken);
@ -35,10 +35,8 @@ namespace obelisk
public: public:
Parser(); Parser();
Parser(obelisk::Lexer* lexer);
~Parser();
obelisk::Lexer* getLexer(); std::unique_ptr<obelisk::Lexer>& getLexer();
int getCurrentToken(); int getCurrentToken();