From e2a0c359925e2615ef5d7d13637ece2af17b7f05 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 25 Jul 2019 17:23:54 +0200 Subject: [PATCH] Large cleanup --- src/Binder/Binder.cpp | 68 ++++++----- .../BoundExpressions/BoundExpression.hpp | 100 ++++++++++------ .../BoundFunctionCallExpression.hpp | 8 +- .../BoundExpressions/BoundTableExpression.hpp | 6 +- .../BoundFunctionDeclarationStatement.hpp | 14 ++- src/Binder/BoundStatements/BoundStatement.hpp | 75 ++++++++---- src/Binder/BoundVariables/BoundScope.cpp | 10 +- src/Binder/BoundVariables/BoundScope.hpp | 8 +- src/Binder/BoundVariables/BoundVariable.hpp | 8 +- .../BoundVariables/BoundVariableKey.hpp | 16 ++- .../VariableAssigmentResult.hpp | 6 +- src/Diagnostics/Diagnostic.hpp | 10 +- src/Diagnostics/DiagnosticCode.hpp | 2 +- src/Diagnostics/DiagnosticsHolder.cpp | 20 ++-- src/Diagnostics/DiagnosticsHolder.hpp | 9 +- src/Evaluator/BinaryEvaluation.cpp | 6 +- src/Evaluator/EvalValues/EvalValue.cpp | 12 +- src/Evaluator/EvalValues/EvalValue.hpp | 51 +++++--- src/Evaluator/EvalValues/EvalValueHelper.hpp | 2 - src/Evaluator/EvalValues/NilEvalValue.hpp | 12 +- src/Evaluator/EvalValues/NumericEvalValue.cpp | 52 ++++----- src/Evaluator/EvalValues/NumericEvalValue.hpp | 69 ++++++----- .../EvalValues/NumericalTableEvalValue.hpp | 30 +++-- .../EvalValues/ScriptFunctionEvalValue.hpp | 42 ++++--- src/Evaluator/EvalValues/StringEvalValue.hpp | 18 +-- src/Evaluator/EvalValues/TableEvalValue.hpp | 39 ++++--- .../EvaluationScope/EvaluationScope.cpp | 20 ++-- .../EvaluationScope/EvaluationScope.hpp | 12 +- src/Evaluator/Evaluator.cpp | 110 +++++++++--------- src/Evaluator/Evaluator.hpp | 52 ++++----- .../Iterator/NumericalKeyIterator.hpp | 2 +- src/Evaluator/Iterator/SimpleKeyIterator.hpp | 4 +- src/Evaluator/UnaryEvaluation.cpp | 4 +- src/FunctionScriptType.hpp | 44 +++---- src/Parser/Lexer.cpp | 5 +- src/Parser/Parser.cpp | 1 - src/Parser/Parser.hpp | 3 - src/Parser/Token.hpp | 3 - src/Parser/TypedVariableIdentifier.hpp | 12 +- src/Script.cpp | 21 ++-- src/Script.hpp | 18 +-- src/ScriptType.cpp | 4 +- src/ScriptType.hpp | 54 +++++---- src/StandardLibraries/BasicLibrary.hpp | 14 +-- src/TableScriptType.hpp | 17 ++- src/UserData/UserData.hpp | 6 +- src/UserData/UserDataField.cpp | 4 +- src/UserData/UserDataField.hpp | 20 ++-- src/UserData/UserDataFunction.cpp | 2 +- src/UserData/UserDataFunction.hpp | 16 ++- src/UserData/UserDataFunctionType.hpp | 7 +- src/UserData/UserDataOperation.hpp | 10 +- src/UserData/UserDataScriptType.hpp | 14 +-- src/UserData/UserDataTemplates.hpp | 14 +-- src/UserData/UserDataValue.hpp | 22 ++-- src/Utilities/Random.cpp | 3 + src/Utilities/Random.hpp | 20 ++++ tests/parser/ParserTests.cpp | 8 +- 58 files changed, 700 insertions(+), 539 deletions(-) create mode 100644 src/Utilities/Random.cpp create mode 100644 src/Utilities/Random.hpp diff --git a/src/Binder/Binder.cpp b/src/Binder/Binder.cpp index 58abb00..6af3e19 100644 --- a/src/Binder/Binder.cpp +++ b/src/Binder/Binder.cpp @@ -5,7 +5,6 @@ #include "BoundExpressions/BoundTableExpression.hpp" #include "BoundExpressions/BoundFunctionCallExpression.hpp" #include "../UserData/UserDataScriptType.hpp" -#include "../FunctionScriptType.hpp" using namespace Porygon::Parser; @@ -16,7 +15,7 @@ namespace Porygon::Binder { binder._scope = scriptScope; auto statements = s->GetStatements(); - vector boundStatements(statements->size()); + vector boundStatements(statements->size()); for (int i = 0; i < statements->size(); i++) { boundStatements[i] = binder.BindStatement(statements->at(i)); } @@ -62,7 +61,7 @@ namespace Porygon::Binder { BoundStatement *Binder::BindBlockStatement(const ParsedStatement *statement) { auto statements = ((ParsedBlockStatement *) statement)->GetStatements(); - vector boundStatements(statements->size()); + vector boundStatements(statements->size()); this->_scope->GoInnerScope(); for (int i = 0; i < statements->size(); i++) { boundStatements[i] = this->BindStatement(statements->at(i)); @@ -114,8 +113,8 @@ namespace Porygon::Binder { return new BoundIndexAssignmentStatement(indexable, valueExpression); } - std::shared_ptr ParseTypeIdentifier(const HashedString& s) { - auto hash = s.GetHash(); + std::shared_ptr ParseTypeIdentifier(const HashedString* s) { + auto hash = s->GetHash(); switch (hash) { case HashedString::ConstHash("number"): return std::make_shared(false, false); @@ -135,11 +134,10 @@ namespace Porygon::Binder { auto functionStatement = (ParsedFunctionDeclarationStatement *) statement; auto parameters = functionStatement->GetParameters(); auto parameterTypes = vector>(parameters->size()); - auto parameterKeys = vector>(parameters->size()); + auto parameterKeys = vector>(parameters->size()); - auto scopeIndex = this->_scope->GetCurrentScope(); this->_scope->GoInnerScope(); - for (int i = 0; i < parameters->size(); i++) { + for (long i = 0; i < parameters->size(); i++) { auto var = parameters->at(i); auto parsedType = ParseTypeIdentifier(var->GetType()); if (parsedType == nullptr) { @@ -148,9 +146,9 @@ namespace Porygon::Binder { return new BoundBadStatement(); } parameterTypes.at(i) = parsedType; - auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier(), parsedType); + auto parameterAssignment = this->_scope->CreateExplicitLocal(*var->GetIdentifier(), parsedType); if (parameterAssignment.GetResult() == VariableAssignmentResult::Ok) { - parameterKeys.at(i) = std::shared_ptr(parameterAssignment.GetKey()); + parameterKeys.at(i) = std::shared_ptr(parameterAssignment.GetKey()); } else { //TODO: log error continue; @@ -162,9 +160,9 @@ namespace Porygon::Binder { auto option = new ScriptFunctionOption(returnType, parameterTypes, parameterKeys); this->_currentFunction = option; - shared_ptr type; + shared_ptr type; auto scope = this -> _scope -> Exists(identifier); - BoundVariableKey* assignmentKey; + const BoundVariableKey* assignmentKey; if (scope >= 0){ auto var = this -> _scope -> GetVariable(scope, identifier); auto varType =var->GetType(); @@ -172,11 +170,11 @@ namespace Porygon::Binder { this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable, statement->GetStartPosition(), statement->GetLength()); } - type = dynamic_pointer_cast(varType); + type = dynamic_pointer_cast(varType); type->RegisterFunctionOption(option); assignmentKey = new BoundVariableKey(identifier, scope, false); } else{ - type = make_shared(); + type = make_shared(); type->RegisterFunctionOption(option); auto assignment = this->_scope->AssignVariable(identifier, type); if (assignment.GetResult() != VariableAssignmentResult::Ok) { @@ -195,7 +193,7 @@ namespace Porygon::Binder { BoundStatement *Binder::BindReturnStatement(const ParsedStatement *statement) { auto expression = ((ParsedReturnStatement *) statement)->GetExpression(); - shared_ptr currentReturnType; + shared_ptr currentReturnType; if (this->_currentFunction == nullptr) { currentReturnType = this->_scriptData->GetReturnType(); } else { @@ -303,7 +301,7 @@ namespace Porygon::Binder { auto valueIdentifier = genericFor -> GetValueIdentifier(); auto isValueVariableDefined = valueIdentifier.GetHash() != 0; - BoundVariableKey* valueVariable = nullptr; + const BoundVariableKey* valueVariable = nullptr; if (isValueVariableDefined){ auto valueType = itType -> GetIndexedType(keyType.get()); auto valueVariableAssignment = this -> _scope -> CreateExplicitLocal(valueIdentifier, valueType); @@ -403,8 +401,8 @@ namespace Porygon::Binder { 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); + auto leftNumeric = std::static_pointer_cast(boundLeftType); + auto rightNumeric = std::static_pointer_cast(boundRightType); if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) { return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Addition, @@ -426,8 +424,8 @@ namespace Porygon::Binder { 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); + 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, @@ -444,8 +442,8 @@ namespace Porygon::Binder { 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); + 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, @@ -462,8 +460,8 @@ namespace Porygon::Binder { 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); + 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, @@ -544,7 +542,7 @@ namespace Porygon::Binder { break; case UnaryOperatorKind::Negation: if (operandType->GetClass() == TypeClass::Number) { - auto innerType = std::dynamic_pointer_cast(operandType); + auto innerType = std::dynamic_pointer_cast(operandType); return new BoundUnaryExpression(operand, BoundUnaryOperation::Negation, std::make_shared( innerType.get()->IsAwareOfFloat(), @@ -577,11 +575,11 @@ namespace Porygon::Binder { expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); } - auto functionType = std::dynamic_pointer_cast(type); + auto functionType = std::dynamic_pointer_cast(type); auto givenParameters = expression->GetParameters(); - auto givenParameterTypes = vector>(givenParameters->size()); + auto givenParameterTypes = vector>(givenParameters->size()); vector boundParameters = vector(givenParameters->size()); - for (int i = 0; i < givenParameters->size(); i++){ + for (long i = 0; i < givenParameters->size(); i++){ boundParameters[i] = this -> BindExpression(givenParameters->at(i)); givenParameterTypes[i] = boundParameters[i]->GetType(); } @@ -609,8 +607,8 @@ namespace Porygon::Binder { return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); } if (indexerType->GetClass() == TypeClass::UserData) { - auto stringKey = dynamic_pointer_cast(index->GetType()); - auto field = dynamic_pointer_cast(indexerType)->GetField(stringKey->GetHashValue()); + auto stringKey = dynamic_pointer_cast(index->GetType()); + auto field = dynamic_pointer_cast(indexerType)->GetField(stringKey->GetHashValue()); if (!setter) { if (!field->HasGetter()) { this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UserDataFieldNoGetter, @@ -643,7 +641,7 @@ namespace Porygon::Binder { return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); } if (indexerType->GetClass() == TypeClass::UserData) { - auto field = dynamic_pointer_cast(indexerType)->GetField(identifier.GetHash()); + auto field = dynamic_pointer_cast(indexerType)->GetField(identifier.GetHash()); if (!setter) { if (!field->HasGetter()) { this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UserDataFieldNoGetter, @@ -670,11 +668,11 @@ namespace Porygon::Binder { BoundExpression *Binder::BindNumericalTableExpression(const ParsedNumericalTableExpression *expression) { auto expressions = expression->GetExpressions(); auto boundExpressions = vector(expressions->size()); - shared_ptr valueType = nullptr; + shared_ptr valueType = nullptr; if (!boundExpressions.empty()) { boundExpressions[0] = this->BindExpression(expressions->at(0)); valueType = boundExpressions[0]->GetType(); - for (int i = 1; i < expressions->size(); i++) { + for (long i = 1; i < expressions->size(); i++) { boundExpressions[i] = this->BindExpression(expressions->at(i)); if (boundExpressions[i]->GetType().get()->operator!=(valueType.get())) { this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidTableValueType, @@ -684,9 +682,9 @@ namespace Porygon::Binder { } } if (valueType == nullptr) { - valueType = std::make_shared(TypeClass::Nil); + valueType = std::make_shared(TypeClass::Nil); } - auto tableType = std::make_shared(valueType); + auto tableType = std::make_shared(valueType); return new BoundNumericalTableExpression(boundExpressions, tableType, expression->GetStartPosition(), expression->GetLength()); } diff --git a/src/Binder/BoundExpressions/BoundExpression.hpp b/src/Binder/BoundExpressions/BoundExpression.hpp index e7b219b..69c5e82 100644 --- a/src/Binder/BoundExpressions/BoundExpression.hpp +++ b/src/Binder/BoundExpressions/BoundExpression.hpp @@ -12,7 +12,7 @@ using namespace std; namespace Porygon::Binder { - enum class BoundExpressionKind { + enum class BoundExpressionKind : u_int8_t { Bad, LiteralInteger, @@ -33,9 +33,9 @@ namespace Porygon::Binder { class BoundExpression { const unsigned int _start; const unsigned int _length; - const shared_ptr _type; + const shared_ptr _type; public: - BoundExpression(unsigned int start, unsigned int length, shared_ptr type) + BoundExpression(unsigned int start, unsigned int length, shared_ptr type) : _start(start), _length(length), _type(std::move(type)) { @@ -43,23 +43,23 @@ namespace Porygon::Binder { virtual ~BoundExpression() = default; - virtual const BoundExpressionKind GetKind() const = 0; + [[nodiscard]] + virtual BoundExpressionKind GetKind() const = 0; - virtual const std::shared_ptr &GetType() const { + [[nodiscard]] + virtual const std::shared_ptr &GetType() const { return _type; }; - inline const unsigned int GetStartPosition() const { + [[nodiscard]] + inline unsigned int GetStartPosition() const { return _start; } - inline const unsigned int GetLength() const { + [[nodiscard]] + inline unsigned int GetLength() const { return _length; } - - inline const unsigned int GetEndPosition() const { - return _start + _length - 1; - } }; class BoundBadExpression : public BoundExpression { @@ -68,7 +68,8 @@ namespace Porygon::Binder { make_shared( TypeClass::Error)) {} - inline const BoundExpressionKind GetKind() const final { + [[nodiscard]] + inline BoundExpressionKind GetKind() const final { return BoundExpressionKind::Bad; } }; @@ -81,11 +82,13 @@ namespace Porygon::Binder { _value(value) { } - inline const BoundExpressionKind GetKind() const final { + [[nodiscard]] + inline BoundExpressionKind GetKind() const final { return BoundExpressionKind::LiteralInteger; } - inline const long GetValue() const { + [[nodiscard]] + inline long GetValue() const { return _value; } }; @@ -98,11 +101,13 @@ namespace Porygon::Binder { _value(value) { } - inline const BoundExpressionKind GetKind() const final { + [[nodiscard]] + inline BoundExpressionKind GetKind() const final { return BoundExpressionKind::LiteralFloat; } - inline const double GetValue() const { + [[nodiscard]] + inline double GetValue() const { return _value; } }; @@ -116,12 +121,14 @@ namespace Porygon::Binder { _value(value) { } - inline const BoundExpressionKind GetKind() const final { + [[nodiscard]] + inline BoundExpressionKind GetKind() const final { return BoundExpressionKind::LiteralString; } - inline const u16string GetValue() const { - return _value; + [[nodiscard]] + inline const u16string* GetValue() const { + return &_value; } }; @@ -133,11 +140,13 @@ namespace Porygon::Binder { _value(value) { } - inline const BoundExpressionKind GetKind() const final { + [[nodiscard]] + inline BoundExpressionKind GetKind() const final { return BoundExpressionKind::LiteralBool; } - inline const bool GetValue() const { + [[nodiscard]] + inline bool GetValue() const { return _value; } }; @@ -145,9 +154,9 @@ namespace Porygon::Binder { class BoundVariableExpression : public BoundExpression { const BoundVariableKey *_key; public: - BoundVariableExpression(BoundVariableKey *key, shared_ptr type, unsigned int start, + BoundVariableExpression(BoundVariableKey *key, const shared_ptr& type, unsigned int start, unsigned int length) - : BoundExpression(start, length, std::move(type)), + : BoundExpression(start, length, type), _key(key) { } @@ -155,10 +164,12 @@ namespace Porygon::Binder { delete _key; } - inline const BoundExpressionKind GetKind() const final { + [[nodiscard]] + inline BoundExpressionKind GetKind() const final { return BoundExpressionKind::Variable; } + [[nodiscard]] inline const BoundVariableKey *GetKey() const { return _key; } @@ -183,19 +194,23 @@ namespace Porygon::Binder { delete _right; } - inline const BoundExpressionKind GetKind() const final { + [[nodiscard]] + inline BoundExpressionKind GetKind() const final { return BoundExpressionKind::Binary; } + [[nodiscard]] inline const BoundExpression *GetLeft() const { return _left; } + [[nodiscard]] inline const BoundExpression *GetRight() const { return _right; } - inline const BoundBinaryOperation GetOperation() const { + [[nodiscard]] + inline BoundBinaryOperation GetOperation() const { return _operation; } }; @@ -215,15 +230,18 @@ namespace Porygon::Binder { delete _operand; } - inline const BoundExpressionKind GetKind() const final { + [[nodiscard]] + inline BoundExpressionKind GetKind() const final { return BoundExpressionKind::Unary; } + [[nodiscard]] inline const BoundExpression *GetOperand() const { return _operand; } - inline const BoundUnaryOperation GetOperation() const { + [[nodiscard]] + inline BoundUnaryOperation GetOperation() const { return _operation; } }; @@ -233,7 +251,7 @@ namespace Porygon::Binder { const BoundExpression *_indexExpression; public: BoundIndexExpression(BoundExpression *indexableExpression, BoundExpression *indexExpression, - shared_ptr result, + shared_ptr result, unsigned int start, unsigned int length) : BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression), _indexExpression(indexExpression) {} @@ -243,14 +261,17 @@ namespace Porygon::Binder { delete _indexExpression; } - inline const BoundExpressionKind GetKind() const final { + [[nodiscard]] + inline BoundExpressionKind GetKind() const final { return BoundExpressionKind::Index; } + [[nodiscard]] inline const BoundExpression *GetIndexableExpression() const { return _indexableExpression; } + [[nodiscard]] inline const BoundExpression *GetIndexExpression() const { return _indexExpression; } @@ -260,8 +281,8 @@ namespace Porygon::Binder { const BoundExpression *_indexableExpression; const Utilities::HashedString _index; public: - BoundPeriodIndexExpression(BoundExpression *indexableExpression, Utilities::HashedString index, - shared_ptr result, + BoundPeriodIndexExpression(BoundExpression *indexableExpression, const Utilities::HashedString& index, + shared_ptr result, unsigned int start, unsigned int length) : BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression), _index(index) {} @@ -270,23 +291,26 @@ namespace Porygon::Binder { delete _indexableExpression; } - inline const BoundExpressionKind GetKind() const final { + [[nodiscard]] + inline BoundExpressionKind GetKind() const final { return BoundExpressionKind::PeriodIndex; } + [[nodiscard]] inline const BoundExpression *GetIndexableExpression() const { return _indexableExpression; } - inline const Utilities::HashedString GetIndex() const { - return _index; + [[nodiscard]] + inline const Utilities::HashedString* GetIndex() const { + return &_index; } }; class BoundNumericalTableExpression : public BoundExpression { const vector _expressions; public: - BoundNumericalTableExpression(vector expressions, shared_ptr type, + BoundNumericalTableExpression(vector expressions, shared_ptr type, unsigned int start, unsigned int length) : BoundExpression(start, length, std::move(type)), _expressions(std::move(expressions)) {} @@ -297,10 +321,12 @@ namespace Porygon::Binder { } } - inline const BoundExpressionKind GetKind() const final { + [[nodiscard]] + inline BoundExpressionKind GetKind() const final { return BoundExpressionKind::NumericalTable; } + [[nodiscard]] inline const vector *GetExpressions() const { return &_expressions; } diff --git a/src/Binder/BoundExpressions/BoundFunctionCallExpression.hpp b/src/Binder/BoundExpressions/BoundFunctionCallExpression.hpp index bd49cee..7342bf4 100644 --- a/src/Binder/BoundExpressions/BoundFunctionCallExpression.hpp +++ b/src/Binder/BoundExpressions/BoundFunctionCallExpression.hpp @@ -10,7 +10,7 @@ namespace Porygon::Binder { const Porygon::GenericFunctionOption *_option; public: BoundFunctionCallExpression(BoundExpression *functionExpression, vector parameters, - Porygon::GenericFunctionOption *option, shared_ptr result, + Porygon::GenericFunctionOption *option, shared_ptr result, unsigned int start, unsigned int length) : BoundExpression(start, length, move(result)), _functionExpression(functionExpression), _parameters(move(parameters)), _option(option) {} @@ -22,18 +22,22 @@ namespace Porygon::Binder { } } - inline const Porygon::Binder::BoundExpressionKind GetKind() const final { + [[nodiscard]] + inline Porygon::Binder::BoundExpressionKind GetKind() const final { return Porygon::Binder::BoundExpressionKind::FunctionCall; } + [[nodiscard]] inline const BoundExpression *GetFunctionExpression() const { return _functionExpression; } + [[nodiscard]] inline const vector *GetParameters() const { return &_parameters; } + [[nodiscard]] inline const Porygon::GenericFunctionOption *GetFunctionOption() const { return _option; } diff --git a/src/Binder/BoundExpressions/BoundTableExpression.hpp b/src/Binder/BoundExpressions/BoundTableExpression.hpp index 403cae3..547e913 100644 --- a/src/Binder/BoundExpressions/BoundTableExpression.hpp +++ b/src/Binder/BoundExpressions/BoundTableExpression.hpp @@ -1,9 +1,9 @@ -#include #ifndef PORYGONLANG_BOUNDTABLEEXPRESSION_HPP #define PORYGONLANG_BOUNDTABLEEXPRESSION_HPP +#include #include "../BoundStatements/BoundStatement.hpp" namespace Porygon::Binder { @@ -20,10 +20,12 @@ namespace Porygon::Binder { delete _block; } - inline const BoundExpressionKind GetKind() const final { + [[nodiscard]] + inline BoundExpressionKind GetKind() const final { return BoundExpressionKind::Table; } + [[nodiscard]] inline const BoundBlockStatement *GetBlock() const { return _block; } diff --git a/src/Binder/BoundStatements/BoundFunctionDeclarationStatement.hpp b/src/Binder/BoundStatements/BoundFunctionDeclarationStatement.hpp index 4675fa1..e8f6a7c 100644 --- a/src/Binder/BoundStatements/BoundFunctionDeclarationStatement.hpp +++ b/src/Binder/BoundStatements/BoundFunctionDeclarationStatement.hpp @@ -10,9 +10,9 @@ namespace Porygon::Binder { class BoundFunctionDeclarationStatement : public BoundStatement { const BoundVariableKey *_key; const std::shared_ptr _block; - const std::shared_ptr _type; + const std::shared_ptr _type; public: - BoundFunctionDeclarationStatement(std::shared_ptr type, BoundVariableKey *key, + BoundFunctionDeclarationStatement(std::shared_ptr type, const BoundVariableKey *key, BoundBlockStatement *block) : _key(key), _block(block), _type(std::move(type)) { } @@ -21,19 +21,23 @@ namespace Porygon::Binder { delete _key; } - inline const BoundStatementKind GetKind() const final { + [[nodiscard]] + inline BoundStatementKind GetKind() const final { return BoundStatementKind::FunctionDeclaration; } + [[nodiscard]] inline const BoundVariableKey *GetKey() const { return _key; } - inline const std::shared_ptr GetBlock() const { + [[nodiscard]] + inline std::shared_ptr GetBlock() const { return _block; } - inline const std::shared_ptr GetType() const { + [[nodiscard]] + inline std::shared_ptr GetType() const { return _type; } }; diff --git a/src/Binder/BoundStatements/BoundStatement.hpp b/src/Binder/BoundStatements/BoundStatement.hpp index 7b95841..8cdb96f 100644 --- a/src/Binder/BoundStatements/BoundStatement.hpp +++ b/src/Binder/BoundStatements/BoundStatement.hpp @@ -10,7 +10,7 @@ using namespace std; namespace Porygon::Binder { - enum class BoundStatementKind { + enum class BoundStatementKind : u_int8_t { Bad, Break, Script, @@ -28,29 +28,32 @@ namespace Porygon::Binder { class BoundStatement { public: - virtual const BoundStatementKind GetKind() const = 0; + [[nodiscard]] + virtual BoundStatementKind GetKind() const = 0; virtual ~BoundStatement() = default; }; class BoundBadStatement : public BoundStatement { public: - inline const BoundStatementKind GetKind() const final { + [[nodiscard]] + inline BoundStatementKind GetKind() const final { return BoundStatementKind::Bad; } }; class BoundBreakStatement : public BoundStatement { public: - inline const BoundStatementKind GetKind() const final { + [[nodiscard]] + inline BoundStatementKind GetKind() const final { return BoundStatementKind::Break; } }; class BoundBlockStatement : public BoundStatement { - const vector _statements; + const vector _statements; public: - explicit BoundBlockStatement(vector statements) + explicit BoundBlockStatement(vector statements) : _statements(std::move(statements)) { } @@ -60,11 +63,13 @@ namespace Porygon::Binder { } } - inline const BoundStatementKind GetKind() const override { + [[nodiscard]] + inline BoundStatementKind GetKind() const override { return BoundStatementKind::Block; } - inline const vector *GetStatements() const { + [[nodiscard]] + inline const vector *GetStatements() const { return &_statements; } }; @@ -72,16 +77,18 @@ namespace Porygon::Binder { class BoundScriptStatement : public BoundBlockStatement { const int _localVariableCount; public: - explicit BoundScriptStatement(vector statements, int localVariableCount) + explicit BoundScriptStatement(vector statements, int localVariableCount) : BoundBlockStatement(std::move(statements)), _localVariableCount(localVariableCount) { } - inline const BoundStatementKind GetKind() const final { + [[nodiscard]] + inline BoundStatementKind GetKind() const final { return BoundStatementKind::Script; } - inline const int GetLocalVariableCount() const { + [[nodiscard]] + inline int GetLocalVariableCount() const { return _localVariableCount; } }; @@ -98,10 +105,12 @@ namespace Porygon::Binder { delete _expression; } - inline const BoundStatementKind GetKind() const final { + [[nodiscard]] + inline BoundStatementKind GetKind() const final { return BoundStatementKind::Expression; } + [[nodiscard]] inline const BoundExpression *GetExpression() const { return _expression; } @@ -111,7 +120,7 @@ namespace Porygon::Binder { const BoundVariableKey *_key; const BoundExpression *_expression; public: - BoundAssignmentStatement(BoundVariableKey *key, BoundExpression *expression) + BoundAssignmentStatement(const BoundVariableKey *key, BoundExpression *expression) : _key(key), _expression(expression) { } @@ -120,14 +129,17 @@ namespace Porygon::Binder { delete _expression; } - inline const BoundStatementKind GetKind() const final { + [[nodiscard]] + inline BoundStatementKind GetKind() const final { return BoundStatementKind::Assignment; } + [[nodiscard]] inline const BoundVariableKey *GetKey() const { return _key; } + [[nodiscard]] inline const BoundExpression *GetExpression() const { return _expression; } @@ -146,14 +158,17 @@ namespace Porygon::Binder { delete _valueExpression; } - inline const BoundStatementKind GetKind() const final { + [[nodiscard]] + inline BoundStatementKind GetKind() const final { return BoundStatementKind::IndexAssignment; } + [[nodiscard]] inline const BoundExpression *GetIndexExpression() const { return _indexExpression; } + [[nodiscard]] inline const BoundExpression *GetValueExpression() const { return _valueExpression; } @@ -170,10 +185,12 @@ namespace Porygon::Binder { delete _expression; } - inline const BoundStatementKind GetKind() const final { + [[nodiscard]] + inline BoundStatementKind GetKind() const final { return BoundStatementKind::Return; } + [[nodiscard]] inline const BoundExpression *GetExpression() const { return _expression; } @@ -194,18 +211,22 @@ namespace Porygon::Binder { delete _elseStatement; } - inline const BoundStatementKind GetKind() const final { + [[nodiscard]] + inline BoundStatementKind GetKind() const final { return BoundStatementKind::Conditional; } + [[nodiscard]] inline const BoundExpression *GetCondition() const { return _condition; } + [[nodiscard]] inline const BoundStatement *GetBlock() const { return _block; } + [[nodiscard]] inline const BoundStatement *GetElseStatement() const { return _elseStatement; } @@ -233,26 +254,32 @@ namespace Porygon::Binder { delete _block; } - inline const BoundStatementKind GetKind() const final { + [[nodiscard]] + inline BoundStatementKind GetKind() const final { return BoundStatementKind::NumericalFor; } + [[nodiscard]] inline const BoundVariableKey* GetIdentifier() const{ return _identifier; } + [[nodiscard]] inline const BoundExpression* GetStart() const{ return _start; } + [[nodiscard]] inline const BoundExpression* GetEnd() const{ return _end; } + [[nodiscard]] inline const BoundExpression* GetStep() const{ return _step; } + [[nodiscard]] inline const BoundStatement* GetBlock() const{ return _block; } @@ -278,22 +305,27 @@ namespace Porygon::Binder { delete _block; } - inline const BoundStatementKind GetKind() const final { + [[nodiscard]] + inline BoundStatementKind GetKind() const final { return BoundStatementKind::GenericFor; } + [[nodiscard]] inline const BoundVariableKey* GetKeyIdentifier() const{ return _keyIdentifier; } + [[nodiscard]] inline const BoundVariableKey* GetValueIdentifier() const{ return _valueIdentifier; } + [[nodiscard]] inline const BoundExpression* GetIterator() const{ return _iterator; } + [[nodiscard]] inline const BoundStatement* GetBlock() const{ return _block; } @@ -312,14 +344,17 @@ namespace Porygon::Binder { delete _block; } - inline const BoundStatementKind GetKind() const final { + [[nodiscard]] + inline BoundStatementKind GetKind() const final { return BoundStatementKind::While; } + [[nodiscard]] inline const BoundExpression* GetCondition() const{ return _condition; } + [[nodiscard]] inline const BoundStatement* GetBlock() const{ return _block; } diff --git a/src/Binder/BoundVariables/BoundScope.cpp b/src/Binder/BoundVariables/BoundScope.cpp index d82394f..ab82c82 100644 --- a/src/Binder/BoundVariables/BoundScope.cpp +++ b/src/Binder/BoundVariables/BoundScope.cpp @@ -1,6 +1,4 @@ #include - - #include "BoundScope.hpp" #include "../../StandardLibraries/StaticScope.hpp" @@ -15,7 +13,7 @@ namespace Porygon::Binder { BoundScope::~BoundScope() { for (auto scope : _localScope) { - for (auto v : *scope) { + for (const auto& v : *scope) { delete v.second; } delete scope; @@ -33,7 +31,7 @@ namespace Porygon::Binder { void BoundScope::GoOuterScope() { auto scope = _localScope[_currentScope - 1]; - for (auto v : *scope) { + for (const auto& v : *scope) { delete v.second; } scope->clear(); @@ -79,7 +77,7 @@ namespace Porygon::Binder { } } - VariableAssignment BoundScope::CreateExplicitLocal(const Utilities::HashedString& identifier, std::shared_ptr type) { + VariableAssignment BoundScope::CreateExplicitLocal(const Utilities::HashedString& identifier, std::shared_ptr type) { auto scope = this->_localScope.at(this->_currentScope - 1); if (scope->find(identifier) != scope->end()) { return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr); @@ -89,7 +87,7 @@ namespace Porygon::Binder { new BoundVariableKey(identifier, this->_currentScope, true)); } - VariableAssignment BoundScope::AssignVariable(const Utilities::HashedString& identifier, const std::shared_ptr &type) { + VariableAssignment BoundScope::AssignVariable(const Utilities::HashedString& identifier, const std::shared_ptr &type) { int exists = this->Exists(identifier); if (exists < 0) { // Creation diff --git a/src/Binder/BoundVariables/BoundScope.hpp b/src/Binder/BoundVariables/BoundScope.hpp index 1306378..1e4b7eb 100644 --- a/src/Binder/BoundVariables/BoundScope.hpp +++ b/src/Binder/BoundVariables/BoundScope.hpp @@ -32,17 +32,13 @@ namespace Porygon::Binder { BoundVariable *GetVariable(uint32_t scope, const Utilities::HashedString& identifier); - VariableAssignment CreateExplicitLocal(const Utilities::HashedString& identifier, std::shared_ptr type); + VariableAssignment CreateExplicitLocal(const Utilities::HashedString& identifier, std::shared_ptr type); - VariableAssignment AssignVariable(const Utilities::HashedString& identifier, const std::shared_ptr &type); + VariableAssignment AssignVariable(const Utilities::HashedString& identifier, const std::shared_ptr &type); inline size_t GetLocalVariableCount() { return _localScope.size(); } - - inline int GetCurrentScope() { - return _currentScope; - } }; } diff --git a/src/Binder/BoundVariables/BoundVariable.hpp b/src/Binder/BoundVariables/BoundVariable.hpp index d2ce0a1..03f41f7 100644 --- a/src/Binder/BoundVariables/BoundVariable.hpp +++ b/src/Binder/BoundVariables/BoundVariable.hpp @@ -1,9 +1,9 @@ -#include #ifndef PORYGONLANG_BOUNDVARIABLE_HPP #define PORYGONLANG_BOUNDVARIABLE_HPP +#include #include #include "../../ScriptType.hpp" @@ -11,12 +11,12 @@ using namespace std; namespace Porygon::Binder { class BoundVariable { - std::shared_ptr _type; + std::shared_ptr _type; public: - inline explicit BoundVariable(std::shared_ptr type) : _type(std::move(type)) { + inline explicit BoundVariable(std::shared_ptr type) : _type(std::move(type)) { } - inline std::shared_ptr GetType() { + inline std::shared_ptr GetType() { return _type; } }; diff --git a/src/Binder/BoundVariables/BoundVariableKey.hpp b/src/Binder/BoundVariables/BoundVariableKey.hpp index 24d9314..fd58263 100644 --- a/src/Binder/BoundVariables/BoundVariableKey.hpp +++ b/src/Binder/BoundVariables/BoundVariableKey.hpp @@ -19,26 +19,30 @@ namespace Porygon::Binder { } public: - BoundVariableKey(Utilities::HashedString id, unsigned int scope, bool creation) + BoundVariableKey(const Utilities::HashedString& id, unsigned int scope, bool creation) : _identifier(id), _scopeId(scope), _isCreation(creation), _hash(KnuthsHash(id.GetHash(), scope)) {} - inline const Utilities::HashedString GetIdentifier() const { - return _identifier; + [[nodiscard]] + inline const Utilities::HashedString* GetIdentifier() const { + return &_identifier; } - inline const unsigned int GetScopeId() const { + [[nodiscard]] + inline unsigned int GetScopeId() const { return _scopeId; } - inline const bool IsCreation() const { + [[nodiscard]] + inline bool IsCreation() const { return _isCreation; } - inline const uint64_t GetHash() const { + [[nodiscard]] + inline uint64_t GetHash() const { return _hash; } }; diff --git a/src/Binder/BoundVariables/VariableAssigmentResult.hpp b/src/Binder/BoundVariables/VariableAssigmentResult.hpp index 3a6c0ae..af89370 100644 --- a/src/Binder/BoundVariables/VariableAssigmentResult.hpp +++ b/src/Binder/BoundVariables/VariableAssigmentResult.hpp @@ -13,18 +13,20 @@ namespace Porygon::Binder { class VariableAssignment { VariableAssignmentResult _result; - BoundVariableKey *_key; + const BoundVariableKey *_key; public: VariableAssignment(VariableAssignmentResult result, BoundVariableKey *key) { _result = result; _key = key; } + [[nodiscard]] inline VariableAssignmentResult GetResult() { return _result; } - inline BoundVariableKey *GetKey() { + [[nodiscard]] + inline const BoundVariableKey *GetKey() { return _key; } }; diff --git a/src/Diagnostics/Diagnostic.hpp b/src/Diagnostics/Diagnostic.hpp index 146c46b..1b69357 100644 --- a/src/Diagnostics/Diagnostic.hpp +++ b/src/Diagnostics/Diagnostic.hpp @@ -1,9 +1,7 @@ -#include - - #ifndef PORYGONLANG_DIAGNOSTIC_HPP #define PORYGONLANG_DIAGNOSTIC_HPP +#include #include #include #include "DiagnosticSeverity.hpp" @@ -28,22 +26,27 @@ namespace Porygon::Diagnostics { delete _message; } + [[nodiscard]] inline DiagnosticSeverity GetSeverity() { return _severity; } + [[nodiscard]] inline DiagnosticCode GetCode() { return _code; } + [[nodiscard]] inline unsigned int GetStartPosition() { return _start; } + [[nodiscard]] inline unsigned int GetLength() { return _length; } + [[nodiscard]] inline std::vector GetArguments(){ return _arguments; } @@ -52,6 +55,7 @@ namespace Porygon::Diagnostics { this -> _message = s; } + [[nodiscard]] inline std::string* GetMessage(){ return _message; } diff --git a/src/Diagnostics/DiagnosticCode.hpp b/src/Diagnostics/DiagnosticCode.hpp index 72e4bb9..f859e26 100644 --- a/src/Diagnostics/DiagnosticCode.hpp +++ b/src/Diagnostics/DiagnosticCode.hpp @@ -3,7 +3,7 @@ #define PORYGONLANG_DIAGNOSTICCODE_HPP namespace Porygon::Diagnostics { - enum class DiagnosticCode { + enum class DiagnosticCode : u_int8_t { // Lex diagnostics UnexpectedCharacter, InvalidStringControlCharacter, diff --git a/src/Diagnostics/DiagnosticsHolder.cpp b/src/Diagnostics/DiagnosticsHolder.cpp index 2ada5cd..5e9e709 100644 --- a/src/Diagnostics/DiagnosticsHolder.cpp +++ b/src/Diagnostics/DiagnosticsHolder.cpp @@ -1,5 +1,3 @@ -#include - #include // For std::unique_ptr #include "DiagnosticsHolder.hpp" #include "ErrorMessages-EN-US.cpp" @@ -10,23 +8,23 @@ vector DiagnosticsHolder::GetDiagnostics() { } void DiagnosticsHolder::Log(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length, - std::vector arguments) { + const std::vector& arguments) { _diagnostics.emplace_back(severity, code, start, length, arguments); if (severity >= DiagnosticSeverity::Error){ _hasErrors = true; } } -void DiagnosticsHolder::LogError(DiagnosticCode code, unsigned int start, unsigned int length, std::vector arguments) { - Log(DiagnosticSeverity::Error, code, start, length, std::move(arguments)); +void DiagnosticsHolder::LogError(DiagnosticCode code, unsigned int start, unsigned int length, const std::vector& arguments) { + Log(DiagnosticSeverity::Error, code, start, length, arguments); } -void DiagnosticsHolder::LogWarning(DiagnosticCode code, unsigned int start, unsigned int length, std::vector arguments) { - Log(DiagnosticSeverity::Warning, code, start, length, std::move(arguments)); +void DiagnosticsHolder::LogWarning(DiagnosticCode code, unsigned int start, unsigned int length, const std::vector& arguments) { + Log(DiagnosticSeverity::Warning, code, start, length, arguments); } -void DiagnosticsHolder::LogInfo(DiagnosticCode code, unsigned int start, unsigned int length, std::vector arguments) { - Log(DiagnosticSeverity::Info, code, start, length, std::move(arguments)); +void DiagnosticsHolder::LogInfo(DiagnosticCode code, unsigned int start, unsigned int length, const std::vector& arguments) { + Log(DiagnosticSeverity::Info, code, start, length, arguments); } bool DiagnosticsHolder::HasErrors() { @@ -91,10 +89,10 @@ void findAndReplaceAll(std::string & data, const std::string& toSearch, const st } } -const string PrettyDiagnostic ( const char * format, vector arguments) +string PrettyDiagnostic ( const char * format, vector arguments) { string result = string(format); - for (int i = 0; i < arguments.size(); i++){ + for (size_t i = 0; i < arguments.size(); i++){ auto keyToReplace = "{" + to_string(i) + "}"; auto argument = arguments[i]; findAndReplaceAll(result, keyToReplace, argument); diff --git a/src/Diagnostics/DiagnosticsHolder.hpp b/src/Diagnostics/DiagnosticsHolder.hpp index 3194ef4..3337e5c 100644 --- a/src/Diagnostics/DiagnosticsHolder.hpp +++ b/src/Diagnostics/DiagnosticsHolder.hpp @@ -35,13 +35,13 @@ namespace Porygon::Diagnostics { _diagnostics.clear(); } - void Log(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length, std::vector arguments = {}); + void Log(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length, const std::vector& arguments = {}); - void LogError(DiagnosticCode code, unsigned int start, unsigned int length, std::vector arguments = {}); + void LogError(DiagnosticCode code, unsigned int start, unsigned int length, const std::vector& arguments = {}); - void LogWarning(DiagnosticCode code, unsigned int start, unsigned int length, std::vector arguments = {}); + void LogWarning(DiagnosticCode code, unsigned int start, unsigned int length, const std::vector& arguments = {}); - void LogInfo(DiagnosticCode code, unsigned int start, unsigned int length, std::vector arguments = {}); + void LogInfo(DiagnosticCode code, unsigned int start, unsigned int length, const std::vector& arguments = {}); bool HasErrors(); @@ -52,6 +52,7 @@ namespace Porygon::Diagnostics { Diagnostic *GetDiagnosticAt(int position); size_t GetLineFromPosition(size_t i); + [[nodiscard]] inline size_t GetStartPositionForLine(size_t i){ return _lineStarts[i]; } diff --git a/src/Evaluator/BinaryEvaluation.cpp b/src/Evaluator/BinaryEvaluation.cpp index a119a52..5960cc4 100644 --- a/src/Evaluator/BinaryEvaluation.cpp +++ b/src/Evaluator/BinaryEvaluation.cpp @@ -8,7 +8,7 @@ using namespace Porygon::Binder; namespace Porygon::Evaluation { - const shared_ptr Evaluator::EvaluateIntegerBinary(const BoundBinaryExpression *expression) { + shared_ptr Evaluator::EvaluateIntegerBinary(const BoundBinaryExpression *expression) { auto leftValue = this->EvaluateIntegerExpression(expression->GetLeft()); auto rightValue = this->EvaluateIntegerExpression(expression->GetRight()); @@ -26,7 +26,7 @@ namespace Porygon::Evaluation { } } - const shared_ptr Evaluator::EvaluateBooleanBinary(const BoundBinaryExpression *expression) { + shared_ptr Evaluator::EvaluateBooleanBinary(const BoundBinaryExpression *expression) { switch (expression->GetOperation()) { case BoundBinaryOperation::Equality: { auto leftValue = this->EvaluateExpression(expression->GetLeft()); @@ -78,7 +78,7 @@ namespace Porygon::Evaluation { } } - const shared_ptr Evaluator::EvaluateStringBinary(const BoundBinaryExpression *expression) { + shared_ptr Evaluator::EvaluateStringBinary(const BoundBinaryExpression *expression) { if (expression->GetOperation() != BoundBinaryOperation::Concatenation) throw; std::basic_ostringstream stringStream; diff --git a/src/Evaluator/EvalValues/EvalValue.cpp b/src/Evaluator/EvalValues/EvalValue.cpp index a0beafd..765b0fc 100644 --- a/src/Evaluator/EvalValues/EvalValue.cpp +++ b/src/Evaluator/EvalValues/EvalValue.cpp @@ -7,28 +7,28 @@ namespace Porygon::Evaluation { extern "C" { - Porygon::TypeClass GetEvalValueTypeClass(EvalValue *v) { + Porygon::TypeClass GetEvalValueTypeClass(const EvalValue *v) { return v->GetTypeClass(); } - int64_t EvaluateEvalValueInteger(EvalValue *v) { + int64_t EvaluateEvalValueInteger(const EvalValue *v) { return v->EvaluateInteger(); } - double EvaluateEvalValueFloat(EvalValue *v) { + double EvaluateEvalValueFloat(const EvalValue *v) { return v->EvaluateFloat(); } - bool EvaluateEvalValueBool(EvalValue *v) { + bool EvaluateEvalValueBool(const EvalValue *v) { return v->EvaluateBool(); } - size_t GetEvalValueStringLength(EvalValue *v) { + size_t GetEvalValueStringLength(const EvalValue *v) { auto result = v->EvaluateString(); return result.size(); } - int EvaluateEvalValueString(EvalValue *v, char16_t* dst, size_t capacity){ + int EvaluateEvalValueString(const EvalValue *v, char16_t* dst, size_t capacity){ auto result = v->EvaluateString(); for (int i = 0; i < capacity; i++){ dst[i] = result[i]; diff --git a/src/Evaluator/EvalValues/EvalValue.hpp b/src/Evaluator/EvalValues/EvalValue.hpp index f05e10b..265f87e 100644 --- a/src/Evaluator/EvalValues/EvalValue.hpp +++ b/src/Evaluator/EvalValues/EvalValue.hpp @@ -21,46 +21,58 @@ namespace Porygon::Evaluation { virtual ~EvalValue() = default; - virtual const TypeClass GetTypeClass() const = 0; + [[nodiscard]] + virtual TypeClass GetTypeClass() const = 0; - virtual const bool operator==(EvalValue *b) const = 0; + [[nodiscard]] + virtual bool operator==(const EvalValue *b) const = 0; - virtual const bool operator!=(EvalValue *b) const { + [[nodiscard]] + virtual bool operator!=(const EvalValue *b) const { return !(this->operator==(b)); } - virtual const shared_ptr Clone() const = 0; + [[nodiscard]] + virtual shared_ptr Clone() const = 0; - virtual const long EvaluateInteger() const { + [[nodiscard]] + virtual long EvaluateInteger() const { throw EvaluationException("Can't evaluate this EvalValue as integer."); } - virtual const double EvaluateFloat() const { + [[nodiscard]] + virtual double EvaluateFloat() const { throw EvaluationException("Can't evaluate this EvalValue as float."); } - virtual const bool EvaluateBool() const { + [[nodiscard]] + virtual bool EvaluateBool() const { throw EvaluationException("Can't evaluate this EvalValue as bool."); } - virtual const std::u16string EvaluateString() const { + [[nodiscard]] + virtual std::u16string EvaluateString() const { throw EvaluationException("Can't evaluate this EvalValue as string."); } - virtual const std::size_t GetHashCode() const = 0; + [[nodiscard]] + virtual std::size_t GetHashCode() const = 0; - virtual const shared_ptr IndexValue(EvalValue *val) const { + [[nodiscard]] + virtual shared_ptr IndexValue(const EvalValue *val) const { throw EvaluationException("Can't index this EvalValue"); } - virtual const shared_ptr IndexValue(uint32_t hash) const { + [[nodiscard]] + virtual shared_ptr IndexValue(uint32_t hash) const { throw EvaluationException("Can't index this EvalValue"); } - virtual void SetIndexValue(EvalValue *key, const shared_ptr &value) const { + virtual void SetIndexValue(const EvalValue *key, const shared_ptr &value) const { throw EvaluationException("Can't index this EvalValue"); } + [[nodiscard]] virtual Iterator * GetKeyIterator() const{ throw EvaluationException("Can't iterate over this EvalValue"); } @@ -73,25 +85,30 @@ namespace Porygon::Evaluation { : _value(val) { } - inline const shared_ptr Clone() const final { + [[nodiscard]] + inline shared_ptr Clone() const final { return make_shared(_value); } - inline const TypeClass GetTypeClass() const final { + [[nodiscard]] + inline TypeClass GetTypeClass() const final { return TypeClass::Bool; } - inline const bool EvaluateBool() const final { + [[nodiscard]] + inline bool EvaluateBool() const final { return _value; } - const bool operator==(EvalValue *b) const final { + [[nodiscard]] + bool operator==(const EvalValue *b) const final { if (b->GetTypeClass() != TypeClass::Bool) return false; return this->EvaluateBool() == b->EvaluateBool(); }; - inline const std::size_t GetHashCode() const final { + [[nodiscard]] + inline std::size_t GetHashCode() const final { return _value; } }; diff --git a/src/Evaluator/EvalValues/EvalValueHelper.hpp b/src/Evaluator/EvalValues/EvalValueHelper.hpp index 8b37e82..17764b7 100644 --- a/src/Evaluator/EvalValues/EvalValueHelper.hpp +++ b/src/Evaluator/EvalValues/EvalValueHelper.hpp @@ -1,5 +1,3 @@ -#include - #ifndef PORYGONLANG_EVALVALUEHELPER_HPP #define PORYGONLANG_EVALVALUEHELPER_HPP diff --git a/src/Evaluator/EvalValues/NilEvalValue.hpp b/src/Evaluator/EvalValues/NilEvalValue.hpp index 900409d..dabb37b 100644 --- a/src/Evaluator/EvalValues/NilEvalValue.hpp +++ b/src/Evaluator/EvalValues/NilEvalValue.hpp @@ -5,18 +5,22 @@ namespace Porygon::Evaluation{ class NilEvalValue : public EvalValue{ - inline const TypeClass GetTypeClass() const final{ + [[nodiscard]] + inline TypeClass GetTypeClass() const final{ return TypeClass ::Nil; } - inline const bool operator==(EvalValue *b) const final{ + [[nodiscard]] + inline bool operator==(const EvalValue *b) const final{ return b->GetTypeClass() == TypeClass ::Nil; } - inline const shared_ptr Clone() const final{ + [[nodiscard]] + inline shared_ptr Clone() const final{ return make_shared(); } - inline const std::size_t GetHashCode() const final{ + [[nodiscard]] + inline std::size_t GetHashCode() const final{ return 0; } }; diff --git a/src/Evaluator/EvalValues/NumericEvalValue.cpp b/src/Evaluator/EvalValues/NumericEvalValue.cpp index 06b1c7d..9f92d96 100644 --- a/src/Evaluator/EvalValues/NumericEvalValue.cpp +++ b/src/Evaluator/EvalValues/NumericEvalValue.cpp @@ -2,74 +2,74 @@ #include "NumericEvalValue.hpp" namespace Porygon::Evaluation { - const shared_ptr NumericEvalValue::operator+(const shared_ptr &b) const { + shared_ptr NumericEvalValue::operator+(const shared_ptr &b) const { if (this->IsFloat()) { if (b->IsFloat()) { return make_shared(this->GetFloatValue() + b->GetFloatValue()); } else { - return make_shared(this->GetFloatValue() + b->GetIntegerValue()); + return make_shared(this->GetFloatValue() + (double) b->GetIntegerValue()); } } else { if (b->IsFloat()) { - return make_shared(this->GetIntegerValue() + b->GetFloatValue()); + return make_shared((double) this->GetIntegerValue() + b->GetFloatValue()); } else { return make_shared(this->GetIntegerValue() + b->GetIntegerValue()); } } } - const shared_ptr NumericEvalValue::operator-(const shared_ptr &b) const { + shared_ptr NumericEvalValue::operator-(const shared_ptr &b) const { if (this->IsFloat()) { if (b->IsFloat()) { return make_shared(this->GetFloatValue() - b->GetFloatValue()); } else { - return make_shared(this->GetFloatValue() - b->GetIntegerValue()); + return make_shared(this->GetFloatValue() - (double) b->GetIntegerValue()); } } else { if (b->IsFloat()) { - return make_shared(this->GetIntegerValue() - b->GetFloatValue()); + return make_shared((double) this->GetIntegerValue() - b->GetFloatValue()); } else { return make_shared(this->GetIntegerValue() - b->GetIntegerValue()); } } } - const shared_ptr NumericEvalValue::operator*(const shared_ptr &b) const { + shared_ptr NumericEvalValue::operator*(const shared_ptr &b) const { if (this->IsFloat()) { if (b->IsFloat()) { return make_shared(this->GetFloatValue() * b->GetFloatValue()); } else { - return make_shared(this->GetFloatValue() * b->GetIntegerValue()); + return make_shared(this->GetFloatValue() * (double) b->GetIntegerValue()); } } else { if (b->IsFloat()) { - return make_shared(this->GetIntegerValue() * b->GetFloatValue()); + return make_shared((double) this->GetIntegerValue() * b->GetFloatValue()); } else { return make_shared(this->GetIntegerValue() * b->GetIntegerValue()); } } } - const shared_ptr NumericEvalValue::operator/(const shared_ptr &b) const { + shared_ptr NumericEvalValue::operator/(const shared_ptr &b) const { if (this->IsFloat()) { if (b->IsFloat()) { return make_shared(this->GetFloatValue() / b->GetFloatValue()); } else { - return make_shared(this->GetFloatValue() / b->GetIntegerValue()); + return make_shared(this->GetFloatValue() / (double) b->GetIntegerValue()); } } else { if (b->IsFloat()) { - return make_shared(this->GetIntegerValue() / b->GetFloatValue()); + return make_shared((double) this->GetIntegerValue() / b->GetFloatValue()); } else { return make_shared(this->GetIntegerValue() / b->GetIntegerValue()); } } } - const bool NumericEvalValue::operator==(EvalValue *b) const { + bool NumericEvalValue::operator==(const EvalValue *b) const { if (b->GetTypeClass() != TypeClass::Number) return false; - auto numVal = dynamic_cast(b); + auto numVal = dynamic_cast(b); if (this->IsFloat() != numVal->IsFloat()) return false; @@ -80,64 +80,64 @@ namespace Porygon::Evaluation { } } - const shared_ptr NumericEvalValue::operator<(const shared_ptr &b) const { + shared_ptr NumericEvalValue::operator<(const shared_ptr &b) const { if (this->IsFloat()) { if (b->IsFloat()) { return make_shared(this->GetFloatValue() < b->GetFloatValue()); } else { - return make_shared(this->GetFloatValue() < b->GetIntegerValue()); + return make_shared(this->GetFloatValue() < (double) b->GetIntegerValue()); } } else { if (b->IsFloat()) { - return make_shared(this->GetIntegerValue() < b->GetFloatValue()); + return make_shared((double) this->GetIntegerValue() < b->GetFloatValue()); } else { return make_shared(this->GetIntegerValue() < b->GetIntegerValue()); } } } - const shared_ptr NumericEvalValue::operator<=(const shared_ptr &b) const { + shared_ptr NumericEvalValue::operator<=(const shared_ptr &b) const { if (this->IsFloat()) { if (b->IsFloat()) { return make_shared(this->GetFloatValue() <= b->GetFloatValue()); } else { - return make_shared(this->GetFloatValue() <= b->GetIntegerValue()); + return make_shared(this->GetFloatValue() <=(double) b->GetIntegerValue()); } } else { if (b->IsFloat()) { - return make_shared(this->GetIntegerValue() <= b->GetFloatValue()); + return make_shared((double) this->GetIntegerValue() <= b->GetFloatValue()); } else { return make_shared(this->GetIntegerValue() <= b->GetIntegerValue()); } } } - const shared_ptr NumericEvalValue::operator>(const shared_ptr &b) const { + shared_ptr NumericEvalValue::operator>(const shared_ptr &b) const { if (this->IsFloat()) { if (b->IsFloat()) { return make_shared(this->GetFloatValue() > b->GetFloatValue()); } else { - return make_shared(this->GetFloatValue() > b->GetIntegerValue()); + return make_shared(this->GetFloatValue() > (double) b->GetIntegerValue()); } } else { if (b->IsFloat()) { - return make_shared(this->GetIntegerValue() > b->GetFloatValue()); + return make_shared((double) this->GetIntegerValue() > b->GetFloatValue()); } else { return make_shared(this->GetIntegerValue() > b->GetIntegerValue()); } } } - const shared_ptr NumericEvalValue::operator>=(const shared_ptr &b) const { + shared_ptr NumericEvalValue::operator>=(const shared_ptr &b) const { if (this->IsFloat()) { if (b->IsFloat()) { return make_shared(this->GetFloatValue() >= b->GetFloatValue()); } else { - return make_shared(this->GetFloatValue() >= b->GetIntegerValue()); + return make_shared(this->GetFloatValue() >= (double) b->GetIntegerValue()); } } else { if (b->IsFloat()) { - return make_shared(this->GetIntegerValue() >= b->GetFloatValue()); + return make_shared((double) this->GetIntegerValue() >= b->GetFloatValue()); } else { return make_shared(this->GetIntegerValue() >= b->GetIntegerValue()); } diff --git a/src/Evaluator/EvalValues/NumericEvalValue.hpp b/src/Evaluator/EvalValues/NumericEvalValue.hpp index 5e2889c..7bbb6e0 100644 --- a/src/Evaluator/EvalValues/NumericEvalValue.hpp +++ b/src/Evaluator/EvalValues/NumericEvalValue.hpp @@ -9,42 +9,48 @@ namespace Porygon::Evaluation { class NumericEvalValue : public EvalValue { - virtual const long GetIntegerValue() const = 0; + [[nodiscard]] + virtual long GetIntegerValue() const = 0; - virtual const double GetFloatValue() const = 0; + [[nodiscard]] + virtual double GetFloatValue() const = 0; public: - virtual const bool IsFloat() const = 0; + [[nodiscard]] + virtual bool IsFloat() const = 0; - inline const TypeClass GetTypeClass() const final { + [[nodiscard]] + inline TypeClass GetTypeClass() const final { return TypeClass::Number; } - const shared_ptr operator+(const shared_ptr &b) const; + shared_ptr operator+(const shared_ptr &b) const; - const shared_ptr operator-(const shared_ptr &b) const; + shared_ptr operator-(const shared_ptr &b) const; - const shared_ptr operator*(const shared_ptr &b) const; + shared_ptr operator*(const shared_ptr &b) const; - const shared_ptr operator/(const shared_ptr &b) const; + shared_ptr operator/(const shared_ptr &b) const; - const shared_ptr operator<(const shared_ptr &b) const; + shared_ptr operator<(const shared_ptr &b) const; - const shared_ptr operator<=(const shared_ptr &b) const; + shared_ptr operator<=(const shared_ptr &b) const; - const shared_ptr operator>(const shared_ptr &b) const; + shared_ptr operator>(const shared_ptr &b) const; - const shared_ptr operator>=(const shared_ptr &b) const; + shared_ptr operator>=(const shared_ptr &b) const; - const bool operator==(EvalValue *b) const final; + bool operator==(const EvalValue *b) const final; }; class IntegerEvalValue : public NumericEvalValue { const long _value; - const long GetIntegerValue() const final { return _value; } + [[nodiscard]] + long GetIntegerValue() const final { return _value; } - const double GetFloatValue() const final { + [[nodiscard]] + double GetFloatValue() const final { throw EvaluationException("Attempting to retrieve float from int eval value."); } @@ -52,23 +58,28 @@ namespace Porygon::Evaluation { explicit IntegerEvalValue(long value) : _value(value) { } - inline const bool IsFloat() const final { + [[nodiscard]] + inline bool IsFloat() const final { return false; } - inline const long EvaluateInteger() const final { + [[nodiscard]] + inline long EvaluateInteger() const final { return _value; } - inline const std::u16string EvaluateString() const final{ + [[nodiscard]] + inline std::u16string EvaluateString() const final{ return Utilities::StringUtils::IntToString(_value); } - inline const shared_ptr Clone() const final { + [[nodiscard]] + inline shared_ptr Clone() const final { return make_shared(_value); } - inline const std::size_t GetHashCode() const final { + [[nodiscard]] + inline std::size_t GetHashCode() const final { return std::hash{}(_value); } }; @@ -76,11 +87,13 @@ namespace Porygon::Evaluation { class FloatEvalValue : public NumericEvalValue { const double _value; - inline const long GetIntegerValue() const final { + [[nodiscard]] + inline long GetIntegerValue() const final { throw EvaluationException("Attempting to retrieve float from int eval value."); } - inline const double GetFloatValue() const final { + [[nodiscard]] + inline double GetFloatValue() const final { return _value; } @@ -88,19 +101,23 @@ namespace Porygon::Evaluation { explicit FloatEvalValue(double value) : _value(value) { } - inline const bool IsFloat() const final { + [[nodiscard]] + inline bool IsFloat() const final { return true; } - inline const double EvaluateFloat() const final { + [[nodiscard]] + inline double EvaluateFloat() const final { return _value; } - inline const shared_ptr Clone() const final { + [[nodiscard]] + inline shared_ptr Clone() const final { return make_shared(_value); } - inline const std::size_t GetHashCode() const final { + [[nodiscard]] + inline std::size_t GetHashCode() const final { return std::hash{}(_value); } }; diff --git a/src/Evaluator/EvalValues/NumericalTableEvalValue.hpp b/src/Evaluator/EvalValues/NumericalTableEvalValue.hpp index fdd30ef..32f3069 100644 --- a/src/Evaluator/EvalValues/NumericalTableEvalValue.hpp +++ b/src/Evaluator/EvalValues/NumericalTableEvalValue.hpp @@ -10,55 +10,63 @@ using namespace std; namespace Porygon::Evaluation { class NumericalTableEvalValue : public EvalValue { - const shared_ptr>> _table; + const shared_ptr>> _table; const size_t _hash; - explicit NumericalTableEvalValue(shared_ptr>> table, size_t hash) + explicit NumericalTableEvalValue(shared_ptr>> table, size_t hash) : _table(std::move(table)), _hash(hash) { } public: - explicit NumericalTableEvalValue(shared_ptr>> table) : + explicit NumericalTableEvalValue(shared_ptr>> table) : _table(std::move(table)), _hash(rand()) { } - inline const TypeClass GetTypeClass() const final { + [[nodiscard]] + inline TypeClass GetTypeClass() const final { return TypeClass::Table; } - inline const size_t GetHashCode() const final { + [[nodiscard]] + inline size_t GetHashCode() const final { return _hash; } - inline const bool operator==(EvalValue *b) const final { + [[nodiscard]] + inline bool operator==(const EvalValue *b) const final { return this->_hash == b->GetHashCode(); } - inline const shared_ptr Clone() const final { + [[nodiscard]] + inline shared_ptr Clone() const final { return shared_ptr(new NumericalTableEvalValue(_table, _hash)); } - inline const shared_ptr IndexValue(EvalValue *val) const final { + [[nodiscard]] + inline shared_ptr IndexValue(const EvalValue *val) const final { const auto index = val->EvaluateInteger() - 1; return this->_table->at(index); } - inline const shared_ptr IndexValue(uint32_t hash) const final { + [[nodiscard]] + inline shared_ptr IndexValue(uint32_t hash) const final { return this->_table->at(hash - 1); } - inline void SetIndexValue(EvalValue *key, const shared_ptr &value) const final { + inline void SetIndexValue(const EvalValue *key, const shared_ptr &value) const final { auto index = key->EvaluateInteger(); this->_table->at(index - 1) = value; } + [[nodiscard]] Iterator * GetKeyIterator() const final; - inline const shared_ptr>> GetTable() const{ + [[nodiscard]] + inline shared_ptr>> GetTable() const{ return _table; }; }; diff --git a/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp b/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp index 2405e9c..1539e50 100644 --- a/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp +++ b/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp @@ -20,16 +20,16 @@ namespace Porygon::Evaluation { }; class EvaluationScriptFunctionOption : public GenericFunctionOption{ - const std::shared_ptr _innerBlock; + const std::shared_ptr _innerBlock; const std::shared_ptr _scope; public: - EvaluationScriptFunctionOption(shared_ptr innerBlock, shared_ptr scope) + EvaluationScriptFunctionOption(shared_ptr innerBlock, shared_ptr scope) : _innerBlock(std::move(innerBlock)), _scope(std::move(scope)) { } ~EvaluationScriptFunctionOption() final = default; - inline const std::shared_ptr &GetInnerBlock() const { + inline std::shared_ptr GetInnerBlock() const { return _innerBlock; } @@ -40,47 +40,53 @@ namespace Porygon::Evaluation { class GenericFunctionEvalValue : public EvalValue{ protected: - const shared_ptr _type; + const shared_ptr _type; const size_t _hash; - vector> _options; + vector>* _options; public: - GenericFunctionEvalValue(shared_ptr type, size_t hash) + GenericFunctionEvalValue(shared_ptr type, size_t hash) : _type(move(type)), - _hash(hash){ + _hash(hash), _options(new vector>()){ } - const shared_ptr Clone() const final { + GenericFunctionEvalValue(const GenericFunctionEvalValue& _) = delete; + GenericFunctionEvalValue() = delete; + GenericFunctionEvalValue& operator =(GenericFunctionEvalValue v) = delete; + + [[nodiscard]] + shared_ptr Clone() const final { auto t = make_shared(_type, _hash); - for (const auto& o: _options){ - t->_options.push_back(o); + for (const auto& o: *_options){ + t->_options->push_back(o); } return t; } - inline void RegisterOption(GenericFunctionOption* option){ - _options.push_back(shared_ptr(option)); + inline void RegisterOption(GenericFunctionOption* option) const{ + _options->push_back(shared_ptr(option)); } - inline const std::shared_ptr GetType() const { + inline std::shared_ptr GetType() const { return _type; } - inline const TypeClass GetTypeClass() const final { + inline TypeClass GetTypeClass() const final { return TypeClass::Function; } - const bool operator==(EvalValue *b) const final { + bool operator==(const EvalValue *b) const final { if (b->GetTypeClass() != TypeClass::Function) return false; return this->_hash == ((GenericFunctionEvalValue *) b)->_hash; }; - inline const std::size_t GetHashCode() const final { + inline std::size_t GetHashCode() const final { return _hash; } - inline const shared_ptr GetOption(const size_t id) const{ - return this->_options.at(id); + [[nodiscard]] + inline shared_ptr GetOption(const size_t id) const{ + return this->_options->at(id); } }; } diff --git a/src/Evaluator/EvalValues/StringEvalValue.hpp b/src/Evaluator/EvalValues/StringEvalValue.hpp index 23f6e85..fc405a9 100644 --- a/src/Evaluator/EvalValues/StringEvalValue.hpp +++ b/src/Evaluator/EvalValues/StringEvalValue.hpp @@ -17,31 +17,35 @@ namespace Porygon::Evaluation { _hash = Utilities::HashedString::ConstHash(_value.c_str()); } - inline const TypeClass GetTypeClass() const final { + [[nodiscard]] + inline TypeClass GetTypeClass() const final { return TypeClass::String; } - const bool operator==(EvalValue *b) const final { + bool operator==(const EvalValue *b) const final { if (b->GetTypeClass() != TypeClass::String) return false; return this->_hash == b->GetHashCode(); }; - inline const u16string EvaluateString() const final { + [[nodiscard]] + inline u16string EvaluateString() const final { return _value; } - inline const shared_ptr Clone() const final { + [[nodiscard]] + inline shared_ptr Clone() const final { return make_shared(_value); } - const shared_ptr IndexValue(EvalValue *val) const final { + shared_ptr IndexValue(const EvalValue *val) const final { // Porygon is 1-indexed, so we convert to that. auto l = val->EvaluateInteger() - 1; - return make_shared(u16string(1, _value[l])); + return make_shared(u16string(1, _value[l])); } - inline const std::size_t GetHashCode() const final { + [[nodiscard]] + inline std::size_t GetHashCode() const final { return _hash; } }; diff --git a/src/Evaluator/EvalValues/TableEvalValue.hpp b/src/Evaluator/EvalValues/TableEvalValue.hpp index 94c5489..bb382df 100644 --- a/src/Evaluator/EvalValues/TableEvalValue.hpp +++ b/src/Evaluator/EvalValues/TableEvalValue.hpp @@ -4,68 +4,79 @@ #include #include #include "EvalValue.hpp" +#include "../../Utilities/Random.hpp" using namespace std; namespace Porygon::Evaluation { class TableEvalValue : public EvalValue { - const shared_ptr>> _table; + const shared_ptr>> _table; const size_t _hash; - explicit TableEvalValue(shared_ptr>> table, size_t hash) + explicit TableEvalValue(shared_ptr>> table, size_t hash) : _table(std::move(table)), _hash(hash) { } public: - explicit TableEvalValue(shared_ptr>> table) : + explicit TableEvalValue(shared_ptr>> table) : _table(std::move(table)), - _hash(rand()) + _hash(Utilities::Random::Get()) { } - inline const TypeClass GetTypeClass() const final { + [[nodiscard]] + inline TypeClass GetTypeClass() const final { return TypeClass::Table; } - inline const size_t GetHashCode() const final { + [[nodiscard]] + inline size_t GetHashCode() const final { return _hash; } - inline const bool operator==(EvalValue *b) const final { + [[nodiscard]] + inline bool operator==(const EvalValue *b) const final { return this->_hash == b->GetHashCode(); } - inline const shared_ptr Clone() const final { + [[nodiscard]] + inline shared_ptr Clone() const final { return shared_ptr(new TableEvalValue(_table, _hash)); } - inline const shared_ptr IndexValue(EvalValue *val) const final { + [[nodiscard]] + inline shared_ptr IndexValue(const EvalValue *val) const final { const auto stringKey = val->EvaluateString(); return this->_table->at(Utilities::HashedString::CreateLookup(stringKey)); } - inline const shared_ptr IndexValue(uint32_t hash) const final { + [[nodiscard]] + inline shared_ptr IndexValue(uint32_t hash) const final { return this->_table->at(Utilities::HashedString::CreateLookup(hash)); } - inline const shared_ptr IndexValue(const char *val) const { + [[nodiscard]] + inline shared_ptr IndexValue(const char *val) const { auto hash = Utilities::HashedString::ConstHash(val); return this->_table->at(Utilities::HashedString::CreateLookup(hash)); } - inline void SetIndexValue(EvalValue *key, const shared_ptr &value) const final { + inline void SetIndexValue(const EvalValue *key, const shared_ptr &value) const final { auto hash = key->GetHashCode(); this->_table->at(Utilities::HashedString::CreateLookup(hash)) = value; } + [[nodiscard]] Iterator * GetKeyIterator() const final; - inline const _Rb_tree_const_iterator>> GetTableIterator() const{ + [[nodiscard]] + inline _Rb_tree_const_iterator>> GetTableIterator() const{ return _table->cbegin(); }; - inline const _Rb_tree_const_iterator>> GetTableIteratorEnd() const{ + [[nodiscard]] + inline _Rb_tree_const_iterator>> GetTableIteratorEnd() const{ return _table->cend(); }; }; diff --git a/src/Evaluator/EvaluationScope/EvaluationScope.cpp b/src/Evaluator/EvaluationScope/EvaluationScope.cpp index ec6cf2f..78a5f2a 100644 --- a/src/Evaluator/EvaluationScope/EvaluationScope.cpp +++ b/src/Evaluator/EvaluationScope/EvaluationScope.cpp @@ -4,14 +4,14 @@ #include namespace Porygon::Evaluation { - EvaluationScope::EvaluationScope(map> *scriptVariables, - int localVariableCount) : _scriptScope(scriptVariables), - _localScope(map>()) { + EvaluationScope::EvaluationScope(map> *scriptVariables) + : _scriptScope(scriptVariables), + _localScope(map>()) { } - void EvaluationScope::CreateVariable(const BoundVariableKey *key, const shared_ptr &value) { + void EvaluationScope::CreateVariable(const BoundVariableKey *key, const shared_ptr& value) { if (key->GetScopeId() == 0) { - _scriptScope->at(key->GetIdentifier()) = value; + _scriptScope->at(*key->GetIdentifier()) = value; } else { auto insert = _localScope.insert({key->GetHash(), value}); if (!insert.second) { @@ -20,20 +20,20 @@ namespace Porygon::Evaluation { } } - void EvaluationScope::SetVariable(const BoundVariableKey *key, const shared_ptr &value) { + void EvaluationScope::SetVariable(const BoundVariableKey *key, const shared_ptr &value) { if (key->GetScopeId() == 0) { - _scriptScope->at(key->GetIdentifier()) = value; + _scriptScope->at(*key->GetIdentifier()) = value; } else { _localScope[key->GetHash()] = value; } } - shared_ptr EvaluationScope::GetVariable(const BoundVariableKey *key) { + shared_ptr EvaluationScope::GetVariable(const BoundVariableKey *key) { auto scopeId = key -> GetScopeId(); if (scopeId== 0) { - return _scriptScope->at(key->GetIdentifier()); + return _scriptScope->at(*key->GetIdentifier()); } else if(scopeId == -2){ - return StandardLibraries::StaticScope::GetVariable(key->GetIdentifier()); + return StandardLibraries::StaticScope::GetVariable(*key->GetIdentifier()); } else { return _localScope[key->GetHash()]; } diff --git a/src/Evaluator/EvaluationScope/EvaluationScope.hpp b/src/Evaluator/EvaluationScope/EvaluationScope.hpp index 9829aef..6001684 100644 --- a/src/Evaluator/EvaluationScope/EvaluationScope.hpp +++ b/src/Evaluator/EvaluationScope/EvaluationScope.hpp @@ -9,18 +9,18 @@ using namespace Porygon::Binder; namespace Porygon::Evaluation { class EvaluationScope { - map> *_scriptScope; - map> _localScope; + map> *_scriptScope; + map> _localScope; public: - explicit EvaluationScope(map> *scriptVariables, int deepestScope); + explicit EvaluationScope(map> *scriptVariables); ~EvaluationScope() = default; - void CreateVariable(const BoundVariableKey *key, const shared_ptr &value); + void CreateVariable(const BoundVariableKey *key, const shared_ptr&value); - void SetVariable(const BoundVariableKey *key, const shared_ptr &value); + void SetVariable(const BoundVariableKey *key, const shared_ptr &value); - shared_ptr GetVariable(const BoundVariableKey *key); + shared_ptr GetVariable(const BoundVariableKey *key); }; } diff --git a/src/Evaluator/Evaluator.cpp b/src/Evaluator/Evaluator.cpp index a094db4..950a809 100644 --- a/src/Evaluator/Evaluator.cpp +++ b/src/Evaluator/Evaluator.cpp @@ -1,9 +1,7 @@ -#include #include #include "Evaluator.hpp" #include "EvaluationException.hpp" -#include "../Script.hpp" #include "EvaluationScope/EvaluationScope.hpp" #include "EvalValues/ScriptFunctionEvalValue.hpp" #include "EvalValues/TableEvalValue.hpp" @@ -11,17 +9,15 @@ #include "../Binder/BoundExpressions/BoundFunctionCallExpression.hpp" #include "../TableScriptType.hpp" #include "../UserData/UserDataFunction.hpp" -#include "../Utilities/StringUtils.hpp" #include "EvalValues/NumericalTableEvalValue.hpp" -#include "../FunctionScriptType.hpp" +#include "../Utilities/Random.hpp" using namespace std; using namespace Porygon::Binder; namespace Porygon::Evaluation { - EvalValue *Evaluator::Evaluate(const BoundScriptStatement *statement) { - this->_evaluationScope = make_shared(this->_scriptVariables, - statement->GetLocalVariableCount()); + const EvalValue *Evaluator::Evaluate(const BoundScriptStatement *statement) { + this->_evaluationScope = make_shared(this->_scriptVariables); EvaluateBlockStatement(statement); return this->_returnValue.get(); } @@ -101,11 +97,11 @@ namespace Porygon::Evaluation { auto option = new Evaluation::EvaluationScriptFunctionOption(block, this->_evaluationScope); if (key->IsCreation()) { - auto value = make_shared(type, rand()); + auto value = make_shared(type, Utilities::Random::Get()); value->RegisterOption(option); this->_evaluationScope->CreateVariable(key, value); } else { - auto var = dynamic_pointer_cast(this -> _evaluationScope ->GetVariable(key)); + auto var = dynamic_pointer_cast(this -> _evaluationScope ->GetVariable(key)); var->RegisterOption(option); this->_evaluationScope->SetVariable(key, var); } @@ -223,7 +219,7 @@ namespace Porygon::Evaluation { // Expressions // ///////////////// - const shared_ptr Evaluator::EvaluateExpression(const BoundExpression *expression) { + shared_ptr Evaluator::EvaluateExpression(const BoundExpression *expression) { auto type = expression->GetType(); switch (type->GetClass()) { case TypeClass::Number: @@ -245,7 +241,7 @@ namespace Porygon::Evaluation { } } - const shared_ptr Evaluator::GetVariable(const BoundVariableExpression *expression) { + shared_ptr Evaluator::GetVariable(const BoundVariableExpression *expression) { auto variable = this->_evaluationScope->GetVariable(expression->GetKey()); if (variable == nullptr) { throw EvaluationException("Variable not found"); @@ -253,7 +249,7 @@ namespace Porygon::Evaluation { return variable->Clone(); } - const shared_ptr Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) { + shared_ptr Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind::LiteralInteger: return make_shared(((BoundLiteralIntegerExpression *) expression)->GetValue()); @@ -264,21 +260,21 @@ namespace Porygon::Evaluation { case BoundExpressionKind::Binary: return this->EvaluateIntegerBinary((BoundBinaryExpression *) expression); case BoundExpressionKind::Variable: - return dynamic_pointer_cast( + return dynamic_pointer_cast( this->GetVariable((BoundVariableExpression *) expression)); case BoundExpressionKind::FunctionCall: - return dynamic_pointer_cast(this->EvaluateFunctionCallExpression(expression)); + return dynamic_pointer_cast(this->EvaluateFunctionCallExpression(expression)); case BoundExpressionKind::Index: - return dynamic_pointer_cast(this->EvaluateIndexExpression(expression)); + return dynamic_pointer_cast(this->EvaluateIndexExpression(expression)); case BoundExpressionKind::PeriodIndex: - return dynamic_pointer_cast(this->EvaluatePeriodIndexExpression(expression)); + return dynamic_pointer_cast(this->EvaluatePeriodIndexExpression(expression)); default: throw; } } - const shared_ptr Evaluator::EvaluateBoolExpression(const BoundExpression *expression) { + shared_ptr Evaluator::EvaluateBoolExpression(const BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind::LiteralBool: return make_shared(((BoundLiteralBoolExpression *) expression)->GetValue()); @@ -287,14 +283,14 @@ namespace Porygon::Evaluation { case BoundExpressionKind::Binary: return this->EvaluateBooleanBinary((BoundBinaryExpression *) expression); case BoundExpressionKind::Variable: - return dynamic_pointer_cast( + return dynamic_pointer_cast( this->GetVariable((BoundVariableExpression *) expression)); case BoundExpressionKind::FunctionCall: - return dynamic_pointer_cast(this->EvaluateFunctionCallExpression(expression)); + return dynamic_pointer_cast(this->EvaluateFunctionCallExpression(expression)); case BoundExpressionKind::Index: - return dynamic_pointer_cast(this->EvaluateIndexExpression(expression)); + return dynamic_pointer_cast(this->EvaluateIndexExpression(expression)); case BoundExpressionKind::PeriodIndex: - return dynamic_pointer_cast(this->EvaluatePeriodIndexExpression(expression)); + return dynamic_pointer_cast(this->EvaluatePeriodIndexExpression(expression)); default: throw; @@ -302,20 +298,20 @@ namespace Porygon::Evaluation { } } - const shared_ptr Evaluator::EvaluateStringExpression(const BoundExpression *expression) { + shared_ptr Evaluator::EvaluateStringExpression(const BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind::LiteralString: - return make_shared(((BoundLiteralStringExpression *) expression)->GetValue()); + return make_shared(*((BoundLiteralStringExpression *) expression)->GetValue()); case BoundExpressionKind::Binary: return this->EvaluateStringBinary((BoundBinaryExpression *) expression); case BoundExpressionKind::Variable: - return dynamic_pointer_cast(this->GetVariable((BoundVariableExpression *) expression)); + return dynamic_pointer_cast(this->GetVariable((BoundVariableExpression *) expression)); case BoundExpressionKind::FunctionCall: - return dynamic_pointer_cast(this->EvaluateFunctionCallExpression(expression)); + return dynamic_pointer_cast(this->EvaluateFunctionCallExpression(expression)); case BoundExpressionKind::Index: - return dynamic_pointer_cast(this->EvaluateIndexExpression(expression)); + return dynamic_pointer_cast(this->EvaluateIndexExpression(expression)); case BoundExpressionKind::PeriodIndex: - return dynamic_pointer_cast(this->EvaluatePeriodIndexExpression(expression)); + return dynamic_pointer_cast(this->EvaluatePeriodIndexExpression(expression)); default: throw; @@ -323,7 +319,7 @@ namespace Porygon::Evaluation { } } - const shared_ptr Evaluator::EvaluateFunctionExpression(const BoundExpression *expression) { + shared_ptr Evaluator::EvaluateFunctionExpression(const BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind::Variable: return this->GetVariable((BoundVariableExpression *) expression); @@ -336,7 +332,7 @@ namespace Porygon::Evaluation { } } - const shared_ptr Evaluator::EvaluateNilExpression(const BoundExpression *expression) { + shared_ptr Evaluator::EvaluateNilExpression(const BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind::FunctionCall: return this->EvaluateFunctionCallExpression(expression); @@ -345,7 +341,7 @@ namespace Porygon::Evaluation { } } - const shared_ptr Evaluator::EvaluateTableExpression(const BoundExpression *expression) { + shared_ptr Evaluator::EvaluateTableExpression(const BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind::FunctionCall: return this->EvaluateFunctionCallExpression(expression); @@ -365,19 +361,19 @@ namespace Porygon::Evaluation { } - const shared_ptr Evaluator::EvaluateFunctionCallExpression(const BoundExpression *expression) { + shared_ptr Evaluator::EvaluateFunctionCallExpression(const BoundExpression *expression) { auto functionCall = (BoundFunctionCallExpression *) expression; - auto function = dynamic_pointer_cast( + auto function = dynamic_pointer_cast( this->EvaluateExpression(functionCall->GetFunctionExpression())); auto boundParameters = functionCall->GetParameters(); - auto parameters = vector>(boundParameters->size()); - for (int i = 0; i < boundParameters->size(); i++) { + auto parameters = vector>(boundParameters->size()); + for (size_t i = 0; i < boundParameters->size(); i++) { parameters[i] = this->EvaluateExpression(boundParameters->at(i)); } - auto type = std::dynamic_pointer_cast(function->GetType()); - auto func = dynamic_pointer_cast(function); + auto type = std::dynamic_pointer_cast(function->GetType()); + auto func = dynamic_pointer_cast(function); auto option = functionCall ->GetFunctionOption(); auto opt = func->GetOption(option->GetOptionId()); if (option -> IsScriptFunction()){ @@ -385,7 +381,7 @@ namespace Porygon::Evaluation { auto scriptFunctionType = dynamic_cast(option); auto parameterKeys = scriptFunctionType->GetParameterKeys(); auto originalScope = this->_evaluationScope; - auto scriptOption = dynamic_pointer_cast(opt); + auto scriptOption = dynamic_pointer_cast(opt); this->_evaluationScope = scriptOption->GetScope(); for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++) { @@ -401,25 +397,25 @@ namespace Porygon::Evaluation { this->_returnValue = nullptr; return r; } else{ - auto scriptOption = dynamic_pointer_cast(opt); - EvalValue* arr[parameters.size()]; - for (int i = 0; i < parameters.size(); i++){ + auto scriptOption = dynamic_pointer_cast(opt); + const EvalValue* arr[parameters.size()]; + for (size_t i = 0; i < parameters.size(); i++){ arr[i] = parameters[i].get(); } - return shared_ptr(scriptOption -> Call(arr, parameters.size())); + return shared_ptr(scriptOption -> Call(arr, parameters.size())); } } - const shared_ptr Evaluator::EvaluateFunction(const GenericFunctionEvalValue *function, + shared_ptr Evaluator::EvaluateFunction(const GenericFunctionEvalValue *function, const vector ¶meters) { - auto type = std::dynamic_pointer_cast(function->GetType()); + auto type = std::dynamic_pointer_cast(function->GetType()); auto option = dynamic_cast(type->GetFirstOption()); auto parameterTypes = option->GetParameterTypes(); auto parameterKeys = option->GetParameterKeys(); auto originalScope = this->_evaluationScope; - auto scriptOption = dynamic_pointer_cast(function->GetOption(0)); + auto scriptOption = dynamic_pointer_cast(function->GetOption(0)); this->_evaluationScope = scriptOption->GetScope(); for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++) { @@ -435,41 +431,41 @@ namespace Porygon::Evaluation { return r; } - const shared_ptr Evaluator::EvaluateIndexExpression(const BoundExpression *expression) { + shared_ptr Evaluator::EvaluateIndexExpression(const BoundExpression *expression) { auto indexExpression = (BoundIndexExpression *) expression; auto index = this->EvaluateExpression(indexExpression->GetIndexExpression()); auto indexable = this->EvaluateExpression(indexExpression->GetIndexableExpression()); - return indexable->IndexValue(index.get())->Clone(); + return indexable->IndexValue(index.get()); } - const shared_ptr Evaluator::EvaluatePeriodIndexExpression(const BoundExpression *expression) { + shared_ptr Evaluator::EvaluatePeriodIndexExpression(const BoundExpression *expression) { auto indexExpression = (BoundPeriodIndexExpression *) expression; - auto index = indexExpression->GetIndex().GetHash(); + auto index = indexExpression->GetIndex()->GetHash(); auto indexable = this->EvaluateExpression(indexExpression->GetIndexableExpression()); return indexable->IndexValue(index)->Clone(); } - const shared_ptr Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) { + shared_ptr Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) { auto tableExpression = (BoundNumericalTableExpression *) expression; auto valueExpressions = tableExpression->GetExpressions(); - auto values = new vector>(valueExpressions->size()); - for (int i = 0; i < valueExpressions->size(); i++) { + auto values = new vector>(valueExpressions->size()); + for (size_t i = 0; i < valueExpressions->size(); i++) { auto val = this->EvaluateExpression(valueExpressions->at(i)); values->at(i) = val; } - auto valuesPointer = shared_ptr>>(values); + auto valuesPointer = shared_ptr>>(values); return make_shared(valuesPointer); } - const shared_ptr Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) { + shared_ptr Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) { auto tableExpression = (BoundTableExpression *) expression; - auto type = dynamic_pointer_cast(tableExpression->GetType()); + auto type = dynamic_pointer_cast(tableExpression->GetType()); auto declaredVars = type->GetValues(); - auto variables = make_shared>>(); + auto variables = make_shared>>(); for (const auto& i : *declaredVars) { variables->insert({i.first, nullptr}); } - auto evaluator = make_shared(variables.get(), type->GetLocalVariableCount()); + auto evaluator = make_shared(variables.get()); auto currentEvaluator = this->_evaluationScope; this->_evaluationScope = evaluator; this->EvaluateBlockStatement(tableExpression->GetBlock()); @@ -477,7 +473,7 @@ namespace Porygon::Evaluation { return make_shared(variables); } - const shared_ptr Evaluator::EvaluateUserDataExpression(const BoundExpression *expression) { + shared_ptr Evaluator::EvaluateUserDataExpression(const BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind::Variable: return this->GetVariable((BoundVariableExpression *) expression); diff --git a/src/Evaluator/Evaluator.hpp b/src/Evaluator/Evaluator.hpp index b98d4e4..8feab27 100644 --- a/src/Evaluator/Evaluator.hpp +++ b/src/Evaluator/Evaluator.hpp @@ -15,11 +15,11 @@ using namespace std; namespace Porygon::Evaluation{ class Evaluator { - shared_ptr _returnValue; - map>* _scriptVariables; + shared_ptr _returnValue; + map>* _scriptVariables; bool _hasReturned; bool _hasBroken; - shared_ptr _lastValue; + shared_ptr _lastValue; //Porygon::Script* _scriptData; shared_ptr _evaluationScope; @@ -36,39 +36,39 @@ namespace Porygon::Evaluation{ void EvaluateGenericForStatement(const BoundGenericForStatement *statement); void EvaluateWhileStatement(const BoundWhileStatement *statement); - const shared_ptr EvaluateExpression(const BoundExpression *expression); - const shared_ptr EvaluateIntegerExpression(const BoundExpression *expression); - const shared_ptr EvaluateBoolExpression(const BoundExpression *expression); - const shared_ptr EvaluateStringExpression(const BoundExpression *expression); - const shared_ptr EvaluateFunctionExpression(const BoundExpression *expression); - const shared_ptr EvaluateNilExpression(const BoundExpression *expression); - const shared_ptr EvaluateTableExpression(const BoundExpression *expression); + shared_ptr EvaluateExpression(const BoundExpression *expression); + shared_ptr EvaluateIntegerExpression(const BoundExpression *expression); + shared_ptr EvaluateBoolExpression(const BoundExpression *expression); + shared_ptr EvaluateStringExpression(const BoundExpression *expression); + shared_ptr EvaluateFunctionExpression(const BoundExpression *expression); + shared_ptr EvaluateNilExpression(const BoundExpression *expression); + shared_ptr EvaluateTableExpression(const BoundExpression *expression); - const shared_ptr EvaluateIntegerBinary(const BoundBinaryExpression *expression); - const shared_ptr EvaluateBooleanBinary(const BoundBinaryExpression *expression); - const shared_ptr EvaluateStringBinary(const BoundBinaryExpression *expression); + shared_ptr EvaluateIntegerBinary(const BoundBinaryExpression *expression); + shared_ptr EvaluateBooleanBinary(const BoundBinaryExpression *expression); + shared_ptr EvaluateStringBinary(const BoundBinaryExpression *expression); - const shared_ptr EvaluateIntegerUnary(const BoundUnaryExpression *expression); - const shared_ptr EvaluateBooleanUnary(const BoundUnaryExpression *expression); - const shared_ptr EvaluateFunctionCallExpression(const BoundExpression *expression); - const shared_ptr EvaluateIndexExpression(const BoundExpression *expression); - const shared_ptr EvaluatePeriodIndexExpression(const BoundExpression *expression); - const shared_ptr EvaluateNumericTableExpression(const BoundExpression *expression); - const shared_ptr EvaluateComplexTableExpression(const BoundExpression *expression); - const shared_ptr EvaluateUserDataExpression(const BoundExpression *expression); + shared_ptr EvaluateIntegerUnary(const BoundUnaryExpression *expression); + shared_ptr EvaluateBooleanUnary(const BoundUnaryExpression *expression); + shared_ptr EvaluateFunctionCallExpression(const BoundExpression *expression); + shared_ptr EvaluateIndexExpression(const BoundExpression *expression); + shared_ptr EvaluatePeriodIndexExpression(const BoundExpression *expression); + shared_ptr EvaluateNumericTableExpression(const BoundExpression *expression); + shared_ptr EvaluateUserDataExpression(const BoundExpression *expression); + shared_ptr EvaluateComplexTableExpression(const BoundExpression *expression); - const shared_ptr GetVariable(const BoundVariableExpression *expression); + shared_ptr GetVariable(const BoundVariableExpression *expression); public: - explicit Evaluator(map>* scriptVariables) + explicit Evaluator(map>* scriptVariables) : _scriptVariables(scriptVariables), _hasReturned(false), _hasBroken(false), _returnValue(nullptr), _evaluationScope(nullptr){ } - EvalValue* Evaluate(const BoundScriptStatement* statement); - const shared_ptr EvaluateFunction(const GenericFunctionEvalValue *function, + const EvalValue* Evaluate(const BoundScriptStatement* statement); + shared_ptr EvaluateFunction(const GenericFunctionEvalValue *function, const vector ¶meters); - EvalValue* GetLastValue(){ + inline const EvalValue* GetLastValue(){ return _lastValue.get(); } diff --git a/src/Evaluator/Iterator/NumericalKeyIterator.hpp b/src/Evaluator/Iterator/NumericalKeyIterator.hpp index c869180..db8d18d 100644 --- a/src/Evaluator/Iterator/NumericalKeyIterator.hpp +++ b/src/Evaluator/Iterator/NumericalKeyIterator.hpp @@ -9,7 +9,7 @@ namespace Porygon::Evaluation{ class NumericalKeyIterator : public Iterator{ - const shared_ptr>> _vec; + const shared_ptr>> _vec; const size_t _size; long _position = 0; public: diff --git a/src/Evaluator/Iterator/SimpleKeyIterator.hpp b/src/Evaluator/Iterator/SimpleKeyIterator.hpp index 9f212f4..5641ca0 100644 --- a/src/Evaluator/Iterator/SimpleKeyIterator.hpp +++ b/src/Evaluator/Iterator/SimpleKeyIterator.hpp @@ -8,8 +8,8 @@ namespace Porygon::Evaluation{ class TableKeyIterator : public Iterator{ - _Rb_tree_const_iterator>> _iterator; - _Rb_tree_const_iterator>> _end; + _Rb_tree_const_iterator>> _iterator; + _Rb_tree_const_iterator>> _end; bool _hasStarted = false; public: explicit TableKeyIterator(const TableEvalValue* table) diff --git a/src/Evaluator/UnaryEvaluation.cpp b/src/Evaluator/UnaryEvaluation.cpp index 577f80a..aba38d8 100644 --- a/src/Evaluator/UnaryEvaluation.cpp +++ b/src/Evaluator/UnaryEvaluation.cpp @@ -5,7 +5,7 @@ #include "../Script.hpp" namespace Porygon::Evaluation { - const shared_ptr Evaluator::EvaluateIntegerUnary(const BoundUnaryExpression *expression) { + shared_ptr Evaluator::EvaluateIntegerUnary(const BoundUnaryExpression *expression) { switch (expression->GetOperation()) { case BoundUnaryOperation::Negation: { auto operandValue = EvaluateIntegerExpression(expression->GetOperand()); @@ -22,7 +22,7 @@ namespace Porygon::Evaluation { } } - const shared_ptr Evaluator::EvaluateBooleanUnary(const BoundUnaryExpression *expression) { + shared_ptr Evaluator::EvaluateBooleanUnary(const BoundUnaryExpression *expression) { switch (expression->GetOperation()) { case BoundUnaryOperation::LogicalNegation: { auto val = EvaluateBoolExpression(expression->GetOperand()); diff --git a/src/FunctionScriptType.hpp b/src/FunctionScriptType.hpp index 772e6f7..f67f6c3 100644 --- a/src/FunctionScriptType.hpp +++ b/src/FunctionScriptType.hpp @@ -7,7 +7,7 @@ namespace Porygon { class GenericFunctionOption{ - shared_ptr _returnType; + shared_ptr _returnType; vector> _parameterTypes; size_t _option = 0; public: @@ -17,7 +17,7 @@ namespace Porygon { virtual ~GenericFunctionOption() = default; - inline const shared_ptr GetReturnType() const { + [[nodiscard]] inline shared_ptr GetReturnType() const { return _returnType; } @@ -25,19 +25,19 @@ namespace Porygon { _option = v; } - inline void SetReturnType(shared_ptr t) { - _returnType = move(t); + inline void SetReturnType(const shared_ptr& t) { + _returnType = t; } - inline const vector> GetParameterTypes() const { + [[nodiscard]] inline vector> GetParameterTypes() const { return _parameterTypes; } - const bool IsValid(const vector>& parameters){ + bool IsValid(const vector>& parameters){ if (parameters.size() != _parameterTypes.size()){ return false; } - for (int i = 0; i < parameters.size(); i++){ + for (size_t i = 0; i < parameters.size(); i++){ if (parameters[i]->operator!=(_parameterTypes[i].get())){ return false; } @@ -45,15 +45,15 @@ namespace Porygon { return true; } - inline const size_t GetOptionId() const{ + [[nodiscard]] inline size_t GetOptionId() const{ return _option; } - virtual const bool IsScriptFunction() const = 0; + [[nodiscard]] virtual bool IsScriptFunction() const = 0; }; class GenericFunctionScriptType : public Porygon::ScriptType { - vector _options; + vector* _options = new vector; public: GenericFunctionScriptType() : ScriptType(Porygon::TypeClass::Function){}; @@ -64,18 +64,18 @@ namespace Porygon { }; ~GenericFunctionScriptType() final{ - for (auto o: _options){ + for (auto o: *_options){ delete o; } } - void RegisterFunctionOption (GenericFunctionOption * opt){ - opt->SetOption(_options.size()); - _options.push_back(opt); + void RegisterFunctionOption (GenericFunctionOption * opt) const{ + opt->SetOption(_options->size()); + _options->push_back(opt); } - GenericFunctionOption* GetFunctionOption(const vector>& parameters){ - for (auto o: _options){ + GenericFunctionOption* GetFunctionOption(const vector>& parameters) const{ + for (auto o: *_options){ if (o->IsValid(parameters)){ return o; } @@ -83,24 +83,24 @@ namespace Porygon { return nullptr; } - inline GenericFunctionOption* GetFirstOption(){ - return _options[0]; + [[nodiscard]] inline GenericFunctionOption* GetFirstOption() const{ + return _options->at(0); } }; class ScriptFunctionOption : public GenericFunctionOption { - vector> _parameterKeys; + vector> _parameterKeys; public: ScriptFunctionOption(shared_ptr returnType, vector> parameterTypes, - vector> parameterKeys) + vector> parameterKeys) : GenericFunctionOption(move(returnType), std::move(parameterTypes)), _parameterKeys(move(parameterKeys)) { } - inline const vector> GetParameterKeys() const { + [[nodiscard]] inline vector> GetParameterKeys() const { return _parameterKeys; } - inline const bool IsScriptFunction() const final { + [[nodiscard]] inline bool IsScriptFunction() const final { return true; } }; diff --git a/src/Parser/Lexer.cpp b/src/Parser/Lexer.cpp index 18c3078..23b3b2e 100644 --- a/src/Parser/Lexer.cpp +++ b/src/Parser/Lexer.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -175,7 +174,7 @@ namespace Porygon::Parser { this->Next(); has_point = true; decimal_index = 0; - float_value = int_value; + float_value = (double) int_value; length++; continue; default: @@ -296,7 +295,7 @@ namespace Porygon::Parser { u16string s = this->_scriptString.substr(start + 1, end - start); std::basic_ostringstream stream; - for (int i = 0; i < s.size(); i++) { + for (size_t i = 0; i < s.size(); i++) { c = s[i]; if (c == '\\') { i++; diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index 826dfcf..f65a59c 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -1,4 +1,3 @@ -#include #include #include "Parser.hpp" #include "ParsedStatements/ParsedStatement.hpp" diff --git a/src/Parser/Parser.hpp b/src/Parser/Parser.hpp index 503ca86..01f1494 100644 --- a/src/Parser/Parser.hpp +++ b/src/Parser/Parser.hpp @@ -1,6 +1,3 @@ -#include - - #ifndef PORYGONLANG_PARSER_HPP #define PORYGONLANG_PARSER_HPP diff --git a/src/Parser/Token.hpp b/src/Parser/Token.hpp index 4c44da6..354f3d3 100644 --- a/src/Parser/Token.hpp +++ b/src/Parser/Token.hpp @@ -1,6 +1,3 @@ -#include - - #ifndef PORYGONLANG_TOKEN_HPP #define PORYGONLANG_TOKEN_HPP diff --git a/src/Parser/TypedVariableIdentifier.hpp b/src/Parser/TypedVariableIdentifier.hpp index b701e73..7c5677a 100644 --- a/src/Parser/TypedVariableIdentifier.hpp +++ b/src/Parser/TypedVariableIdentifier.hpp @@ -6,19 +6,19 @@ namespace Porygon::Parser { class TypedVariableIdentifier { - HashedString _type; - HashedString _identifier; + const HashedString _type; + const HashedString _identifier; public: TypedVariableIdentifier(const HashedString& type, const HashedString& identifier) : _type(type), _identifier(identifier) { } - inline HashedString GetType() { - return _type; + inline const HashedString* GetType() { + return &_type; } - inline HashedString GetIdentifier() { - return _identifier; + inline const HashedString* GetIdentifier() { + return &_identifier; } }; } diff --git a/src/Script.cpp b/src/Script.cpp index ecb9f92..52a0023 100644 --- a/src/Script.cpp +++ b/src/Script.cpp @@ -1,10 +1,7 @@ -#include - #include #include #include #include -#include #include #include "Script.hpp" #include "Parser/Lexer.hpp" @@ -27,13 +24,13 @@ Porygon::Script *Porygon::Script::Create(const string &script) { Porygon::Script::Script(const u16string& s) : Diagnostics(make_shared(s)), _boundScript(nullptr), - _scriptVariables(new map>()) + _scriptVariables(new map>()) { _evaluator = new Evaluator(this -> _scriptVariables); this -> Parse(s); } -EvalValue* Porygon::Script::Evaluate() { +const EvalValue* Porygon::Script::Evaluate() { return _evaluator->Evaluate(_boundScript.get()); } @@ -65,7 +62,7 @@ void Porygon::Script::Parse(const u16string& script) { delete parseResult; } -EvalValue *Porygon::Script::GetVariable(const u16string &key) { +const EvalValue *Porygon::Script::GetVariable(const u16string &key) { return _scriptVariables -> at(HashedString::CreateLookup(key)).get(); } @@ -74,7 +71,7 @@ bool Porygon::Script::HasVariable(const u16string &key) { return f != _scriptVariables->end(); } -EvalValue *Porygon::Script::GetLastValue() { +const EvalValue *Porygon::Script::GetLastValue() { return _evaluator->GetLastValue(); } @@ -83,7 +80,7 @@ bool Porygon::Script::HasFunction(const u16string &key) { return f != _scriptVariables->end() && f.operator->()->second->GetTypeClass() == TypeClass ::Function; } -shared_ptr Porygon::Script::CallFunction(const u16string &key, const vector& variables) { +shared_ptr Porygon::Script::CallFunction(const u16string &key, const vector& variables) { auto var = (GenericFunctionEvalValue*)GetVariable(key); return this->_evaluator->EvaluateFunction(var, variables); } @@ -101,7 +98,7 @@ Porygon::Script::Script(shared_ptr boundScript, shared_ptr diagnostics) : _boundScript(std::move(boundScript)), Diagnostics(std::move(diagnostics)), - _scriptVariables(new map>()) + _scriptVariables(new map>()) { _evaluator = new Evaluator(_scriptVariables); } @@ -116,7 +113,7 @@ extern "C" { script->Evaluate(); } - EvalValue* GetLastValue(Porygon::Script* script){ + const EvalValue* GetLastValue(Porygon::Script* script){ return script->GetLastValue(); } @@ -124,7 +121,7 @@ extern "C" { return script->HasVariable(key); } - EvalValue* GetVariable(Porygon::Script* script, const char16_t* key){ + const EvalValue* GetVariable(Porygon::Script* script, const char16_t* key){ return script->GetVariable(key); } @@ -132,7 +129,7 @@ extern "C" { return script->HasFunction(key); } - EvalValue* CallFunction(Porygon::Script* script, const char16_t* key, EvalValue* parameters[], int parameterCount){ + const EvalValue* CallFunction(Porygon::Script* script, const char16_t* key, EvalValue* parameters[], int parameterCount){ std::vector v(parameters, parameters + parameterCount); return script->CallFunction(key, v).get(); } diff --git a/src/Script.hpp b/src/Script.hpp index 4ac06a6..52fc453 100644 --- a/src/Script.hpp +++ b/src/Script.hpp @@ -17,9 +17,9 @@ using namespace Porygon::Evaluation; namespace Porygon{ class Script { Evaluator* _evaluator; - map>* _scriptVariables; + map>* _scriptVariables; shared_ptr _boundScript; - shared_ptr _returnType; + shared_ptr _returnType; explicit Script(const u16string&); Script(shared_ptr boundScript, shared_ptr diagnostics); @@ -33,22 +33,22 @@ namespace Porygon{ ~Script(); - inline shared_ptr GetReturnType(){ + inline shared_ptr GetReturnType(){ return _returnType; } - inline void SetReturnType(shared_ptr t){ - _returnType = std::move(t); + inline void SetReturnType(const shared_ptr& t){ + _returnType = t; } - EvalValue* Evaluate(); + const EvalValue* Evaluate(); - EvalValue* GetLastValue(); + const EvalValue* GetLastValue(); - EvalValue* GetVariable(const u16string& key); + const EvalValue* GetVariable(const u16string& key); bool HasVariable(const u16string& key); - shared_ptr CallFunction(const u16string& key, const vector& variables); + shared_ptr CallFunction(const u16string& key, const vector& variables); bool HasFunction(const u16string& key); }; diff --git a/src/ScriptType.cpp b/src/ScriptType.cpp index de4e34a..b2d2fc1 100644 --- a/src/ScriptType.cpp +++ b/src/ScriptType.cpp @@ -2,11 +2,11 @@ #include "UserData/UserDataFunctionType.hpp" namespace Porygon{ - inline const bool ScriptType::CanBeIndexedWith(ScriptType *indexer) const{ + inline bool ScriptType::CanBeIndexedWith(const ScriptType *) const{ return false; } - const shared_ptr ScriptType::GetIndexedType(ScriptType*) const{ + shared_ptr ScriptType::GetIndexedType(const ScriptType*) const{ if (_class == TypeClass::String){ return make_shared(TypeClass::String); } diff --git a/src/ScriptType.hpp b/src/ScriptType.hpp index c9a4830..9a283ce 100644 --- a/src/ScriptType.hpp +++ b/src/ScriptType.hpp @@ -41,31 +41,36 @@ namespace Porygon{ return _class == b._class; }; - virtual bool operator ==(ScriptType* b) const{ + virtual bool operator ==(const ScriptType* b) const{ return _class == b->_class; }; virtual bool operator !=(const ScriptType& b) const{ return ! (operator==(b)); } - virtual bool operator !=(ScriptType* b) const{ + virtual bool operator !=(const ScriptType* b) const{ return ! (operator==(b)); } - virtual const bool CanBeIndexedWith(ScriptType* indexer) const; - virtual const bool CanBeIndexedWithIdentifier(uint32_t hash) const{ + virtual bool CanBeIndexedWith(const ScriptType* indexer) const; + [[nodiscard]] + virtual bool CanBeIndexedWithIdentifier(uint32_t hash) const{ return false; } - virtual const shared_ptr GetIndexedType(ScriptType* indexer) const; - virtual const shared_ptr GetIndexedType(uint32_t hash) const{ + [[nodiscard]] + virtual shared_ptr GetIndexedType(const ScriptType* indexer) const; + [[nodiscard]] + virtual shared_ptr GetIndexedType(uint32_t hash) const{ throw "This type told the binder it can be indexed, but it does not implement the resulting type."; } - virtual const bool CanBeIterated() const{ + [[nodiscard]] + virtual bool CanBeIterated() const{ return false; } - virtual shared_ptr GetIteratorKeyType() const{ + [[nodiscard]] + virtual shared_ptr GetIteratorKeyType() const{ throw "This type told the binder it can be iterated, but it does not implement the resulting type."; } }; @@ -81,11 +86,11 @@ namespace Porygon{ _isFloat = isFloat; } - inline const bool IsAwareOfFloat() const{ + [[nodiscard]] inline bool IsAwareOfFloat() const{ return _awareOfFloat; } - inline const bool IsFloat() const{ + [[nodiscard]] inline bool IsFloat() const{ return _isFloat; } }; @@ -99,49 +104,54 @@ namespace Porygon{ _hashValue = hashValue; } - const bool CanBeIndexedWith(ScriptType* indexer) const final{ + [[nodiscard]] + bool CanBeIndexedWith(const ScriptType* indexer) const final{ if (indexer -> GetClass() != TypeClass::Number) return false; - auto num = dynamic_cast(indexer); + auto num = dynamic_cast(indexer); return !(num->IsAwareOfFloat() && num->IsFloat()); } - inline const shared_ptr GetIndexedType(ScriptType* indexer) const final{ + inline shared_ptr GetIndexedType(const ScriptType* indexer) const final{ return make_shared(false, 0); } - inline const bool IsKnownAtBind() const{ + [[nodiscard]] + inline bool IsKnownAtBind() const{ return _isKnownAtBind; } - inline const uint32_t GetHashValue() const{ + [[nodiscard]] + inline uint32_t GetHashValue() const{ return _hashValue; } }; class NumericalTableScriptType : public ScriptType{ - shared_ptr _valueType; + shared_ptr _valueType; // Consider adding a check whether the table actually contains a type if every key is static. public: - explicit NumericalTableScriptType(shared_ptr valueType) + explicit NumericalTableScriptType(shared_ptr valueType) : ScriptType(TypeClass::Table), _valueType(std::move(valueType)){ } - const bool CanBeIndexedWith(ScriptType* indexer) const final{ + bool CanBeIndexedWith(const ScriptType* indexer) const final{ if (indexer -> GetClass() != TypeClass::Number) return false; - auto num = dynamic_cast(indexer); + auto num = dynamic_cast(indexer); return !(num->IsAwareOfFloat() && num->IsFloat()); } - inline const shared_ptr GetIndexedType(ScriptType* indexer) const final{ + inline shared_ptr GetIndexedType(const ScriptType* indexer) const final{ return _valueType; } - inline const bool CanBeIterated() const final{ + [[nodiscard]] + inline bool CanBeIterated() const final{ return true; } - inline shared_ptr GetIteratorKeyType() const final{ + [[nodiscard]] + inline shared_ptr GetIteratorKeyType() const final{ return make_shared(true, false); } }; diff --git a/src/StandardLibraries/BasicLibrary.hpp b/src/StandardLibraries/BasicLibrary.hpp index 4f43cb2..8705749 100644 --- a/src/StandardLibraries/BasicLibrary.hpp +++ b/src/StandardLibraries/BasicLibrary.hpp @@ -15,13 +15,13 @@ namespace Porygon::StandardLibraries{ class BasicLibrary{ - static Evaluation::EvalValue* _error(void*, Evaluation::EvalValue* parameters[], int parameterCount){ + static const Evaluation::EvalValue* _error(void*, const Evaluation::EvalValue* parameters[], int parameterCount){ auto message = parameters[0]->EvaluateString(); auto conv = Utilities::StringUtils::FromUTF8(message); throw Evaluation::EvaluationException(conv); } - static Evaluation::EvalValue* _assert(void*, Evaluation::EvalValue* parameters[], int parameterCount){ + static const Evaluation::EvalValue* _assert(void*, const Evaluation::EvalValue* parameters[], int parameterCount){ auto assertion = parameters[0]->EvaluateBool(); if (!assertion){ if (parameterCount >= 2){ @@ -34,13 +34,13 @@ namespace Porygon::StandardLibraries{ return new Evaluation::BooleanEvalValue(true); } - static Evaluation::EvalValue* _print(void*, Evaluation::EvalValue* parameters[], int parameterCount){ + static const Evaluation::EvalValue* _print(void*, const Evaluation::EvalValue* parameters[], int parameterCount){ auto message = parameters[0]->EvaluateString(); GlobalScriptOptions::Print(message.c_str()); return new Evaluation::NilEvalValue(); } - static Evaluation::EvalValue* _toInt(void*, Evaluation::EvalValue* parameters[], int parameterCount){ + static const Evaluation::EvalValue* _toInt(void*, const Evaluation::EvalValue* parameters[], int parameterCount){ auto parameter = parameters[0]->EvaluateString(); auto parsed = Utilities::StringUtils::ParseInteger(parameter); return new Evaluation::IntegerEvalValue(parsed); @@ -75,10 +75,10 @@ namespace Porygon::StandardLibraries{ static shared_ptr GetFuncEvalValue( - Evaluation::EvalValue* (*func)(void* obj, Evaluation::EvalValue* parameters[], int parameterCount), - shared_ptr type, size_t optionLength){ + const Evaluation::EvalValue* (*func)(void* obj, const Evaluation::EvalValue* parameters[], int parameterCount), + const shared_ptr& type, size_t optionLength){ auto f = make_shared(type, rand()); - for (int i = 0; i < optionLength; i++){ + for (size_t i = 0; i < optionLength; i++){ auto funcOption = new UserData::UserDataFunction(func, nullptr); f->RegisterOption(funcOption); } diff --git a/src/TableScriptType.hpp b/src/TableScriptType.hpp index b601a16..0af352b 100644 --- a/src/TableScriptType.hpp +++ b/src/TableScriptType.hpp @@ -22,33 +22,30 @@ namespace Porygon{ delete _values; } - inline const bool CanBeIndexedWith(ScriptType* indexer) const final{ + [[nodiscard]] + inline bool CanBeIndexedWith(const ScriptType* indexer) const final{ return indexer->GetClass() == TypeClass ::String; } - const bool CanBeIndexedWithIdentifier(uint32_t hash) const final{ + [[nodiscard]] bool CanBeIndexedWithIdentifier(uint32_t hash) const final{ return true; } - const shared_ptr GetIndexedType(ScriptType* indexer) const final{ - auto stringKey = dynamic_cast(indexer); + shared_ptr GetIndexedType(const ScriptType* indexer) const final{ + auto stringKey = dynamic_cast(indexer); if (stringKey->IsKnownAtBind()){ return _values-> at(Utilities::HashedString::CreateLookup(stringKey->GetHashValue()))->GetType(); } throw "TODO: indexing with dynamic keys"; } - inline const shared_ptr GetIndexedType(uint32_t hash) const final{ + [[nodiscard]] inline shared_ptr GetIndexedType(uint32_t hash) const final{ return _values-> at(Utilities::HashedString::CreateLookup(hash))->GetType(); } - inline const map* GetValues() const{ + [[nodiscard]] inline const map* GetValues() const{ return _values; } - - inline const int GetLocalVariableCount() const{ - return _localVariableCount; - } }; } diff --git a/src/UserData/UserData.hpp b/src/UserData/UserData.hpp index b76ff59..326c83a 100644 --- a/src/UserData/UserData.hpp +++ b/src/UserData/UserData.hpp @@ -1,5 +1,3 @@ -#include - #ifndef PORYGONLANG_USERDATA_HPP #define PORYGONLANG_USERDATA_HPP @@ -52,10 +50,12 @@ namespace Porygon::UserData { delete _concatenation; } + [[nodiscard]] inline bool ContainsField(uint32_t fieldId) const{ return _fields.find(fieldId) != _fields.end(); } + [[nodiscard]] inline UserDataField *GetField(uint32_t fieldId) const { return _fields.at(fieldId); } @@ -64,10 +64,12 @@ namespace Porygon::UserData { _fields.insert({fieldId, field}); } + [[nodiscard]] inline int32_t GetFieldCount() const{ return _fields.size(); } + [[nodiscard]] UserDataBinaryOperation* GetBinaryOperation(Binder::BoundBinaryOperation op){ switch (op){ diff --git a/src/UserData/UserDataField.cpp b/src/UserData/UserDataField.cpp index 6275dc4..4756010 100644 --- a/src/UserData/UserDataField.cpp +++ b/src/UserData/UserDataField.cpp @@ -4,7 +4,9 @@ namespace Porygon::UserData { extern "C" { UserDataField * - CreateUserDataField(ScriptType *type, Evaluation::EvalValue *(*getter)(void *obj), void (*setter)(void *obj, Evaluation::EvalValue *val)) { + CreateUserDataField(ScriptType *type, + const Evaluation::EvalValue *(*getter)(void *), + void (*setter)(void *, const Evaluation::EvalValue *)) { return new UserDataField(type, getter, setter); } } diff --git a/src/UserData/UserDataField.hpp b/src/UserData/UserDataField.hpp index 8a92898..4d527cc 100644 --- a/src/UserData/UserDataField.hpp +++ b/src/UserData/UserDataField.hpp @@ -8,31 +8,35 @@ namespace Porygon::UserData{ class UserDataField { - shared_ptr _type; - Evaluation::EvalValue* (*_get)(void* obj); - void (*_set)(void* obj, Evaluation::EvalValue* val); + shared_ptr _type; + const Evaluation::EvalValue* (*_get)(void* obj); + void (*_set)(void* obj, const Evaluation::EvalValue* val); public: - UserDataField(ScriptType* type, Evaluation::EvalValue* (*getter)(void* obj), void (*setter)(void* obj, Evaluation::EvalValue* val)) - : _type(shared_ptr(type)), _get(getter), _set(setter){ + 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){ } - inline shared_ptr GetType(){ + [[nodiscard]] + inline shared_ptr GetType(){ return _type; } + [[nodiscard]] inline bool HasGetter(){ return _get != nullptr; } - inline Evaluation::EvalValue* Get(void* obj){ + [[nodiscard]] + inline const Evaluation::EvalValue* Get(void* obj){ return this ->_get(obj); } + [[nodiscard]] inline bool HasSetter(){ return _set != nullptr; } - inline void Set(void* obj, Evaluation::EvalValue* val){ + inline void Set(void* obj, const Evaluation::EvalValue* val){ this->_set(obj, val); } }; diff --git a/src/UserData/UserDataFunction.cpp b/src/UserData/UserDataFunction.cpp index a729c9e..fff5406 100644 --- a/src/UserData/UserDataFunction.cpp +++ b/src/UserData/UserDataFunction.cpp @@ -5,7 +5,7 @@ using namespace Porygon::Evaluation; namespace Porygon::UserData{ extern "C" { - EvalValue * CreateFunctionEvalValue(Evaluation::EvalValue* (*func)(void* , EvalValue* [], int ), void* obj) { + const EvalValue * CreateFunctionEvalValue(const Evaluation::EvalValue* (*func)(void* , const EvalValue* [], int ), void* obj) { auto opt = new UserDataFunction(func, obj); auto t = new GenericFunctionEvalValue(make_shared(), rand()); t->RegisterOption(opt); diff --git a/src/UserData/UserDataFunction.hpp b/src/UserData/UserDataFunction.hpp index d743651..027f93e 100644 --- a/src/UserData/UserDataFunction.hpp +++ b/src/UserData/UserDataFunction.hpp @@ -1,33 +1,31 @@ #ifndef PORYGONLANG_USERDATAFUNCTION_HPP #define PORYGONLANG_USERDATAFUNCTION_HPP -#include #include "../Evaluator/EvalValues/ScriptFunctionEvalValue.hpp" #include "UserDataFunctionType.hpp" #include "../FunctionScriptType.hpp" namespace Porygon::UserData{ class UserDataFunction : public Evaluation::GenericFunctionOption { - Evaluation::EvalValue* (*_call)(void* obj, Evaluation::EvalValue* parameters[], int parameterCount); + const Evaluation::EvalValue* (*_call)(void* obj, const Evaluation::EvalValue* parameters[], int parameterCount); void *_obj; - UserDataFunction(Evaluation::EvalValue* (*call)(void* obj, Evaluation::EvalValue* parameters[], int parameterCount), void* obj, - shared_ptr type, size_t hash) : GenericFunctionOption(){ + UserDataFunction(const Evaluation::EvalValue* (*call)(void* obj, const Evaluation::EvalValue* parameters[], int parameterCount), void* obj, + const shared_ptr& type, size_t hash) : GenericFunctionOption(){ _call = call; _obj = obj; } public: - UserDataFunction(Evaluation::EvalValue* (*call)(void* obj, Evaluation::EvalValue* parameters[], int parameterCount), void* obj) : + UserDataFunction(const Evaluation::EvalValue* (*call)(void* obj, const Evaluation::EvalValue* parameters[], int parameterCount), void* obj) : GenericFunctionOption(){ _call = call; _obj = obj; } - ~UserDataFunction() final{ + ~UserDataFunction() final = default; - } - - inline Evaluation::EvalValue* Call(Evaluation::EvalValue* parameters[], int parameterCount){ + [[nodiscard]] + inline const Evaluation::EvalValue* Call(const Evaluation::EvalValue* parameters[], int parameterCount) const{ return _call(_obj, parameters, parameterCount); } }; diff --git a/src/UserData/UserDataFunctionType.hpp b/src/UserData/UserDataFunctionType.hpp index 047f0cc..4ade87a 100644 --- a/src/UserData/UserDataFunctionType.hpp +++ b/src/UserData/UserDataFunctionType.hpp @@ -15,7 +15,7 @@ namespace Porygon::UserData{ static UserDataFunctionOption* FromRawPointers(ScriptType* returnType, vector parameterTypes){ auto rt = shared_ptr(returnType); auto p = vector>(parameterTypes.size()); - for (int i = 0; i < parameterTypes.size(); i++){ + for (size_t i = 0; i < parameterTypes.size(); i++){ p[i] = shared_ptr(parameterTypes[i]); } return new UserDataFunctionOption(rt, p); @@ -26,10 +26,7 @@ namespace Porygon::UserData{ return new UserDataFunctionOption(rt, {}); } - - - - inline const bool IsScriptFunction() const final{ + [[nodiscard]] inline bool IsScriptFunction() const final{ return false; } }; diff --git a/src/UserData/UserDataOperation.hpp b/src/UserData/UserDataOperation.hpp index b8515f1..63fa39f 100644 --- a/src/UserData/UserDataOperation.hpp +++ b/src/UserData/UserDataOperation.hpp @@ -9,8 +9,8 @@ namespace Porygon::UserData { class UserDataBinaryOperation { void* _parent; Evaluation::EvalValue *(*_func)(void *obj, Evaluation::EvalValue *b); - const shared_ptr _secondParameter; - const shared_ptr _returnType; + const shared_ptr _secondParameter; + const shared_ptr _returnType; public: UserDataBinaryOperation(void *parent, Evaluation::EvalValue *(*func)(void *, Evaluation::EvalValue *), @@ -23,11 +23,13 @@ namespace Porygon::UserData { return _func(_parent, b); } - inline const shared_ptr GetSecondParameterType() const{ + [[nodiscard]] + inline shared_ptr GetSecondParameterType() const{ return _secondParameter; } - inline const shared_ptr GetReturnType() const{ + [[nodiscard]] + inline shared_ptr GetReturnType() const{ return _returnType; } }; diff --git a/src/UserData/UserDataScriptType.hpp b/src/UserData/UserDataScriptType.hpp index 67cc24e..d829369 100644 --- a/src/UserData/UserDataScriptType.hpp +++ b/src/UserData/UserDataScriptType.hpp @@ -20,33 +20,33 @@ namespace Porygon::UserData { _userData = ud; } - const bool CanBeIndexedWith(ScriptType *indexer) const final { + bool CanBeIndexedWith(const ScriptType *indexer) const final { if (indexer->GetClass() != TypeClass::String) { return false; } - auto str = dynamic_cast(indexer); + auto str = dynamic_cast(indexer); if (!str->IsKnownAtBind()) return false; return _userData->ContainsField(str->GetHashValue()); } - inline const bool CanBeIndexedWithIdentifier(uint32_t hash) const final { + [[nodiscard]] inline bool CanBeIndexedWithIdentifier(uint32_t hash) const final { return _userData -> ContainsField(hash); } - inline UserDataField *GetField(uint32_t id) { + [[nodiscard]] inline UserDataField *GetField(uint32_t id) const{ return _userData->GetField(id); } - const shared_ptr GetIndexedType(ScriptType *indexer) const final { - auto stringKey = dynamic_cast(indexer); + shared_ptr GetIndexedType(const ScriptType *indexer) const final { + auto stringKey = dynamic_cast(indexer); if (stringKey->IsKnownAtBind()) { return _userData->GetField(stringKey->GetHashValue())->GetType(); } throw "TODO: indexing with dynamic keys"; } - inline const shared_ptr GetIndexedType(uint32_t hash) const final { + [[nodiscard]] inline shared_ptr GetIndexedType(uint32_t hash) const final { return _userData->GetField(hash)->GetType(); } }; diff --git a/src/UserData/UserDataTemplates.hpp b/src/UserData/UserDataTemplates.hpp index bdfe3b9..6ec7894 100644 --- a/src/UserData/UserDataTemplates.hpp +++ b/src/UserData/UserDataTemplates.hpp @@ -33,8 +33,8 @@ { \ Porygon::Utilities::HashedString::ConstHash(#fieldName), \ new Porygon::UserData::UserDataField(fieldType, \ - [](void* obj) -> Porygon::Evaluation::EvalValue* { return new getterHelper;}, \ - [](void* obj, Porygon::Evaluation::EvalValue* val) { ((T_USERDATA*)obj)->fieldName = setterHelper;} \ + [](void* obj) -> const Porygon::Evaluation::EvalValue* { return new getterHelper;}, \ + [](void* obj, const Porygon::Evaluation::EvalValue* val) { ((T_USERDATA*)obj)->fieldName = setterHelper;} \ ) \ }, \ @@ -49,7 +49,7 @@ #define PORYGON_INTEGER_FIELD(fieldName) \ PORYGON_FIELD(fieldName, PORYGON_INTEGER_TYPE, \ - Porygon::Evaluation::IntegerEvalValue(((T_USERDATA*)obj)->fieldName), val->EvaluateInteger()) + const Porygon::Evaluation::IntegerEvalValue(((T_USERDATA*)obj)->fieldName), val->EvaluateInteger()) #define PORYGON_READONLY_INTEGER_FIELD(fieldName) \ PORYGON_READONLY_FIELD(fieldName, PORYGON_INTEGER_TYPE, \ @@ -72,11 +72,11 @@ Porygon::UserData::UserDataFunctionOption::FromRawPointers(returnType, {__VA_ARGS__} )), \ \ \ - [](void* obj) -> Porygon::Evaluation::EvalValue* { \ + [](void* obj) -> const Porygon::Evaluation::EvalValue* { \ auto t = new Porygon::Evaluation::GenericFunctionEvalValue(make_shared(), rand()); \ t->RegisterOption(new Porygon::UserData::UserDataFunction( \ - [](void* obj, Porygon::Evaluation::EvalValue* par[], int parameterCount) \ - -> Porygon::Evaluation::EvalValue*{return ((T_USERDATA*)obj)->invoke__##fieldName(obj, par, parameterCount);}, \ + [](void* obj, const Porygon::Evaluation::EvalValue* par[], int parameterCount) \ + -> const Porygon::Evaluation::EvalValue*{return ((const T_USERDATA*)obj)->invoke__##fieldName(obj, par, parameterCount);}, \ obj)); \ return t;}, \ nullptr) \ @@ -96,7 +96,7 @@ \returns An invokable function. */ #define PORYGON_PREPARE_FUNCTION(userDataTypeName, fieldName, returnType, ...) \ - static Porygon::Evaluation::EvalValue* invoke__##fieldName(void* obj, Porygon::Evaluation::EvalValue* par[], int parameterCount){ \ + static const Porygon::Evaluation::EvalValue* invoke__##fieldName(void* obj, const Porygon::Evaluation::EvalValue* par[], int parameterCount){ \ return Porygon::Evaluation::EvalValueHelper::Create(((userDataTypeName*)obj)->fieldName( \ __VA_ARGS__ \ ));} diff --git a/src/UserData/UserDataValue.hpp b/src/UserData/UserDataValue.hpp index 47f56d0..e89b64b 100644 --- a/src/UserData/UserDataValue.hpp +++ b/src/UserData/UserDataValue.hpp @@ -22,36 +22,40 @@ namespace Porygon::UserData { _obj = obj; } - inline const TypeClass GetTypeClass() const final { + [[nodiscard]] + inline TypeClass GetTypeClass() const final { return TypeClass::UserData; } - const bool operator==(EvalValue *b) const final { + bool operator==(const EvalValue *b) const final { if (b->GetTypeClass() != TypeClass::UserData) return false; return _obj == ((UserDataValue *) b)->_obj; } - inline const shared_ptr Clone() const final { + [[nodiscard]] + inline shared_ptr Clone() const final { return make_shared(_userData, _obj); } - inline const std::size_t GetHashCode() const final { + [[nodiscard]] + inline std::size_t GetHashCode() const final { return reinterpret_cast(_obj); } - const shared_ptr IndexValue(EvalValue *val) const final { + shared_ptr IndexValue(const EvalValue *val) const final { auto fieldId = val->GetHashCode(); auto field = _userData->GetField(fieldId); - return shared_ptr(field->Get(_obj)); + return shared_ptr(field->Get(_obj)); } - inline const shared_ptr IndexValue(uint32_t hash) const final { + [[nodiscard]] + inline shared_ptr IndexValue(uint32_t hash) const final { auto field = _userData->GetField(hash); - return shared_ptr(field->Get(_obj)); + return shared_ptr(field->Get(_obj)); } - void SetIndexValue(EvalValue *key, const shared_ptr &value) const final { + void SetIndexValue(const EvalValue *key, const shared_ptr &value) const final { auto fieldId = key->GetHashCode(); auto field = _userData->GetField(fieldId); field->Set(_obj, value.get()); diff --git a/src/Utilities/Random.cpp b/src/Utilities/Random.cpp new file mode 100644 index 0000000..0cc1117 --- /dev/null +++ b/src/Utilities/Random.cpp @@ -0,0 +1,3 @@ +#include "Random.hpp" + +std::mt19937_64 Porygon::Utilities::Random::_rng; \ No newline at end of file diff --git a/src/Utilities/Random.hpp b/src/Utilities/Random.hpp new file mode 100644 index 0000000..549b3b8 --- /dev/null +++ b/src/Utilities/Random.hpp @@ -0,0 +1,20 @@ +#ifndef PORYGONLANG_RANDOM_HPP +#define PORYGONLANG_RANDOM_HPP +#include + +namespace Porygon::Utilities { + class Random { + static std::mt19937_64 _rng; + public: + static void Seed(uint64_t new_seed) { + _rng.seed(new_seed); + } + + static inline long Get() { + return _rng.operator()(); + } + }; +} + + +#endif //PORYGONLANG_RANDOM_HPP diff --git a/tests/parser/ParserTests.cpp b/tests/parser/ParserTests.cpp index ee4ce3a..30cee3d 100644 --- a/tests/parser/ParserTests.cpp +++ b/tests/parser/ParserTests.cpp @@ -277,10 +277,10 @@ TEST_CASE( "Parse function declaration", "[parser]" ){ auto functionDeclaration = (ParsedFunctionDeclarationStatement*)firstStatement; REQUIRE(functionDeclaration->GetIdentifier() == HashedString::CreateLookup(u"foo")); auto parameters = functionDeclaration->GetParameters(); - CHECK(parameters -> at(0) ->GetType() == HashedString::CreateLookup(u"number")); - CHECK(parameters -> at(0) ->GetIdentifier() == HashedString::CreateLookup(u"bar")); - CHECK(parameters -> at(1) ->GetType() == HashedString::CreateLookup(u"number")); - CHECK(parameters -> at(1) ->GetIdentifier() == HashedString::CreateLookup(u"par")); + CHECK(*parameters -> at(0) ->GetType() == HashedString::CreateLookup(u"number")); + CHECK(*parameters -> at(0) ->GetIdentifier() == HashedString::CreateLookup(u"bar")); + CHECK(*parameters -> at(1) ->GetType() == HashedString::CreateLookup(u"number")); + CHECK(*parameters -> at(1) ->GetIdentifier() == HashedString::CreateLookup(u"par")); for (auto t : v){ delete t;