Skip to content

Commit

Permalink
Fix boolean utility test
Browse files Browse the repository at this point in the history
  • Loading branch information
asynchroza committed Nov 6, 2023
1 parent ae3ab3e commit 4ad9508
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
14 changes: 13 additions & 1 deletion nulascript/ast/ast.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
#include <algorithm>
#include <ast.h>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <token.h>

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() {
Expand Down Expand Up @@ -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; }

Expand Down
5 changes: 5 additions & 0 deletions nulascript/parser/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions nulascript/parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 0 additions & 14 deletions nulascript/tests/parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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<ExpressionStatement*>(program->statements[0]);

ASSERT_TRUE(checkLiteral(program->statements[0], true));
}

Expand Down

0 comments on commit 4ad9508

Please sign in to comment.