diff --git a/src/Binder/Binder.cpp b/src/Binder/Binder.cpp index dbf2c9b..6398acd 100644 --- a/src/Binder/Binder.cpp +++ b/src/Binder/Binder.cpp @@ -106,7 +106,7 @@ namespace Porygon::Binder { auto valueExpression = this->BindExpression(s->GetValueExpression()); auto boundIndexType = indexable->GetType(); if (boundIndexType->GetClass() != TypeClass::Error && - boundIndexType->operator!=(valueExpression->GetType().get())) { + boundIndexType->operator!=(valueExpression->GetType())) { this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidTableValueType, statement->GetStartPosition(), statement->GetLength()); return new BoundBadStatement(); @@ -115,15 +115,15 @@ namespace Porygon::Binder { return new BoundIndexAssignmentStatement(indexable, valueExpression); } - std::shared_ptr ParseTypeIdentifier(const HashedString *s) { + std::shared_ptr ParseTypeIdentifier(const HashedString *s) { auto hash = s->GetHash(); switch (hash) { case HashedString::ConstHash("number"): - return std::make_shared(false, false); + return NumericScriptType::Unaware; case HashedString::ConstHash("bool"): - return std::make_shared(TypeClass::Bool); + return ScriptType::BoolType; case HashedString::ConstHash("string"): - return std::make_shared(false, 0); + return StringScriptType::Dynamic; default: if (!UserData::UserDataStorage::HasUserDataType(hash)) { return nullptr; @@ -135,7 +135,7 @@ namespace Porygon::Binder { BoundStatement *Binder::BindFunctionDeclarationStatement(const ParsedStatement *statement) { auto functionStatement = (ParsedFunctionDeclarationStatement *) statement; auto parameters = functionStatement->GetParameters(); - auto parameterTypes = vector>(parameters->size()); + auto parameterTypes = vector>(parameters->size()); auto parameterKeys = vector>(parameters->size()); this->_scope->GoInnerScope(); @@ -159,7 +159,7 @@ namespace Porygon::Binder { } auto identifier = functionStatement->GetIdentifier(); - auto returnType = make_shared(TypeClass::Nil); + auto returnType = ScriptType::NilType; auto option = new ScriptFunctionOption(returnType, parameterTypes, parameterKeys); this->_currentFunction = option; @@ -211,7 +211,7 @@ namespace Porygon::Binder { statement->GetLength()); return new BoundBadStatement(); } else if (expression == nullptr) { - currentReturnType = make_shared(TypeClass::Nil); + currentReturnType = ScriptType::NilType; return new BoundReturnStatement(nullptr); } auto boundExpression = this->BindExpression(expression); @@ -224,7 +224,7 @@ namespace Porygon::Binder { } return new BoundReturnStatement(boundExpression); } - if (currentReturnType.get()->operator!=(expresionType.get())) { + if (currentReturnType.get()->operator!=(expresionType)) { this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidReturnType, statement->GetStartPosition(), statement->GetLength()); @@ -280,7 +280,7 @@ namespace Porygon::Binder { } this->_scope->GoInnerScope(); - auto variableKey = this->_scope->CreateExplicitLocal(identifier, make_shared(true, false)); + auto variableKey = this->_scope->CreateExplicitLocal(identifier, NumericScriptType::AwareInt); if (variableKey.GetResult() != VariableAssignmentResult::Ok) { this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable, statement->GetStartPosition(), @@ -424,19 +424,18 @@ namespace Porygon::Binder { if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) { return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Addition, - std::make_shared(true, + NumericScriptType::ResolveType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()), expression->GetStartPosition(), expression->GetLength()); } else { return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Addition, - std::make_shared(false, false), + NumericScriptType::Unaware, expression->GetStartPosition(), expression->GetLength()); } } else if (boundLeftType->GetClass() == TypeClass::String) { return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Concatenation, - std::make_shared(false, - 0), + StringScriptType::Dynamic, expression->GetStartPosition(), expression->GetLength()); } break; @@ -447,13 +446,13 @@ namespace Porygon::Binder { if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) { return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Subtraction, - std::make_shared(true, + NumericScriptType::ResolveType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()), expression->GetStartPosition(), expression->GetLength()); } else { return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Subtraction, - std::make_shared(false, false), + NumericScriptType::Unaware, expression->GetStartPosition(), expression->GetLength()); } } @@ -465,13 +464,13 @@ namespace Porygon::Binder { if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) { return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Multiplication, - std::make_shared(true, + NumericScriptType::ResolveType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()), expression->GetStartPosition(), expression->GetLength()); } else { return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Multiplication, - std::make_shared(false, false), + NumericScriptType::Unaware, expression->GetStartPosition(), expression->GetLength()); } } @@ -483,63 +482,63 @@ namespace Porygon::Binder { if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) { return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Division, - std::make_shared(true, + NumericScriptType::ResolveType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()), expression->GetStartPosition(), expression->GetLength()); } else { return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Division, - std::make_shared(false, false), + NumericScriptType::Unaware, expression->GetStartPosition(), expression->GetLength()); } } break; case BinaryOperatorKind::Equality: return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Equality, - std::make_shared(TypeClass::Bool), + ScriptType::BoolType, expression->GetStartPosition(), expression->GetLength()); case BinaryOperatorKind::Inequality: return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Inequality, - std::make_shared(TypeClass::Bool), + ScriptType::BoolType, 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), + ScriptType::BoolType, 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), + ScriptType::BoolType, 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), + ScriptType::BoolType, 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), + ScriptType::BoolType, 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), + ScriptType::BoolType, 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), + ScriptType::BoolType, expression->GetStartPosition(), expression->GetLength()); break; } @@ -563,7 +562,7 @@ namespace Porygon::Binder { if (operandType->GetClass() == TypeClass::Number) { auto innerType = std::dynamic_pointer_cast(operandType); return new BoundUnaryExpression(operand, BoundUnaryOperation::Negation, - std::make_shared( + NumericScriptType::ResolveType( innerType.get()->IsAwareOfFloat(), innerType.get()->IsFloat()), expression->GetStartPosition(), expression->GetLength()); @@ -572,7 +571,7 @@ namespace Porygon::Binder { case UnaryOperatorKind::LogicalNegation: if (operandType->GetClass() == TypeClass::Bool) { return new BoundUnaryExpression(operand, BoundUnaryOperation::LogicalNegation, - std::make_shared(TypeClass::Bool), + ScriptType::BoolType, expression->GetStartPosition(), expression->GetLength()); } break; @@ -778,7 +777,7 @@ namespace Porygon::Binder { valueType = boundExpressions[0]->GetType(); for (size_t i = 1; i < expressions->size(); i++) { boundExpressions[i] = this->BindExpression(expressions->at(i)); - if (boundExpressions[i]->GetType().get()->operator!=(valueType.get())) { + if (boundExpressions[i]->GetType()->operator!=(valueType)) { this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidTableValueType, boundExpressions[i]->GetStartPosition(), boundExpressions[i]->GetLength()); @@ -786,7 +785,7 @@ namespace Porygon::Binder { } } if (valueType == nullptr) { - valueType = std::make_shared(TypeClass::Nil); + valueType = ScriptType::NilType; } auto tableType = std::make_shared(valueType); return new BoundNumericalTableExpression(boundExpressions, tableType, expression->GetStartPosition(), diff --git a/src/Binder/BoundExpressions/BoundExpression.hpp b/src/Binder/BoundExpressions/BoundExpression.hpp index a36be41..60f2391 100644 --- a/src/Binder/BoundExpressions/BoundExpression.hpp +++ b/src/Binder/BoundExpressions/BoundExpression.hpp @@ -79,7 +79,7 @@ namespace Porygon::Binder { const int64_t _value; public: BoundLiteralIntegerExpression(int64_t value, unsigned int start, unsigned int length) - : BoundExpression(start, length, make_shared(true, false)), + : BoundExpression(start, length, NumericScriptType::AwareInt), _value(value) { } @@ -98,7 +98,7 @@ namespace Porygon::Binder { const double _value; public: BoundLiteralFloatExpression(double value, unsigned int start, unsigned int length) - : BoundExpression(start, length, make_shared(true, true)), + : BoundExpression(start, length, NumericScriptType::AwareFloat), _value(value) { } @@ -137,7 +137,7 @@ namespace Porygon::Binder { const bool _value; public: BoundLiteralBoolExpression(bool value, unsigned int start, unsigned int length) - : BoundExpression(start, length, make_shared(TypeClass::Bool)), + : BoundExpression(start, length, ScriptType::BoolType), _value(value) { } @@ -182,7 +182,7 @@ namespace Porygon::Binder { const BoundBinaryOperation _operation; public: BoundBinaryExpression(BoundExpression *left, BoundExpression *right, BoundBinaryOperation op, - shared_ptr result, + shared_ptr result, unsigned int start, unsigned int length) : BoundExpression(start, length, std::move(result)), _left(left), @@ -220,7 +220,7 @@ namespace Porygon::Binder { const BoundExpression *_operand; const BoundUnaryOperation _operation; public: - BoundUnaryExpression(BoundExpression *operand, BoundUnaryOperation op, shared_ptr result, + BoundUnaryExpression(BoundExpression *operand, BoundUnaryOperation op, shared_ptr result, unsigned int start, unsigned int length) : BoundExpression(start, length, std::move(result)), _operand(operand), diff --git a/src/Binder/BoundVariables/BoundScope.cpp b/src/Binder/BoundVariables/BoundScope.cpp index 38c3cf4..e373ea0 100644 --- a/src/Binder/BoundVariables/BoundScope.cpp +++ b/src/Binder/BoundVariables/BoundScope.cpp @@ -101,7 +101,7 @@ namespace Porygon::Binder { } else { // Assigning auto var = this->GetVariable(exists, identifier); - if (var->GetType().get()->operator!=(type.get())) { + if (var->GetType()->operator!=(type)) { return VariableAssignment(VariableAssignmentResult::VariableDefinedWithDifferentType, nullptr); } return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, exists, false)); diff --git a/src/ScriptTypes/FunctionScriptType.hpp b/src/ScriptTypes/FunctionScriptType.hpp index 14d774f..f9cead8 100644 --- a/src/ScriptTypes/FunctionScriptType.hpp +++ b/src/ScriptTypes/FunctionScriptType.hpp @@ -10,10 +10,10 @@ namespace Porygon { class GenericFunctionOption{ shared_ptr _returnType; - vector> _parameterTypes; + vector> _parameterTypes; size_t _option = 0; public: - GenericFunctionOption(shared_ptr returnType, vector> parameterTypes) + GenericFunctionOption(shared_ptr returnType, vector> parameterTypes) : _returnType(std::move(returnType)), _parameterTypes(std::move(parameterTypes)){ } @@ -31,7 +31,7 @@ namespace Porygon { _returnType = t; } - [[nodiscard]] inline vector> GetParameterTypes() const { + [[nodiscard]] inline vector> GetParameterTypes() const { return _parameterTypes; } @@ -45,7 +45,7 @@ namespace Porygon { continue; auto parameter = parameters->at(i); const auto& parameterType = parameter->GetType(); - if (parameterType->operator!=(_parameterTypes[i].get())){ + if (parameterType->operator!=(_parameterTypes[i])){ auto castResult = parameterType->CastableTo(_parameterTypes[i], false); if (castResult == CastResult::InvalidCast){ return false; @@ -125,7 +125,7 @@ namespace Porygon { class ScriptFunctionOption : public GenericFunctionOption { vector> _parameterKeys; public: - ScriptFunctionOption(shared_ptr returnType, vector> parameterTypes, + ScriptFunctionOption(shared_ptr returnType, vector> parameterTypes, vector> parameterKeys) : GenericFunctionOption(move(returnType), std::move(parameterTypes)), _parameterKeys(move(parameterKeys)) { } diff --git a/src/ScriptTypes/ScriptType.cpp b/src/ScriptTypes/ScriptType.cpp index 2c005ae..a568926 100644 --- a/src/ScriptTypes/ScriptType.cpp +++ b/src/ScriptTypes/ScriptType.cpp @@ -6,10 +6,14 @@ namespace Porygon{ return false; } + shared_ptr ScriptType::BoolType = make_shared(TypeClass::Bool); + shared_ptr ScriptType::NilType = make_shared(TypeClass::Nil); + shared_ptr NumericScriptType::AwareInt = make_shared(true, false); + shared_ptr NumericScriptType::AwareFloat = make_shared(true, true); + shared_ptr NumericScriptType::Unaware = make_shared(false, false); + shared_ptr StringScriptType::Dynamic = make_shared(false, 0); + shared_ptr ScriptType::GetIndexedType(const ScriptType*) const{ - if (_class == TypeClass::String){ - return make_shared(TypeClass::String); - } return make_shared(TypeClass::Error); } @@ -18,8 +22,13 @@ namespace Porygon{ return new ScriptType(t); } - ScriptType* CreateNumericScriptType(bool isAware, bool isFloat){ - return new NumericScriptType(isAware, isFloat); + const ScriptType* CreateNumericScriptType(bool isAware, bool isFloat){ + if (isAware){ + if (isFloat) + return NumericScriptType::AwareFloat.get(); + return NumericScriptType::AwareInt.get(); + } + return NumericScriptType::Unaware.get(); } ScriptType* CreateStringScriptType(bool knownAtBind, uint32_t hash){ @@ -27,11 +36,11 @@ namespace Porygon{ } ScriptType* CreateUserDataFunctionScriptType(ScriptType* returnType, ScriptType* parameters[], size_t parameterCount){ - vector> vector(parameterCount); - for (int i = 0; i < parameterCount; i++){ - vector[i] = shared_ptr(parameters[i]); + vector> vector(parameterCount); + for (size_t i = 0; i < parameterCount; i++){ + vector[i] = shared_ptr(parameters[i]); } - auto option = new UserData::UserDataFunctionOption(shared_ptr(returnType), vector); + auto option = new UserData::UserDataFunctionOption(shared_ptr(returnType), vector); auto type = new GenericFunctionScriptType(); type->RegisterFunctionOption(option); return type; diff --git a/src/ScriptTypes/ScriptType.hpp b/src/ScriptTypes/ScriptType.hpp index 0006d25..7a3c3dd 100644 --- a/src/ScriptTypes/ScriptType.hpp +++ b/src/ScriptTypes/ScriptType.hpp @@ -33,24 +33,19 @@ namespace Porygon{ _class = c; } + static shared_ptr BoolType; + static shared_ptr NilType; virtual ~ScriptType() = default; [[nodiscard]] inline TypeClass GetClass() const{ return _class; } - virtual bool operator ==(const ScriptType& b) const{ - return _class == b._class; - }; - - virtual bool operator ==(const ScriptType* b) const{ + virtual bool operator ==(const shared_ptr& b) const{ return _class == b->_class; - }; - - virtual bool operator !=(const ScriptType& b) const{ - return ! (operator==(b)); } - virtual bool operator !=(const ScriptType* b) const{ + + virtual bool operator !=(const shared_ptr& b) const{ return ! (operator==(b)); } @@ -88,7 +83,20 @@ namespace Porygon{ bool _awareOfFloat; // Is this value a float? bool _isFloat; + public: + static shared_ptr AwareInt; + static shared_ptr AwareFloat; + static shared_ptr Unaware; + + static shared_ptr ResolveType(bool isAware, bool isFloat){ + if (isAware){ + if (isFloat) return AwareFloat; + return AwareInt; + } + return Unaware; + } + explicit NumericScriptType(bool floatAware, bool isFloat) : ScriptType(TypeClass::Number){ _awareOfFloat = floatAware; _isFloat = isFloat; @@ -102,22 +110,19 @@ namespace Porygon{ return _isFloat; } - bool operator ==(const ScriptType& b) const final{ - if (b.GetClass() != TypeClass::Number) + bool operator ==(const shared_ptr& b) const final{ + if (b->GetClass() != TypeClass::Number) return false; - auto bNum = dynamic_cast(b); - if (bNum.IsAwareOfFloat() && IsAwareOfFloat()){ - return bNum.IsFloat() == IsFloat(); + auto bNum = dynamic_pointer_cast(b); + if (bNum->IsAwareOfFloat() && IsAwareOfFloat()){ + return bNum->IsFloat() == IsFloat(); } return true; }; - bool operator !=(const ScriptType& b) const final{ + bool operator !=(const shared_ptr& b) const final{ return ! (operator==(b)); } - bool operator !=(const ScriptType* b) const final{ - return ! (operator==(*b)); - } [[nodiscard]] CastResult CastableTo(const shared_ptr& castType, bool explicitCast) const final{ if (!explicitCast){ @@ -140,6 +145,8 @@ namespace Porygon{ _hashValue = hashValue; } + static shared_ptr Dynamic; + [[nodiscard]] bool CanBeIndexedWith(const ScriptType* indexer) const final{ if (indexer -> GetClass() != TypeClass::Number) @@ -149,7 +156,7 @@ namespace Porygon{ } inline shared_ptr GetIndexedType(const ScriptType* indexer) const final{ - return make_shared(false, 0); + return StringScriptType::Dynamic; } [[nodiscard]] @@ -188,7 +195,7 @@ namespace Porygon{ } [[nodiscard]] inline shared_ptr GetIteratorKeyType() const final{ - return make_shared(true, false); + return NumericScriptType::AwareInt; } }; } diff --git a/src/StandardLibraries/BasicLibrary.hpp b/src/StandardLibraries/BasicLibrary.hpp index cc91eb2..3cf0d8a 100644 --- a/src/StandardLibraries/BasicLibrary.hpp +++ b/src/StandardLibraries/BasicLibrary.hpp @@ -34,8 +34,8 @@ namespace Porygon::StandardLibraries{ static shared_ptr GetAssertFuncType(){ return GetFuncType(make_shared(TypeClass::Bool), - {{make_shared(TypeClass::Bool)}, - {make_shared(TypeClass::Bool), make_shared(false, 0)}}); + {{ScriptType::BoolType}, + {ScriptType::BoolType, StringScriptType::Dynamic}}); } //endregion //region Error @@ -46,8 +46,7 @@ namespace Porygon::StandardLibraries{ throw Evaluation::EvaluationException(conv); } static shared_ptr GetErrorFuncType(){ - return GetFuncType(make_shared(TypeClass::Nil), - {{make_shared(false, 0)}}); + return GetFuncType(ScriptType::NilType, {{StringScriptType::Dynamic}}); } //endregion //region Print @@ -58,7 +57,7 @@ namespace Porygon::StandardLibraries{ return new Evaluation::NilEvalValue(); } static shared_ptr GetPrintFuncType(){ - return GetFuncType(make_shared(TypeClass::Nil), {{make_shared(false, 0)}}); + return GetFuncType(ScriptType::NilType, {{StringScriptType::Dynamic}}); } //endregion //region ToInt @@ -69,7 +68,8 @@ namespace Porygon::StandardLibraries{ return new Evaluation::IntegerEvalValue(parsed); } static shared_ptr GetToIntFuncType(){ - return GetFuncType(make_shared(true, false), {{make_shared(false, 0)}}); + return GetFuncType(NumericScriptType::AwareInt, + {{StringScriptType::Dynamic}}); } //endregion //region ToFloat @@ -80,8 +80,8 @@ namespace Porygon::StandardLibraries{ return new Evaluation::FloatEvalValue(parsed); } static shared_ptr GetToFloatFuncType(){ - return GetFuncType(make_shared(true, true), - {{make_shared(false, 0)}}); + return GetFuncType(NumericScriptType::AwareFloat, + {{StringScriptType::Dynamic}}); } //endregion //region ToString @@ -91,8 +91,7 @@ namespace Porygon::StandardLibraries{ return new Evaluation::StringEvalValue(parameter); } static shared_ptr GetToStringFuncType(){ - return GetFuncType(make_shared(false, 0), - {{make_shared(TypeClass::All)}}); + return GetFuncType(StringScriptType::Dynamic, {{make_shared(TypeClass::All)}}); } //endregion //region Type @@ -113,8 +112,7 @@ namespace Porygon::StandardLibraries{ } } static shared_ptr GetTypeFuncType(){ - return GetFuncType(make_shared(false, 0), - {{make_shared(TypeClass::All)}}); + return GetFuncType(StringScriptType::Dynamic,{{make_shared(TypeClass::All)}}); } //endregion //region IsFloat @@ -124,14 +122,14 @@ namespace Porygon::StandardLibraries{ return new Evaluation::BooleanEvalValue(parameter->IsFloat()); } static shared_ptr GetIsFloatFuncType(){ - return GetFuncType(make_shared(TypeClass::Bool), - {{make_shared(false, false)}}); + return GetFuncType(ScriptType::BoolType, {{NumericScriptType::Unaware}}); } //endregion //region Generic handling - static shared_ptr GetFuncType(const shared_ptr& result, const vector>>& options){ + static shared_ptr GetFuncType(const shared_ptr& result, + const vector>>& options){ auto funcType = make_shared(); for (const auto& o: options){ auto option = new UserData::UserDataFunctionOption(result, o); diff --git a/src/StandardLibraries/MathLibrary.hpp b/src/StandardLibraries/MathLibrary.hpp index 7d64749..a9980e7 100644 --- a/src/StandardLibraries/MathLibrary.hpp +++ b/src/StandardLibraries/MathLibrary.hpp @@ -27,9 +27,9 @@ namespace Porygon::StandardLibraries { //region templates -#define FLOAT_TYPE new NumericScriptType(true, true) -#define INTEGER_TYPE new NumericScriptType(true, false) -#define BOOL_TYPE new ScriptType(TypeClass::Bool) +#define FLOAT_TYPE NumericScriptType::AwareFloat +#define INTEGER_TYPE NumericScriptType::AwareInt +#define BOOL_TYPE ScriptType::BoolType #define FUNCTION(fieldName) \ [](void* obj) -> const Porygon::Evaluation::EvalValue* { \ auto t = new Porygon::Evaluation::GenericFunctionEvalValue(make_shared(), \ @@ -150,8 +150,9 @@ namespace Porygon::StandardLibraries { return new BooleanEvalValue(parameter1 < parameter2); } - static UserData::UserDataFunctionOption* CreateFunctionOption(ScriptType* returnType, std::vector params){ - return UserData::UserDataFunctionOption::FromRawPointers(returnType, std::move(params)); + static UserData::UserDataFunctionOption* CreateFunctionOption(const shared_ptr& returnType, + const std::vector>& params){ + return new UserData::UserDataFunctionOption(returnType, params); } diff --git a/src/UserData/UserDataCollections/UserDataCollectionType.hpp b/src/UserData/UserDataCollections/UserDataCollectionType.hpp index da4271b..396740a 100644 --- a/src/UserData/UserDataCollections/UserDataCollectionType.hpp +++ b/src/UserData/UserDataCollections/UserDataCollectionType.hpp @@ -24,11 +24,16 @@ namespace Porygon::UserData { return make_shared(keyType, valueType, true, true); } - static UserDataCollectionType* CreateIndexable(const ScriptType* keyType, const ScriptType* valueType){ - return new UserDataCollectionType(shared_ptr(keyType), + static shared_ptr CreateIndexable(const ScriptType* keyType, const ScriptType* valueType){ + return make_shared(shared_ptr(keyType), shared_ptr(valueType), true, true); } + static UserDataCollectionType* CreateIndexableExtern(const ScriptType* keyType, const ScriptType* valueType){ + return new UserDataCollectionType(shared_ptr(keyType), + shared_ptr(valueType), true, true); + } + static shared_ptr CreateIterable(const shared_ptr& valueType){ return make_shared(nullptr, valueType, false, true); } @@ -42,7 +47,7 @@ namespace Porygon::UserData { if (!_indexable){ return false; } - return indexer->operator==(_keyType.get()); + return indexer->operator==(_keyType); } [[nodiscard]] shared_ptr GetIndexedType(const ScriptType* indexer) const final{ diff --git a/src/UserData/UserDataCollections/UserDataCollectionValue.cpp b/src/UserData/UserDataCollections/UserDataCollectionValue.cpp index 9d61a3a..894b47f 100644 --- a/src/UserData/UserDataCollections/UserDataCollectionValue.cpp +++ b/src/UserData/UserDataCollections/UserDataCollectionValue.cpp @@ -3,13 +3,13 @@ namespace Porygon::UserData{ extern "C" { UserDataCollectionType* CreateCollectionType(const ScriptType* keyType, const ScriptType* valueType){ - return UserDataCollectionType::CreateIndexable(keyType, valueType); + return UserDataCollectionType::CreateIndexableExtern(keyType, valueType); } UserDataCollectionValue* CreateCollectionValue(UserDataCollectionType* type, void* obj, const EvalValue* (*get)(void*, const EvalValue*), void (*set)(void*, const EvalValue*, const EvalValue*), Iterator* (*getIterator)(void*)){ - return new UserDataCollectionValue(type, new UserDataCollectionHelper(obj, get, set, getIterator)); + return new UserDataCollectionValue(shared_ptr(type), new UserDataCollectionHelper(obj, get, set, getIterator)); } } } diff --git a/src/UserData/UserDataCollections/UserDataCollectionValue.hpp b/src/UserData/UserDataCollections/UserDataCollectionValue.hpp index 0fd1f2f..1d382aa 100644 --- a/src/UserData/UserDataCollections/UserDataCollectionValue.hpp +++ b/src/UserData/UserDataCollections/UserDataCollectionValue.hpp @@ -36,18 +36,18 @@ namespace Porygon::UserData { }; class UserDataCollectionValue : public Evaluation::EvalValue{ - shared_ptr _type; + shared_ptr _type; shared_ptr _helper; const size_t _hash; - UserDataCollectionValue(shared_ptr type, + UserDataCollectionValue(shared_ptr type, shared_ptr helper, size_t hash) : _type(std::move(type)), _helper(std::move(helper)), _hash(hash) { } public: - UserDataCollectionValue(ScriptType* type, const UserDataCollectionHelper* helper) - : _type(dynamic_cast(type)), _helper(helper), _hash(Utilities::Random::Get()) + UserDataCollectionValue(shared_ptr type, const UserDataCollectionHelper* helper) + : _type(dynamic_pointer_cast(type)), _helper(helper), _hash(Utilities::Random::Get()) { } diff --git a/src/UserData/UserDataField.hpp b/src/UserData/UserDataField.hpp index 4d527cc..434d310 100644 --- a/src/UserData/UserDataField.hpp +++ b/src/UserData/UserDataField.hpp @@ -3,6 +3,8 @@ #define PORYGONLANG_USERDATAFIELD_HPP +#include + #include "../Evaluator/EvalValues/EvalValue.hpp" #include "../Evaluator/EvalValues/NumericEvalValue.hpp" @@ -12,10 +14,17 @@ namespace Porygon::UserData{ const Evaluation::EvalValue* (*_get)(void* obj); void (*_set)(void* obj, const Evaluation::EvalValue* val); public: - UserDataField(const ScriptType* type, const Evaluation::EvalValue* (*getter)(void* obj), void (*setter)(void* obj, const Evaluation::EvalValue* val)) + UserDataField(const ScriptType* type, const Evaluation::EvalValue* (*getter)(void* obj), + void (*setter)(void* obj, const Evaluation::EvalValue* val)) : _type(shared_ptr(type)), _get(getter), _set(setter){ } + UserDataField(shared_ptr type, const Evaluation::EvalValue* (*getter)(void* obj), + void (*setter)(void* obj, const Evaluation::EvalValue* val)) + : _type(std::move(type)), _get(getter), _set(setter){ + } + + [[nodiscard]] inline shared_ptr GetType(){ return _type; diff --git a/src/UserData/UserDataFunctionType.hpp b/src/UserData/UserDataFunctionType.hpp index b3060dd..c63eaf4 100644 --- a/src/UserData/UserDataFunctionType.hpp +++ b/src/UserData/UserDataFunctionType.hpp @@ -8,15 +8,15 @@ namespace Porygon::UserData{ class UserDataFunctionOption : public GenericFunctionOption{ public: - UserDataFunctionOption(std::shared_ptr returnType, vector> parameterTypes) + UserDataFunctionOption(std::shared_ptr returnType, vector> parameterTypes) : GenericFunctionOption(std::move(returnType), std::move(parameterTypes)) { } - static UserDataFunctionOption* FromRawPointers(ScriptType* returnType, vector parameterTypes){ - auto rt = shared_ptr(returnType); - auto p = vector>(parameterTypes.size()); + static UserDataFunctionOption* FromRawPointers(const ScriptType* returnType, vector parameterTypes){ + auto rt = shared_ptr(returnType); + auto p = vector>(parameterTypes.size()); for (size_t i = 0; i < parameterTypes.size(); i++){ - p[i] = shared_ptr(parameterTypes[i]); + p[i] = shared_ptr(parameterTypes[i]); } return new UserDataFunctionOption(rt, p); } diff --git a/src/UserData/UserDataTemplates.hpp b/src/UserData/UserDataTemplates.hpp index ded2940..8909f67 100644 --- a/src/UserData/UserDataTemplates.hpp +++ b/src/UserData/UserDataTemplates.hpp @@ -25,11 +25,11 @@ fields \ PORYGON_USERDATA_END() -#define PORYGON_INTEGER_TYPE ((Porygon::ScriptType*)new Porygon::NumericScriptType(true, false)) -#define PORYGON_FLOAT_TYPE ((Porygon::ScriptType*)new Porygon::NumericScriptType(true, true)) +#define PORYGON_INTEGER_TYPE (NumericScriptType::AwareInt) +#define PORYGON_FLOAT_TYPE (NumericScriptType::AwareFloat) #define PORYGON_STRING_TYPE ((Porygon::ScriptType*)new Porygon::StringScriptType(false, 0)) #define PORYGON_INDEXABLE_TYPE(keyType, valueType) \ - ((Porygon::ScriptType*)Porygon::UserData::UserDataCollectionType::CreateIndexable(keyType, valueType)) + (Porygon::UserData::UserDataCollectionType::CreateIndexable(keyType, valueType)) #define PORYGON_FIELD(fieldName, fieldType, getterHelper, setterHelper) \ { \ @@ -101,7 +101,7 @@ Porygon::Utilities::HashedString::ConstHash(#fieldName), \ new Porygon::UserData::UserDataField( \ new Porygon::GenericFunctionScriptType( \ - Porygon::UserData::UserDataFunctionOption::FromRawPointers(returnType, {__VA_ARGS__} )), \ + new Porygon::UserData::UserDataFunctionOption(returnType, {__VA_ARGS__} )), \ \ \ [](void* obj) -> const Porygon::Evaluation::EvalValue* { \ @@ -117,7 +117,7 @@ #define PORYGON_INTEGER_FUNCTION(fieldName, ...) \ - PORYGON_FUNCTION(fieldName, new Porygon::NumericScriptType(true, false), __VA_ARGS__ ) + PORYGON_FUNCTION(fieldName, Porygon::NumericScriptType::AwareInt, __VA_ARGS__ ) /*!