2019-05-21 15:16:53 +00:00
|
|
|
|
2019-05-21 15:23:56 +00:00
|
|
|
|
2019-05-21 15:16:53 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
#include <catch.hpp>
|
2019-05-21 15:23:56 +00:00
|
|
|
#include "../../src/Parser/Parser.hpp"
|
2019-06-17 16:35:12 +00:00
|
|
|
using namespace Porygon::Parser;
|
2019-05-21 15:16:53 +00:00
|
|
|
|
|
|
|
TEST_CASE( "Parse single true keyword", "[parser]" ) {
|
2019-07-04 17:08:13 +00:00
|
|
|
vector<const Token*> v {new SimpleToken(TokenKind::TrueKeyword,0,0), new SimpleToken(TokenKind::EndOfFile,0,0)};
|
2019-05-21 15:16:53 +00:00
|
|
|
Parser parser = Parser(v, nullptr);
|
2019-06-24 11:38:41 +00:00
|
|
|
auto parsedScript = parser.Parse();
|
|
|
|
auto parsedStatements = parsedScript -> GetStatements();
|
2019-06-11 12:58:43 +00:00
|
|
|
REQUIRE(parsedStatements->size() == 1);
|
|
|
|
auto firstStatement = parsedStatements -> at(0);
|
2019-05-21 15:16:53 +00:00
|
|
|
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
|
|
|
|
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
|
|
|
|
REQUIRE(expression -> GetKind() == ParsedExpressionKind::LiteralBool);
|
|
|
|
auto boolean = ((LiteralBoolExpression*)expression);
|
2019-06-11 12:58:43 +00:00
|
|
|
REQUIRE(boolean->GetValue());
|
2019-06-24 11:38:41 +00:00
|
|
|
|
|
|
|
for (auto t : v){
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
delete parsedScript;
|
2019-05-21 15:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Parse single false keyword", "[parser]" ) {
|
2019-07-04 17:08:13 +00:00
|
|
|
vector<const Token*> v {new SimpleToken(TokenKind::FalseKeyword,0,0), new SimpleToken(TokenKind::EndOfFile,0,0)};
|
2019-05-21 15:16:53 +00:00
|
|
|
Parser parser = Parser(v, nullptr);
|
2019-06-24 11:38:41 +00:00
|
|
|
auto parsedScript = parser.Parse();
|
|
|
|
auto parsedStatements = parsedScript -> GetStatements();
|
2019-06-11 12:58:43 +00:00
|
|
|
REQUIRE(parsedStatements->size() == 1);
|
|
|
|
auto firstStatement = parsedStatements -> at(0);
|
2019-05-21 15:16:53 +00:00
|
|
|
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
|
|
|
|
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
|
|
|
|
REQUIRE(expression -> GetKind() == ParsedExpressionKind::LiteralBool);
|
|
|
|
auto boolean = ((LiteralBoolExpression*)expression);
|
2019-06-11 12:58:43 +00:00
|
|
|
REQUIRE_FALSE(boolean->GetValue());
|
2019-06-24 11:38:41 +00:00
|
|
|
|
|
|
|
for (auto t : v){
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
delete parsedScript;
|
2019-05-21 15:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Parse simple addition", "[parser]" ) {
|
2019-07-04 17:08:13 +00:00
|
|
|
vector<const Token*> v {
|
2019-05-21 15:16:53 +00:00
|
|
|
new IntegerToken(5, 0, 0),
|
|
|
|
new SimpleToken(TokenKind::PlusToken,0,0),
|
|
|
|
new IntegerToken(10, 0, 0),
|
|
|
|
new SimpleToken(TokenKind::EndOfFile,0,0)
|
|
|
|
};
|
|
|
|
Parser parser = Parser(v, nullptr);
|
2019-06-24 11:38:41 +00:00
|
|
|
auto parsedScript = parser.Parse();
|
|
|
|
auto parsedStatements = parsedScript -> GetStatements();
|
2019-06-11 12:58:43 +00:00
|
|
|
REQUIRE(parsedStatements->size() == 1);
|
|
|
|
auto firstStatement = parsedStatements -> at(0);
|
2019-05-21 15:16:53 +00:00
|
|
|
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
|
|
|
|
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
|
|
|
|
REQUIRE(expression -> GetKind() == ParsedExpressionKind::Binary);
|
|
|
|
auto binary = ((BinaryExpression*)expression);
|
|
|
|
CHECK(binary -> GetOperatorKind() == BinaryOperatorKind::Addition);
|
|
|
|
auto left = binary->GetLeft();
|
|
|
|
auto right = binary->GetRight();
|
|
|
|
REQUIRE(left->GetKind() == ParsedExpressionKind::LiteralInteger);
|
|
|
|
REQUIRE(right->GetKind() == ParsedExpressionKind::LiteralInteger);
|
|
|
|
CHECK(((LiteralIntegerExpression*)left)->GetValue() == 5);
|
|
|
|
CHECK(((LiteralIntegerExpression*)right)->GetValue() == 10);
|
2019-06-24 11:38:41 +00:00
|
|
|
|
|
|
|
for (auto t : v){
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
delete parsedScript;
|
2019-05-21 15:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Parse simple negation", "[parser]" ) {
|
2019-07-04 17:08:13 +00:00
|
|
|
vector<const Token*> v {
|
2019-05-21 15:16:53 +00:00
|
|
|
new SimpleToken(TokenKind::MinusToken,0,0),
|
|
|
|
new IntegerToken(10, 0, 0),
|
|
|
|
new SimpleToken(TokenKind::EndOfFile,0,0)
|
|
|
|
};
|
|
|
|
Parser parser = Parser(v, nullptr);
|
2019-06-24 11:38:41 +00:00
|
|
|
auto parsedScript = parser.Parse();
|
|
|
|
auto parsedStatements = parsedScript -> GetStatements();
|
2019-06-11 12:58:43 +00:00
|
|
|
REQUIRE(parsedStatements->size() == 1);
|
|
|
|
auto firstStatement = parsedStatements -> at(0);
|
2019-05-21 15:16:53 +00:00
|
|
|
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
|
|
|
|
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
|
|
|
|
REQUIRE(expression -> GetKind() == ParsedExpressionKind::Unary);
|
|
|
|
auto unary = ((UnaryExpression*)expression);
|
|
|
|
CHECK(unary -> GetOperatorKind() == UnaryOperatorKind::Negation);
|
|
|
|
auto operand = unary->GetOperand();
|
|
|
|
REQUIRE(operand->GetKind() == ParsedExpressionKind::LiteralInteger);
|
|
|
|
CHECK(((LiteralIntegerExpression*)operand)->GetValue() == 10);
|
2019-06-24 11:38:41 +00:00
|
|
|
for (auto t : v){
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
delete parsedScript;
|
2019-05-21 15:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Parse logical negation", "[parser]" ) {
|
2019-07-04 17:08:13 +00:00
|
|
|
vector<const Token*> v {
|
2019-05-21 15:16:53 +00:00
|
|
|
new SimpleToken(TokenKind::NotKeyword,0,0),
|
|
|
|
new SimpleToken(TokenKind::FalseKeyword,0,0),
|
|
|
|
new SimpleToken(TokenKind::EndOfFile,0,0)
|
|
|
|
};
|
|
|
|
Parser parser = Parser(v, nullptr);
|
2019-06-24 11:38:41 +00:00
|
|
|
auto parsedScript = parser.Parse();
|
|
|
|
auto parsedStatements = parsedScript -> GetStatements();
|
2019-06-11 12:58:43 +00:00
|
|
|
REQUIRE(parsedStatements->size() == 1);
|
|
|
|
auto firstStatement = parsedStatements -> at(0);
|
2019-05-21 15:16:53 +00:00
|
|
|
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
|
|
|
|
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
|
|
|
|
REQUIRE(expression -> GetKind() == ParsedExpressionKind::Unary);
|
|
|
|
auto unary = ((UnaryExpression*)expression);
|
|
|
|
CHECK(unary -> GetOperatorKind() == UnaryOperatorKind::LogicalNegation);
|
|
|
|
auto operand = unary->GetOperand();
|
|
|
|
REQUIRE(operand->GetKind() == ParsedExpressionKind::LiteralBool);
|
2019-06-11 12:58:43 +00:00
|
|
|
CHECK_FALSE(((LiteralBoolExpression*)operand)->GetValue());
|
2019-06-24 11:38:41 +00:00
|
|
|
for (auto t : v){
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
delete parsedScript;
|
2019-05-21 15:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Are parenthesized expressions valid", "[parser]" ) {
|
2019-07-04 17:08:13 +00:00
|
|
|
vector<const Token*> v {
|
2019-05-21 15:16:53 +00:00
|
|
|
new IntegerToken(5, 0, 0),
|
|
|
|
new SimpleToken(TokenKind::PlusToken,0,0),
|
|
|
|
new IntegerToken(10, 0, 0),
|
|
|
|
new SimpleToken(TokenKind::StarToken,0,0),
|
|
|
|
new IntegerToken(6, 0, 0),
|
|
|
|
new SimpleToken(TokenKind::EndOfFile,0,0)
|
|
|
|
};
|
|
|
|
Parser parser = Parser(v, nullptr);
|
2019-06-24 11:38:41 +00:00
|
|
|
auto parsedScript = parser.Parse();
|
|
|
|
auto parsedStatements = parsedScript -> GetStatements();
|
2019-06-11 12:58:43 +00:00
|
|
|
REQUIRE(parsedStatements->size() == 1);
|
|
|
|
auto firstStatement = parsedStatements -> at(0);
|
2019-05-21 15:16:53 +00:00
|
|
|
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
|
|
|
|
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
|
|
|
|
REQUIRE(expression -> GetKind() == ParsedExpressionKind::Binary);
|
|
|
|
auto binary = ((BinaryExpression*)expression);
|
|
|
|
CHECK(binary -> GetOperatorKind() == BinaryOperatorKind::Addition);
|
|
|
|
auto left = binary->GetLeft();
|
|
|
|
auto right = binary->GetRight();
|
|
|
|
REQUIRE(left->GetKind() == ParsedExpressionKind::LiteralInteger);
|
|
|
|
REQUIRE(right->GetKind() == ParsedExpressionKind::Binary);
|
|
|
|
CHECK(((LiteralIntegerExpression*)left)->GetValue() == 5);
|
|
|
|
left = ((BinaryExpression*)right)->GetLeft();
|
|
|
|
right = ((BinaryExpression*)right)->GetRight();
|
|
|
|
CHECK(((LiteralIntegerExpression*)left)->GetValue() == 10);
|
|
|
|
CHECK(((LiteralIntegerExpression*)right)->GetValue() == 6);
|
2019-06-24 11:38:41 +00:00
|
|
|
|
|
|
|
for (auto t : v){
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
delete parsedScript;
|
2019-05-21 15:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Assert binary precedence", "[parser]" ) {
|
2019-07-04 17:08:13 +00:00
|
|
|
vector<const Token*> v {
|
2019-05-21 15:16:53 +00:00
|
|
|
new SimpleToken(TokenKind::OpenParenthesis,0,0),
|
|
|
|
new IntegerToken(10, 0, 0),
|
|
|
|
new SimpleToken(TokenKind::CloseParenthesis,0,0),
|
|
|
|
new SimpleToken(TokenKind::EndOfFile,0,0)
|
|
|
|
};
|
|
|
|
Parser parser = Parser(v, nullptr);
|
2019-06-24 11:38:41 +00:00
|
|
|
auto parsedScript = parser.Parse();
|
|
|
|
auto parsedStatements = parsedScript -> GetStatements();
|
2019-06-11 12:58:43 +00:00
|
|
|
REQUIRE(parsedStatements->size() == 1);
|
|
|
|
auto firstStatement = parsedStatements -> at(0);
|
2019-05-21 15:16:53 +00:00
|
|
|
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
|
|
|
|
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
|
|
|
|
REQUIRE(expression -> GetKind() == ParsedExpressionKind::Parenthesized);
|
|
|
|
auto innerExpression = ((ParenthesizedExpression*)expression) -> GetInnerExpression();
|
|
|
|
REQUIRE(innerExpression -> GetKind() == ParsedExpressionKind::LiteralInteger);
|
2019-06-24 11:38:41 +00:00
|
|
|
|
|
|
|
for (auto t : v){
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
delete parsedScript;
|
2019-05-22 11:29:35 +00:00
|
|
|
}
|
2019-05-21 15:16:53 +00:00
|
|
|
|
2019-05-22 11:29:35 +00:00
|
|
|
TEST_CASE( "Parse String Tokens", "[parser]" ) {
|
2019-07-04 17:08:13 +00:00
|
|
|
vector<const Token*> v {new StringToken(u"foo bar", 0,0), new SimpleToken(TokenKind::EndOfFile,0,0)};
|
2019-05-22 11:29:35 +00:00
|
|
|
Parser parser = Parser(v, nullptr);
|
2019-06-24 11:38:41 +00:00
|
|
|
auto parsedScript = parser.Parse();
|
|
|
|
auto parsedStatements = parsedScript -> GetStatements();
|
2019-06-11 12:58:43 +00:00
|
|
|
REQUIRE(parsedStatements->size() == 1);
|
|
|
|
auto firstStatement = parsedStatements -> at(0);
|
2019-05-22 11:29:35 +00:00
|
|
|
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
|
|
|
|
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
|
|
|
|
REQUIRE(expression -> GetKind() == ParsedExpressionKind::LiteralString);
|
|
|
|
auto boolean = ((LiteralStringExpression*)expression);
|
2019-06-15 15:20:27 +00:00
|
|
|
REQUIRE(boolean->GetValue() == u"foo bar");
|
2019-06-24 11:38:41 +00:00
|
|
|
|
|
|
|
for (auto t : v){
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
delete parsedScript;
|
2019-05-21 15:16:53 +00:00
|
|
|
}
|
|
|
|
|
2019-05-26 12:20:40 +00:00
|
|
|
TEST_CASE( "Parse Global Assignment", "[parser]" ) {
|
2019-07-04 17:08:13 +00:00
|
|
|
vector<const Token*> v {
|
2019-06-25 13:18:36 +00:00
|
|
|
new IdentifierToken(HashedString::CreateLookup(u"foo"),0,0),
|
2019-05-26 12:20:40 +00:00
|
|
|
new SimpleToken(TokenKind::AssignmentToken,0,0),
|
|
|
|
new SimpleToken(TokenKind::TrueKeyword,0,0),
|
|
|
|
new SimpleToken(TokenKind::EndOfFile,0,0)
|
|
|
|
};
|
|
|
|
Parser parser = Parser(v, nullptr);
|
2019-06-24 11:38:41 +00:00
|
|
|
auto parsedScript = parser.Parse();
|
|
|
|
auto parsedStatements = parsedScript -> GetStatements();
|
2019-06-11 12:58:43 +00:00
|
|
|
REQUIRE(parsedStatements->size() == 1);
|
|
|
|
auto firstStatement = parsedStatements -> at(0);
|
2019-05-26 12:20:40 +00:00
|
|
|
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Assignment);
|
|
|
|
auto assignment = (ParsedAssignmentStatement*)firstStatement;
|
|
|
|
REQUIRE(!assignment -> IsLocal());
|
2019-06-25 13:18:36 +00:00
|
|
|
REQUIRE(assignment->GetIdentifier().GetHash() == HashedString::CreateLookup(u"foo").GetHash());
|
2019-05-26 12:20:40 +00:00
|
|
|
REQUIRE(((LiteralBoolExpression*)assignment->GetExpression()) -> GetValue());
|
2019-06-24 11:38:41 +00:00
|
|
|
|
|
|
|
for (auto t : v){
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
delete parsedScript;
|
2019-05-26 12:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Parse local Assignment", "[parser]" ) {
|
2019-07-04 17:08:13 +00:00
|
|
|
vector<const Token*> v {
|
2019-05-26 12:20:40 +00:00
|
|
|
new SimpleToken(TokenKind::LocalKeyword,0,0),
|
2019-06-25 13:18:36 +00:00
|
|
|
new IdentifierToken(HashedString(new u16string(u"foo")),0,0),
|
2019-05-26 12:20:40 +00:00
|
|
|
new SimpleToken(TokenKind::AssignmentToken,0,0),
|
|
|
|
new SimpleToken(TokenKind::TrueKeyword,0,0),
|
|
|
|
new SimpleToken(TokenKind::EndOfFile,0,0)
|
|
|
|
};
|
|
|
|
Parser parser = Parser(v, nullptr);
|
2019-06-24 11:38:41 +00:00
|
|
|
auto parsedScript = parser.Parse();
|
|
|
|
auto parsedStatements = parsedScript -> GetStatements();
|
2019-06-11 12:58:43 +00:00
|
|
|
REQUIRE(parsedStatements->size() == 1);
|
|
|
|
auto firstStatement = parsedStatements -> at(0);
|
2019-05-26 12:20:40 +00:00
|
|
|
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Assignment);
|
|
|
|
auto assignment = (ParsedAssignmentStatement*)firstStatement;
|
|
|
|
REQUIRE(assignment -> IsLocal());
|
2019-06-25 13:18:36 +00:00
|
|
|
REQUIRE(assignment->GetIdentifier().GetHash() == HashedString::CreateLookup(u"foo").GetHash());
|
2019-05-26 12:20:40 +00:00
|
|
|
REQUIRE(((LiteralBoolExpression*)assignment->GetExpression()) -> GetValue());
|
2019-06-24 11:38:41 +00:00
|
|
|
|
|
|
|
for (auto t : v){
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
delete parsedScript;
|
|
|
|
|
2019-05-26 12:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 13:00:14 +00:00
|
|
|
TEST_CASE( "Parse function declaration", "[parser]" ){
|
2019-07-04 17:08:13 +00:00
|
|
|
vector<const Token*> v {
|
2019-05-31 13:00:14 +00:00
|
|
|
new SimpleToken(TokenKind::FunctionKeyword,0,0),
|
2019-06-25 13:18:36 +00:00
|
|
|
new IdentifierToken(HashedString(new u16string(u"foo")),0,0),
|
2019-05-31 13:00:14 +00:00
|
|
|
new SimpleToken(TokenKind::OpenParenthesis,0,0),
|
2019-06-25 13:18:36 +00:00
|
|
|
new IdentifierToken(HashedString(new u16string(u"number")),0,0),
|
|
|
|
new IdentifierToken(HashedString(new u16string(u"bar")),0,0),
|
2019-05-31 13:00:14 +00:00
|
|
|
new SimpleToken(TokenKind::CommaToken,0,0),
|
2019-06-25 13:18:36 +00:00
|
|
|
new IdentifierToken(HashedString(new u16string(u"number")),0,0),
|
|
|
|
new IdentifierToken(HashedString(new u16string(u"par")),0,0),
|
2019-05-31 13:00:14 +00:00
|
|
|
new SimpleToken(TokenKind::CloseParenthesis,0,0),
|
2019-06-25 13:18:36 +00:00
|
|
|
new IdentifierToken(HashedString(new u16string(u"bar")),0,0),
|
2019-05-31 13:00:14 +00:00
|
|
|
new SimpleToken(TokenKind::PlusToken,0,0),
|
2019-06-25 13:18:36 +00:00
|
|
|
new IdentifierToken(HashedString(new u16string(u"par")),0,0),
|
2019-05-31 13:00:14 +00:00
|
|
|
new SimpleToken(TokenKind::EndKeyword,0,0),
|
|
|
|
new SimpleToken(TokenKind::EndOfFile,0,0),
|
|
|
|
};
|
|
|
|
Parser parser = Parser(v, nullptr);
|
2019-06-24 11:38:41 +00:00
|
|
|
auto parsedScript = parser.Parse();
|
|
|
|
auto parsedStatements = parsedScript -> GetStatements();
|
2019-06-11 12:58:43 +00:00
|
|
|
REQUIRE(parsedStatements->size() == 1);
|
|
|
|
auto firstStatement = parsedStatements -> at(0);
|
2019-05-31 13:00:14 +00:00
|
|
|
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::FunctionDeclaration);
|
|
|
|
auto functionDeclaration = (ParsedFunctionDeclarationStatement*)firstStatement;
|
2019-06-25 13:18:36 +00:00
|
|
|
REQUIRE(functionDeclaration->GetIdentifier() == HashedString::CreateLookup(u"foo"));
|
2019-05-31 13:00:14 +00:00
|
|
|
auto parameters = functionDeclaration->GetParameters();
|
2019-06-25 13:18:36 +00:00
|
|
|
CHECK(parameters -> at(0) ->GetType() == HashedString::CreateLookup(u"number"));
|
|
|
|
CHECK(parameters -> at(0) ->GetIdentifier() == HashedString::CreateLookup(u"bar"));
|
|
|
|
CHECK(parameters -> at(1) ->GetType() == HashedString::CreateLookup(u"number"));
|
|
|
|
CHECK(parameters -> at(1) ->GetIdentifier() == HashedString::CreateLookup(u"par"));
|
2019-06-24 11:38:41 +00:00
|
|
|
|
|
|
|
for (auto t : v){
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
delete parsedScript;
|
2019-05-31 13:00:14 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 11:29:35 +00:00
|
|
|
|
2019-05-21 15:16:53 +00:00
|
|
|
#endif
|