Implements indexing, currently can only be used with strings

This commit is contained in:
2019-06-06 17:35:51 +02:00
parent b275e1fbd6
commit cb5d9e2f62
15 changed files with 140 additions and 9 deletions

View File

@@ -146,10 +146,11 @@ ParsedExpression* Parser::ParseExpression(IToken* current){
if (peekKind == TokenKind::OpenParenthesis){
expression = this->ParseFunctionCallExpression(expression);
} else if (peekKind == TokenKind::OpenSquareBracket){
//TODO: index expression
expression = this->ParseIndexExpression(expression);
} else {
//TODO: index period expression
}
peekKind = this->Peek()->GetKind();
}
return expression;
}
@@ -283,7 +284,17 @@ ParsedExpression *Parser::ParseFunctionCallExpression(ParsedExpression* function
return new FunctionCallExpression(functionExpression, parameters, start, peeked->GetEndPosition() - start);
}
ParsedExpression* Parser::ParseIndexExpression(ParsedExpression* indexingExpression){
this->Next(); // consume '[' token
auto indexExpression = this -> ParseExpression(this -> Next());
auto closeBracket = this->Next();
if (closeBracket->GetKind() != TokenKind::CloseSquareBracket){
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, closeBracket->GetStartPosition(), closeBracket->GetLength());
return new BadExpression(closeBracket->GetStartPosition(), closeBracket->GetLength());
}
auto start = indexingExpression->GetStartPosition();
return new IndexExpression(indexingExpression, indexExpression, start, closeBracket->GetEndPosition() - start);
}