diff --git a/src/Binder/Binder.cpp b/src/Binder/Binder.cpp index 96361d0..cbb5f0c 100644 --- a/src/Binder/Binder.cpp +++ b/src/Binder/Binder.cpp @@ -40,6 +40,8 @@ BoundExpression* Binder::BindExpression(ParsedExpression* expression){ return new BoundLiteralIntegerExpression(((LiteralIntegerExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength()); case ParsedExpressionKind ::LiteralFloat: return new BoundLiteralFloatExpression(((LiteralFloatExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength()); + case ParsedExpressionKind ::LiteralString: + return new BoundLiteralStringExpression(((LiteralStringExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength()); case ParsedExpressionKind ::LiteralBool: return new BoundLiteralBoolExpression(((LiteralBoolExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength()); diff --git a/src/Parser/ParsedExpressions/ParsedExpression.hpp b/src/Parser/ParsedExpressions/ParsedExpression.hpp index 2b52fb6..5b6941a 100644 --- a/src/Parser/ParsedExpressions/ParsedExpression.hpp +++ b/src/Parser/ParsedExpressions/ParsedExpression.hpp @@ -83,6 +83,21 @@ public: } }; +class LiteralStringExpression : public ParsedExpression{ + string _value; +public: + ParsedExpressionKind GetKind() final{ + return ParsedExpressionKind::LiteralString; + } + explicit LiteralStringExpression(StringToken* token) : ParsedExpression(token -> GetStartPosition(), token -> GetLength()){ + _value = std::move(token->Value); + } + + string GetValue(){ + return _value; + } +}; + class LiteralBoolExpression : public ParsedExpression{ bool _value; public: diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index 0c138f8..2196992 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -108,6 +108,7 @@ ParsedExpression *Parser::ParsePrimaryExpression(IToken *current) { switch (current -> GetKind()){ case TokenKind ::Integer: return new LiteralIntegerExpression((IntegerToken*)current); case TokenKind ::Float: return new LiteralFloatExpression((FloatToken*)current); + case TokenKind ::String: return new LiteralStringExpression((StringToken*)current); case TokenKind ::TrueKeyword: return new LiteralBoolExpression(current); case TokenKind ::FalseKeyword: return new LiteralBoolExpression(current); case TokenKind ::OpenParenthesis: return this -> ParseParenthesizedExpression(current); diff --git a/tests/parser/ParserTests.cpp b/tests/parser/ParserTests.cpp index b7ac8d6..c44bf7a 100644 --- a/tests/parser/ParserTests.cpp +++ b/tests/parser/ParserTests.cpp @@ -139,7 +139,20 @@ TEST_CASE( "Assert binary precedence", "[parser]" ) { REQUIRE(expression -> GetKind() == ParsedExpressionKind::Parenthesized); auto innerExpression = ((ParenthesizedExpression*)expression) -> GetInnerExpression(); REQUIRE(innerExpression -> GetKind() == ParsedExpressionKind::LiteralInteger); - } +TEST_CASE( "Parse String Tokens", "[parser]" ) { + vector v {new StringToken("foo bar", 0,0), new SimpleToken(TokenKind::EndOfFile,0,0)}; + Parser parser = Parser(v, nullptr); + auto parsedStatements = parser.Parse() -> GetStatements(); + REQUIRE(parsedStatements.size() == 1); + auto firstStatement = parsedStatements[0]; + REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression); + auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression(); + REQUIRE(expression -> GetKind() == ParsedExpressionKind::LiteralString); + auto boolean = ((LiteralStringExpression*)expression); + REQUIRE(boolean->GetValue() == "foo bar"); +} + + #endif \ No newline at end of file