Implements parsing function declarations

This commit is contained in:
2019-05-31 15:00:14 +02:00
parent 6fad5a0a7d
commit c407ba2f50
8 changed files with 191 additions and 15 deletions

View File

@@ -191,5 +191,36 @@ TEST_CASE( "Parse local Assignment", "[parser]" ) {
REQUIRE(((LiteralBoolExpression*)assignment->GetExpression()) -> GetValue());
}
TEST_CASE( "Parse function declaration", "[parser]" ){
vector<IToken*> v {
new SimpleToken(TokenKind::FunctionKeyword,0,0),
new IdentifierToken("foo",0,0),
new SimpleToken(TokenKind::OpenParenthesis,0,0),
new IdentifierToken("number",0,0),
new IdentifierToken("bar",0,0),
new SimpleToken(TokenKind::CommaToken,0,0),
new IdentifierToken("number",0,0),
new IdentifierToken("par",0,0),
new SimpleToken(TokenKind::CloseParenthesis,0,0),
new IdentifierToken("bar",0,0),
new SimpleToken(TokenKind::PlusToken,0,0),
new IdentifierToken("par",0,0),
new SimpleToken(TokenKind::EndKeyword,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::FunctionDeclaration);
auto functionDeclaration = (ParsedFunctionDeclarationStatement*)firstStatement;
REQUIRE(functionDeclaration->GetIdentifier() == HashedString("foo"));
auto parameters = functionDeclaration->GetParameters();
CHECK(parameters[0]->GetType() == HashedString("number"));
CHECK(parameters[0]->GetIdentifier() == HashedString("bar"));
CHECK(parameters[1]->GetType() == HashedString("number"));
CHECK(parameters[1]->GetIdentifier() == HashedString("par"));
}
#endif