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

@@ -128,6 +128,9 @@ BoundExpression* Binder::BindExpression(ParsedExpression* expression){
case ParsedExpressionKind ::FunctionCall:
return this->BindFunctionCall((FunctionCallExpression*)expression);
case ParsedExpressionKind ::Indexer:
return this->BindIndexExpression((IndexExpression*)expression);
case ParsedExpressionKind ::Bad:
return new BoundBadExpression(expression->GetStartPosition(), expression-> GetLength());
}
@@ -308,4 +311,17 @@ BoundExpression* Binder::BindFunctionCall(FunctionCallExpression* expression){
return new BoundFunctionCallExpression(functionExpression, boundParameters, functionType.get()->GetReturnType(),
expression->GetStartPosition(), expression->GetLength());
}
}
BoundExpression *Binder::BindIndexExpression(IndexExpression *expression) {
auto indexer = this->BindExpression(expression->GetIndexer());
auto index = this->BindExpression(expression->GetIndex());
if (!indexer->GetType()->CanBeIndexedWith(index->GetType().get())){
this->_scriptData->Diagnostics->LogError(DiagnosticCode::CantIndex, index->GetStartPosition(),
index->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
auto resultType = shared_ptr<ScriptType>(indexer->GetType()->GetIndexedType(index->GetType().get()));
return new BoundIndexExpression(indexer, index, resultType, expression->GetStartPosition(), expression->GetLength());
}