Pass parsed block statements as reference instead of value

This commit is contained in:
Deukhoofd 2019-06-11 14:58:43 +02:00
parent 081def0be0
commit ba4fe888fa
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 34 additions and 34 deletions

View File

@ -9,9 +9,9 @@ BoundScriptStatement *Binder::Bind(Script* script, ParsedScriptStatement *s, Bou
binder._scope = scriptScope;
auto statements = s->GetStatements();
vector<BoundStatement*> boundStatements (statements.size());
for (int i = 0; i < statements.size(); i++){
boundStatements[i] = binder.BindStatement(statements[i]);
vector<BoundStatement*> boundStatements (statements->size());
for (int i = 0; i < statements->size(); i++){
boundStatements[i] = binder.BindStatement(statements->at(i));
}
return new BoundScriptStatement(boundStatements, scriptScope->GetDeepestScope());
}
@ -36,10 +36,10 @@ BoundStatement* Binder::BindStatement(ParsedStatement* statement){
BoundStatement *Binder::BindBlockStatement(ParsedStatement *statement) {
auto statements = ((ParsedBlockStatement*)statement)->GetStatements();
vector<BoundStatement*> boundStatements (statements.size());
vector<BoundStatement*> boundStatements (statements->size());
this->_scope->GoInnerScope();
for (int i = 0; i < statements.size(); i++){
boundStatements[i] = this -> BindStatement(statements[i]);
for (int i = 0; i < statements->size(); i++){
boundStatements[i] = this -> BindStatement(statements->at(i));
}
this->_scope->GoOuterScope();
return new BoundBlockStatement(boundStatements);

View File

@ -77,8 +77,8 @@ public:
return ParsedStatementKind ::Block;
}
std::vector<ParsedStatement*> GetStatements(){
return _statements;
std::vector<ParsedStatement*>* GetStatements(){
return &_statements;
}
};

View File

@ -399,7 +399,7 @@ ParsedExpression* Parser::ParseTableExpression(IToken* current){
else {
auto block = (ParsedBlockStatement*)this -> ParseBlock({TokenKind ::CloseCurlyBracket});
auto statements = block->GetStatements();
statements.insert(statements.begin(), firstItem);
statements->insert(statements->begin(), firstItem);
throw "not implemented TODO";
}
}

View File

@ -8,26 +8,26 @@ TEST_CASE( "Parse single true keyword", "[parser]" ) {
vector<IToken*> v {new SimpleToken(TokenKind::TrueKeyword,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(parsedStatements->size() == 1);
auto firstStatement = parsedStatements -> at(0);
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
REQUIRE(expression -> GetKind() == ParsedExpressionKind::LiteralBool);
auto boolean = ((LiteralBoolExpression*)expression);
REQUIRE(boolean->GetValue() == true);
REQUIRE(boolean->GetValue());
}
TEST_CASE( "Parse single false keyword", "[parser]" ) {
vector<IToken*> v {new SimpleToken(TokenKind::FalseKeyword,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(parsedStatements->size() == 1);
auto firstStatement = parsedStatements -> at(0);
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
REQUIRE(expression -> GetKind() == ParsedExpressionKind::LiteralBool);
auto boolean = ((LiteralBoolExpression*)expression);
REQUIRE(boolean->GetValue() == false);
REQUIRE_FALSE(boolean->GetValue());
}
TEST_CASE( "Parse simple addition", "[parser]" ) {
@ -39,8 +39,8 @@ TEST_CASE( "Parse simple addition", "[parser]" ) {
};
Parser parser = Parser(v, nullptr);
auto parsedStatements = parser.Parse() -> GetStatements();
REQUIRE(parsedStatements.size() == 1);
auto firstStatement = parsedStatements[0];
REQUIRE(parsedStatements->size() == 1);
auto firstStatement = parsedStatements -> at(0);
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
REQUIRE(expression -> GetKind() == ParsedExpressionKind::Binary);
@ -62,8 +62,8 @@ TEST_CASE( "Parse simple negation", "[parser]" ) {
};
Parser parser = Parser(v, nullptr);
auto parsedStatements = parser.Parse() -> GetStatements();
REQUIRE(parsedStatements.size() == 1);
auto firstStatement = parsedStatements[0];
REQUIRE(parsedStatements->size() == 1);
auto firstStatement = parsedStatements -> at(0);
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
REQUIRE(expression -> GetKind() == ParsedExpressionKind::Unary);
@ -82,8 +82,8 @@ TEST_CASE( "Parse logical negation", "[parser]" ) {
};
Parser parser = Parser(v, nullptr);
auto parsedStatements = parser.Parse() -> GetStatements();
REQUIRE(parsedStatements.size() == 1);
auto firstStatement = parsedStatements[0];
REQUIRE(parsedStatements->size() == 1);
auto firstStatement = parsedStatements -> at(0);
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
REQUIRE(expression -> GetKind() == ParsedExpressionKind::Unary);
@ -91,7 +91,7 @@ TEST_CASE( "Parse logical negation", "[parser]" ) {
CHECK(unary -> GetOperatorKind() == UnaryOperatorKind::LogicalNegation);
auto operand = unary->GetOperand();
REQUIRE(operand->GetKind() == ParsedExpressionKind::LiteralBool);
CHECK(((LiteralBoolExpression*)operand)->GetValue() == false);
CHECK_FALSE(((LiteralBoolExpression*)operand)->GetValue());
}
TEST_CASE( "Are parenthesized expressions valid", "[parser]" ) {
@ -105,8 +105,8 @@ TEST_CASE( "Are parenthesized expressions valid", "[parser]" ) {
};
Parser parser = Parser(v, nullptr);
auto parsedStatements = parser.Parse() -> GetStatements();
REQUIRE(parsedStatements.size() == 1);
auto firstStatement = parsedStatements[0];
REQUIRE(parsedStatements->size() == 1);
auto firstStatement = parsedStatements -> at(0);
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
REQUIRE(expression -> GetKind() == ParsedExpressionKind::Binary);
@ -132,8 +132,8 @@ TEST_CASE( "Assert binary precedence", "[parser]" ) {
};
Parser parser = Parser(v, nullptr);
auto parsedStatements = parser.Parse() -> GetStatements();
REQUIRE(parsedStatements.size() == 1);
auto firstStatement = parsedStatements[0];
REQUIRE(parsedStatements->size() == 1);
auto firstStatement = parsedStatements -> at(0);
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
REQUIRE(expression -> GetKind() == ParsedExpressionKind::Parenthesized);
@ -145,8 +145,8 @@ TEST_CASE( "Parse String Tokens", "[parser]" ) {
vector<IToken*> 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(parsedStatements->size() == 1);
auto firstStatement = parsedStatements -> at(0);
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Expression);
auto expression = ((ParsedExpressionStatement*)firstStatement)->GetExpression();
REQUIRE(expression -> GetKind() == ParsedExpressionKind::LiteralString);
@ -163,8 +163,8 @@ TEST_CASE( "Parse Global Assignment", "[parser]" ) {
};
Parser parser = Parser(v, nullptr);
auto parsedStatements = parser.Parse() -> GetStatements();
REQUIRE(parsedStatements.size() == 1);
auto firstStatement = parsedStatements[0];
REQUIRE(parsedStatements->size() == 1);
auto firstStatement = parsedStatements -> at(0);
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Assignment);
auto assignment = (ParsedAssignmentStatement*)firstStatement;
REQUIRE(!assignment -> IsLocal());
@ -182,8 +182,8 @@ TEST_CASE( "Parse local Assignment", "[parser]" ) {
};
Parser parser = Parser(v, nullptr);
auto parsedStatements = parser.Parse() -> GetStatements();
REQUIRE(parsedStatements.size() == 1);
auto firstStatement = parsedStatements[0];
REQUIRE(parsedStatements->size() == 1);
auto firstStatement = parsedStatements -> at(0);
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::Assignment);
auto assignment = (ParsedAssignmentStatement*)firstStatement;
REQUIRE(assignment -> IsLocal());
@ -210,8 +210,8 @@ TEST_CASE( "Parse function declaration", "[parser]" ){
};
Parser parser = Parser(v, nullptr);
auto parsedStatements = parser.Parse() -> GetStatements();
REQUIRE(parsedStatements.size() == 1);
auto firstStatement = parsedStatements[0];
REQUIRE(parsedStatements->size() == 1);
auto firstStatement = parsedStatements -> at(0);
REQUIRE(firstStatement -> GetKind() == ParsedStatementKind::FunctionDeclaration);
auto functionDeclaration = (ParsedFunctionDeclarationStatement*)firstStatement;
REQUIRE(functionDeclaration->GetIdentifier() == HashedString("foo"));