Large overhaul of pointers to shared_ptrs, implemented function evaluation

This commit is contained in:
2019-06-01 19:20:31 +02:00
parent 8b70eed516
commit 4408cf00cd
17 changed files with 261 additions and 129 deletions

View File

@@ -51,8 +51,8 @@ BoundStatement* Binder::BindAssignmentStatement(ParsedStatement *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());
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);
@@ -63,28 +63,28 @@ BoundStatement* Binder::BindAssignmentStatement(ParsedStatement *statement){
}
}
ScriptType* ParseTypeIdentifier(HashedString s){
std::shared_ptr<ScriptType> ParseTypeIdentifier(HashedString s){
switch (s.GetHash()){
case HashedString::ConstHash("number"): return new NumericScriptType(false, false);
case HashedString::ConstHash("bool"): return new ScriptType(TypeClass::Bool);
case HashedString::ConstHash("string"): return new ScriptType(TypeClass::String);
default: return new ScriptType(TypeClass::Error); // todo: change to userdata
case HashedString::ConstHash("number"): return std::make_shared<NumericScriptType>(false, false);
case HashedString::ConstHash("bool"): return std::make_shared<ScriptType>(TypeClass::Bool);
case HashedString::ConstHash("string"): return std::make_shared<ScriptType>(TypeClass::String);
default: return std::make_shared<ScriptType>(TypeClass::Error); // todo: change to userdata
}
}
BoundStatement *Binder::BindFunctionDeclarationStatement(ParsedStatement *statement) {
auto functionStatement = (ParsedFunctionDeclarationStatement*) statement;
auto parameters = functionStatement->GetParameters();
vector<std::shared_ptr<ScriptType>> parameterTypes = vector<std::shared_ptr<ScriptType>>(parameters.size());
vector<std::shared_ptr<BoundVariableKey>> parameterKeys = vector<std::shared_ptr<BoundVariableKey>>(parameters.size());
auto parameterTypes = new vector<std::shared_ptr<ScriptType>>(parameters.size());
auto parameterKeys = new vector<std::shared_ptr<BoundVariableKey>>(parameters.size());
this->_scope->GoInnerScope();
for (int i = 0; i < parameters.size(); i++){
auto var = parameters[i];
auto parsedType = ParseTypeIdentifier(var->GetType());
parameterTypes[i] = std::shared_ptr<ScriptType>(parsedType);
auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier().GetHash(), *parsedType);
parameterTypes->at(i) = parsedType;
auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier().GetHash(), parsedType);
if (parameterAssignment.GetResult() == VariableAssignmentResult::Ok){
parameterKeys[i] = std::shared_ptr<BoundVariableKey>(parameterAssignment.GetKey());
parameterKeys -> at(i) = std::shared_ptr<BoundVariableKey>(parameterAssignment.GetKey());
}
else{
//TODO: log error
@@ -95,8 +95,10 @@ BoundStatement *Binder::BindFunctionDeclarationStatement(ParsedStatement *statem
this->_scope->GoOuterScope();
auto identifier = functionStatement->GetIdentifier();
auto returnType = std::make_shared<ScriptType>(TypeClass::Nil);
auto type = new FunctionScriptType(returnType, parameterTypes, parameterKeys);
auto assignment = this->_scope->AssignVariable(identifier.GetHash(), *type);
auto parameterTypesPtr = std::shared_ptr<std::vector<std::shared_ptr<ScriptType>>>(parameterTypes);
auto parameterKeysPtr = std::shared_ptr<std::vector<std::shared_ptr<BoundVariableKey>>>(parameterKeys);
auto type = make_shared<FunctionScriptType>(returnType, parameterTypesPtr, parameterKeysPtr);
auto assignment = this->_scope->AssignVariable(identifier.GetHash(), type);
if (assignment.GetResult() == VariableAssignmentResult::Ok){
return new BoundFunctionDeclarationStatement(type, assignment.GetKey(), (BoundBlockStatement*)boundBlock);
}
@@ -123,6 +125,8 @@ BoundExpression* Binder::BindExpression(ParsedExpression* expression){
case ParsedExpressionKind ::Parenthesized:
return BindExpression(((ParenthesizedExpression*)expression)->GetInnerExpression());
case ParsedExpressionKind ::FunctionCall:
return this->BindFunctionCall((FunctionCallExpression*)expression);
case ParsedExpressionKind ::Bad:
return new BoundBadExpression(expression->GetStartPosition(), expression-> GetLength());
@@ -151,84 +155,89 @@ BoundExpression* Binder::BindBinaryOperator(BinaryExpression* expression){
switch (expression->GetOperatorKind()){
case BinaryOperatorKind ::Addition:
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
auto leftNumeric = (NumericScriptType*)boundLeftType;
auto rightNumeric = (NumericScriptType*)boundRightType;
auto leftNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundLeftType);
auto rightNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundRightType);
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Addition, new NumericScriptType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
BoundBinaryOperation::Addition,
std::make_shared<NumericScriptType>(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
expression->GetStartPosition(), expression->GetLength());
}
else{
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Addition, new NumericScriptType(false, false),
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Addition,
std::make_shared<NumericScriptType>(false, false),
expression->GetStartPosition(), expression->GetLength());
}
} else if (boundLeftType->GetClass() == TypeClass::String){
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Concatenation, new ScriptType(TypeClass::String),
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Concatenation, std::make_shared<ScriptType>(TypeClass::String),
expression->GetStartPosition(), expression->GetLength());
}
break;
case BinaryOperatorKind ::Subtraction:
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
auto leftNumeric = (NumericScriptType*)boundLeftType;
auto rightNumeric = (NumericScriptType*)boundRightType;
auto leftNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundLeftType);
auto rightNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundRightType);
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Subtraction,
new NumericScriptType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
std::make_shared<NumericScriptType>(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
expression->GetStartPosition(), expression->GetLength());
}
else{
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Subtraction, new NumericScriptType(false, false),
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Subtraction,
std::make_shared<NumericScriptType>(false, false),
expression->GetStartPosition(), expression->GetLength());
}
}
break;
case BinaryOperatorKind ::Multiplication:
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
auto leftNumeric = (NumericScriptType*)boundLeftType;
auto rightNumeric = (NumericScriptType*)boundRightType;
auto leftNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundLeftType);
auto rightNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundRightType);
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Multiplication,
new NumericScriptType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
std::make_shared<NumericScriptType>(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
expression->GetStartPosition(), expression->GetLength());
}
else{
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Multiplication, new NumericScriptType(false, false),
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Multiplication,
std::make_shared<NumericScriptType>(false, false),
expression->GetStartPosition(), expression->GetLength());
}
}
break;
case BinaryOperatorKind ::Division:
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
auto leftNumeric = (NumericScriptType*)boundLeftType;
auto rightNumeric = (NumericScriptType*)boundRightType;
auto leftNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundLeftType);
auto rightNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundRightType);
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Division,
new NumericScriptType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
std::make_shared<NumericScriptType>(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()),
expression->GetStartPosition(), expression->GetLength());
}
else{
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Division, new NumericScriptType(false, false),
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Division,
std::make_shared<NumericScriptType>(false, false),
expression->GetStartPosition(), expression->GetLength());
}
}
break;
case BinaryOperatorKind ::Equality:
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Equality, new ScriptType(TypeClass::Bool),
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Equality, std::make_shared<ScriptType>(TypeClass::Bool),
expression->GetStartPosition(), expression->GetLength());
case BinaryOperatorKind ::Inequality:
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Inequality, new ScriptType(TypeClass::Bool),
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Inequality, std::make_shared<ScriptType>(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, new ScriptType(TypeClass::Bool),
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalAnd, std::make_shared<ScriptType>(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, new ScriptType(TypeClass::Bool),
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalOr, std::make_shared<ScriptType>(TypeClass::Bool),
expression->GetStartPosition(), expression->GetLength());
break;
}
@@ -248,14 +257,16 @@ BoundExpression* Binder::BindUnaryOperator(UnaryExpression* expression){
break;
case UnaryOperatorKind ::Negation:
if (operandType->GetClass() == TypeClass::Number){
auto innerType = (NumericScriptType*)operandType;
return new BoundUnaryExpression(operand, BoundUnaryOperation::Negation, new NumericScriptType(innerType->IsAwareOfFloat(),
innerType->IsFloat()), expression->GetStartPosition(), expression->GetLength());
auto innerType = std::dynamic_pointer_cast<NumericScriptType>(operandType);
return new BoundUnaryExpression(operand, BoundUnaryOperation::Negation,
std::make_shared<NumericScriptType>(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, new ScriptType(TypeClass::Bool),
return new BoundUnaryExpression(operand, BoundUnaryOperation::LogicalNegation,
std::make_shared<ScriptType>(TypeClass::Bool),
expression->GetStartPosition(), expression->GetLength());
}
break;
@@ -267,4 +278,34 @@ BoundExpression* Binder::BindUnaryOperator(UnaryExpression* expression){
}
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<FunctionScriptType>(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<BoundExpression*> boundParameters = vector<BoundExpression*>(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());
}