Implements basic numerical tables
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-06-09 20:15:09 +02:00
parent ec2419bc7d
commit 081def0be0
21 changed files with 324 additions and 24 deletions

View File

@@ -1,3 +1,5 @@
#include <memory>
#include "Binder.hpp"
#include <memory>
@@ -174,6 +176,8 @@ BoundExpression* Binder::BindExpression(ParsedExpression* expression){
case ParsedExpressionKind ::Indexer:
return this->BindIndexExpression((IndexExpression*)expression);
case ParsedExpressionKind::NumericalTable:
return this -> BindNumericalTableExpression((ParsedNumericalTableExpression*)expression);
case ParsedExpressionKind ::Bad:
return new BoundBadExpression(expression->GetStartPosition(), expression-> GetLength());
@@ -386,7 +390,29 @@ BoundExpression *Binder::BindIndexExpression(IndexExpression *expression) {
index->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
auto resultType = shared_ptr<ScriptType>(indexer->GetType()->GetIndexedType(index->GetType().get()));
auto resultType = indexer->GetType()->GetIndexedType(index->GetType().get());
return new BoundIndexExpression(indexer, index, resultType, expression->GetStartPosition(), expression->GetLength());
}
BoundExpression* Binder::BindNumericalTableExpression(ParsedNumericalTableExpression* expression){
auto expressions = expression->GetExpressions();
auto boundExpressions = vector<BoundExpression*>(expressions.size());
shared_ptr<ScriptType> valueType = nullptr;
if (!boundExpressions.empty()){
boundExpressions[0] = this -> BindExpression(expressions[0]);
valueType = boundExpressions[0] -> GetType();
for (int i = 1; i < expressions.size(); i++){
boundExpressions[i] = this -> BindExpression(expressions[i]);
if (boundExpressions[i] -> GetType().get()->operator!=(valueType.get())){
this->_scriptData->Diagnostics->LogError(DiagnosticCode::InvalidTableValueType, boundExpressions[i]->GetStartPosition(),
boundExpressions[i]->GetLength());
}
}
}
if (valueType == nullptr){
valueType = std::make_shared<ScriptType>(TypeClass::Nil);
}
auto keyType = std::make_shared<ScriptType>(TypeClass::Number);
auto tableType = std::make_shared<TableScriptType>(keyType, valueType);
return new BoundNumericalTableExpression(boundExpressions, tableType, expression->GetStartPosition(), expression->GetLength());
}

View File

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

View File

@@ -1,15 +1,10 @@
#include <utility>
#include <utility>
#include <utility>
#ifndef PORYGONLANG_BOUNDEXPRESSION_HPP
#define PORYGONLANG_BOUNDEXPRESSION_HPP
#include <string>
#include <memory>
#include <utility>
#include "../../ScriptType.hpp"
#include "../BoundOperators.hpp"
#include "../BoundVariables/BoundVariableKey.hpp"
@@ -29,6 +24,7 @@ enum class BoundExpressionKind{
Binary,
FunctionCall,
Index,
NumericalTable,
};
class BoundExpression{
@@ -276,5 +272,28 @@ public:
}
};
class BoundNumericalTableExpression : public BoundExpression{
vector<BoundExpression*> _expressions;
public:
BoundNumericalTableExpression(vector<BoundExpression*> expressions, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
: BoundExpression(start, length, type){
_expressions = std::move(expressions);
}
~BoundNumericalTableExpression(){
for (auto e: _expressions){
delete e;
}
}
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::NumericalTable;
}
vector<BoundExpression*> GetExpressions(){
return _expressions;
}
};
#endif //PORYGONLANG_BOUNDEXPRESSION_HPP