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());
}

View File

@@ -24,6 +24,7 @@ class Binder {
BoundExpression *BindBinaryOperator(BinaryExpression *expression);
BoundExpression *BindUnaryOperator(UnaryExpression *expression);
BoundExpression *BindFunctionCall(FunctionCallExpression *expression);
BoundExpression *BindIndexExpression(IndexExpression *expression);
public:
static BoundScriptStatement* Bind(Script* script, ParsedScriptStatement* s, BoundScope* scriptScope);

View File

@@ -2,6 +2,8 @@
#include <utility>
#include <utility>
#ifndef PORYGONLANG_BOUNDEXPRESSION_HPP
#define PORYGONLANG_BOUNDEXPRESSION_HPP
@@ -26,6 +28,7 @@ enum class BoundExpressionKind{
Unary,
Binary,
FunctionCall,
Index,
};
class BoundExpression{
@@ -36,7 +39,7 @@ public:
BoundExpression(unsigned int start, unsigned int length, std::shared_ptr<ScriptType> type){
_start = start;
_length = length;
_type = type;
_type = std::move(type);
}
virtual ~BoundExpression() = default;
@@ -247,5 +250,31 @@ public:
}
};
class BoundIndexExpression : public BoundExpression {
BoundExpression* _indexableExpression;
BoundExpression* _indexExpression;
public:
BoundIndexExpression(BoundExpression* indexableExpression, BoundExpression* indexExpression, shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression), _indexExpression(indexExpression) {}
~BoundIndexExpression() final{
delete _indexableExpression;
delete _indexExpression;
}
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::Index;
}
BoundExpression* GetIndexableExpression(){
return _indexableExpression;
}
BoundExpression* GetIndexExpression(){
return _indexExpression;
}
};
#endif //PORYGONLANG_BOUNDEXPRESSION_HPP