From 10a2535c968b7231b8e3fcc9d10a60d83484d926 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 13 Jun 2019 17:12:46 +0200 Subject: [PATCH] Handle bound classes as constants during evaluation --- .../BoundExpressions/BoundExpression.hpp | 160 ++++++++++-------- .../BoundExpressions/BoundTableExpression.hpp | 2 +- .../BoundFunctionDeclarationStatement.hpp | 23 +-- src/Binder/BoundStatements/BoundStatement.hpp | 90 +++++----- .../BoundVariables/BoundVariableKey.hpp | 8 +- src/Evaluator/BinaryEvaluation.cpp | 6 +- .../EvalValues/ScriptFunctionEvalValue.hpp | 6 +- .../EvaluationScope/EvaluationScope.cpp | 6 +- .../EvaluationScope/EvaluationScope.hpp | 6 +- src/Evaluator/Evaluator.cpp | 66 ++++---- src/Evaluator/Evaluator.hpp | 52 +++--- src/Evaluator/UnaryEvaluation.cpp | 4 +- 12 files changed, 225 insertions(+), 204 deletions(-) diff --git a/src/Binder/BoundExpressions/BoundExpression.hpp b/src/Binder/BoundExpressions/BoundExpression.hpp index ff08fe4..5fdb380 100644 --- a/src/Binder/BoundExpressions/BoundExpression.hpp +++ b/src/Binder/BoundExpressions/BoundExpression.hpp @@ -1,3 +1,7 @@ +#include + +#include + #ifndef PORYGONLANG_BOUNDEXPRESSION_HPP #define PORYGONLANG_BOUNDEXPRESSION_HPP @@ -29,29 +33,31 @@ enum class BoundExpressionKind{ }; class BoundExpression{ - unsigned int _start; - unsigned int _length; - std::shared_ptr _type; + const unsigned int _start; + const unsigned int _length; + const shared_ptr _type; public: - BoundExpression(unsigned int start, unsigned int length, std::shared_ptr type){ - _start = start; - _length = length; - _type = std::move(type); + BoundExpression(unsigned int start, unsigned int length, shared_ptr type) + : _start(start), + _length(length), + _type(std::move(type)) + { } + virtual ~BoundExpression() = default; - virtual BoundExpressionKind GetKind() = 0; - virtual std::shared_ptr GetType(){ + virtual const BoundExpressionKind GetKind() const = 0; + virtual const std::shared_ptr GetType() const{ return _type; }; - unsigned int GetStartPosition(){ + const unsigned int GetStartPosition() const{ return _start; } - unsigned int GetLength(){ + const unsigned int GetLength() const{ return _length; } - unsigned int GetEndPosition(){ + const unsigned int GetEndPosition() const{ return _start + _length - 1; } }; @@ -60,164 +66,172 @@ class BoundBadExpression : public BoundExpression{ public: BoundBadExpression(unsigned int start, unsigned int length) : BoundExpression(start, length, make_shared(TypeClass::Error)){} - BoundExpressionKind GetKind() final{ + const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::Bad; } }; class BoundLiteralIntegerExpression : public BoundExpression{ - long _value; + const long _value; public: BoundLiteralIntegerExpression(long value, unsigned int start, unsigned int length) - : BoundExpression(start, length, make_shared(true, false)){ - _value = value; + : BoundExpression(start, length, make_shared(true, false)), + _value(value) + { } - BoundExpressionKind GetKind() final{ + const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::LiteralInteger; } - long GetValue(){ + const long GetValue() const{ return _value; } }; class BoundLiteralFloatExpression : public BoundExpression{ - double _value; + const double _value; public: BoundLiteralFloatExpression(double value, unsigned int start, unsigned int length) - : BoundExpression(start, length, make_shared(true, true)){ - _value = value; + : BoundExpression(start, length, make_shared(true, true)), + _value(value) + { } - BoundExpressionKind GetKind() final{ + const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::LiteralFloat; } - double GetValue(){ + const double GetValue() const{ return _value; } }; class BoundLiteralStringExpression : public BoundExpression{ - string _value; + const string _value; public: BoundLiteralStringExpression(string value, unsigned int start, unsigned int length) - : BoundExpression(start, length, make_shared(true, HashedString::ConstHash(value.c_str()))){ - _value = std::move(value); + : BoundExpression(start, length, make_shared(true, HashedString::ConstHash(value.c_str()))), + _value(value) + { } - BoundExpressionKind GetKind() final{ + const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::LiteralString; } - string GetValue(){ + const string GetValue() const{ return _value; } }; class BoundLiteralBoolExpression : public BoundExpression{ - bool _value; + const bool _value; public: BoundLiteralBoolExpression(bool value, unsigned int start, unsigned int length) - : BoundExpression(start, length, make_shared(TypeClass::Bool)){ - _value = value; + : BoundExpression(start, length, make_shared(TypeClass::Bool)), + _value(value) + { } - BoundExpressionKind GetKind() final{ + const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::LiteralBool; } - bool GetValue(){ + const bool GetValue() const{ return _value; } }; class BoundVariableExpression : public BoundExpression{ - BoundVariableKey* _key; + const BoundVariableKey* _key; public: BoundVariableExpression(BoundVariableKey* key, shared_ptr type, unsigned int start, unsigned int length) - : BoundExpression(start, length, std::move(type)){ - _key = key; + : BoundExpression(start, length, std::move(type)), + _key(key) + { } ~BoundVariableExpression() override{ delete _key; } - BoundExpressionKind GetKind() final{ + const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::Variable; } - BoundVariableKey* GetKey(){ + const BoundVariableKey* GetKey() const{ return _key; } }; class BoundBinaryExpression : public BoundExpression { - BoundExpression* _left; - BoundExpression* _right; - BoundBinaryOperation _operation; + const BoundExpression* _left; + const BoundExpression* _right; + const BoundBinaryOperation _operation; public: BoundBinaryExpression(BoundExpression* left, BoundExpression* right, BoundBinaryOperation op, shared_ptr result, unsigned int start, unsigned int length) - : BoundExpression(start, length, result){ - _left = left; - _right = right; - _operation = op; + : BoundExpression(start, length, std::move(result)), + _left(left), + _right(right), + _operation(op) + { } + ~BoundBinaryExpression() final{ delete _left; delete _right; } - BoundExpressionKind GetKind() final{ + const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::Binary; } - BoundExpression* GetLeft(){ + const BoundExpression* GetLeft() const{ return _left; } - BoundExpression* GetRight(){ + const BoundExpression* GetRight() const{ return _right; } - BoundBinaryOperation GetOperation(){ + const BoundBinaryOperation GetOperation() const{ return _operation; } }; class BoundUnaryExpression : public BoundExpression { - BoundExpression* _operand; - BoundUnaryOperation _operation; + const BoundExpression* _operand; + const BoundUnaryOperation _operation; public: BoundUnaryExpression(BoundExpression* operand, BoundUnaryOperation op, shared_ptr result, unsigned int start, unsigned int length) - :BoundExpression(start, length, result){ - _operand = operand; - _operation = op; + : BoundExpression(start, length, result), + _operand(operand), + _operation(op) + { } ~BoundUnaryExpression() final{ delete _operand; } - BoundExpressionKind GetKind() final{ + const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::Unary; } - BoundExpression* GetOperand(){ + const BoundExpression* GetOperand() const{ return _operand; } - BoundUnaryOperation GetOperation(){ + const BoundUnaryOperation GetOperation() const{ return _operation; } }; class BoundFunctionCallExpression : public BoundExpression { - BoundExpression* _functionExpression; - vector _parameters; + const BoundExpression* _functionExpression; + const vector _parameters; public: BoundFunctionCallExpression(BoundExpression *functionExpression, vector parameters, shared_ptr result, unsigned int start, unsigned int length) @@ -230,16 +244,16 @@ public: } } - BoundExpressionKind GetKind() final{ + const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::FunctionCall; } - BoundExpression* GetFunctionExpression(){ + const BoundExpression* GetFunctionExpression() const{ return _functionExpression; } - vector GetParameters(){ - return _parameters; + const vector* GetParameters() const{ + return &_parameters; } }; @@ -256,26 +270,26 @@ public: delete _indexExpression; } - BoundExpressionKind GetKind() final{ + const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::Index; } - BoundExpression* GetIndexableExpression(){ + BoundExpression* GetIndexableExpression() const{ return _indexableExpression; } - BoundExpression* GetIndexExpression(){ + BoundExpression* GetIndexExpression() const{ return _indexExpression; } }; class BoundNumericalTableExpression : public BoundExpression{ - vector _expressions; + const vector _expressions; public: BoundNumericalTableExpression(vector expressions, shared_ptr type, unsigned int start, unsigned int length) - : BoundExpression(start, length, std::move(type)){ - _expressions = std::move(expressions); - } + : BoundExpression(start, length, std::move(type)), + _expressions(std::move(expressions)) + {} ~BoundNumericalTableExpression() final{ for (auto e: _expressions){ @@ -283,12 +297,12 @@ public: } } - BoundExpressionKind GetKind() final{ + const BoundExpressionKind GetKind() const final{ return BoundExpressionKind ::NumericalTable; } - vector GetExpressions(){ - return _expressions; + const vector* GetExpressions() const{ + return &_expressions; } }; diff --git a/src/Binder/BoundExpressions/BoundTableExpression.hpp b/src/Binder/BoundExpressions/BoundTableExpression.hpp index b4a449e..52f6f17 100644 --- a/src/Binder/BoundExpressions/BoundTableExpression.hpp +++ b/src/Binder/BoundExpressions/BoundTableExpression.hpp @@ -18,7 +18,7 @@ public: delete _block; } - BoundExpressionKind GetKind() final{ + const BoundExpressionKind GetKind() const final { return BoundExpressionKind ::Table; } diff --git a/src/Binder/BoundStatements/BoundFunctionDeclarationStatement.hpp b/src/Binder/BoundStatements/BoundFunctionDeclarationStatement.hpp index 0c5869f..5a019ea 100644 --- a/src/Binder/BoundStatements/BoundFunctionDeclarationStatement.hpp +++ b/src/Binder/BoundStatements/BoundFunctionDeclarationStatement.hpp @@ -1,3 +1,5 @@ +#include + #ifndef PORYGONLANG_BOUNDFUNCTIONDECLARATIONSTATEMENT_HPP #define PORYGONLANG_BOUNDFUNCTIONDECLARATIONSTATEMENT_HPP @@ -6,33 +8,32 @@ #include "BoundStatement.hpp" class BoundFunctionDeclarationStatement : public BoundStatement{ - BoundVariableKey* _key; - std::shared_ptr _block; - std::shared_ptr _type; + const BoundVariableKey* _key; + const std::shared_ptr _block; + const std::shared_ptr _type; public: - BoundFunctionDeclarationStatement(std::shared_ptr type, BoundVariableKey* key, BoundBlockStatement* block){ - _key = key; - _block = shared_ptr(block); - _type = type; + BoundFunctionDeclarationStatement(std::shared_ptr type, BoundVariableKey* key, BoundBlockStatement* block) + :_key(key), _block(block), _type(std::move(type)) + { } ~BoundFunctionDeclarationStatement() final{ delete _key; } - BoundStatementKind GetKind() final{ + const BoundStatementKind GetKind() const final{ return BoundStatementKind ::FunctionDeclaration; } - BoundVariableKey* GetKey(){ + const BoundVariableKey* GetKey() const{ return _key; } - std::shared_ptr GetBlock(){ + const std::shared_ptr GetBlock() const{ return _block; } - std::shared_ptr GetType(){ + const std::shared_ptr GetType() const{ return _type; } }; diff --git a/src/Binder/BoundStatements/BoundStatement.hpp b/src/Binder/BoundStatements/BoundStatement.hpp index 1109cdd..666fe7b 100644 --- a/src/Binder/BoundStatements/BoundStatement.hpp +++ b/src/Binder/BoundStatements/BoundStatement.hpp @@ -1,3 +1,5 @@ +#include + #ifndef PORYGONLANG_BOUNDSTATEMENT_HPP @@ -23,129 +25,133 @@ enum class BoundStatementKind{ class BoundStatement{ public: - virtual BoundStatementKind GetKind() = 0; + virtual const BoundStatementKind GetKind() const = 0; virtual ~BoundStatement() = default; }; class BoundBadStatement : public BoundStatement{ public: - BoundStatementKind GetKind() final{ + const BoundStatementKind GetKind() const final{ return BoundStatementKind ::Bad; } }; class BoundBlockStatement : public BoundStatement{ - vector _statements; + const vector _statements; public: - explicit BoundBlockStatement(vector statements){ - _statements = std::move(statements); + explicit BoundBlockStatement(vector statements) + : _statements(std::move(statements)) + { } ~BoundBlockStatement() override { for (auto s : _statements){ delete s; } - _statements.clear(); } - BoundStatementKind GetKind() override{ + const BoundStatementKind GetKind() const override { return BoundStatementKind ::Block; } - vector GetStatements(){ - return _statements; + const vector* GetStatements() const{ + return &_statements; } }; class BoundScriptStatement : public BoundBlockStatement{ - int _localVariableCount; + const int _localVariableCount; public: explicit BoundScriptStatement(vector statements, int localVariableCount) - : BoundBlockStatement(std::move(statements)){ - _localVariableCount = localVariableCount; + : BoundBlockStatement(std::move(statements)), + _localVariableCount(localVariableCount) + { } - BoundStatementKind GetKind() final{ + const BoundStatementKind GetKind() const final{ return BoundStatementKind ::Script; } - int GetLocalVariableCount(){ + const int GetLocalVariableCount() const{ return _localVariableCount; } }; class BoundExpressionStatement : public BoundStatement{ - BoundExpression* _expression; + const BoundExpression* _expression; public: - explicit BoundExpressionStatement(BoundExpression* expression){ + explicit BoundExpressionStatement(BoundExpression* expression) + : _expression(expression) + { _expression = expression; } ~BoundExpressionStatement() final{ delete _expression; } - BoundStatementKind GetKind() final{ + const BoundStatementKind GetKind() const final{ return BoundStatementKind ::Expression; } - BoundExpression* GetExpression(){ + const BoundExpression* GetExpression() const{ return _expression; } }; class BoundAssignmentStatement : public BoundStatement{ - BoundVariableKey* _key; - BoundExpression* _expression; + const BoundVariableKey* _key; + const BoundExpression* _expression; public: - BoundAssignmentStatement(BoundVariableKey* key, BoundExpression* expression){ - _key = key; - _expression = expression; + BoundAssignmentStatement(BoundVariableKey* key, BoundExpression* expression) + : _key(key), _expression(expression) + { } + ~BoundAssignmentStatement() final{ delete _key; delete _expression; } - BoundStatementKind GetKind() final{ + const BoundStatementKind GetKind() const final{ return BoundStatementKind ::Assignment; } - BoundVariableKey* GetKey(){ + const BoundVariableKey* GetKey() const { return _key; } - BoundExpression* GetExpression(){ + const BoundExpression* GetExpression() const { return _expression; } }; class BoundReturnStatement : public BoundStatement{ - BoundExpression* _expression; + const BoundExpression* _expression; public: - explicit BoundReturnStatement(BoundExpression* expression){ - _expression = expression; + explicit BoundReturnStatement(BoundExpression* expression) + : _expression(expression) + { } ~BoundReturnStatement() final{ delete _expression; } - BoundStatementKind GetKind() final{ + const BoundStatementKind GetKind() const final{ return BoundStatementKind ::Return; } - BoundExpression* GetExpression(){ + const BoundExpression* GetExpression() const{ return _expression; } }; class BoundConditionalStatement : public BoundStatement{ - BoundExpression* _condition; - BoundStatement* _block; - BoundStatement* _elseStatement; + const BoundExpression* _condition; + const BoundStatement* _block; + const BoundStatement* _elseStatement; public: - explicit BoundConditionalStatement(BoundExpression* condition, BoundStatement* block, BoundStatement* next){ - _condition = condition; - _block = block; - _elseStatement = next; + explicit BoundConditionalStatement(BoundExpression* condition, BoundStatement* block, BoundStatement* next) + :_condition(condition), _block(block), _elseStatement(next) + { } ~BoundConditionalStatement() final{ @@ -154,19 +160,19 @@ public: delete _elseStatement; } - BoundStatementKind GetKind() final{ + const BoundStatementKind GetKind() const final{ return BoundStatementKind ::Conditional; } - BoundExpression* GetCondition(){ + const BoundExpression* GetCondition() const{ return _condition; } - BoundStatement* GetBlock(){ + const BoundStatement* GetBlock() const{ return _block; } - BoundStatement* GetElseStatement(){ + const BoundStatement* GetElseStatement() const{ return _elseStatement; } }; diff --git a/src/Binder/BoundVariables/BoundVariableKey.hpp b/src/Binder/BoundVariables/BoundVariableKey.hpp index f3ac63f..ba0e1d2 100644 --- a/src/Binder/BoundVariables/BoundVariableKey.hpp +++ b/src/Binder/BoundVariables/BoundVariableKey.hpp @@ -25,19 +25,19 @@ public: {} - const int GetIdentifier(){ + const int GetIdentifier() const{ return _identifier; } - const unsigned int GetScopeId(){ + const unsigned int GetScopeId() const{ return _scopeId; } - const bool IsCreation(){ + const bool IsCreation() const{ return _isCreation; } - const uint64_t GetHash(){ + const uint64_t GetHash() const{ return _hash; } }; diff --git a/src/Evaluator/BinaryEvaluation.cpp b/src/Evaluator/BinaryEvaluation.cpp index f880b69..8715265 100644 --- a/src/Evaluator/BinaryEvaluation.cpp +++ b/src/Evaluator/BinaryEvaluation.cpp @@ -5,7 +5,7 @@ #include "EvalValues/NumericEvalValue.hpp" #include "EvalValues/StringEvalValue.hpp" -shared_ptr Evaluator::EvaluateIntegerBinary(BoundBinaryExpression *expression) { +shared_ptr Evaluator::EvaluateIntegerBinary(const BoundBinaryExpression *expression) { auto leftValue = this -> EvaluateIntegerExpression(expression->GetLeft()); auto rightValue = this -> EvaluateIntegerExpression(expression->GetRight()); @@ -29,7 +29,7 @@ shared_ptr Evaluator::EvaluateIntegerBinary(BoundBinaryExpress return shared_ptr(result); } -shared_ptr Evaluator::EvaluateBooleanBinary(BoundBinaryExpression* expression){ +shared_ptr Evaluator::EvaluateBooleanBinary(const BoundBinaryExpression* expression){ switch (expression->GetOperation()){ case BoundBinaryOperation::Equality: { @@ -93,7 +93,7 @@ shared_ptr Evaluator::EvaluateBooleanBinary(BoundBinaryExpress } } -shared_ptr Evaluator::EvaluateStringBinary(BoundBinaryExpression* expression){ +shared_ptr Evaluator::EvaluateStringBinary(const BoundBinaryExpression* expression){ if (expression->GetOperation() != BoundBinaryOperation::Concatenation) throw; std::ostringstream strs; diff --git a/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp b/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp index 4b20f2d..8d503ba 100644 --- a/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp +++ b/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp @@ -38,7 +38,7 @@ public: _hash = rand(); } - std::shared_ptr GetType(){ + std::shared_ptr GetType() const{ return _type; } @@ -57,7 +57,7 @@ public: return this->_hash == ((ScriptFunctionEvalValue*)b)->_hash; }; - std::shared_ptr GetInnerBlock(){ + std::shared_ptr GetInnerBlock() const{ return _innerBlock; } @@ -65,7 +65,7 @@ public: return _hash; } - std::shared_ptr GetScope(){ + std::shared_ptr GetScope() const{ return _scope; } }; diff --git a/src/Evaluator/EvaluationScope/EvaluationScope.cpp b/src/Evaluator/EvaluationScope/EvaluationScope.cpp index 4f93909..d068cd1 100644 --- a/src/Evaluator/EvaluationScope/EvaluationScope.cpp +++ b/src/Evaluator/EvaluationScope/EvaluationScope.cpp @@ -7,7 +7,7 @@ EvaluationScope::EvaluationScope(unordered_map> *s _localScope = unordered_map>(localVariableCount); } -void EvaluationScope::CreateVariable(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; } else{ @@ -18,7 +18,7 @@ void EvaluationScope::CreateVariable(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; } else{ @@ -26,7 +26,7 @@ void EvaluationScope::SetVariable(BoundVariableKey* key, const shared_ptr EvaluationScope::GetVariable(BoundVariableKey* key) { +shared_ptr EvaluationScope::GetVariable(const BoundVariableKey* key) { if (key->GetScopeId() == 0){ return _scriptScope -> at(key->GetIdentifier()); } else{ diff --git a/src/Evaluator/EvaluationScope/EvaluationScope.hpp b/src/Evaluator/EvaluationScope/EvaluationScope.hpp index ecd0f6a..8a2269f 100644 --- a/src/Evaluator/EvaluationScope/EvaluationScope.hpp +++ b/src/Evaluator/EvaluationScope/EvaluationScope.hpp @@ -13,9 +13,9 @@ public: explicit EvaluationScope(unordered_map>* scriptVariables, int deepestScope); ~EvaluationScope() = default; - void CreateVariable(BoundVariableKey* key, const shared_ptr& value); - void SetVariable(BoundVariableKey* key, const shared_ptr& value); - shared_ptr GetVariable(BoundVariableKey* key); + void CreateVariable(const BoundVariableKey* key, const shared_ptr& value); + void SetVariable(const BoundVariableKey* key, const shared_ptr& value); + shared_ptr GetVariable(const BoundVariableKey* key); }; diff --git a/src/Evaluator/Evaluator.cpp b/src/Evaluator/Evaluator.cpp index 10382af..2d6ec45 100644 --- a/src/Evaluator/Evaluator.cpp +++ b/src/Evaluator/Evaluator.cpp @@ -13,18 +13,18 @@ using namespace std; -EvalValue* Evaluator::Evaluate(BoundScriptStatement *statement) { +EvalValue* Evaluator::Evaluate(const BoundScriptStatement *statement) { this->_evaluationScope = make_shared(this->_scriptData->_scriptVariables, statement->GetLocalVariableCount()); - EvaluateBlockStatement(statement, false); + EvaluateBlockStatement(statement); return this -> _returnValue.get(); } -void Evaluator::EvaluateStatement(BoundStatement *statement) { +void Evaluator::EvaluateStatement(const BoundStatement *statement) { if (this->_hasReturned) return; switch (statement->GetKind()){ case BoundStatementKind ::Script: throw; // Should never happen - case BoundStatementKind ::Block: return this -> EvaluateBlockStatement((BoundBlockStatement*)statement, true); + case BoundStatementKind ::Block: return this->EvaluateBlockStatement((BoundBlockStatement *) statement); case BoundStatementKind ::Expression: return this -> EvaluateExpressionStatement((BoundExpressionStatement*)statement); case BoundStatementKind ::Assignment: return this -> EvaluateAssignmentStatement((BoundAssignmentStatement*)statement); case BoundStatementKind ::FunctionDeclaration: return this->EvaluateFunctionDeclarationStatement((BoundFunctionDeclarationStatement*)statement); @@ -36,20 +36,20 @@ void Evaluator::EvaluateStatement(BoundStatement *statement) { } } -void Evaluator::EvaluateBlockStatement(BoundBlockStatement* statement, bool clearScope) { - for (auto s: statement->GetStatements()){ +void Evaluator::EvaluateBlockStatement(const BoundBlockStatement *statement) { + for (auto s: *statement->GetStatements()){ this -> EvaluateStatement(s); if (this->_hasReturned) break; } } -void Evaluator::EvaluateExpressionStatement(BoundExpressionStatement *statement) { +void Evaluator::EvaluateExpressionStatement(const BoundExpressionStatement *statement) { // Save new value this->_lastValue = this -> EvaluateExpression(statement->GetExpression()); } -void Evaluator::EvaluateAssignmentStatement(BoundAssignmentStatement *statement) { +void Evaluator::EvaluateAssignmentStatement(const BoundAssignmentStatement *statement) { auto value = this -> EvaluateExpression(statement->GetExpression()); auto key = statement->GetKey(); if (key->IsCreation()){ @@ -59,7 +59,7 @@ void Evaluator::EvaluateAssignmentStatement(BoundAssignmentStatement *statement) } } -void Evaluator::EvaluateFunctionDeclarationStatement(BoundFunctionDeclarationStatement *statement) { +void Evaluator::EvaluateFunctionDeclarationStatement(const BoundFunctionDeclarationStatement *statement) { auto type = statement->GetType(); auto key = statement->GetKey(); auto block = statement->GetBlock(); @@ -71,7 +71,7 @@ void Evaluator::EvaluateFunctionDeclarationStatement(BoundFunctionDeclarationSta } } -void Evaluator::EvaluateReturnStatement(BoundReturnStatement* statement){ +void Evaluator::EvaluateReturnStatement(const BoundReturnStatement* statement){ auto expression = statement->GetExpression(); if (expression == nullptr){ this->_hasReturned = true; @@ -82,7 +82,7 @@ void Evaluator::EvaluateReturnStatement(BoundReturnStatement* statement){ this -> _returnValue = value; } -void Evaluator::EvaluateConditionalStatement(BoundConditionalStatement *statement) { +void Evaluator::EvaluateConditionalStatement(const BoundConditionalStatement *statement) { auto condition = statement->GetCondition(); if (EvaluateBoolExpression(condition) -> EvaluateBool()){ this -> EvaluateStatement(statement->GetBlock()); @@ -94,7 +94,7 @@ void Evaluator::EvaluateConditionalStatement(BoundConditionalStatement *statemen } } -shared_ptr Evaluator::EvaluateExpression(BoundExpression *expression) { +shared_ptr Evaluator::EvaluateExpression(const BoundExpression *expression) { auto type = expression -> GetType(); switch (type->GetClass()){ case TypeClass ::Number: return this -> EvaluateIntegerExpression(expression); @@ -107,7 +107,7 @@ shared_ptr Evaluator::EvaluateExpression(BoundExpression *expression) } } -shared_ptr Evaluator::GetVariable(BoundVariableExpression* expression){ +shared_ptr Evaluator::GetVariable(const BoundVariableExpression* expression){ auto variable = this->_evaluationScope->GetVariable(expression->GetKey()); if (variable == nullptr){ throw EvaluationException("Variable not found"); @@ -115,7 +115,7 @@ shared_ptr Evaluator::GetVariable(BoundVariableExpression* expression return variable->Clone(); } -shared_ptr Evaluator::EvaluateIntegerExpression(BoundExpression *expression) { +shared_ptr Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) { switch (expression->GetKind()){ case BoundExpressionKind ::LiteralInteger: return make_shared(((BoundLiteralIntegerExpression*)expression)->GetValue()); case BoundExpressionKind ::LiteralFloat: return make_shared(((BoundLiteralFloatExpression*)expression)->GetValue()); @@ -134,7 +134,7 @@ shared_ptr Evaluator::EvaluateIntegerExpression(BoundExpressio } } -shared_ptr Evaluator::EvaluateBoolExpression(BoundExpression *expression) { +shared_ptr Evaluator::EvaluateBoolExpression(const BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind ::LiteralBool: return make_shared(((BoundLiteralBoolExpression*)expression)->GetValue()); case BoundExpressionKind::Unary: return this -> EvaluateBooleanUnary((BoundUnaryExpression*)expression); @@ -154,7 +154,7 @@ shared_ptr Evaluator::EvaluateBoolExpression(BoundExpression * } } -shared_ptr Evaluator::EvaluateStringExpression(BoundExpression *expression) { +shared_ptr Evaluator::EvaluateStringExpression(const BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind ::LiteralString: return make_shared(((BoundLiteralStringExpression*)expression)->GetValue()); @@ -176,14 +176,14 @@ shared_ptr Evaluator::EvaluateStringExpression(BoundExpression } } -shared_ptr Evaluator::EvaluateFunctionExpression(BoundExpression * expression){ +shared_ptr Evaluator::EvaluateFunctionExpression(const BoundExpression * expression){ switch (expression->GetKind()){ case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression); case BoundExpressionKind ::Index: return this->EvaluateIndexExpression(expression); default: throw; } } -shared_ptr Evaluator::EvaluateNilExpression(BoundExpression * expression){ +shared_ptr Evaluator::EvaluateNilExpression(const BoundExpression * expression){ switch (expression->GetKind()){ case BoundExpressionKind ::FunctionCall: return this->EvaluateFunctionCallExpression(expression); @@ -191,7 +191,7 @@ shared_ptr Evaluator::EvaluateNilExpression(BoundExpression * express return nullptr; } } -shared_ptr Evaluator::EvaluateTableExpression(BoundExpression * expression){ +shared_ptr Evaluator::EvaluateTableExpression(const BoundExpression * expression){ switch (expression->GetKind()){ case BoundExpressionKind ::FunctionCall: return this->EvaluateFunctionCallExpression(expression); @@ -207,14 +207,14 @@ shared_ptr Evaluator::EvaluateTableExpression(BoundExpression * expre -shared_ptr Evaluator::EvaluateFunctionCallExpression(BoundExpression* expression){ +shared_ptr Evaluator::EvaluateFunctionCallExpression(const BoundExpression* expression){ auto functionCall = (BoundFunctionCallExpression*)expression; 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++){ - parameters[i] = this->EvaluateExpression(boundParameters[i]); + auto parameters = vector>(boundParameters->size()); + for (int i = 0; i < boundParameters->size(); i++){ + parameters[i] = this->EvaluateExpression(boundParameters->at(i)); } auto type = std::dynamic_pointer_cast(function->GetType()); @@ -228,7 +228,7 @@ shared_ptr Evaluator::EvaluateFunctionCallExpression(BoundExpression* auto key = parameterKeys.at(i); this->_evaluationScope->CreateVariable(key.get(), parameter->Clone()); } - this->EvaluateBlockStatement(function->GetInnerBlock().get(), true); + this->EvaluateBlockStatement(function->GetInnerBlock().get()); this->_evaluationScope = originalScope; this->_hasReturned = false; @@ -237,7 +237,7 @@ shared_ptr Evaluator::EvaluateFunctionCallExpression(BoundExpression* return r; } -shared_ptr Evaluator::EvaluateFunction(ScriptFunctionEvalValue *function, vector parameters) { +shared_ptr Evaluator::EvaluateFunction(const ScriptFunctionEvalValue *function, const vector& parameters) { auto type = std::dynamic_pointer_cast(function->GetType()); auto parameterTypes = type->GetParameterTypes(); auto parameterKeys = type->GetParameterKeys(); @@ -250,7 +250,7 @@ shared_ptr Evaluator::EvaluateFunction(ScriptFunctionEvalValue *funct auto key = parameterKeys.at(i); this->_evaluationScope->CreateVariable(key.get(), parameter->Clone()); } - this->EvaluateBlockStatement(function->GetInnerBlock().get(), true); + this->EvaluateBlockStatement(function->GetInnerBlock().get()); this->_evaluationScope = originalScope; this->_hasReturned = false; auto r = this -> _returnValue; @@ -258,26 +258,26 @@ shared_ptr Evaluator::EvaluateFunction(ScriptFunctionEvalValue *funct return r; } -shared_ptr Evaluator::EvaluateIndexExpression(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(); } -shared_ptr Evaluator::EvaluateNumericTableExpression(BoundExpression *expression) { +shared_ptr Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) { auto tableExpression = (BoundNumericalTableExpression*)expression; auto valueExpressions = tableExpression->GetExpressions(); - auto values = new unordered_map>(valueExpressions.size()); - for (int i = 0; i < valueExpressions.size(); i++){ - auto val = this -> EvaluateExpression(valueExpressions[i]); + auto values = new unordered_map>(valueExpressions->size()); + for (int i = 0; i < valueExpressions->size(); i++){ + auto val = this -> EvaluateExpression(valueExpressions -> at(i)); values -> insert({i + 1, val}); } auto valuesPointer = shared_ptr>>(values); return make_shared(valuesPointer); } -shared_ptr Evaluator::EvaluateComplexTableExpression(BoundExpression *expression) { +shared_ptr Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) { auto tableExpression = (BoundTableExpression*)expression; auto type = dynamic_pointer_cast(tableExpression->GetType()); auto declaredVars = type -> GetValues(); @@ -288,7 +288,7 @@ shared_ptr Evaluator::EvaluateComplexTableExpression(BoundExpression auto evaluator = make_shared(variables.get(), type -> GetLocalVariableCount()); auto currentEvaluator = this -> _evaluationScope; this -> _evaluationScope = evaluator; - this -> EvaluateBlockStatement(tableExpression->GetBlock(), false); + this->EvaluateBlockStatement(tableExpression->GetBlock()); this -> _evaluationScope = currentEvaluator; return make_shared(variables); } diff --git a/src/Evaluator/Evaluator.hpp b/src/Evaluator/Evaluator.hpp index fe5b217..d106586 100644 --- a/src/Evaluator/Evaluator.hpp +++ b/src/Evaluator/Evaluator.hpp @@ -22,34 +22,34 @@ class Evaluator { Script* _scriptData; shared_ptr _evaluationScope; - void EvaluateStatement(BoundStatement* statement); - void EvaluateBlockStatement(BoundBlockStatement* statement, bool clearScope); - void EvaluateExpressionStatement(BoundExpressionStatement* statement); - void EvaluateAssignmentStatement(BoundAssignmentStatement* statement); - void EvaluateFunctionDeclarationStatement(BoundFunctionDeclarationStatement *statement); - void EvaluateReturnStatement(BoundReturnStatement *statement); - void EvaluateConditionalStatement(BoundConditionalStatement *statement); + void EvaluateStatement(const BoundStatement* statement); + void EvaluateBlockStatement(const BoundBlockStatement *statement); + void EvaluateExpressionStatement(const BoundExpressionStatement* statement); + void EvaluateAssignmentStatement(const BoundAssignmentStatement* statement); + void EvaluateFunctionDeclarationStatement(const BoundFunctionDeclarationStatement *statement); + void EvaluateReturnStatement(const BoundReturnStatement *statement); + void EvaluateConditionalStatement(const BoundConditionalStatement *statement); - shared_ptr EvaluateExpression(BoundExpression* expression); - shared_ptr EvaluateIntegerExpression(BoundExpression* expression); - shared_ptr EvaluateBoolExpression(BoundExpression* expression); - shared_ptr EvaluateStringExpression(BoundExpression* expression); - shared_ptr EvaluateFunctionExpression(BoundExpression *expression); - shared_ptrEvaluateNilExpression(BoundExpression *expression); - shared_ptrEvaluateTableExpression(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_ptrEvaluateNilExpression(const BoundExpression *expression); + shared_ptrEvaluateTableExpression(const BoundExpression *expression); - shared_ptr EvaluateIntegerBinary(BoundBinaryExpression* expression); - shared_ptr EvaluateBooleanBinary(BoundBinaryExpression *expression); - shared_ptr EvaluateStringBinary(BoundBinaryExpression *expression); + shared_ptr EvaluateIntegerBinary(const BoundBinaryExpression* expression); + shared_ptr EvaluateBooleanBinary(const BoundBinaryExpression *expression); + shared_ptr EvaluateStringBinary(const BoundBinaryExpression *expression); - shared_ptr EvaluateIntegerUnary(BoundUnaryExpression* expression); - shared_ptr EvaluateBooleanUnary(BoundUnaryExpression *expression); - shared_ptr EvaluateFunctionCallExpression(BoundExpression *expression); - shared_ptr EvaluateIndexExpression(BoundExpression* expression); - shared_ptr EvaluateNumericTableExpression(BoundExpression* expression); - shared_ptr EvaluateComplexTableExpression(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 EvaluateNumericTableExpression(const BoundExpression* expression); + shared_ptr EvaluateComplexTableExpression(const BoundExpression *expression); - shared_ptr GetVariable(BoundVariableExpression *expression); + shared_ptr GetVariable(const BoundVariableExpression *expression); public: explicit Evaluator(Script* script){ _scriptData = script; @@ -58,8 +58,8 @@ public: _evaluationScope = nullptr; } - EvalValue* Evaluate(BoundScriptStatement* statement); - shared_ptr EvaluateFunction(ScriptFunctionEvalValue* func, vector parameters); + EvalValue* Evaluate(const BoundScriptStatement* statement); + shared_ptr EvaluateFunction(const ScriptFunctionEvalValue *function, const vector& parameters); EvalValue* GetLastValue(){ return _lastValue.get(); diff --git a/src/Evaluator/UnaryEvaluation.cpp b/src/Evaluator/UnaryEvaluation.cpp index cb4e925..2ca7f32 100644 --- a/src/Evaluator/UnaryEvaluation.cpp +++ b/src/Evaluator/UnaryEvaluation.cpp @@ -4,7 +4,7 @@ #include "EvaluationException.hpp" #include "../Script.hpp" -shared_ptr Evaluator::EvaluateIntegerUnary(BoundUnaryExpression *expression) { +shared_ptr Evaluator::EvaluateIntegerUnary(const BoundUnaryExpression *expression) { switch (expression->GetOperation()){ case BoundUnaryOperation::Negation: { @@ -22,7 +22,7 @@ shared_ptr Evaluator::EvaluateIntegerUnary(BoundUnaryExpressio } } -shared_ptr Evaluator::EvaluateBooleanUnary(BoundUnaryExpression *expression) { +shared_ptr Evaluator::EvaluateBooleanUnary(const BoundUnaryExpression *expression) { switch (expression->GetOperation()){ case BoundUnaryOperation::LogicalNegation: {