#include "Binder.hpp" #include BoundScriptStatement *Binder::Bind(Script* script, ParsedScriptStatement *s, BoundScope* scriptScope) { auto binder = Binder(); binder._scriptData = script; binder._scope = scriptScope; auto statements = s->GetStatements(); vector boundStatements (statements.size()); for (int i = 0; i < statements.size(); i++){ boundStatements[i] = binder.BindStatement(statements[i]); } return new BoundScriptStatement(boundStatements, scriptScope->GetDeepestScope()); } Binder::~Binder() { delete _scope; } BoundStatement* Binder::BindStatement(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 ::FunctionDeclaration: return this->BindFunctionDeclarationStatement(statement); case ParsedStatementKind::Return: return this -> BindReturnStatement(statement); case ParsedStatementKind::Conditional: return this -> BindConditionalStatement(statement); case ParsedStatementKind::Bad: return new BoundBadStatement(); } } BoundStatement *Binder::BindBlockStatement(ParsedStatement *statement) { auto statements = ((ParsedBlockStatement*)statement)->GetStatements(); vector boundStatements (statements.size()); this->_scope->GoInnerScope(); for (int i = 0; i < statements.size(); i++){ boundStatements[i] = this -> BindStatement(statements[i]); } this->_scope->GoOuterScope(); return new BoundBlockStatement(boundStatements); } BoundStatement *Binder::BindExpressionStatement(ParsedStatement *statement) { auto exp = ((ParsedExpressionStatement*)statement)->GetExpression(); return new BoundExpressionStatement(this -> BindExpression(exp)); } BoundStatement* Binder::BindAssignmentStatement(ParsedStatement *statement){ auto s = (ParsedAssignmentStatement*) statement; auto boundExpression = this->BindExpression(s->GetExpression()); VariableAssignment assignment = s->IsLocal() ? this->_scope->CreateExplicitLocal(s->GetIdentifier().GetHash(), boundExpression->GetType()) : this->_scope->AssignVariable(s->GetIdentifier().GetHash(), boundExpression->GetType()); if (assignment.GetResult() == VariableAssignmentResult::Ok){ auto key = assignment.GetKey(); return new BoundAssignmentStatement(key, boundExpression); } else{ this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::CantAssignVariable, statement->GetStartPosition(), statement->GetLength()); return new BoundBadStatement(); } } std::shared_ptr ParseTypeIdentifier(HashedString s){ switch (s.GetHash()){ case HashedString::ConstHash("number"): return std::make_shared(false, false); case HashedString::ConstHash("bool"): return std::make_shared(TypeClass::Bool); case HashedString::ConstHash("string"): return std::make_shared(TypeClass::String); default: return std::make_shared(TypeClass::Error); // todo: change to userdata } } BoundStatement *Binder::BindFunctionDeclarationStatement(ParsedStatement *statement) { auto functionStatement = (ParsedFunctionDeclarationStatement*) statement; auto parameters = functionStatement->GetParameters(); auto parameterTypes = new vector>(parameters.size()); auto parameterKeys = new vector>(parameters.size()); this->_scope->GoInnerScope(); for (int i = 0; i < parameters.size(); i++){ auto var = parameters[i]; auto parsedType = ParseTypeIdentifier(var->GetType()); parameterTypes->at(i) = parsedType; auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier().GetHash(), parsedType); if (parameterAssignment.GetResult() == VariableAssignmentResult::Ok){ parameterKeys -> at(i) = std::shared_ptr(parameterAssignment.GetKey()); } else{ //TODO: log error continue; } } auto boundBlock = this -> BindBlockStatement(functionStatement->GetBlock()); this->_scope->GoOuterScope(); auto identifier = functionStatement->GetIdentifier(); auto returnType = std::make_shared(TypeClass::Nil); auto parameterTypesPtr = std::shared_ptr>>(parameterTypes); auto parameterKeysPtr = std::shared_ptr>>(parameterKeys); auto type = make_shared(returnType, parameterTypesPtr, parameterKeysPtr); auto assignment = this->_scope->AssignVariable(identifier.GetHash(), type); if (assignment.GetResult() == VariableAssignmentResult::Ok){ return new BoundFunctionDeclarationStatement(type, assignment.GetKey(), (BoundBlockStatement*)boundBlock); } return new BoundBadStatement(); } BoundStatement *Binder::BindReturnStatement(ParsedStatement* statement){ auto expression = ((ParsedReturnStatement*)statement)->GetExpression(); shared_ptr currentReturnType; if (this->_currentFunction == nullptr){ currentReturnType = this->_scriptData->GetReturnType(); } else{ currentReturnType = this->_currentFunction; } if (expression == nullptr && currentReturnType != nullptr){ this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::InvalidReturnType, statement->GetStartPosition(), statement->GetLength()); return new BoundBadStatement(); } auto boundExpression = this->BindExpression(expression); auto expresionType = boundExpression->GetType(); if (currentReturnType == nullptr){ currentReturnType.swap(expresionType); return new BoundReturnStatement(boundExpression); } if (currentReturnType.get()->operator!=(expresionType.get())){ this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::InvalidReturnType, statement->GetStartPosition(), statement->GetLength()); return new BoundBadStatement(); } return new BoundReturnStatement(boundExpression); } BoundStatement *Binder::BindConditionalStatement(ParsedStatement* statement) { auto conditionalStatement = (ParsedConditionalStatement*)statement; auto boundCondition = this -> BindExpression(conditionalStatement -> GetCondition()); if (boundCondition->GetType() -> GetClass() != TypeClass::Bool){ this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::ConditionNotABool, statement->GetStartPosition(), 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); } BoundExpression* Binder::BindExpression(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()); 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); case ParsedExpressionKind ::Bad: return new BoundBadExpression(expression->GetStartPosition(), expression-> GetLength()); } } BoundExpression* Binder::BindVariableExpression(VariableExpression* expression){ auto key = expression->GetValue(); auto scope = this->_scope->Exists(key.GetHash()); if (scope == -1){ this -> _scriptData -> Diagnostics->LogError(DiagnosticCode::VariableNotFound, expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); } auto var = this->_scope->GetVariable(scope, key.GetHash()); auto type = var->GetType(); return new BoundVariableExpression(scope, key.GetHash(), type, expression->GetStartPosition(), expression->GetLength()); } BoundExpression* Binder::BindBinaryOperator(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){ auto leftNumeric = std::dynamic_pointer_cast(boundLeftType); auto rightNumeric = std::dynamic_pointer_cast(boundRightType); if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){ return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Addition, std::make_shared(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()), expression->GetStartPosition(), expression->GetLength()); } else{ return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Addition, std::make_shared(false, false), expression->GetStartPosition(), expression->GetLength()); } } else if (boundLeftType->GetClass() == TypeClass::String){ return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Concatenation, std::make_shared(TypeClass::String), expression->GetStartPosition(), expression->GetLength()); } break; case BinaryOperatorKind ::Subtraction: if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){ auto leftNumeric = std::dynamic_pointer_cast(boundLeftType); auto rightNumeric = std::dynamic_pointer_cast(boundRightType); if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){ return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Subtraction, std::make_shared(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()), expression->GetStartPosition(), expression->GetLength()); } else{ return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Subtraction, std::make_shared(false, false), expression->GetStartPosition(), expression->GetLength()); } } break; case BinaryOperatorKind ::Multiplication: if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){ auto leftNumeric = std::dynamic_pointer_cast(boundLeftType); auto rightNumeric = std::dynamic_pointer_cast(boundRightType); if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){ return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Multiplication, std::make_shared(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()), expression->GetStartPosition(), expression->GetLength()); } else{ return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Multiplication, std::make_shared(false, false), expression->GetStartPosition(), expression->GetLength()); } } break; case BinaryOperatorKind ::Division: if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){ auto leftNumeric = std::dynamic_pointer_cast(boundLeftType); auto rightNumeric = std::dynamic_pointer_cast(boundRightType); if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){ return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Division, std::make_shared(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()), expression->GetStartPosition(), expression->GetLength()); } else{ return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Division, std::make_shared(false, false), expression->GetStartPosition(), expression->GetLength()); } } break; case BinaryOperatorKind ::Equality: return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Equality, std::make_shared(TypeClass::Bool), expression->GetStartPosition(), expression->GetLength()); case BinaryOperatorKind ::Inequality: return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Inequality, std::make_shared(TypeClass::Bool), 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, std::make_shared(TypeClass::Bool), expression->GetStartPosition(), expression->GetLength()); } case BinaryOperatorKind ::LessOrEquals: if (boundLeft->GetType()->GetClass() == TypeClass::Number && boundRight->GetType()->GetClass() == TypeClass::Number){ return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LessThanEquals, std::make_shared(TypeClass::Bool), expression->GetStartPosition(), expression->GetLength()); } case BinaryOperatorKind ::Greater: if (boundLeft->GetType()->GetClass() == TypeClass::Number && boundRight->GetType()->GetClass() == TypeClass::Number){ return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::GreaterThan, std::make_shared(TypeClass::Bool), expression->GetStartPosition(), expression->GetLength()); } case BinaryOperatorKind ::GreaterOrEquals: if (boundLeft->GetType()->GetClass() == TypeClass::Number && boundRight->GetType()->GetClass() == TypeClass::Number){ return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::GreaterThanEquals, std::make_shared(TypeClass::Bool), expression->GetStartPosition(), expression->GetLength()); } case BinaryOperatorKind ::LogicalAnd: if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool) return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalAnd, std::make_shared(TypeClass::Bool), expression->GetStartPosition(), expression->GetLength()); break; case BinaryOperatorKind ::LogicalOr: if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool) return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalOr, std::make_shared(TypeClass::Bool), expression->GetStartPosition(), expression->GetLength()); break; } this -> _scriptData -> Diagnostics->LogError(DiagnosticCode::NoBinaryOperationFound, expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); } BoundExpression* Binder::BindUnaryOperator(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){ auto innerType = std::dynamic_pointer_cast(operandType); return new BoundUnaryExpression(operand, BoundUnaryOperation::Negation, std::make_shared(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, std::make_shared(TypeClass::Bool), expression->GetStartPosition(), expression->GetLength()); } break; default: break; } this -> _scriptData -> Diagnostics->LogError(DiagnosticCode::NoUnaryOperationFound, expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); } BoundExpression* Binder::BindFunctionCall(FunctionCallExpression* expression){ auto functionExpression = BindExpression(expression->GetFunction()); auto type = functionExpression->GetType(); if (type->GetClass() != TypeClass::Function){ this->_scriptData->Diagnostics->LogError(DiagnosticCode::ExpressionIsNotAFunction, expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); } auto functionType = std::dynamic_pointer_cast(type); auto parameterTypes = functionType->GetParameterTypes(); auto givenParameters = expression->GetParameters(); if (parameterTypes->size() != givenParameters.size()){ this->_scriptData->Diagnostics->LogError(DiagnosticCode::ParameterCountMismatch, expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); } vector boundParameters = vector(givenParameters.size()); for (int i = 0; i < givenParameters.size(); i++){ auto parameter = givenParameters[i]; auto boundParameter = this -> BindExpression(parameter); if (boundParameter->GetType().get()->operator!=(parameterTypes.get()-> at(i).get())){ this->_scriptData->Diagnostics->LogError(DiagnosticCode::ParameterTypeMismatch, parameter->GetStartPosition(), parameter->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); } boundParameters[i] = boundParameter; } 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(indexer->GetType()->GetIndexedType(index->GetType().get())); return new BoundIndexExpression(indexer, index, resultType, expression->GetStartPosition(), expression->GetLength()); }