2019-06-13 16:14:59 +00:00
|
|
|
|
2019-08-18 12:32:14 +00:00
|
|
|
#include <memory>
|
2019-05-21 16:09:08 +00:00
|
|
|
#include "Binder.hpp"
|
2019-07-28 10:58:38 +00:00
|
|
|
#include "../ScriptTypes/TableScriptType.hpp"
|
2019-06-12 13:19:28 +00:00
|
|
|
#include "BoundExpressions/BoundTableExpression.hpp"
|
2019-06-29 17:59:42 +00:00
|
|
|
#include "BoundExpressions/BoundFunctionCallExpression.hpp"
|
2019-08-10 14:45:15 +00:00
|
|
|
#include "BoundExpressions/BoundRequireExpression.hpp"
|
2019-06-14 12:59:38 +00:00
|
|
|
#include "../UserData/UserDataScriptType.hpp"
|
2019-05-21 16:09:08 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
using namespace Porygon::Parser;
|
|
|
|
|
|
|
|
namespace Porygon::Binder {
|
|
|
|
BoundScriptStatement *Binder::Bind(Script *script, const ParsedScriptStatement *s, BoundScope *scriptScope) {
|
|
|
|
auto binder = Binder();
|
|
|
|
binder._scriptData = script;
|
|
|
|
|
|
|
|
binder._scope = scriptScope;
|
|
|
|
auto statements = s->GetStatements();
|
2019-07-25 15:23:54 +00:00
|
|
|
vector<const BoundStatement *> boundStatements(statements->size());
|
2019-08-10 14:45:15 +00:00
|
|
|
for (size_t i = 0; i < statements->size(); i++) {
|
2019-06-17 16:35:12 +00:00
|
|
|
boundStatements[i] = binder.BindStatement(statements->at(i));
|
|
|
|
}
|
|
|
|
return new BoundScriptStatement(boundStatements, scriptScope->GetLocalVariableCount());
|
2019-05-21 18:59:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
Binder::~Binder() {
|
|
|
|
delete _scope;
|
2019-05-21 18:59:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
BoundStatement *Binder::BindStatement(const ParsedStatement *statement) {
|
|
|
|
switch (statement->GetKind()) {
|
|
|
|
case ParsedStatementKind::Script:
|
|
|
|
throw; // This shouldn't happen.
|
|
|
|
case ParsedStatementKind::Block:
|
|
|
|
return this->BindBlockStatement(statement);
|
|
|
|
case ParsedStatementKind::Expression:
|
|
|
|
return this->BindExpressionStatement(statement);
|
|
|
|
case ParsedStatementKind::Assignment:
|
|
|
|
return this->BindAssignmentStatement(statement);
|
|
|
|
case ParsedStatementKind::IndexAssignment:
|
|
|
|
return this->BindIndexAssignmentStatement(statement);
|
|
|
|
case ParsedStatementKind::FunctionDeclaration:
|
|
|
|
return this->BindFunctionDeclarationStatement(statement);
|
|
|
|
case ParsedStatementKind::Return:
|
|
|
|
return this->BindReturnStatement(statement);
|
|
|
|
case ParsedStatementKind::Conditional:
|
|
|
|
return this->BindConditionalStatement(statement);
|
2019-06-22 15:35:33 +00:00
|
|
|
case ParsedStatementKind::NumericalFor:
|
|
|
|
return this->BindNumericalForStatement(statement);
|
2019-06-26 14:19:34 +00:00
|
|
|
case ParsedStatementKind::GenericFor:
|
2019-08-18 12:32:14 +00:00
|
|
|
return this->BindGenericForStatement(statement);
|
2019-06-28 11:28:39 +00:00
|
|
|
case ParsedStatementKind::While:
|
2019-08-18 12:32:14 +00:00
|
|
|
return this->BindWhileStatement(statement);
|
2019-06-27 13:55:46 +00:00
|
|
|
case ParsedStatementKind::Break:
|
|
|
|
//TODO: Validate we're in a loop
|
|
|
|
return new BoundBreakStatement();
|
2019-06-17 16:35:12 +00:00
|
|
|
case ParsedStatementKind::Bad:
|
|
|
|
return new BoundBadStatement();
|
|
|
|
}
|
2019-06-28 11:32:28 +00:00
|
|
|
throw "unreachable";
|
2019-05-21 18:59:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
BoundStatement *Binder::BindBlockStatement(const ParsedStatement *statement) {
|
|
|
|
auto statements = ((ParsedBlockStatement *) statement)->GetStatements();
|
2019-07-25 15:23:54 +00:00
|
|
|
vector<const BoundStatement *> boundStatements(statements->size());
|
2019-06-17 16:35:12 +00:00
|
|
|
this->_scope->GoInnerScope();
|
2019-08-10 14:45:15 +00:00
|
|
|
for (size_t i = 0; i < statements->size(); i++) {
|
2019-06-17 16:35:12 +00:00
|
|
|
boundStatements[i] = this->BindStatement(statements->at(i));
|
|
|
|
}
|
|
|
|
this->_scope->GoOuterScope();
|
|
|
|
return new BoundBlockStatement(boundStatements);
|
2019-05-28 15:49:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
BoundStatement *Binder::BindExpressionStatement(const ParsedStatement *statement) {
|
|
|
|
auto exp = ((ParsedExpressionStatement *) statement)->GetExpression();
|
|
|
|
return new BoundExpressionStatement(this->BindExpression(exp));
|
2019-06-17 13:45:33 +00:00
|
|
|
}
|
2019-06-14 15:12:27 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
BoundStatement *Binder::BindAssignmentStatement(const ParsedStatement *statement) {
|
|
|
|
auto s = (ParsedAssignmentStatement *) statement;
|
|
|
|
auto boundExpression = this->BindExpression(s->GetExpression());
|
|
|
|
VariableAssignment assignment =
|
|
|
|
s->IsLocal() ?
|
2019-06-23 13:58:14 +00:00
|
|
|
this->_scope->CreateExplicitLocal(s->GetIdentifier(), boundExpression->GetType())
|
|
|
|
: this->_scope->AssignVariable(s->GetIdentifier(), boundExpression->GetType());
|
2019-06-17 16:35:12 +00:00
|
|
|
if (assignment.GetResult() == VariableAssignmentResult::Ok) {
|
|
|
|
auto key = assignment.GetKey();
|
|
|
|
return new BoundAssignmentStatement(key, boundExpression);
|
|
|
|
} else {
|
2019-08-18 12:32:14 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable,
|
|
|
|
statement->GetStartPosition(),
|
2019-06-17 16:35:12 +00:00
|
|
|
statement->GetLength());
|
|
|
|
return new BoundBadStatement();
|
|
|
|
}
|
2019-06-01 10:33:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
BoundStatement *Binder::BindIndexAssignmentStatement(const ParsedStatement *statement) {
|
|
|
|
auto s = (ParsedIndexAssignmentStatement *) statement;
|
|
|
|
auto indexExp = s->GetIndexExpression();
|
|
|
|
const BoundExpression *indexable;
|
|
|
|
if (indexExp->GetKind() == ParsedExpressionKind::Indexer) {
|
|
|
|
indexable = this->BindIndexExpression((IndexExpression *) indexExp, true);
|
|
|
|
} else {
|
|
|
|
indexable = this->BindPeriodIndexExpression((PeriodIndexExpression *) indexExp, true);
|
2019-06-14 20:21:33 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
auto valueExpression = this->BindExpression(s->GetValueExpression());
|
|
|
|
auto boundIndexType = indexable->GetType();
|
|
|
|
if (boundIndexType->GetClass() != TypeClass::Error &&
|
2019-09-01 13:35:45 +00:00
|
|
|
boundIndexType->operator!=(valueExpression->GetType())) {
|
2019-06-18 14:39:36 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidTableValueType,
|
2019-06-17 16:35:12 +00:00
|
|
|
statement->GetStartPosition(), statement->GetLength());
|
|
|
|
return new BoundBadStatement();
|
2019-06-01 11:43:25 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
return new BoundIndexAssignmentStatement(indexable, valueExpression);
|
|
|
|
}
|
|
|
|
|
2019-09-01 13:35:45 +00:00
|
|
|
std::shared_ptr<const ScriptType> ParseTypeIdentifier(const HashedString *s) {
|
2019-07-25 15:23:54 +00:00
|
|
|
auto hash = s->GetHash();
|
2019-06-17 16:35:12 +00:00
|
|
|
switch (hash) {
|
|
|
|
case HashedString::ConstHash("number"):
|
2019-09-01 13:35:45 +00:00
|
|
|
return NumericScriptType::Unaware;
|
2019-06-17 16:35:12 +00:00
|
|
|
case HashedString::ConstHash("bool"):
|
2019-09-01 13:35:45 +00:00
|
|
|
return ScriptType::BoolType;
|
2019-06-17 16:35:12 +00:00
|
|
|
case HashedString::ConstHash("string"):
|
2019-09-01 13:35:45 +00:00
|
|
|
return StringScriptType::Dynamic;
|
2019-09-01 18:07:09 +00:00
|
|
|
case HashedString::ConstHash("table"):
|
|
|
|
return make_shared<TableScriptType>();
|
2019-06-17 16:35:12 +00:00
|
|
|
default:
|
|
|
|
if (!UserData::UserDataStorage::HasUserDataType(hash)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return std::make_shared<UserData::UserDataScriptType>(hash);
|
2019-06-01 11:43:25 +00:00
|
|
|
}
|
2019-06-01 10:33:52 +00:00
|
|
|
}
|
2019-06-08 14:02:21 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
BoundStatement *Binder::BindFunctionDeclarationStatement(const ParsedStatement *statement) {
|
|
|
|
auto functionStatement = (ParsedFunctionDeclarationStatement *) statement;
|
|
|
|
auto parameters = functionStatement->GetParameters();
|
2019-09-01 13:35:45 +00:00
|
|
|
auto parameterTypes = vector<shared_ptr<const ScriptType>>(parameters->size());
|
2019-07-25 15:23:54 +00:00
|
|
|
auto parameterKeys = vector<shared_ptr<const BoundVariableKey>>(parameters->size());
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
this->_scope->GoInnerScope();
|
2019-08-10 14:45:15 +00:00
|
|
|
for (size_t i = 0; i < parameters->size(); i++) {
|
2019-06-17 16:35:12 +00:00
|
|
|
auto var = parameters->at(i);
|
|
|
|
auto parsedType = ParseTypeIdentifier(var->GetType());
|
|
|
|
if (parsedType == nullptr) {
|
2019-08-18 12:32:14 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidTypeName,
|
|
|
|
statement->GetStartPosition(),
|
2019-06-17 16:35:12 +00:00
|
|
|
statement->GetLength());
|
|
|
|
return new BoundBadStatement();
|
|
|
|
}
|
|
|
|
parameterTypes.at(i) = parsedType;
|
2019-07-25 15:23:54 +00:00
|
|
|
auto parameterAssignment = this->_scope->CreateExplicitLocal(*var->GetIdentifier(), parsedType);
|
2019-06-17 16:35:12 +00:00
|
|
|
if (parameterAssignment.GetResult() == VariableAssignmentResult::Ok) {
|
2019-07-25 15:23:54 +00:00
|
|
|
parameterKeys.at(i) = std::shared_ptr<const BoundVariableKey>(parameterAssignment.GetKey());
|
2019-06-17 16:35:12 +00:00
|
|
|
} else {
|
|
|
|
//TODO: log error
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-06-12 16:45:47 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
auto identifier = functionStatement->GetIdentifier();
|
2019-09-01 13:35:45 +00:00
|
|
|
auto returnType = ScriptType::NilType;
|
2019-06-29 17:59:42 +00:00
|
|
|
auto option = new ScriptFunctionOption(returnType, parameterTypes, parameterKeys);
|
|
|
|
this->_currentFunction = option;
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
shared_ptr<const GenericFunctionScriptType> type;
|
2019-08-18 12:32:14 +00:00
|
|
|
auto scope = this->_scope->Exists(identifier);
|
|
|
|
const BoundVariableKey *assignmentKey;
|
|
|
|
if (scope >= 0) {
|
|
|
|
auto var = this->_scope->GetVariable(scope, identifier);
|
|
|
|
auto varType = var->GetType();
|
|
|
|
if (varType->GetClass() != TypeClass::Function) {
|
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable,
|
|
|
|
statement->GetStartPosition(),
|
2019-06-29 17:59:42 +00:00
|
|
|
statement->GetLength());
|
|
|
|
}
|
2019-07-25 15:23:54 +00:00
|
|
|
type = dynamic_pointer_cast<const GenericFunctionScriptType>(varType);
|
2019-06-29 17:59:42 +00:00
|
|
|
type->RegisterFunctionOption(option);
|
|
|
|
assignmentKey = new BoundVariableKey(identifier, scope, false);
|
2019-08-18 12:32:14 +00:00
|
|
|
} else {
|
2019-07-25 15:23:54 +00:00
|
|
|
type = make_shared<const GenericFunctionScriptType>();
|
2019-06-29 17:59:42 +00:00
|
|
|
type->RegisterFunctionOption(option);
|
|
|
|
auto assignment = this->_scope->AssignVariable(identifier, type);
|
|
|
|
if (assignment.GetResult() != VariableAssignmentResult::Ok) {
|
2019-08-18 12:32:14 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable,
|
|
|
|
statement->GetStartPosition(),
|
2019-06-29 17:59:42 +00:00
|
|
|
statement->GetLength());
|
|
|
|
return new BoundBadStatement();
|
|
|
|
}
|
|
|
|
assignmentKey = assignment.GetKey();
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
2019-06-29 17:59:42 +00:00
|
|
|
|
2019-08-18 12:32:14 +00:00
|
|
|
auto boundBlock = dynamic_cast<BoundBlockStatement *>(this->BindBlockStatement(functionStatement->GetBlock()));
|
2019-06-17 16:35:12 +00:00
|
|
|
this->_scope->GoOuterScope();
|
|
|
|
this->_currentFunction = nullptr;
|
2019-07-04 16:24:49 +00:00
|
|
|
return new BoundFunctionDeclarationStatement(type, assignmentKey, boundBlock);
|
2019-06-07 13:23:13 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
BoundStatement *Binder::BindReturnStatement(const ParsedStatement *statement) {
|
|
|
|
auto expression = ((ParsedReturnStatement *) statement)->GetExpression();
|
2019-07-25 15:23:54 +00:00
|
|
|
shared_ptr<const ScriptType> currentReturnType;
|
2019-06-17 16:35:12 +00:00
|
|
|
if (this->_currentFunction == nullptr) {
|
|
|
|
currentReturnType = this->_scriptData->GetReturnType();
|
|
|
|
} else {
|
|
|
|
currentReturnType = this->_currentFunction->GetReturnType();
|
|
|
|
}
|
2019-08-18 12:32:14 +00:00
|
|
|
if (expression == nullptr &&
|
|
|
|
(currentReturnType != nullptr && currentReturnType->GetClass() != TypeClass::Nil)) {
|
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidReturnType,
|
|
|
|
statement->GetStartPosition(),
|
2019-06-17 16:35:12 +00:00
|
|
|
statement->GetLength());
|
|
|
|
return new BoundBadStatement();
|
2019-08-18 12:32:14 +00:00
|
|
|
} else if (expression == nullptr) {
|
2019-09-01 13:35:45 +00:00
|
|
|
currentReturnType = ScriptType::NilType;
|
2019-06-19 14:21:21 +00:00
|
|
|
return new BoundReturnStatement(nullptr);
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
|
|
|
auto boundExpression = this->BindExpression(expression);
|
|
|
|
auto expresionType = boundExpression->GetType();
|
|
|
|
if (currentReturnType == nullptr || currentReturnType->GetClass() == TypeClass::Nil) {
|
|
|
|
if (this->_currentFunction == nullptr) {
|
|
|
|
this->_scriptData->SetReturnType(expresionType);
|
|
|
|
} else {
|
|
|
|
this->_currentFunction->SetReturnType(expresionType);
|
|
|
|
}
|
|
|
|
return new BoundReturnStatement(boundExpression);
|
|
|
|
}
|
2019-09-01 13:35:45 +00:00
|
|
|
if (currentReturnType.get()->operator!=(expresionType)) {
|
2019-08-18 12:32:14 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidReturnType,
|
|
|
|
statement->GetStartPosition(),
|
2019-06-17 16:35:12 +00:00
|
|
|
statement->GetLength());
|
|
|
|
return new BoundBadStatement();
|
2019-06-12 16:45:47 +00:00
|
|
|
}
|
2019-06-07 13:23:13 +00:00
|
|
|
return new BoundReturnStatement(boundExpression);
|
|
|
|
}
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
BoundStatement *Binder::BindConditionalStatement(const ParsedStatement *statement) {
|
|
|
|
auto conditionalStatement = (ParsedConditionalStatement *) statement;
|
|
|
|
auto boundCondition = this->BindExpression(conditionalStatement->GetCondition());
|
|
|
|
if (boundCondition->GetType()->GetClass() != TypeClass::Bool) {
|
2019-08-18 12:32:14 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::ConditionNotABool,
|
|
|
|
statement->GetStartPosition(),
|
2019-06-17 16:35:12 +00:00
|
|
|
statement->GetLength());
|
|
|
|
return new BoundBadStatement();
|
|
|
|
}
|
|
|
|
auto boundBlock = this->BindStatement(conditionalStatement->GetBlock());
|
|
|
|
BoundStatement *elseStatement = nullptr;
|
|
|
|
if (conditionalStatement->GetElseStatement() != nullptr) {
|
|
|
|
elseStatement = this->BindStatement(conditionalStatement->GetElseStatement());
|
|
|
|
}
|
|
|
|
return new BoundConditionalStatement(boundCondition, boundBlock, elseStatement);
|
2019-06-08 12:25:15 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 15:35:33 +00:00
|
|
|
BoundStatement *Binder::BindNumericalForStatement(const ParsedStatement *statement) {
|
2019-08-18 12:32:14 +00:00
|
|
|
auto forStatement = (ParsedNumericalForStatement *) statement;
|
2019-06-22 15:35:33 +00:00
|
|
|
auto identifier = forStatement->GetIdentifier();
|
2019-08-18 12:32:14 +00:00
|
|
|
auto start = this->BindExpression(forStatement->GetStart());
|
|
|
|
auto end = this->BindExpression(forStatement->GetEnd());
|
|
|
|
auto parsedStep = forStatement->GetStep();
|
|
|
|
BoundExpression *step = nullptr;
|
|
|
|
if (parsedStep != nullptr) {
|
|
|
|
step = this->BindExpression(parsedStep);
|
|
|
|
}
|
|
|
|
if (start->GetType()->GetClass() != TypeClass::Number) {
|
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::NumericalForArgumentNotANumber,
|
|
|
|
start->GetStartPosition(),
|
2019-06-22 15:35:33 +00:00
|
|
|
start->GetLength());
|
|
|
|
return new BoundBadStatement();
|
|
|
|
}
|
2019-08-18 12:32:14 +00:00
|
|
|
if (end->GetType()->GetClass() != TypeClass::Number) {
|
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::NumericalForArgumentNotANumber,
|
|
|
|
end->GetStartPosition(),
|
2019-06-22 15:35:33 +00:00
|
|
|
end->GetLength());
|
|
|
|
return new BoundBadStatement();
|
|
|
|
}
|
2019-08-18 12:32:14 +00:00
|
|
|
if (step != nullptr && step->GetType()->GetClass() != TypeClass::Number) {
|
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::NumericalForArgumentNotANumber,
|
|
|
|
step->GetStartPosition(),
|
2019-06-22 15:35:33 +00:00
|
|
|
step->GetLength());
|
|
|
|
return new BoundBadStatement();
|
|
|
|
}
|
|
|
|
|
2019-08-18 12:32:14 +00:00
|
|
|
this->_scope->GoInnerScope();
|
2019-09-01 13:35:45 +00:00
|
|
|
auto variableKey = this->_scope->CreateExplicitLocal(identifier, NumericScriptType::AwareInt);
|
2019-08-18 12:32:14 +00:00
|
|
|
if (variableKey.GetResult() != VariableAssignmentResult::Ok) {
|
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable,
|
|
|
|
statement->GetStartPosition(),
|
2019-06-22 15:35:33 +00:00
|
|
|
statement->GetLength());
|
|
|
|
return new BoundBadStatement();
|
|
|
|
}
|
2019-08-18 12:32:14 +00:00
|
|
|
auto block = this->BindBlockStatement(forStatement->GetBlock());
|
|
|
|
this->_scope->GoOuterScope();
|
2019-06-22 15:35:33 +00:00
|
|
|
return new BoundNumericalForStatement(variableKey.GetKey(), start, end, step, block);
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:19:34 +00:00
|
|
|
BoundStatement *Binder::BindGenericForStatement(const ParsedStatement *statement) {
|
2019-08-18 12:32:14 +00:00
|
|
|
auto genericFor = (ParsedGenericForStatement *) statement;
|
|
|
|
auto boundIterator = BindExpression(genericFor->GetIteratorExpression());
|
|
|
|
const auto &itType = boundIterator->GetType();
|
|
|
|
if (!itType->CanBeIterated()) {
|
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantIterateExpression,
|
|
|
|
statement->GetStartPosition(),
|
2019-06-26 14:19:34 +00:00
|
|
|
statement->GetLength());
|
|
|
|
return new BoundBadStatement();
|
|
|
|
}
|
2019-08-18 12:32:14 +00:00
|
|
|
auto keyType = itType->GetIteratorKeyType();
|
|
|
|
auto keyIdentifier = genericFor->GetKeyIdentifier();
|
|
|
|
this->_scope->GoInnerScope();
|
|
|
|
auto keyVariableAssignment = this->_scope->CreateExplicitLocal(keyIdentifier, keyType);
|
|
|
|
if (keyVariableAssignment.GetResult() != VariableAssignmentResult::Ok) {
|
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable,
|
|
|
|
statement->GetStartPosition(),
|
2019-06-26 14:19:34 +00:00
|
|
|
statement->GetLength());
|
|
|
|
return new BoundBadStatement();
|
|
|
|
}
|
|
|
|
auto keyVariable = keyVariableAssignment.GetKey();
|
|
|
|
|
2019-08-18 12:32:14 +00:00
|
|
|
auto valueIdentifier = genericFor->GetValueIdentifier();
|
2019-06-26 14:19:34 +00:00
|
|
|
auto isValueVariableDefined = valueIdentifier.GetHash() != 0;
|
2019-08-18 12:32:14 +00:00
|
|
|
const BoundVariableKey *valueVariable = nullptr;
|
|
|
|
if (isValueVariableDefined) {
|
|
|
|
auto valueType = itType->GetIndexedType(keyType.get());
|
|
|
|
auto valueVariableAssignment = this->_scope->CreateExplicitLocal(valueIdentifier, valueType);
|
|
|
|
if (valueVariableAssignment.GetResult() != VariableAssignmentResult::Ok) {
|
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable,
|
|
|
|
statement->GetStartPosition(),
|
2019-06-26 14:19:34 +00:00
|
|
|
statement->GetLength());
|
|
|
|
return new BoundBadStatement();
|
|
|
|
}
|
|
|
|
valueVariable = valueVariableAssignment.GetKey();
|
|
|
|
}
|
2019-08-18 12:32:14 +00:00
|
|
|
auto boundBlock = this->BindBlockStatement(genericFor->GetBlock());
|
2019-06-26 14:19:34 +00:00
|
|
|
|
2019-08-18 12:32:14 +00:00
|
|
|
this->_scope->GoOuterScope();
|
2019-06-26 14:19:34 +00:00
|
|
|
|
|
|
|
return new BoundGenericForStatement(keyVariable, valueVariable, boundIterator, boundBlock);
|
|
|
|
}
|
|
|
|
|
2019-06-28 11:28:39 +00:00
|
|
|
BoundStatement *Binder::BindWhileStatement(const ParsedStatement *statement) {
|
2019-08-18 12:32:14 +00:00
|
|
|
auto whileStatement = (ParsedWhileStatement *) statement;
|
|
|
|
auto boundCondition = this->BindExpression(whileStatement->GetCondition());
|
|
|
|
if (boundCondition->GetType()->GetClass() != TypeClass::Bool) {
|
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::ConditionNotABool,
|
|
|
|
statement->GetStartPosition(),
|
2019-06-28 11:28:39 +00:00
|
|
|
statement->GetLength());
|
|
|
|
return new BoundBadStatement();
|
|
|
|
}
|
2019-08-18 12:32:14 +00:00
|
|
|
auto boundBlock = this->BindBlockStatement(whileStatement->GetBlock());
|
2019-06-28 11:28:39 +00:00
|
|
|
return new BoundWhileStatement(boundCondition, boundBlock);
|
|
|
|
}
|
|
|
|
|
2019-06-22 15:35:33 +00:00
|
|
|
/////////////////
|
|
|
|
// Expressions //
|
|
|
|
/////////////////
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
BoundExpression *Binder::BindExpression(const ParsedExpression *expression) {
|
|
|
|
switch (expression->GetKind()) {
|
|
|
|
case ParsedExpressionKind::LiteralInteger:
|
|
|
|
return new BoundLiteralIntegerExpression(((LiteralIntegerExpression *) expression)->GetValue(),
|
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
|
|
|
case ParsedExpressionKind::LiteralFloat:
|
|
|
|
return new BoundLiteralFloatExpression(((LiteralFloatExpression *) expression)->GetValue(),
|
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
|
|
|
case ParsedExpressionKind::LiteralString:
|
|
|
|
return new BoundLiteralStringExpression(((LiteralStringExpression *) expression)->GetValue(),
|
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
|
|
|
case ParsedExpressionKind::LiteralBool:
|
|
|
|
return new BoundLiteralBoolExpression(((LiteralBoolExpression *) expression)->GetValue(),
|
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
2019-09-01 14:16:36 +00:00
|
|
|
case ParsedExpressionKind ::Nil:
|
|
|
|
return new BoundNilExpression(expression->GetStartPosition(), expression->GetLength());
|
2019-06-17 16:35:12 +00:00
|
|
|
case ParsedExpressionKind::Variable:
|
|
|
|
return this->BindVariableExpression((VariableExpression *) expression);
|
|
|
|
|
|
|
|
case ParsedExpressionKind::Binary:
|
|
|
|
return this->BindBinaryOperator((BinaryExpression *) expression);
|
|
|
|
case ParsedExpressionKind::Unary:
|
|
|
|
return this->BindUnaryOperator((UnaryExpression *) expression);
|
|
|
|
|
|
|
|
case ParsedExpressionKind::Parenthesized:
|
|
|
|
return BindExpression(((ParenthesizedExpression *) expression)->GetInnerExpression());
|
|
|
|
case ParsedExpressionKind::FunctionCall:
|
|
|
|
return this->BindFunctionCall((FunctionCallExpression *) expression);
|
|
|
|
|
|
|
|
case ParsedExpressionKind::Indexer:
|
|
|
|
return this->BindIndexExpression((IndexExpression *) expression, false);
|
|
|
|
case ParsedExpressionKind::PeriodIndexer:
|
|
|
|
return this->BindPeriodIndexExpression((PeriodIndexExpression *) expression, false);
|
|
|
|
case ParsedExpressionKind::NumericalTable:
|
|
|
|
return this->BindNumericalTableExpression((ParsedNumericalTableExpression *) expression);
|
|
|
|
case ParsedExpressionKind::Table:
|
|
|
|
return this->BindTableExpression((ParsedTableExpression *) expression);
|
|
|
|
|
|
|
|
case ParsedExpressionKind::Bad:
|
|
|
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
2019-06-28 11:32:28 +00:00
|
|
|
throw;
|
2019-05-21 18:59:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
BoundExpression *Binder::BindVariableExpression(const VariableExpression *expression) {
|
|
|
|
auto key = expression->GetValue();
|
2019-06-23 13:58:14 +00:00
|
|
|
auto scope = this->_scope->Exists(key);
|
2019-06-17 16:35:12 +00:00
|
|
|
if (scope == -1) {
|
2019-08-18 12:32:14 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::VariableNotFound,
|
|
|
|
expression->GetStartPosition(),
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetLength());
|
|
|
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
2019-06-23 13:58:14 +00:00
|
|
|
auto var = this->_scope->GetVariable(scope, key);
|
2019-06-17 16:35:12 +00:00
|
|
|
auto type = var->GetType();
|
2019-06-23 13:58:14 +00:00
|
|
|
return new BoundVariableExpression(new BoundVariableKey(key, scope, false), type,
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
2019-05-30 13:23:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
BoundExpression *Binder::BindBinaryOperator(const BinaryExpression *expression) {
|
|
|
|
auto boundLeft = this->BindExpression(expression->GetLeft());
|
|
|
|
auto boundRight = this->BindExpression(expression->GetRight());
|
|
|
|
|
|
|
|
auto boundLeftType = boundLeft->GetType();
|
|
|
|
auto boundRightType = boundRight->GetType();
|
|
|
|
|
|
|
|
switch (expression->GetOperatorKind()) {
|
|
|
|
case BinaryOperatorKind::Addition:
|
|
|
|
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number) {
|
2019-07-25 15:23:54 +00:00
|
|
|
auto leftNumeric = std::static_pointer_cast<const NumericScriptType>(boundLeftType);
|
|
|
|
auto rightNumeric = std::static_pointer_cast<const NumericScriptType>(boundRightType);
|
2019-06-17 16:35:12 +00:00
|
|
|
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight,
|
|
|
|
BoundBinaryOperation::Addition,
|
2019-09-01 13:35:45 +00:00
|
|
|
NumericScriptType::ResolveType(true,
|
2019-06-17 16:35:12 +00:00
|
|
|
leftNumeric->IsFloat() ||
|
|
|
|
rightNumeric->IsFloat()),
|
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
|
|
|
} else {
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Addition,
|
2019-09-01 13:35:45 +00:00
|
|
|
NumericScriptType::Unaware,
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
|
|
|
} else if (boundLeftType->GetClass() == TypeClass::String) {
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Concatenation,
|
2019-09-01 13:35:45 +00:00
|
|
|
StringScriptType::Dynamic,
|
2019-05-22 10:29:29 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
2019-05-21 20:15:51 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
break;
|
|
|
|
case BinaryOperatorKind::Subtraction:
|
|
|
|
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number) {
|
2019-07-25 15:23:54 +00:00
|
|
|
auto leftNumeric = std::dynamic_pointer_cast<const NumericScriptType>(boundLeftType);
|
|
|
|
auto rightNumeric = std::dynamic_pointer_cast<const NumericScriptType>(boundRightType);
|
2019-06-17 16:35:12 +00:00
|
|
|
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight,
|
|
|
|
BoundBinaryOperation::Subtraction,
|
2019-09-01 13:35:45 +00:00
|
|
|
NumericScriptType::ResolveType(true,
|
2019-06-17 16:35:12 +00:00
|
|
|
leftNumeric->IsFloat() ||
|
|
|
|
rightNumeric->IsFloat()),
|
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
|
|
|
} else {
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Subtraction,
|
2019-09-01 13:35:45 +00:00
|
|
|
NumericScriptType::Unaware,
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BinaryOperatorKind::Multiplication:
|
|
|
|
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number) {
|
2019-07-25 15:23:54 +00:00
|
|
|
auto leftNumeric = std::dynamic_pointer_cast<const NumericScriptType>(boundLeftType);
|
|
|
|
auto rightNumeric = std::dynamic_pointer_cast<const NumericScriptType>(boundRightType);
|
2019-06-17 16:35:12 +00:00
|
|
|
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight,
|
|
|
|
BoundBinaryOperation::Multiplication,
|
2019-09-01 13:35:45 +00:00
|
|
|
NumericScriptType::ResolveType(true,
|
2019-06-17 16:35:12 +00:00
|
|
|
leftNumeric->IsFloat() ||
|
|
|
|
rightNumeric->IsFloat()),
|
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
|
|
|
} else {
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Multiplication,
|
2019-09-01 13:35:45 +00:00
|
|
|
NumericScriptType::Unaware,
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BinaryOperatorKind::Division:
|
|
|
|
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number) {
|
2019-07-25 15:23:54 +00:00
|
|
|
auto leftNumeric = std::dynamic_pointer_cast<const NumericScriptType>(boundLeftType);
|
|
|
|
auto rightNumeric = std::dynamic_pointer_cast<const NumericScriptType>(boundRightType);
|
2019-06-17 16:35:12 +00:00
|
|
|
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight,
|
|
|
|
BoundBinaryOperation::Division,
|
2019-09-01 13:35:45 +00:00
|
|
|
NumericScriptType::ResolveType(true,
|
2019-06-17 16:35:12 +00:00
|
|
|
leftNumeric->IsFloat() ||
|
|
|
|
rightNumeric->IsFloat()),
|
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
|
|
|
} else {
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Division,
|
2019-09-01 13:35:45 +00:00
|
|
|
NumericScriptType::Unaware,
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BinaryOperatorKind::Equality:
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Equality,
|
2019-09-01 13:35:45 +00:00
|
|
|
ScriptType::BoolType,
|
2019-05-25 14:15:20 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
2019-06-17 16:35:12 +00:00
|
|
|
case BinaryOperatorKind::Inequality:
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Inequality,
|
2019-09-01 13:35:45 +00:00
|
|
|
ScriptType::BoolType,
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
|
|
|
case BinaryOperatorKind::Less:
|
|
|
|
if (boundLeft->GetType()->GetClass() == TypeClass::Number &&
|
|
|
|
boundRight->GetType()->GetClass() == TypeClass::Number) {
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LessThan,
|
2019-09-01 13:35:45 +00:00
|
|
|
ScriptType::BoolType,
|
2019-05-22 10:29:29 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
2019-05-21 20:15:51 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
case BinaryOperatorKind::LessOrEquals:
|
|
|
|
if (boundLeft->GetType()->GetClass() == TypeClass::Number &&
|
|
|
|
boundRight->GetType()->GetClass() == TypeClass::Number) {
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LessThanEquals,
|
2019-09-01 13:35:45 +00:00
|
|
|
ScriptType::BoolType,
|
2019-05-22 10:29:29 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
2019-05-21 20:15:51 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
case BinaryOperatorKind::Greater:
|
|
|
|
if (boundLeft->GetType()->GetClass() == TypeClass::Number &&
|
|
|
|
boundRight->GetType()->GetClass() == TypeClass::Number) {
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::GreaterThan,
|
2019-09-01 13:35:45 +00:00
|
|
|
ScriptType::BoolType,
|
2019-05-22 10:29:29 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
2019-05-21 20:15:51 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
case BinaryOperatorKind::GreaterOrEquals:
|
|
|
|
if (boundLeft->GetType()->GetClass() == TypeClass::Number &&
|
|
|
|
boundRight->GetType()->GetClass() == TypeClass::Number) {
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::GreaterThanEquals,
|
2019-09-01 13:35:45 +00:00
|
|
|
ScriptType::BoolType,
|
2019-05-22 10:29:29 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
2019-05-21 20:15:51 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
case BinaryOperatorKind::LogicalAnd:
|
|
|
|
if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool)
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalAnd,
|
2019-09-01 13:35:45 +00:00
|
|
|
ScriptType::BoolType,
|
2019-05-22 10:29:29 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
2019-06-17 16:35:12 +00:00
|
|
|
break;
|
|
|
|
case BinaryOperatorKind::LogicalOr:
|
|
|
|
if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool)
|
|
|
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalOr,
|
2019-09-01 13:35:45 +00:00
|
|
|
ScriptType::BoolType,
|
2019-05-22 10:29:29 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
2019-06-17 16:35:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-08-18 12:32:14 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::NoBinaryOperationFound,
|
|
|
|
expression->GetStartPosition(),
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetLength());
|
2019-06-01 17:20:31 +00:00
|
|
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
BoundExpression *Binder::BindUnaryOperator(const UnaryExpression *expression) {
|
|
|
|
auto operand = this->BindExpression(expression->GetOperand());
|
|
|
|
auto operandType = operand->GetType();
|
|
|
|
switch (expression->GetOperatorKind()) {
|
|
|
|
case UnaryOperatorKind::Identity:
|
|
|
|
if (operandType->GetClass() == TypeClass::Number) {
|
|
|
|
// Identity won't change anything during evaluation, so just return the inner operand.
|
|
|
|
return operand;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UnaryOperatorKind::Negation:
|
|
|
|
if (operandType->GetClass() == TypeClass::Number) {
|
2019-07-25 15:23:54 +00:00
|
|
|
auto innerType = std::dynamic_pointer_cast<const NumericScriptType>(operandType);
|
2019-06-17 16:35:12 +00:00
|
|
|
return new BoundUnaryExpression(operand, BoundUnaryOperation::Negation,
|
2019-09-01 13:35:45 +00:00
|
|
|
NumericScriptType::ResolveType(
|
2019-06-17 16:35:12 +00:00
|
|
|
innerType.get()->IsAwareOfFloat(),
|
|
|
|
innerType.get()->IsFloat()),
|
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UnaryOperatorKind::LogicalNegation:
|
|
|
|
if (operandType->GetClass() == TypeClass::Bool) {
|
|
|
|
return new BoundUnaryExpression(operand, BoundUnaryOperation::LogicalNegation,
|
2019-09-01 13:35:45 +00:00
|
|
|
ScriptType::BoolType,
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-08-18 12:32:14 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::NoUnaryOperationFound,
|
|
|
|
expression->GetStartPosition(),
|
2019-06-01 17:20:31 +00:00
|
|
|
expression->GetLength());
|
|
|
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
2019-06-17 16:35:12 +00:00
|
|
|
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
BoundExpression *Binder::BindFunctionCall(const FunctionCallExpression *expression) {
|
2019-08-10 14:45:15 +00:00
|
|
|
auto func = expression->GetFunction();
|
2019-08-18 12:32:14 +00:00
|
|
|
if (func->GetKind() == ParsedExpressionKind::Variable) {
|
|
|
|
auto variable = dynamic_cast<const VariableExpression *>(func);
|
|
|
|
auto hash = variable->GetValue().GetHash();
|
|
|
|
if (hash == HashedString::ConstHash("require")) {
|
2019-08-10 14:45:15 +00:00
|
|
|
return this->BindRequire(expression);
|
2019-08-18 12:32:14 +00:00
|
|
|
} else if (hash == HashedString::ConstHash("cast")) {
|
|
|
|
return this->BindCast(expression);
|
2019-08-10 14:45:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
auto functionExpression = BindExpression(func);
|
2019-06-17 16:35:12 +00:00
|
|
|
auto type = functionExpression->GetType();
|
|
|
|
if (type->GetClass() != TypeClass::Function) {
|
2019-06-18 14:39:36 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::ExpressionIsNotAFunction,
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetStartPosition(),
|
|
|
|
expression->GetLength());
|
2019-06-01 17:20:31 +00:00
|
|
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
2019-07-25 15:23:54 +00:00
|
|
|
auto functionType = std::dynamic_pointer_cast<const GenericFunctionScriptType>(type);
|
2019-06-17 16:35:12 +00:00
|
|
|
auto givenParameters = expression->GetParameters();
|
2019-06-29 17:59:42 +00:00
|
|
|
vector<BoundExpression *> boundParameters = vector<BoundExpression *>(givenParameters->size());
|
2019-08-18 12:32:14 +00:00
|
|
|
for (size_t i = 0; i < givenParameters->size(); i++) {
|
|
|
|
boundParameters[i] = this->BindExpression(givenParameters->at(i));
|
2019-06-29 17:59:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 11:17:53 +00:00
|
|
|
auto functionOption = functionType->GetFunctionOption(this->_scriptData->Diagnostics, &boundParameters);
|
2019-08-18 12:32:14 +00:00
|
|
|
if (functionOption == nullptr) {
|
2019-06-29 17:59:42 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidFunctionParameters,
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetStartPosition(),
|
|
|
|
expression->GetLength());
|
|
|
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
|
|
|
|
2019-08-18 12:32:14 +00:00
|
|
|
return new BoundFunctionCallExpression(functionExpression, boundParameters, functionOption,
|
|
|
|
functionOption->GetReturnType(),
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetStartPosition(), expression->GetLength());
|
2019-06-15 13:38:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 12:32:14 +00:00
|
|
|
BoundExpression *Binder::BindRequire(const FunctionCallExpression *exp) {
|
2019-08-10 14:45:15 +00:00
|
|
|
auto parameters = exp->GetParameters();
|
2019-08-18 12:32:14 +00:00
|
|
|
if (parameters->size() != 1) {
|
2019-08-10 14:45:15 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidFunctionParameters,
|
|
|
|
exp->GetStartPosition(),
|
|
|
|
exp->GetLength());
|
2019-08-18 12:32:14 +00:00
|
|
|
return new BoundBadExpression(exp->GetStartPosition(), exp->GetLength());
|
2019-08-10 14:45:15 +00:00
|
|
|
}
|
|
|
|
auto parameter = parameters->at(0);
|
2019-08-18 12:32:14 +00:00
|
|
|
auto boundParameter = this->BindExpression(parameter);
|
|
|
|
if (boundParameter->GetKind() != BoundExpressionKind::LiteralString) {
|
2019-08-10 14:45:15 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidFunctionParameters,
|
|
|
|
exp->GetStartPosition(),
|
|
|
|
exp->GetLength());
|
2019-08-18 12:32:14 +00:00
|
|
|
return new BoundBadExpression(exp->GetStartPosition(), exp->GetLength());
|
2019-08-10 14:45:15 +00:00
|
|
|
}
|
2019-08-18 12:32:14 +00:00
|
|
|
auto key = *dynamic_cast<BoundLiteralStringExpression *>(boundParameter)->GetValue();
|
2019-08-10 14:45:15 +00:00
|
|
|
auto opt = this->_scriptData->GetScriptOptions();
|
|
|
|
auto transformedKey = Utilities::StringUtils::FromUTF8(key);
|
|
|
|
delete boundParameter;
|
2019-08-24 16:52:11 +00:00
|
|
|
if (!opt->DoesModuleExist(transformedKey.c_str(), transformedKey.size())) {
|
2019-08-10 14:45:15 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::ModuleDoesntExist,
|
|
|
|
exp->GetStartPosition(),
|
|
|
|
exp->GetLength());
|
2019-08-18 12:32:14 +00:00
|
|
|
return new BoundBadExpression(exp->GetStartPosition(), exp->GetLength());
|
2019-08-10 14:45:15 +00:00
|
|
|
}
|
2019-08-24 16:52:11 +00:00
|
|
|
auto module = Script::Clone(opt->ResolveModule(transformedKey.c_str(), transformedKey.size()));
|
2019-08-18 12:32:14 +00:00
|
|
|
if (module->GetReturnType() == nullptr) {
|
|
|
|
for (const auto &v: *module->GetScriptVariables()) {
|
2019-08-11 10:32:18 +00:00
|
|
|
auto type = module->GetVariableType(v.first);
|
2019-08-18 12:32:14 +00:00
|
|
|
auto result = this->_scope->AssignVariable(v.first, type);
|
2019-08-11 10:32:18 +00:00
|
|
|
delete result.GetKey();
|
2019-08-10 14:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-08-18 12:32:14 +00:00
|
|
|
return new BoundRequireExpression(module, exp->GetStartPosition(), exp->GetLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
BoundExpression *Binder::BindCast(const FunctionCallExpression* exp){
|
|
|
|
auto parameters = exp->GetParameters();
|
|
|
|
if (parameters->size() != 2) {
|
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidFunctionParameters,
|
|
|
|
exp->GetStartPosition(),
|
|
|
|
exp->GetLength());
|
|
|
|
return new BoundBadExpression(exp->GetStartPosition(), exp->GetLength());
|
|
|
|
}
|
|
|
|
auto toCastParameter = this ->BindExpression(parameters->at(0));
|
|
|
|
const auto& toCastParameterType = toCastParameter->GetType();
|
|
|
|
|
|
|
|
auto destinationTypeParameter = parameters -> at(1);
|
|
|
|
if (destinationTypeParameter ->GetKind() != ParsedExpressionKind::Variable){
|
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidFunctionParameters,
|
|
|
|
exp->GetStartPosition(),
|
|
|
|
exp->GetLength());
|
|
|
|
return new BoundBadExpression(exp->GetStartPosition(), exp->GetLength());
|
|
|
|
}
|
|
|
|
auto destinationTypeContent = dynamic_cast<const VariableExpression*>(destinationTypeParameter)->GetValue();
|
|
|
|
auto destinationType = ParseTypeIdentifier(&destinationTypeContent);
|
|
|
|
auto castResult = toCastParameterType->CastableTo(destinationType, true);
|
|
|
|
if (castResult == CastResult::InvalidCast){
|
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidCast, exp->GetStartPosition(),
|
|
|
|
exp->GetLength());
|
|
|
|
return new BoundBadExpression(exp->GetStartPosition(), exp->GetLength());
|
|
|
|
}
|
|
|
|
else if (castResult == CastResult::DataLoss){
|
|
|
|
this->_scriptData->Diagnostics->LogWarning(Diagnostics::DiagnosticCode::DataLossOnCast, exp->GetStartPosition(),
|
|
|
|
exp->GetLength());
|
|
|
|
}
|
|
|
|
else if (castResult == CastResult::UncheckedCast){
|
|
|
|
this->_scriptData->Diagnostics->LogInfo(Diagnostics::DiagnosticCode::UnvalidatedCast, exp->GetStartPosition(),
|
|
|
|
exp->GetLength());
|
|
|
|
}
|
|
|
|
return new BoundCastExpression(toCastParameter, destinationType);
|
2019-08-10 14:45:15 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
BoundExpression *Binder::BindIndexExpression(const IndexExpression *expression, bool setter) {
|
|
|
|
auto indexer = this->BindExpression(expression->GetIndexer());
|
|
|
|
auto index = this->BindExpression(expression->GetIndex());
|
2019-06-08 12:25:15 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
auto indexerType = indexer->GetType();
|
|
|
|
if (!indexerType->CanBeIndexedWith(index->GetType().get())) {
|
2019-06-18 14:39:36 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantIndex, index->GetStartPosition(),
|
2019-06-17 16:35:12 +00:00
|
|
|
index->GetLength());
|
|
|
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
|
|
|
if (indexerType->GetClass() == TypeClass::UserData) {
|
2019-07-25 15:23:54 +00:00
|
|
|
auto stringKey = dynamic_pointer_cast<const StringScriptType>(index->GetType());
|
|
|
|
auto field = dynamic_pointer_cast<const UserData::UserDataScriptType>(indexerType)->GetField(stringKey->GetHashValue());
|
2019-06-17 16:35:12 +00:00
|
|
|
if (!setter) {
|
|
|
|
if (!field->HasGetter()) {
|
2019-06-18 14:39:36 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UserDataFieldNoGetter,
|
2019-06-17 16:35:12 +00:00
|
|
|
index->GetStartPosition(),
|
|
|
|
index->GetLength());
|
|
|
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!field->HasSetter()) {
|
2019-06-18 14:39:36 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UserDataFieldNoSetter,
|
2019-06-17 16:35:12 +00:00
|
|
|
index->GetStartPosition(),
|
|
|
|
index->GetLength());
|
|
|
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
2019-06-17 13:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
auto resultType = indexer->GetType()->GetIndexedType(index->GetType().get());
|
|
|
|
return new BoundIndexExpression(indexer, index, resultType, expression->GetStartPosition(),
|
|
|
|
expression->GetLength());
|
2019-06-17 13:45:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
BoundExpression *Binder::BindPeriodIndexExpression(const PeriodIndexExpression *expression, bool setter) {
|
|
|
|
auto indexer = this->BindExpression(expression->GetIndexer());
|
|
|
|
const auto &identifier = expression->GetIndex();
|
|
|
|
const auto &indexerType = indexer->GetType();
|
|
|
|
if (!indexerType->CanBeIndexedWithIdentifier(identifier.GetHash())) {
|
2019-06-18 14:39:36 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantIndex, expression->GetStartPosition(),
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetLength());
|
|
|
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
|
|
|
if (indexerType->GetClass() == TypeClass::UserData) {
|
2019-07-25 15:23:54 +00:00
|
|
|
auto field = dynamic_pointer_cast<const UserData::UserDataScriptType>(indexerType)->GetField(identifier.GetHash());
|
2019-06-17 16:35:12 +00:00
|
|
|
if (!setter) {
|
|
|
|
if (!field->HasGetter()) {
|
2019-06-18 14:39:36 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UserDataFieldNoGetter,
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetStartPosition(),
|
|
|
|
expression->GetLength());
|
|
|
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!field->HasSetter()) {
|
2019-06-18 14:39:36 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UserDataFieldNoSetter,
|
2019-06-17 16:35:12 +00:00
|
|
|
expression->GetStartPosition(),
|
|
|
|
expression->GetLength());
|
|
|
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 13:45:33 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
auto resultType = indexer->GetType()->GetIndexedType(identifier.GetHash());
|
|
|
|
return new BoundPeriodIndexExpression(indexer, identifier, resultType, expression->GetStartPosition(),
|
|
|
|
expression->GetLength());
|
2019-06-17 13:45:33 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BoundExpression *Binder::BindNumericalTableExpression(const ParsedNumericalTableExpression *expression) {
|
|
|
|
auto expressions = expression->GetExpressions();
|
|
|
|
auto boundExpressions = vector<const BoundExpression *>(expressions->size());
|
2019-07-25 15:23:54 +00:00
|
|
|
shared_ptr<const ScriptType> valueType = nullptr;
|
2019-06-17 16:35:12 +00:00
|
|
|
if (!boundExpressions.empty()) {
|
|
|
|
boundExpressions[0] = this->BindExpression(expressions->at(0));
|
|
|
|
valueType = boundExpressions[0]->GetType();
|
2019-08-10 14:45:15 +00:00
|
|
|
for (size_t i = 1; i < expressions->size(); i++) {
|
2019-06-17 16:35:12 +00:00
|
|
|
boundExpressions[i] = this->BindExpression(expressions->at(i));
|
2019-09-01 13:35:45 +00:00
|
|
|
if (boundExpressions[i]->GetType()->operator!=(valueType)) {
|
2019-06-18 14:39:36 +00:00
|
|
|
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidTableValueType,
|
2019-06-17 16:35:12 +00:00
|
|
|
boundExpressions[i]->GetStartPosition(),
|
|
|
|
boundExpressions[i]->GetLength());
|
|
|
|
}
|
2019-06-09 18:15:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
if (valueType == nullptr) {
|
2019-09-01 13:35:45 +00:00
|
|
|
valueType = ScriptType::NilType;
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
2019-07-25 15:23:54 +00:00
|
|
|
auto tableType = std::make_shared<const NumericalTableScriptType>(valueType);
|
2019-06-17 16:35:12 +00:00
|
|
|
return new BoundNumericalTableExpression(boundExpressions, tableType, expression->GetStartPosition(),
|
|
|
|
expression->GetLength());
|
2019-06-09 18:15:09 +00:00
|
|
|
}
|
2019-06-12 13:19:28 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
BoundExpression *Binder::BindTableExpression(const ParsedTableExpression *expression) {
|
2019-06-23 13:58:14 +00:00
|
|
|
auto tableScope = new map<Utilities::HashedString, BoundVariable *>();
|
2019-08-11 10:32:18 +00:00
|
|
|
auto innerScope = new BoundScope(tableScope, nullptr);
|
2019-06-17 16:35:12 +00:00
|
|
|
auto currentScope = this->_scope;
|
|
|
|
this->_scope = innerScope;
|
2019-07-04 16:24:49 +00:00
|
|
|
auto block = dynamic_cast<BoundBlockStatement*>(this->BindBlockStatement(expression->GetBlock()));
|
2019-06-17 16:35:12 +00:00
|
|
|
this->_scope = currentScope;
|
2019-06-12 13:19:28 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
auto tableType = std::make_shared<TableScriptType>(tableScope, innerScope->GetLocalVariableCount());
|
|
|
|
delete innerScope;
|
2019-06-12 13:19:28 +00:00
|
|
|
|
2019-07-04 16:24:49 +00:00
|
|
|
return new BoundTableExpression(block, tableType, expression->GetStartPosition(), expression->GetLength());
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
2019-06-28 11:28:39 +00:00
|
|
|
|
2019-06-12 13:19:28 +00:00
|
|
|
}
|