diff --git a/nulascript/ast/ast.cc b/nulascript/ast/ast.cc index 3f12a60..3cd6aa1 100644 --- a/nulascript/ast/ast.cc +++ b/nulascript/ast/ast.cc @@ -1,8 +1,20 @@ +#include #include +#include +#include #include +#include #include #include +bool to_bool(std::string str) { + std::transform(str.begin(), str.end(), str.begin(), ::tolower); + std::istringstream is(str); + bool b; + is >> std::boolalpha >> b; + return b; +} + // Program // ? should this implementation be dropped std::string Program::tokenLiteral() { @@ -74,7 +86,7 @@ std::string Infix::toString() { // Boolean // TODO: string literal to double assigned to value -Boolean::Boolean(Token token) : token(token){}; +Boolean::Boolean(Token token) : token(token), value(to_bool(token.literal)){}; std::string Boolean::tokenLiteral() { return token.literal; } std::string Boolean::toString() { return token.literal; } diff --git a/nulascript/parser/parser.cc b/nulascript/parser/parser.cc index 40da2d1..0ee3764 100644 --- a/nulascript/parser/parser.cc +++ b/nulascript/parser/parser.cc @@ -139,6 +139,10 @@ Parser::Parser(Lexer& l) { registerPrefixFunction(TokenType::IDENT, [&]() -> Expression* { return parseIdentifier(); }); + registerPrefixFunction(TokenType::TRUE, + [&]() -> Expression* { return parseBoolean(); }); + registerPrefixFunction(TokenType::FALSE, + [&]() -> Expression* { return parseBoolean(); }); registerPrefixFunction(TokenType::INT, [&]() -> Expression* { return parseInteger(); }); registerPrefixFunction(TokenType::BANG_OR_NOT, @@ -236,6 +240,7 @@ void Parser::registerInfixFunction(TokenType tokenType, } Identifier* Parser::parseIdentifier() { return new Identifier(currentToken); } +Boolean* Parser::parseBoolean() { return new Boolean(currentToken); } Integer* Parser::parseInteger() { try { diff --git a/nulascript/parser/parser.h b/nulascript/parser/parser.h index 90125fa..cf9eeb3 100644 --- a/nulascript/parser/parser.h +++ b/nulascript/parser/parser.h @@ -48,6 +48,7 @@ class Parser { Integer* parseInteger(); Identifier* parseIdentifier(); Prefix* parsePrefix(); + Boolean* parseBoolean(); Infix* parseInfix(Expression* left); bool isEqualToCurrentTokenType(TokenType tokenType); bool isEqualToPeekedTokenType(TokenType tokenType); diff --git a/nulascript/tests/parser_test.cc b/nulascript/tests/parser_test.cc index e7c7ff3..111f1d8 100644 --- a/nulascript/tests/parser_test.cc +++ b/nulascript/tests/parser_test.cc @@ -93,12 +93,6 @@ bool checkLiteral(Statement* expression, bool value) { return false; } - // if (boolean->tokenLiteral() != (std::string)value) { - // std::cout << "identifier->tokenLiteral not=" << value - // << ". got=" << boolean->tokenLiteral(); - // return false; - // } - return true; } @@ -341,14 +335,6 @@ TEST(ParserSuite, TestBooleanLiteralExpression) { << " statements instead of 1"; } - if (!isCastableToDerivative(program->statements[0], - typeid(ExpressionStatement))) { - FAIL() << "Statement is not of type ExpressionStatement"; - } - - auto expression = - dynamic_cast(program->statements[0]); - ASSERT_TRUE(checkLiteral(program->statements[0], true)); }