Large cleanup
continuous-integration/drone/push Build was killed Details

This commit is contained in:
Deukhoofd 2019-07-25 17:23:54 +02:00
parent e639a2c170
commit e2a0c35992
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
58 changed files with 700 additions and 539 deletions

View File

@ -5,7 +5,6 @@
#include "BoundExpressions/BoundTableExpression.hpp" #include "BoundExpressions/BoundTableExpression.hpp"
#include "BoundExpressions/BoundFunctionCallExpression.hpp" #include "BoundExpressions/BoundFunctionCallExpression.hpp"
#include "../UserData/UserDataScriptType.hpp" #include "../UserData/UserDataScriptType.hpp"
#include "../FunctionScriptType.hpp"
using namespace Porygon::Parser; using namespace Porygon::Parser;
@ -16,7 +15,7 @@ namespace Porygon::Binder {
binder._scope = scriptScope; binder._scope = scriptScope;
auto statements = s->GetStatements(); auto statements = s->GetStatements();
vector<BoundStatement *> boundStatements(statements->size()); vector<const BoundStatement *> boundStatements(statements->size());
for (int i = 0; i < statements->size(); i++) { for (int i = 0; i < statements->size(); i++) {
boundStatements[i] = binder.BindStatement(statements->at(i)); boundStatements[i] = binder.BindStatement(statements->at(i));
} }
@ -62,7 +61,7 @@ namespace Porygon::Binder {
BoundStatement *Binder::BindBlockStatement(const ParsedStatement *statement) { BoundStatement *Binder::BindBlockStatement(const ParsedStatement *statement) {
auto statements = ((ParsedBlockStatement *) statement)->GetStatements(); auto statements = ((ParsedBlockStatement *) statement)->GetStatements();
vector<BoundStatement *> boundStatements(statements->size()); vector<const BoundStatement *> boundStatements(statements->size());
this->_scope->GoInnerScope(); this->_scope->GoInnerScope();
for (int i = 0; i < statements->size(); i++) { for (int i = 0; i < statements->size(); i++) {
boundStatements[i] = this->BindStatement(statements->at(i)); boundStatements[i] = this->BindStatement(statements->at(i));
@ -114,8 +113,8 @@ namespace Porygon::Binder {
return new BoundIndexAssignmentStatement(indexable, valueExpression); return new BoundIndexAssignmentStatement(indexable, valueExpression);
} }
std::shared_ptr<ScriptType> ParseTypeIdentifier(const HashedString& s) { std::shared_ptr<ScriptType> ParseTypeIdentifier(const HashedString* s) {
auto hash = s.GetHash(); auto hash = s->GetHash();
switch (hash) { switch (hash) {
case HashedString::ConstHash("number"): case HashedString::ConstHash("number"):
return std::make_shared<NumericScriptType>(false, false); return std::make_shared<NumericScriptType>(false, false);
@ -135,11 +134,10 @@ namespace Porygon::Binder {
auto functionStatement = (ParsedFunctionDeclarationStatement *) statement; auto functionStatement = (ParsedFunctionDeclarationStatement *) statement;
auto parameters = functionStatement->GetParameters(); auto parameters = functionStatement->GetParameters();
auto parameterTypes = vector<shared_ptr<ScriptType>>(parameters->size()); auto parameterTypes = vector<shared_ptr<ScriptType>>(parameters->size());
auto parameterKeys = vector<shared_ptr<BoundVariableKey>>(parameters->size()); auto parameterKeys = vector<shared_ptr<const BoundVariableKey>>(parameters->size());
auto scopeIndex = this->_scope->GetCurrentScope();
this->_scope->GoInnerScope(); 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 var = parameters->at(i);
auto parsedType = ParseTypeIdentifier(var->GetType()); auto parsedType = ParseTypeIdentifier(var->GetType());
if (parsedType == nullptr) { if (parsedType == nullptr) {
@ -148,9 +146,9 @@ namespace Porygon::Binder {
return new BoundBadStatement(); return new BoundBadStatement();
} }
parameterTypes.at(i) = parsedType; 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) { if (parameterAssignment.GetResult() == VariableAssignmentResult::Ok) {
parameterKeys.at(i) = std::shared_ptr<BoundVariableKey>(parameterAssignment.GetKey()); parameterKeys.at(i) = std::shared_ptr<const BoundVariableKey>(parameterAssignment.GetKey());
} else { } else {
//TODO: log error //TODO: log error
continue; continue;
@ -162,9 +160,9 @@ namespace Porygon::Binder {
auto option = new ScriptFunctionOption(returnType, parameterTypes, parameterKeys); auto option = new ScriptFunctionOption(returnType, parameterTypes, parameterKeys);
this->_currentFunction = option; this->_currentFunction = option;
shared_ptr<GenericFunctionScriptType> type; shared_ptr<const GenericFunctionScriptType> type;
auto scope = this -> _scope -> Exists(identifier); auto scope = this -> _scope -> Exists(identifier);
BoundVariableKey* assignmentKey; const BoundVariableKey* assignmentKey;
if (scope >= 0){ if (scope >= 0){
auto var = this -> _scope -> GetVariable(scope, identifier); auto var = this -> _scope -> GetVariable(scope, identifier);
auto varType =var->GetType(); auto varType =var->GetType();
@ -172,11 +170,11 @@ namespace Porygon::Binder {
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable, statement->GetStartPosition(), this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable, statement->GetStartPosition(),
statement->GetLength()); statement->GetLength());
} }
type = dynamic_pointer_cast<GenericFunctionScriptType>(varType); type = dynamic_pointer_cast<const GenericFunctionScriptType>(varType);
type->RegisterFunctionOption(option); type->RegisterFunctionOption(option);
assignmentKey = new BoundVariableKey(identifier, scope, false); assignmentKey = new BoundVariableKey(identifier, scope, false);
} else{ } else{
type = make_shared<GenericFunctionScriptType>(); type = make_shared<const GenericFunctionScriptType>();
type->RegisterFunctionOption(option); type->RegisterFunctionOption(option);
auto assignment = this->_scope->AssignVariable(identifier, type); auto assignment = this->_scope->AssignVariable(identifier, type);
if (assignment.GetResult() != VariableAssignmentResult::Ok) { if (assignment.GetResult() != VariableAssignmentResult::Ok) {
@ -195,7 +193,7 @@ namespace Porygon::Binder {
BoundStatement *Binder::BindReturnStatement(const ParsedStatement *statement) { BoundStatement *Binder::BindReturnStatement(const ParsedStatement *statement) {
auto expression = ((ParsedReturnStatement *) statement)->GetExpression(); auto expression = ((ParsedReturnStatement *) statement)->GetExpression();
shared_ptr<ScriptType> currentReturnType; shared_ptr<const ScriptType> currentReturnType;
if (this->_currentFunction == nullptr) { if (this->_currentFunction == nullptr) {
currentReturnType = this->_scriptData->GetReturnType(); currentReturnType = this->_scriptData->GetReturnType();
} else { } else {
@ -303,7 +301,7 @@ namespace Porygon::Binder {
auto valueIdentifier = genericFor -> GetValueIdentifier(); auto valueIdentifier = genericFor -> GetValueIdentifier();
auto isValueVariableDefined = valueIdentifier.GetHash() != 0; auto isValueVariableDefined = valueIdentifier.GetHash() != 0;
BoundVariableKey* valueVariable = nullptr; const BoundVariableKey* valueVariable = nullptr;
if (isValueVariableDefined){ if (isValueVariableDefined){
auto valueType = itType -> GetIndexedType(keyType.get()); auto valueType = itType -> GetIndexedType(keyType.get());
auto valueVariableAssignment = this -> _scope -> CreateExplicitLocal(valueIdentifier, valueType); auto valueVariableAssignment = this -> _scope -> CreateExplicitLocal(valueIdentifier, valueType);
@ -403,8 +401,8 @@ namespace Porygon::Binder {
switch (expression->GetOperatorKind()) { switch (expression->GetOperatorKind()) {
case BinaryOperatorKind::Addition: case BinaryOperatorKind::Addition:
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number) { if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number) {
auto leftNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundLeftType); auto leftNumeric = std::static_pointer_cast<const NumericScriptType>(boundLeftType);
auto rightNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundRightType); auto rightNumeric = std::static_pointer_cast<const NumericScriptType>(boundRightType);
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) { if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
return new BoundBinaryExpression(boundLeft, boundRight, return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Addition, BoundBinaryOperation::Addition,
@ -426,8 +424,8 @@ namespace Porygon::Binder {
break; break;
case BinaryOperatorKind::Subtraction: case BinaryOperatorKind::Subtraction:
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number) { if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number) {
auto leftNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundLeftType); auto leftNumeric = std::dynamic_pointer_cast<const NumericScriptType>(boundLeftType);
auto rightNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundRightType); auto rightNumeric = std::dynamic_pointer_cast<const NumericScriptType>(boundRightType);
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) { if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
return new BoundBinaryExpression(boundLeft, boundRight, return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Subtraction, BoundBinaryOperation::Subtraction,
@ -444,8 +442,8 @@ namespace Porygon::Binder {
break; break;
case BinaryOperatorKind::Multiplication: case BinaryOperatorKind::Multiplication:
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number) { if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number) {
auto leftNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundLeftType); auto leftNumeric = std::dynamic_pointer_cast<const NumericScriptType>(boundLeftType);
auto rightNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundRightType); auto rightNumeric = std::dynamic_pointer_cast<const NumericScriptType>(boundRightType);
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) { if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
return new BoundBinaryExpression(boundLeft, boundRight, return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Multiplication, BoundBinaryOperation::Multiplication,
@ -462,8 +460,8 @@ namespace Porygon::Binder {
break; break;
case BinaryOperatorKind::Division: case BinaryOperatorKind::Division:
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number) { if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number) {
auto leftNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundLeftType); auto leftNumeric = std::dynamic_pointer_cast<const NumericScriptType>(boundLeftType);
auto rightNumeric = std::dynamic_pointer_cast<NumericScriptType>(boundRightType); auto rightNumeric = std::dynamic_pointer_cast<const NumericScriptType>(boundRightType);
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) { if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
return new BoundBinaryExpression(boundLeft, boundRight, return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Division, BoundBinaryOperation::Division,
@ -544,7 +542,7 @@ namespace Porygon::Binder {
break; break;
case UnaryOperatorKind::Negation: case UnaryOperatorKind::Negation:
if (operandType->GetClass() == TypeClass::Number) { if (operandType->GetClass() == TypeClass::Number) {
auto innerType = std::dynamic_pointer_cast<NumericScriptType>(operandType); auto innerType = std::dynamic_pointer_cast<const NumericScriptType>(operandType);
return new BoundUnaryExpression(operand, BoundUnaryOperation::Negation, return new BoundUnaryExpression(operand, BoundUnaryOperation::Negation,
std::make_shared<NumericScriptType>( std::make_shared<NumericScriptType>(
innerType.get()->IsAwareOfFloat(), innerType.get()->IsAwareOfFloat(),
@ -577,11 +575,11 @@ namespace Porygon::Binder {
expression->GetLength()); expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
} }
auto functionType = std::dynamic_pointer_cast<GenericFunctionScriptType>(type); auto functionType = std::dynamic_pointer_cast<const GenericFunctionScriptType>(type);
auto givenParameters = expression->GetParameters(); auto givenParameters = expression->GetParameters();
auto givenParameterTypes = vector<shared_ptr<ScriptType>>(givenParameters->size()); auto givenParameterTypes = vector<shared_ptr<const ScriptType>>(givenParameters->size());
vector<BoundExpression *> boundParameters = vector<BoundExpression *>(givenParameters->size()); vector<BoundExpression *> boundParameters = vector<BoundExpression *>(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)); boundParameters[i] = this -> BindExpression(givenParameters->at(i));
givenParameterTypes[i] = boundParameters[i]->GetType(); givenParameterTypes[i] = boundParameters[i]->GetType();
} }
@ -609,8 +607,8 @@ namespace Porygon::Binder {
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
} }
if (indexerType->GetClass() == TypeClass::UserData) { if (indexerType->GetClass() == TypeClass::UserData) {
auto stringKey = dynamic_pointer_cast<StringScriptType>(index->GetType()); auto stringKey = dynamic_pointer_cast<const StringScriptType>(index->GetType());
auto field = dynamic_pointer_cast<UserData::UserDataScriptType>(indexerType)->GetField(stringKey->GetHashValue()); auto field = dynamic_pointer_cast<const UserData::UserDataScriptType>(indexerType)->GetField(stringKey->GetHashValue());
if (!setter) { if (!setter) {
if (!field->HasGetter()) { if (!field->HasGetter()) {
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UserDataFieldNoGetter, this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UserDataFieldNoGetter,
@ -643,7 +641,7 @@ namespace Porygon::Binder {
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
} }
if (indexerType->GetClass() == TypeClass::UserData) { if (indexerType->GetClass() == TypeClass::UserData) {
auto field = dynamic_pointer_cast<UserData::UserDataScriptType>(indexerType)->GetField(identifier.GetHash()); auto field = dynamic_pointer_cast<const UserData::UserDataScriptType>(indexerType)->GetField(identifier.GetHash());
if (!setter) { if (!setter) {
if (!field->HasGetter()) { if (!field->HasGetter()) {
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UserDataFieldNoGetter, this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UserDataFieldNoGetter,
@ -670,11 +668,11 @@ namespace Porygon::Binder {
BoundExpression *Binder::BindNumericalTableExpression(const ParsedNumericalTableExpression *expression) { BoundExpression *Binder::BindNumericalTableExpression(const ParsedNumericalTableExpression *expression) {
auto expressions = expression->GetExpressions(); auto expressions = expression->GetExpressions();
auto boundExpressions = vector<const BoundExpression *>(expressions->size()); auto boundExpressions = vector<const BoundExpression *>(expressions->size());
shared_ptr<ScriptType> valueType = nullptr; shared_ptr<const ScriptType> valueType = nullptr;
if (!boundExpressions.empty()) { if (!boundExpressions.empty()) {
boundExpressions[0] = this->BindExpression(expressions->at(0)); boundExpressions[0] = this->BindExpression(expressions->at(0));
valueType = boundExpressions[0]->GetType(); 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)); boundExpressions[i] = this->BindExpression(expressions->at(i));
if (boundExpressions[i]->GetType().get()->operator!=(valueType.get())) { if (boundExpressions[i]->GetType().get()->operator!=(valueType.get())) {
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidTableValueType, this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidTableValueType,
@ -684,9 +682,9 @@ namespace Porygon::Binder {
} }
} }
if (valueType == nullptr) { if (valueType == nullptr) {
valueType = std::make_shared<ScriptType>(TypeClass::Nil); valueType = std::make_shared<const ScriptType>(TypeClass::Nil);
} }
auto tableType = std::make_shared<NumericalTableScriptType>(valueType); auto tableType = std::make_shared<const NumericalTableScriptType>(valueType);
return new BoundNumericalTableExpression(boundExpressions, tableType, expression->GetStartPosition(), return new BoundNumericalTableExpression(boundExpressions, tableType, expression->GetStartPosition(),
expression->GetLength()); expression->GetLength());
} }

View File

@ -12,7 +12,7 @@
using namespace std; using namespace std;
namespace Porygon::Binder { namespace Porygon::Binder {
enum class BoundExpressionKind { enum class BoundExpressionKind : u_int8_t {
Bad, Bad,
LiteralInteger, LiteralInteger,
@ -33,9 +33,9 @@ namespace Porygon::Binder {
class BoundExpression { class BoundExpression {
const unsigned int _start; const unsigned int _start;
const unsigned int _length; const unsigned int _length;
const shared_ptr<ScriptType> _type; const shared_ptr<const ScriptType> _type;
public: public:
BoundExpression(unsigned int start, unsigned int length, shared_ptr<ScriptType> type) BoundExpression(unsigned int start, unsigned int length, shared_ptr<const ScriptType> type)
: _start(start), : _start(start),
_length(length), _length(length),
_type(std::move(type)) { _type(std::move(type)) {
@ -43,23 +43,23 @@ namespace Porygon::Binder {
virtual ~BoundExpression() = default; virtual ~BoundExpression() = default;
virtual const BoundExpressionKind GetKind() const = 0; [[nodiscard]]
virtual BoundExpressionKind GetKind() const = 0;
virtual const std::shared_ptr<ScriptType> &GetType() const { [[nodiscard]]
virtual const std::shared_ptr<const ScriptType> &GetType() const {
return _type; return _type;
}; };
inline const unsigned int GetStartPosition() const { [[nodiscard]]
inline unsigned int GetStartPosition() const {
return _start; return _start;
} }
inline const unsigned int GetLength() const { [[nodiscard]]
inline unsigned int GetLength() const {
return _length; return _length;
} }
inline const unsigned int GetEndPosition() const {
return _start + _length - 1;
}
}; };
class BoundBadExpression : public BoundExpression { class BoundBadExpression : public BoundExpression {
@ -68,7 +68,8 @@ namespace Porygon::Binder {
make_shared<ScriptType>( make_shared<ScriptType>(
TypeClass::Error)) {} TypeClass::Error)) {}
inline const BoundExpressionKind GetKind() const final { [[nodiscard]]
inline BoundExpressionKind GetKind() const final {
return BoundExpressionKind::Bad; return BoundExpressionKind::Bad;
} }
}; };
@ -81,11 +82,13 @@ namespace Porygon::Binder {
_value(value) { _value(value) {
} }
inline const BoundExpressionKind GetKind() const final { [[nodiscard]]
inline BoundExpressionKind GetKind() const final {
return BoundExpressionKind::LiteralInteger; return BoundExpressionKind::LiteralInteger;
} }
inline const long GetValue() const { [[nodiscard]]
inline long GetValue() const {
return _value; return _value;
} }
}; };
@ -98,11 +101,13 @@ namespace Porygon::Binder {
_value(value) { _value(value) {
} }
inline const BoundExpressionKind GetKind() const final { [[nodiscard]]
inline BoundExpressionKind GetKind() const final {
return BoundExpressionKind::LiteralFloat; return BoundExpressionKind::LiteralFloat;
} }
inline const double GetValue() const { [[nodiscard]]
inline double GetValue() const {
return _value; return _value;
} }
}; };
@ -116,12 +121,14 @@ namespace Porygon::Binder {
_value(value) { _value(value) {
} }
inline const BoundExpressionKind GetKind() const final { [[nodiscard]]
inline BoundExpressionKind GetKind() const final {
return BoundExpressionKind::LiteralString; return BoundExpressionKind::LiteralString;
} }
inline const u16string GetValue() const { [[nodiscard]]
return _value; inline const u16string* GetValue() const {
return &_value;
} }
}; };
@ -133,11 +140,13 @@ namespace Porygon::Binder {
_value(value) { _value(value) {
} }
inline const BoundExpressionKind GetKind() const final { [[nodiscard]]
inline BoundExpressionKind GetKind() const final {
return BoundExpressionKind::LiteralBool; return BoundExpressionKind::LiteralBool;
} }
inline const bool GetValue() const { [[nodiscard]]
inline bool GetValue() const {
return _value; return _value;
} }
}; };
@ -145,9 +154,9 @@ namespace Porygon::Binder {
class BoundVariableExpression : public BoundExpression { class BoundVariableExpression : public BoundExpression {
const BoundVariableKey *_key; const BoundVariableKey *_key;
public: public:
BoundVariableExpression(BoundVariableKey *key, shared_ptr<ScriptType> type, unsigned int start, BoundVariableExpression(BoundVariableKey *key, const shared_ptr<const ScriptType>& type, unsigned int start,
unsigned int length) unsigned int length)
: BoundExpression(start, length, std::move(type)), : BoundExpression(start, length, type),
_key(key) { _key(key) {
} }
@ -155,10 +164,12 @@ namespace Porygon::Binder {
delete _key; delete _key;
} }
inline const BoundExpressionKind GetKind() const final { [[nodiscard]]
inline BoundExpressionKind GetKind() const final {
return BoundExpressionKind::Variable; return BoundExpressionKind::Variable;
} }
[[nodiscard]]
inline const BoundVariableKey *GetKey() const { inline const BoundVariableKey *GetKey() const {
return _key; return _key;
} }
@ -183,19 +194,23 @@ namespace Porygon::Binder {
delete _right; delete _right;
} }
inline const BoundExpressionKind GetKind() const final { [[nodiscard]]
inline BoundExpressionKind GetKind() const final {
return BoundExpressionKind::Binary; return BoundExpressionKind::Binary;
} }
[[nodiscard]]
inline const BoundExpression *GetLeft() const { inline const BoundExpression *GetLeft() const {
return _left; return _left;
} }
[[nodiscard]]
inline const BoundExpression *GetRight() const { inline const BoundExpression *GetRight() const {
return _right; return _right;
} }
inline const BoundBinaryOperation GetOperation() const { [[nodiscard]]
inline BoundBinaryOperation GetOperation() const {
return _operation; return _operation;
} }
}; };
@ -215,15 +230,18 @@ namespace Porygon::Binder {
delete _operand; delete _operand;
} }
inline const BoundExpressionKind GetKind() const final { [[nodiscard]]
inline BoundExpressionKind GetKind() const final {
return BoundExpressionKind::Unary; return BoundExpressionKind::Unary;
} }
[[nodiscard]]
inline const BoundExpression *GetOperand() const { inline const BoundExpression *GetOperand() const {
return _operand; return _operand;
} }
inline const BoundUnaryOperation GetOperation() const { [[nodiscard]]
inline BoundUnaryOperation GetOperation() const {
return _operation; return _operation;
} }
}; };
@ -233,7 +251,7 @@ namespace Porygon::Binder {
const BoundExpression *_indexExpression; const BoundExpression *_indexExpression;
public: public:
BoundIndexExpression(BoundExpression *indexableExpression, BoundExpression *indexExpression, BoundIndexExpression(BoundExpression *indexableExpression, BoundExpression *indexExpression,
shared_ptr<ScriptType> result, shared_ptr<const ScriptType> result,
unsigned int start, unsigned int length) unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression), : BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression),
_indexExpression(indexExpression) {} _indexExpression(indexExpression) {}
@ -243,14 +261,17 @@ namespace Porygon::Binder {
delete _indexExpression; delete _indexExpression;
} }
inline const BoundExpressionKind GetKind() const final { [[nodiscard]]
inline BoundExpressionKind GetKind() const final {
return BoundExpressionKind::Index; return BoundExpressionKind::Index;
} }
[[nodiscard]]
inline const BoundExpression *GetIndexableExpression() const { inline const BoundExpression *GetIndexableExpression() const {
return _indexableExpression; return _indexableExpression;
} }
[[nodiscard]]
inline const BoundExpression *GetIndexExpression() const { inline const BoundExpression *GetIndexExpression() const {
return _indexExpression; return _indexExpression;
} }
@ -260,8 +281,8 @@ namespace Porygon::Binder {
const BoundExpression *_indexableExpression; const BoundExpression *_indexableExpression;
const Utilities::HashedString _index; const Utilities::HashedString _index;
public: public:
BoundPeriodIndexExpression(BoundExpression *indexableExpression, Utilities::HashedString index, BoundPeriodIndexExpression(BoundExpression *indexableExpression, const Utilities::HashedString& index,
shared_ptr<ScriptType> result, shared_ptr<const ScriptType> result,
unsigned int start, unsigned int length) unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression), : BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression),
_index(index) {} _index(index) {}
@ -270,23 +291,26 @@ namespace Porygon::Binder {
delete _indexableExpression; delete _indexableExpression;
} }
inline const BoundExpressionKind GetKind() const final { [[nodiscard]]
inline BoundExpressionKind GetKind() const final {
return BoundExpressionKind::PeriodIndex; return BoundExpressionKind::PeriodIndex;
} }
[[nodiscard]]
inline const BoundExpression *GetIndexableExpression() const { inline const BoundExpression *GetIndexableExpression() const {
return _indexableExpression; return _indexableExpression;
} }
inline const Utilities::HashedString GetIndex() const { [[nodiscard]]
return _index; inline const Utilities::HashedString* GetIndex() const {
return &_index;
} }
}; };
class BoundNumericalTableExpression : public BoundExpression { class BoundNumericalTableExpression : public BoundExpression {
const vector<const BoundExpression *> _expressions; const vector<const BoundExpression *> _expressions;
public: public:
BoundNumericalTableExpression(vector<const BoundExpression *> expressions, shared_ptr<ScriptType> type, BoundNumericalTableExpression(vector<const BoundExpression *> expressions, shared_ptr<const ScriptType> type,
unsigned int start, unsigned int length) unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(type)), : BoundExpression(start, length, std::move(type)),
_expressions(std::move(expressions)) {} _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; return BoundExpressionKind::NumericalTable;
} }
[[nodiscard]]
inline const vector<const BoundExpression *> *GetExpressions() const { inline const vector<const BoundExpression *> *GetExpressions() const {
return &_expressions; return &_expressions;
} }

View File

@ -10,7 +10,7 @@ namespace Porygon::Binder {
const Porygon::GenericFunctionOption *_option; const Porygon::GenericFunctionOption *_option;
public: public:
BoundFunctionCallExpression(BoundExpression *functionExpression, vector<BoundExpression *> parameters, BoundFunctionCallExpression(BoundExpression *functionExpression, vector<BoundExpression *> parameters,
Porygon::GenericFunctionOption *option, shared_ptr<Porygon::ScriptType> result, Porygon::GenericFunctionOption *option, shared_ptr<const Porygon::ScriptType> result,
unsigned int start, unsigned int length) unsigned int start, unsigned int length)
: BoundExpression(start, length, move(result)), _functionExpression(functionExpression), : BoundExpression(start, length, move(result)), _functionExpression(functionExpression),
_parameters(move(parameters)), _option(option) {} _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; return Porygon::Binder::BoundExpressionKind::FunctionCall;
} }
[[nodiscard]]
inline const BoundExpression *GetFunctionExpression() const { inline const BoundExpression *GetFunctionExpression() const {
return _functionExpression; return _functionExpression;
} }
[[nodiscard]]
inline const vector<BoundExpression *> *GetParameters() const { inline const vector<BoundExpression *> *GetParameters() const {
return &_parameters; return &_parameters;
} }
[[nodiscard]]
inline const Porygon::GenericFunctionOption *GetFunctionOption() const { inline const Porygon::GenericFunctionOption *GetFunctionOption() const {
return _option; return _option;
} }

View File

@ -1,9 +1,9 @@
#include <utility>
#ifndef PORYGONLANG_BOUNDTABLEEXPRESSION_HPP #ifndef PORYGONLANG_BOUNDTABLEEXPRESSION_HPP
#define PORYGONLANG_BOUNDTABLEEXPRESSION_HPP #define PORYGONLANG_BOUNDTABLEEXPRESSION_HPP
#include <utility>
#include "../BoundStatements/BoundStatement.hpp" #include "../BoundStatements/BoundStatement.hpp"
namespace Porygon::Binder { namespace Porygon::Binder {
@ -20,10 +20,12 @@ namespace Porygon::Binder {
delete _block; delete _block;
} }
inline const BoundExpressionKind GetKind() const final { [[nodiscard]]
inline BoundExpressionKind GetKind() const final {
return BoundExpressionKind::Table; return BoundExpressionKind::Table;
} }
[[nodiscard]]
inline const BoundBlockStatement *GetBlock() const { inline const BoundBlockStatement *GetBlock() const {
return _block; return _block;
} }

View File

@ -10,9 +10,9 @@ namespace Porygon::Binder {
class BoundFunctionDeclarationStatement : public BoundStatement { class BoundFunctionDeclarationStatement : public BoundStatement {
const BoundVariableKey *_key; const BoundVariableKey *_key;
const std::shared_ptr<BoundBlockStatement> _block; const std::shared_ptr<BoundBlockStatement> _block;
const std::shared_ptr<GenericFunctionScriptType> _type; const std::shared_ptr<const GenericFunctionScriptType> _type;
public: public:
BoundFunctionDeclarationStatement(std::shared_ptr<GenericFunctionScriptType> type, BoundVariableKey *key, BoundFunctionDeclarationStatement(std::shared_ptr<const GenericFunctionScriptType> type, const BoundVariableKey *key,
BoundBlockStatement *block) BoundBlockStatement *block)
: _key(key), _block(block), _type(std::move(type)) { : _key(key), _block(block), _type(std::move(type)) {
} }
@ -21,19 +21,23 @@ namespace Porygon::Binder {
delete _key; delete _key;
} }
inline const BoundStatementKind GetKind() const final { [[nodiscard]]
inline BoundStatementKind GetKind() const final {
return BoundStatementKind::FunctionDeclaration; return BoundStatementKind::FunctionDeclaration;
} }
[[nodiscard]]
inline const BoundVariableKey *GetKey() const { inline const BoundVariableKey *GetKey() const {
return _key; return _key;
} }
inline const std::shared_ptr<BoundBlockStatement> GetBlock() const { [[nodiscard]]
inline std::shared_ptr<const BoundBlockStatement> GetBlock() const {
return _block; return _block;
} }
inline const std::shared_ptr<GenericFunctionScriptType> GetType() const { [[nodiscard]]
inline std::shared_ptr<const GenericFunctionScriptType> GetType() const {
return _type; return _type;
} }
}; };

View File

@ -10,7 +10,7 @@
using namespace std; using namespace std;
namespace Porygon::Binder { namespace Porygon::Binder {
enum class BoundStatementKind { enum class BoundStatementKind : u_int8_t {
Bad, Bad,
Break, Break,
Script, Script,
@ -28,29 +28,32 @@ namespace Porygon::Binder {
class BoundStatement { class BoundStatement {
public: public:
virtual const BoundStatementKind GetKind() const = 0; [[nodiscard]]
virtual BoundStatementKind GetKind() const = 0;
virtual ~BoundStatement() = default; virtual ~BoundStatement() = default;
}; };
class BoundBadStatement : public BoundStatement { class BoundBadStatement : public BoundStatement {
public: public:
inline const BoundStatementKind GetKind() const final { [[nodiscard]]
inline BoundStatementKind GetKind() const final {
return BoundStatementKind::Bad; return BoundStatementKind::Bad;
} }
}; };
class BoundBreakStatement : public BoundStatement { class BoundBreakStatement : public BoundStatement {
public: public:
inline const BoundStatementKind GetKind() const final { [[nodiscard]]
inline BoundStatementKind GetKind() const final {
return BoundStatementKind::Break; return BoundStatementKind::Break;
} }
}; };
class BoundBlockStatement : public BoundStatement { class BoundBlockStatement : public BoundStatement {
const vector<BoundStatement *> _statements; const vector<const BoundStatement *> _statements;
public: public:
explicit BoundBlockStatement(vector<BoundStatement *> statements) explicit BoundBlockStatement(vector<const BoundStatement *> statements)
: _statements(std::move(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; return BoundStatementKind::Block;
} }
inline const vector<BoundStatement *> *GetStatements() const { [[nodiscard]]
inline const vector<const BoundStatement *> *GetStatements() const {
return &_statements; return &_statements;
} }
}; };
@ -72,16 +77,18 @@ namespace Porygon::Binder {
class BoundScriptStatement : public BoundBlockStatement { class BoundScriptStatement : public BoundBlockStatement {
const int _localVariableCount; const int _localVariableCount;
public: public:
explicit BoundScriptStatement(vector<BoundStatement *> statements, int localVariableCount) explicit BoundScriptStatement(vector<const BoundStatement *> statements, int localVariableCount)
: BoundBlockStatement(std::move(statements)), : BoundBlockStatement(std::move(statements)),
_localVariableCount(localVariableCount) { _localVariableCount(localVariableCount) {
} }
inline const BoundStatementKind GetKind() const final { [[nodiscard]]
inline BoundStatementKind GetKind() const final {
return BoundStatementKind::Script; return BoundStatementKind::Script;
} }
inline const int GetLocalVariableCount() const { [[nodiscard]]
inline int GetLocalVariableCount() const {
return _localVariableCount; return _localVariableCount;
} }
}; };
@ -98,10 +105,12 @@ namespace Porygon::Binder {
delete _expression; delete _expression;
} }
inline const BoundStatementKind GetKind() const final { [[nodiscard]]
inline BoundStatementKind GetKind() const final {
return BoundStatementKind::Expression; return BoundStatementKind::Expression;
} }
[[nodiscard]]
inline const BoundExpression *GetExpression() const { inline const BoundExpression *GetExpression() const {
return _expression; return _expression;
} }
@ -111,7 +120,7 @@ namespace Porygon::Binder {
const BoundVariableKey *_key; const BoundVariableKey *_key;
const BoundExpression *_expression; const BoundExpression *_expression;
public: public:
BoundAssignmentStatement(BoundVariableKey *key, BoundExpression *expression) BoundAssignmentStatement(const BoundVariableKey *key, BoundExpression *expression)
: _key(key), _expression(expression) { : _key(key), _expression(expression) {
} }
@ -120,14 +129,17 @@ namespace Porygon::Binder {
delete _expression; delete _expression;
} }
inline const BoundStatementKind GetKind() const final { [[nodiscard]]
inline BoundStatementKind GetKind() const final {
return BoundStatementKind::Assignment; return BoundStatementKind::Assignment;
} }
[[nodiscard]]
inline const BoundVariableKey *GetKey() const { inline const BoundVariableKey *GetKey() const {
return _key; return _key;
} }
[[nodiscard]]
inline const BoundExpression *GetExpression() const { inline const BoundExpression *GetExpression() const {
return _expression; return _expression;
} }
@ -146,14 +158,17 @@ namespace Porygon::Binder {
delete _valueExpression; delete _valueExpression;
} }
inline const BoundStatementKind GetKind() const final { [[nodiscard]]
inline BoundStatementKind GetKind() const final {
return BoundStatementKind::IndexAssignment; return BoundStatementKind::IndexAssignment;
} }
[[nodiscard]]
inline const BoundExpression *GetIndexExpression() const { inline const BoundExpression *GetIndexExpression() const {
return _indexExpression; return _indexExpression;
} }
[[nodiscard]]
inline const BoundExpression *GetValueExpression() const { inline const BoundExpression *GetValueExpression() const {
return _valueExpression; return _valueExpression;
} }
@ -170,10 +185,12 @@ namespace Porygon::Binder {
delete _expression; delete _expression;
} }
inline const BoundStatementKind GetKind() const final { [[nodiscard]]
inline BoundStatementKind GetKind() const final {
return BoundStatementKind::Return; return BoundStatementKind::Return;
} }
[[nodiscard]]
inline const BoundExpression *GetExpression() const { inline const BoundExpression *GetExpression() const {
return _expression; return _expression;
} }
@ -194,18 +211,22 @@ namespace Porygon::Binder {
delete _elseStatement; delete _elseStatement;
} }
inline const BoundStatementKind GetKind() const final { [[nodiscard]]
inline BoundStatementKind GetKind() const final {
return BoundStatementKind::Conditional; return BoundStatementKind::Conditional;
} }
[[nodiscard]]
inline const BoundExpression *GetCondition() const { inline const BoundExpression *GetCondition() const {
return _condition; return _condition;
} }
[[nodiscard]]
inline const BoundStatement *GetBlock() const { inline const BoundStatement *GetBlock() const {
return _block; return _block;
} }
[[nodiscard]]
inline const BoundStatement *GetElseStatement() const { inline const BoundStatement *GetElseStatement() const {
return _elseStatement; return _elseStatement;
} }
@ -233,26 +254,32 @@ namespace Porygon::Binder {
delete _block; delete _block;
} }
inline const BoundStatementKind GetKind() const final { [[nodiscard]]
inline BoundStatementKind GetKind() const final {
return BoundStatementKind::NumericalFor; return BoundStatementKind::NumericalFor;
} }
[[nodiscard]]
inline const BoundVariableKey* GetIdentifier() const{ inline const BoundVariableKey* GetIdentifier() const{
return _identifier; return _identifier;
} }
[[nodiscard]]
inline const BoundExpression* GetStart() const{ inline const BoundExpression* GetStart() const{
return _start; return _start;
} }
[[nodiscard]]
inline const BoundExpression* GetEnd() const{ inline const BoundExpression* GetEnd() const{
return _end; return _end;
} }
[[nodiscard]]
inline const BoundExpression* GetStep() const{ inline const BoundExpression* GetStep() const{
return _step; return _step;
} }
[[nodiscard]]
inline const BoundStatement* GetBlock() const{ inline const BoundStatement* GetBlock() const{
return _block; return _block;
} }
@ -278,22 +305,27 @@ namespace Porygon::Binder {
delete _block; delete _block;
} }
inline const BoundStatementKind GetKind() const final { [[nodiscard]]
inline BoundStatementKind GetKind() const final {
return BoundStatementKind::GenericFor; return BoundStatementKind::GenericFor;
} }
[[nodiscard]]
inline const BoundVariableKey* GetKeyIdentifier() const{ inline const BoundVariableKey* GetKeyIdentifier() const{
return _keyIdentifier; return _keyIdentifier;
} }
[[nodiscard]]
inline const BoundVariableKey* GetValueIdentifier() const{ inline const BoundVariableKey* GetValueIdentifier() const{
return _valueIdentifier; return _valueIdentifier;
} }
[[nodiscard]]
inline const BoundExpression* GetIterator() const{ inline const BoundExpression* GetIterator() const{
return _iterator; return _iterator;
} }
[[nodiscard]]
inline const BoundStatement* GetBlock() const{ inline const BoundStatement* GetBlock() const{
return _block; return _block;
} }
@ -312,14 +344,17 @@ namespace Porygon::Binder {
delete _block; delete _block;
} }
inline const BoundStatementKind GetKind() const final { [[nodiscard]]
inline BoundStatementKind GetKind() const final {
return BoundStatementKind::While; return BoundStatementKind::While;
} }
[[nodiscard]]
inline const BoundExpression* GetCondition() const{ inline const BoundExpression* GetCondition() const{
return _condition; return _condition;
} }
[[nodiscard]]
inline const BoundStatement* GetBlock() const{ inline const BoundStatement* GetBlock() const{
return _block; return _block;
} }

View File

@ -1,6 +1,4 @@
#include <utility> #include <utility>
#include "BoundScope.hpp" #include "BoundScope.hpp"
#include "../../StandardLibraries/StaticScope.hpp" #include "../../StandardLibraries/StaticScope.hpp"
@ -15,7 +13,7 @@ namespace Porygon::Binder {
BoundScope::~BoundScope() { BoundScope::~BoundScope() {
for (auto scope : _localScope) { for (auto scope : _localScope) {
for (auto v : *scope) { for (const auto& v : *scope) {
delete v.second; delete v.second;
} }
delete scope; delete scope;
@ -33,7 +31,7 @@ namespace Porygon::Binder {
void BoundScope::GoOuterScope() { void BoundScope::GoOuterScope() {
auto scope = _localScope[_currentScope - 1]; auto scope = _localScope[_currentScope - 1];
for (auto v : *scope) { for (const auto& v : *scope) {
delete v.second; delete v.second;
} }
scope->clear(); scope->clear();
@ -79,7 +77,7 @@ namespace Porygon::Binder {
} }
} }
VariableAssignment BoundScope::CreateExplicitLocal(const Utilities::HashedString& identifier, std::shared_ptr<ScriptType> type) { VariableAssignment BoundScope::CreateExplicitLocal(const Utilities::HashedString& identifier, std::shared_ptr<const ScriptType> type) {
auto scope = this->_localScope.at(this->_currentScope - 1); auto scope = this->_localScope.at(this->_currentScope - 1);
if (scope->find(identifier) != scope->end()) { if (scope->find(identifier) != scope->end()) {
return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr); return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr);
@ -89,7 +87,7 @@ namespace Porygon::Binder {
new BoundVariableKey(identifier, this->_currentScope, true)); new BoundVariableKey(identifier, this->_currentScope, true));
} }
VariableAssignment BoundScope::AssignVariable(const Utilities::HashedString& identifier, const std::shared_ptr<ScriptType> &type) { VariableAssignment BoundScope::AssignVariable(const Utilities::HashedString& identifier, const std::shared_ptr<const ScriptType> &type) {
int exists = this->Exists(identifier); int exists = this->Exists(identifier);
if (exists < 0) { if (exists < 0) {
// Creation // Creation

View File

@ -32,17 +32,13 @@ namespace Porygon::Binder {
BoundVariable *GetVariable(uint32_t scope, const Utilities::HashedString& identifier); BoundVariable *GetVariable(uint32_t scope, const Utilities::HashedString& identifier);
VariableAssignment CreateExplicitLocal(const Utilities::HashedString& identifier, std::shared_ptr<ScriptType> type); VariableAssignment CreateExplicitLocal(const Utilities::HashedString& identifier, std::shared_ptr<const ScriptType> type);
VariableAssignment AssignVariable(const Utilities::HashedString& identifier, const std::shared_ptr<ScriptType> &type); VariableAssignment AssignVariable(const Utilities::HashedString& identifier, const std::shared_ptr<const ScriptType> &type);
inline size_t GetLocalVariableCount() { inline size_t GetLocalVariableCount() {
return _localScope.size(); return _localScope.size();
} }
inline int GetCurrentScope() {
return _currentScope;
}
}; };
} }

View File

@ -1,9 +1,9 @@
#include <utility>
#ifndef PORYGONLANG_BOUNDVARIABLE_HPP #ifndef PORYGONLANG_BOUNDVARIABLE_HPP
#define PORYGONLANG_BOUNDVARIABLE_HPP #define PORYGONLANG_BOUNDVARIABLE_HPP
#include <utility>
#include <memory> #include <memory>
#include "../../ScriptType.hpp" #include "../../ScriptType.hpp"
@ -11,12 +11,12 @@ using namespace std;
namespace Porygon::Binder { namespace Porygon::Binder {
class BoundVariable { class BoundVariable {
std::shared_ptr<ScriptType> _type; std::shared_ptr<const ScriptType> _type;
public: public:
inline explicit BoundVariable(std::shared_ptr<ScriptType> type) : _type(std::move(type)) { inline explicit BoundVariable(std::shared_ptr<const ScriptType> type) : _type(std::move(type)) {
} }
inline std::shared_ptr<ScriptType> GetType() { inline std::shared_ptr<const ScriptType> GetType() {
return _type; return _type;
} }
}; };

View File

@ -19,26 +19,30 @@ namespace Porygon::Binder {
} }
public: public:
BoundVariableKey(Utilities::HashedString id, unsigned int scope, bool creation) BoundVariableKey(const Utilities::HashedString& id, unsigned int scope, bool creation)
: _identifier(id), : _identifier(id),
_scopeId(scope), _scopeId(scope),
_isCreation(creation), _isCreation(creation),
_hash(KnuthsHash(id.GetHash(), scope)) {} _hash(KnuthsHash(id.GetHash(), scope)) {}
inline const Utilities::HashedString GetIdentifier() const { [[nodiscard]]
return _identifier; inline const Utilities::HashedString* GetIdentifier() const {
return &_identifier;
} }
inline const unsigned int GetScopeId() const { [[nodiscard]]
inline unsigned int GetScopeId() const {
return _scopeId; return _scopeId;
} }
inline const bool IsCreation() const { [[nodiscard]]
inline bool IsCreation() const {
return _isCreation; return _isCreation;
} }
inline const uint64_t GetHash() const { [[nodiscard]]
inline uint64_t GetHash() const {
return _hash; return _hash;
} }
}; };

View File

@ -13,18 +13,20 @@ namespace Porygon::Binder {
class VariableAssignment { class VariableAssignment {
VariableAssignmentResult _result; VariableAssignmentResult _result;
BoundVariableKey *_key; const BoundVariableKey *_key;
public: public:
VariableAssignment(VariableAssignmentResult result, BoundVariableKey *key) { VariableAssignment(VariableAssignmentResult result, BoundVariableKey *key) {
_result = result; _result = result;
_key = key; _key = key;
} }
[[nodiscard]]
inline VariableAssignmentResult GetResult() { inline VariableAssignmentResult GetResult() {
return _result; return _result;
} }
inline BoundVariableKey *GetKey() { [[nodiscard]]
inline const BoundVariableKey *GetKey() {
return _key; return _key;
} }
}; };

View File

@ -1,9 +1,7 @@
#include <utility>
#ifndef PORYGONLANG_DIAGNOSTIC_HPP #ifndef PORYGONLANG_DIAGNOSTIC_HPP
#define PORYGONLANG_DIAGNOSTIC_HPP #define PORYGONLANG_DIAGNOSTIC_HPP
#include <utility>
#include <vector> #include <vector>
#include <string> #include <string>
#include "DiagnosticSeverity.hpp" #include "DiagnosticSeverity.hpp"
@ -28,22 +26,27 @@ namespace Porygon::Diagnostics {
delete _message; delete _message;
} }
[[nodiscard]]
inline DiagnosticSeverity GetSeverity() { inline DiagnosticSeverity GetSeverity() {
return _severity; return _severity;
} }
[[nodiscard]]
inline DiagnosticCode GetCode() { inline DiagnosticCode GetCode() {
return _code; return _code;
} }
[[nodiscard]]
inline unsigned int GetStartPosition() { inline unsigned int GetStartPosition() {
return _start; return _start;
} }
[[nodiscard]]
inline unsigned int GetLength() { inline unsigned int GetLength() {
return _length; return _length;
} }
[[nodiscard]]
inline std::vector<std::string> GetArguments(){ inline std::vector<std::string> GetArguments(){
return _arguments; return _arguments;
} }
@ -52,6 +55,7 @@ namespace Porygon::Diagnostics {
this -> _message = s; this -> _message = s;
} }
[[nodiscard]]
inline std::string* GetMessage(){ inline std::string* GetMessage(){
return _message; return _message;
} }

View File

@ -3,7 +3,7 @@
#define PORYGONLANG_DIAGNOSTICCODE_HPP #define PORYGONLANG_DIAGNOSTICCODE_HPP
namespace Porygon::Diagnostics { namespace Porygon::Diagnostics {
enum class DiagnosticCode { enum class DiagnosticCode : u_int8_t {
// Lex diagnostics // Lex diagnostics
UnexpectedCharacter, UnexpectedCharacter,
InvalidStringControlCharacter, InvalidStringControlCharacter,

View File

@ -1,5 +1,3 @@
#include <utility>
#include <memory> // For std::unique_ptr #include <memory> // For std::unique_ptr
#include "DiagnosticsHolder.hpp" #include "DiagnosticsHolder.hpp"
#include "ErrorMessages-EN-US.cpp" #include "ErrorMessages-EN-US.cpp"
@ -10,23 +8,23 @@ vector<Diagnostic> DiagnosticsHolder::GetDiagnostics() {
} }
void DiagnosticsHolder::Log(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length, void DiagnosticsHolder::Log(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length,
std::vector<string> arguments) { const std::vector<string>& arguments) {
_diagnostics.emplace_back(severity, code, start, length, arguments); _diagnostics.emplace_back(severity, code, start, length, arguments);
if (severity >= DiagnosticSeverity::Error){ if (severity >= DiagnosticSeverity::Error){
_hasErrors = true; _hasErrors = true;
} }
} }
void DiagnosticsHolder::LogError(DiagnosticCode code, unsigned int start, unsigned int length, std::vector<string> arguments) { void DiagnosticsHolder::LogError(DiagnosticCode code, unsigned int start, unsigned int length, const std::vector<string>& arguments) {
Log(DiagnosticSeverity::Error, code, start, length, std::move(arguments)); Log(DiagnosticSeverity::Error, code, start, length, arguments);
} }
void DiagnosticsHolder::LogWarning(DiagnosticCode code, unsigned int start, unsigned int length, std::vector<string> arguments) { void DiagnosticsHolder::LogWarning(DiagnosticCode code, unsigned int start, unsigned int length, const std::vector<string>& arguments) {
Log(DiagnosticSeverity::Warning, code, start, length, std::move(arguments)); Log(DiagnosticSeverity::Warning, code, start, length, arguments);
} }
void DiagnosticsHolder::LogInfo(DiagnosticCode code, unsigned int start, unsigned int length, std::vector<string> arguments) { void DiagnosticsHolder::LogInfo(DiagnosticCode code, unsigned int start, unsigned int length, const std::vector<string>& arguments) {
Log(DiagnosticSeverity::Info, code, start, length, std::move(arguments)); Log(DiagnosticSeverity::Info, code, start, length, arguments);
} }
bool DiagnosticsHolder::HasErrors() { 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<string> arguments) string PrettyDiagnostic ( const char * format, vector<string> arguments)
{ {
string result = string(format); 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 keyToReplace = "{" + to_string(i) + "}";
auto argument = arguments[i]; auto argument = arguments[i];
findAndReplaceAll(result, keyToReplace, argument); findAndReplaceAll(result, keyToReplace, argument);

View File

@ -35,13 +35,13 @@ namespace Porygon::Diagnostics {
_diagnostics.clear(); _diagnostics.clear();
} }
void Log(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length, std::vector<string> arguments = {}); void Log(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length, const std::vector<string>& arguments = {});
void LogError(DiagnosticCode code, unsigned int start, unsigned int length, std::vector<string> arguments = {}); void LogError(DiagnosticCode code, unsigned int start, unsigned int length, const std::vector<string>& arguments = {});
void LogWarning(DiagnosticCode code, unsigned int start, unsigned int length, std::vector<string> arguments = {}); void LogWarning(DiagnosticCode code, unsigned int start, unsigned int length, const std::vector<string>& arguments = {});
void LogInfo(DiagnosticCode code, unsigned int start, unsigned int length, std::vector<string> arguments = {}); void LogInfo(DiagnosticCode code, unsigned int start, unsigned int length, const std::vector<string>& arguments = {});
bool HasErrors(); bool HasErrors();
@ -52,6 +52,7 @@ namespace Porygon::Diagnostics {
Diagnostic *GetDiagnosticAt(int position); Diagnostic *GetDiagnosticAt(int position);
size_t GetLineFromPosition(size_t i); size_t GetLineFromPosition(size_t i);
[[nodiscard]]
inline size_t GetStartPositionForLine(size_t i){ inline size_t GetStartPositionForLine(size_t i){
return _lineStarts[i]; return _lineStarts[i];
} }

View File

@ -8,7 +8,7 @@
using namespace Porygon::Binder; using namespace Porygon::Binder;
namespace Porygon::Evaluation { namespace Porygon::Evaluation {
const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerBinary(const BoundBinaryExpression *expression) { shared_ptr<const NumericEvalValue> Evaluator::EvaluateIntegerBinary(const BoundBinaryExpression *expression) {
auto leftValue = this->EvaluateIntegerExpression(expression->GetLeft()); auto leftValue = this->EvaluateIntegerExpression(expression->GetLeft());
auto rightValue = this->EvaluateIntegerExpression(expression->GetRight()); auto rightValue = this->EvaluateIntegerExpression(expression->GetRight());
@ -26,7 +26,7 @@ namespace Porygon::Evaluation {
} }
} }
const shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanBinary(const BoundBinaryExpression *expression) { shared_ptr<const BooleanEvalValue> Evaluator::EvaluateBooleanBinary(const BoundBinaryExpression *expression) {
switch (expression->GetOperation()) { switch (expression->GetOperation()) {
case BoundBinaryOperation::Equality: { case BoundBinaryOperation::Equality: {
auto leftValue = this->EvaluateExpression(expression->GetLeft()); auto leftValue = this->EvaluateExpression(expression->GetLeft());
@ -78,7 +78,7 @@ namespace Porygon::Evaluation {
} }
} }
const shared_ptr<StringEvalValue> Evaluator::EvaluateStringBinary(const BoundBinaryExpression *expression) { shared_ptr<const StringEvalValue> Evaluator::EvaluateStringBinary(const BoundBinaryExpression *expression) {
if (expression->GetOperation() != BoundBinaryOperation::Concatenation) if (expression->GetOperation() != BoundBinaryOperation::Concatenation)
throw; throw;
std::basic_ostringstream<char16_t> stringStream; std::basic_ostringstream<char16_t> stringStream;

View File

@ -7,28 +7,28 @@
namespace Porygon::Evaluation { namespace Porygon::Evaluation {
extern "C" { extern "C" {
Porygon::TypeClass GetEvalValueTypeClass(EvalValue *v) { Porygon::TypeClass GetEvalValueTypeClass(const EvalValue *v) {
return v->GetTypeClass(); return v->GetTypeClass();
} }
int64_t EvaluateEvalValueInteger(EvalValue *v) { int64_t EvaluateEvalValueInteger(const EvalValue *v) {
return v->EvaluateInteger(); return v->EvaluateInteger();
} }
double EvaluateEvalValueFloat(EvalValue *v) { double EvaluateEvalValueFloat(const EvalValue *v) {
return v->EvaluateFloat(); return v->EvaluateFloat();
} }
bool EvaluateEvalValueBool(EvalValue *v) { bool EvaluateEvalValueBool(const EvalValue *v) {
return v->EvaluateBool(); return v->EvaluateBool();
} }
size_t GetEvalValueStringLength(EvalValue *v) { size_t GetEvalValueStringLength(const EvalValue *v) {
auto result = v->EvaluateString(); auto result = v->EvaluateString();
return result.size(); 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(); auto result = v->EvaluateString();
for (int i = 0; i < capacity; i++){ for (int i = 0; i < capacity; i++){
dst[i] = result[i]; dst[i] = result[i];

View File

@ -21,46 +21,58 @@ namespace Porygon::Evaluation {
virtual ~EvalValue() = default; 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)); return !(this->operator==(b));
} }
virtual const shared_ptr<EvalValue> Clone() const = 0; [[nodiscard]]
virtual shared_ptr<const EvalValue> Clone() const = 0;
virtual const long EvaluateInteger() const { [[nodiscard]]
virtual long EvaluateInteger() const {
throw EvaluationException("Can't evaluate this EvalValue as integer."); 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."); 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."); 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."); 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<EvalValue> IndexValue(EvalValue *val) const { [[nodiscard]]
virtual shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const {
throw EvaluationException("Can't index this EvalValue"); throw EvaluationException("Can't index this EvalValue");
} }
virtual const shared_ptr<EvalValue> IndexValue(uint32_t hash) const { [[nodiscard]]
virtual shared_ptr<const EvalValue> IndexValue(uint32_t hash) const {
throw EvaluationException("Can't index this EvalValue"); throw EvaluationException("Can't index this EvalValue");
} }
virtual void SetIndexValue(EvalValue *key, const shared_ptr<EvalValue> &value) const { virtual void SetIndexValue(const EvalValue *key, const shared_ptr<const EvalValue> &value) const {
throw EvaluationException("Can't index this EvalValue"); throw EvaluationException("Can't index this EvalValue");
} }
[[nodiscard]]
virtual Iterator * GetKeyIterator() const{ virtual Iterator * GetKeyIterator() const{
throw EvaluationException("Can't iterate over this EvalValue"); throw EvaluationException("Can't iterate over this EvalValue");
} }
@ -73,25 +85,30 @@ namespace Porygon::Evaluation {
: _value(val) { : _value(val) {
} }
inline const shared_ptr<EvalValue> Clone() const final { [[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return make_shared<BooleanEvalValue>(_value); return make_shared<BooleanEvalValue>(_value);
} }
inline const TypeClass GetTypeClass() const final { [[nodiscard]]
inline TypeClass GetTypeClass() const final {
return TypeClass::Bool; return TypeClass::Bool;
} }
inline const bool EvaluateBool() const final { [[nodiscard]]
inline bool EvaluateBool() const final {
return _value; return _value;
} }
const bool operator==(EvalValue *b) const final { [[nodiscard]]
bool operator==(const EvalValue *b) const final {
if (b->GetTypeClass() != TypeClass::Bool) if (b->GetTypeClass() != TypeClass::Bool)
return false; return false;
return this->EvaluateBool() == b->EvaluateBool(); return this->EvaluateBool() == b->EvaluateBool();
}; };
inline const std::size_t GetHashCode() const final { [[nodiscard]]
inline std::size_t GetHashCode() const final {
return _value; return _value;
} }
}; };

View File

@ -1,5 +1,3 @@
#include <utility>
#ifndef PORYGONLANG_EVALVALUEHELPER_HPP #ifndef PORYGONLANG_EVALVALUEHELPER_HPP
#define PORYGONLANG_EVALVALUEHELPER_HPP #define PORYGONLANG_EVALVALUEHELPER_HPP

View File

@ -5,18 +5,22 @@
namespace Porygon::Evaluation{ namespace Porygon::Evaluation{
class NilEvalValue : public EvalValue{ class NilEvalValue : public EvalValue{
inline const TypeClass GetTypeClass() const final{ [[nodiscard]]
inline TypeClass GetTypeClass() const final{
return TypeClass ::Nil; 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; return b->GetTypeClass() == TypeClass ::Nil;
} }
inline const shared_ptr<EvalValue> Clone() const final{ [[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final{
return make_shared<NilEvalValue>(); return make_shared<NilEvalValue>();
} }
inline const std::size_t GetHashCode() const final{ [[nodiscard]]
inline std::size_t GetHashCode() const final{
return 0; return 0;
} }
}; };

View File

@ -2,74 +2,74 @@
#include "NumericEvalValue.hpp" #include "NumericEvalValue.hpp"
namespace Porygon::Evaluation { namespace Porygon::Evaluation {
const shared_ptr<NumericEvalValue> NumericEvalValue::operator+(const shared_ptr<NumericEvalValue> &b) const { shared_ptr<const NumericEvalValue> NumericEvalValue::operator+(const shared_ptr<const NumericEvalValue> &b) const {
if (this->IsFloat()) { if (this->IsFloat()) {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetFloatValue() + b->GetFloatValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() + b->GetFloatValue());
} else { } else {
return make_shared<FloatEvalValue>(this->GetFloatValue() + b->GetIntegerValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() + (double) b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetIntegerValue() + b->GetFloatValue()); return make_shared<FloatEvalValue>((double) this->GetIntegerValue() + b->GetFloatValue());
} else { } else {
return make_shared<IntegerEvalValue>(this->GetIntegerValue() + b->GetIntegerValue()); return make_shared<IntegerEvalValue>(this->GetIntegerValue() + b->GetIntegerValue());
} }
} }
} }
const shared_ptr<NumericEvalValue> NumericEvalValue::operator-(const shared_ptr<NumericEvalValue> &b) const { shared_ptr<const NumericEvalValue> NumericEvalValue::operator-(const shared_ptr<const NumericEvalValue> &b) const {
if (this->IsFloat()) { if (this->IsFloat()) {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetFloatValue() - b->GetFloatValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() - b->GetFloatValue());
} else { } else {
return make_shared<FloatEvalValue>(this->GetFloatValue() - b->GetIntegerValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() - (double) b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetIntegerValue() - b->GetFloatValue()); return make_shared<FloatEvalValue>((double) this->GetIntegerValue() - b->GetFloatValue());
} else { } else {
return make_shared<IntegerEvalValue>(this->GetIntegerValue() - b->GetIntegerValue()); return make_shared<IntegerEvalValue>(this->GetIntegerValue() - b->GetIntegerValue());
} }
} }
} }
const shared_ptr<NumericEvalValue> NumericEvalValue::operator*(const shared_ptr<NumericEvalValue> &b) const { shared_ptr<const NumericEvalValue> NumericEvalValue::operator*(const shared_ptr<const NumericEvalValue> &b) const {
if (this->IsFloat()) { if (this->IsFloat()) {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetFloatValue() * b->GetFloatValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() * b->GetFloatValue());
} else { } else {
return make_shared<FloatEvalValue>(this->GetFloatValue() * b->GetIntegerValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() * (double) b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetIntegerValue() * b->GetFloatValue()); return make_shared<FloatEvalValue>((double) this->GetIntegerValue() * b->GetFloatValue());
} else { } else {
return make_shared<IntegerEvalValue>(this->GetIntegerValue() * b->GetIntegerValue()); return make_shared<IntegerEvalValue>(this->GetIntegerValue() * b->GetIntegerValue());
} }
} }
} }
const shared_ptr<NumericEvalValue> NumericEvalValue::operator/(const shared_ptr<NumericEvalValue> &b) const { shared_ptr<const NumericEvalValue> NumericEvalValue::operator/(const shared_ptr<const NumericEvalValue> &b) const {
if (this->IsFloat()) { if (this->IsFloat()) {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetFloatValue() / b->GetFloatValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() / b->GetFloatValue());
} else { } else {
return make_shared<FloatEvalValue>(this->GetFloatValue() / b->GetIntegerValue()); return make_shared<FloatEvalValue>(this->GetFloatValue() / (double) b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetIntegerValue() / b->GetFloatValue()); return make_shared<FloatEvalValue>((double) this->GetIntegerValue() / b->GetFloatValue());
} else { } else {
return make_shared<IntegerEvalValue>(this->GetIntegerValue() / b->GetIntegerValue()); return make_shared<IntegerEvalValue>(this->GetIntegerValue() / b->GetIntegerValue());
} }
} }
} }
const bool NumericEvalValue::operator==(EvalValue *b) const { bool NumericEvalValue::operator==(const EvalValue *b) const {
if (b->GetTypeClass() != TypeClass::Number) if (b->GetTypeClass() != TypeClass::Number)
return false; return false;
auto numVal = dynamic_cast<NumericEvalValue*>(b); auto numVal = dynamic_cast<const NumericEvalValue*>(b);
if (this->IsFloat() != numVal->IsFloat()) if (this->IsFloat() != numVal->IsFloat())
return false; return false;
@ -80,64 +80,64 @@ namespace Porygon::Evaluation {
} }
} }
const shared_ptr<BooleanEvalValue> NumericEvalValue::operator<(const shared_ptr<NumericEvalValue> &b) const { shared_ptr<const BooleanEvalValue> NumericEvalValue::operator<(const shared_ptr<const NumericEvalValue> &b) const {
if (this->IsFloat()) { if (this->IsFloat()) {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetFloatValue() < b->GetFloatValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() < b->GetFloatValue());
} else { } else {
return make_shared<BooleanEvalValue>(this->GetFloatValue() < b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() < (double) b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetIntegerValue() < b->GetFloatValue()); return make_shared<BooleanEvalValue>((double) this->GetIntegerValue() < b->GetFloatValue());
} else { } else {
return make_shared<BooleanEvalValue>(this->GetIntegerValue() < b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetIntegerValue() < b->GetIntegerValue());
} }
} }
} }
const shared_ptr<BooleanEvalValue> NumericEvalValue::operator<=(const shared_ptr<NumericEvalValue> &b) const { shared_ptr<const BooleanEvalValue> NumericEvalValue::operator<=(const shared_ptr<const NumericEvalValue> &b) const {
if (this->IsFloat()) { if (this->IsFloat()) {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetFloatValue() <= b->GetFloatValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() <= b->GetFloatValue());
} else { } else {
return make_shared<BooleanEvalValue>(this->GetFloatValue() <= b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() <=(double) b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetIntegerValue() <= b->GetFloatValue()); return make_shared<BooleanEvalValue>((double) this->GetIntegerValue() <= b->GetFloatValue());
} else { } else {
return make_shared<BooleanEvalValue>(this->GetIntegerValue() <= b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetIntegerValue() <= b->GetIntegerValue());
} }
} }
} }
const shared_ptr<BooleanEvalValue> NumericEvalValue::operator>(const shared_ptr<NumericEvalValue> &b) const { shared_ptr<const BooleanEvalValue> NumericEvalValue::operator>(const shared_ptr<const NumericEvalValue> &b) const {
if (this->IsFloat()) { if (this->IsFloat()) {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetFloatValue() > b->GetFloatValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() > b->GetFloatValue());
} else { } else {
return make_shared<BooleanEvalValue>(this->GetFloatValue() > b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() > (double) b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetIntegerValue() > b->GetFloatValue()); return make_shared<BooleanEvalValue>((double) this->GetIntegerValue() > b->GetFloatValue());
} else { } else {
return make_shared<BooleanEvalValue>(this->GetIntegerValue() > b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetIntegerValue() > b->GetIntegerValue());
} }
} }
} }
const shared_ptr<BooleanEvalValue> NumericEvalValue::operator>=(const shared_ptr<NumericEvalValue> &b) const { shared_ptr<const BooleanEvalValue> NumericEvalValue::operator>=(const shared_ptr<const NumericEvalValue> &b) const {
if (this->IsFloat()) { if (this->IsFloat()) {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetFloatValue() >= b->GetFloatValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() >= b->GetFloatValue());
} else { } else {
return make_shared<BooleanEvalValue>(this->GetFloatValue() >= b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetFloatValue() >= (double) b->GetIntegerValue());
} }
} else { } else {
if (b->IsFloat()) { if (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetIntegerValue() >= b->GetFloatValue()); return make_shared<BooleanEvalValue>((double) this->GetIntegerValue() >= b->GetFloatValue());
} else { } else {
return make_shared<BooleanEvalValue>(this->GetIntegerValue() >= b->GetIntegerValue()); return make_shared<BooleanEvalValue>(this->GetIntegerValue() >= b->GetIntegerValue());
} }

View File

@ -9,42 +9,48 @@
namespace Porygon::Evaluation { namespace Porygon::Evaluation {
class NumericEvalValue : public EvalValue { 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: 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; return TypeClass::Number;
} }
const shared_ptr<NumericEvalValue> operator+(const shared_ptr<NumericEvalValue> &b) const; shared_ptr<const NumericEvalValue> operator+(const shared_ptr<const NumericEvalValue> &b) const;
const shared_ptr<NumericEvalValue> operator-(const shared_ptr<NumericEvalValue> &b) const; shared_ptr<const NumericEvalValue> operator-(const shared_ptr<const NumericEvalValue> &b) const;
const shared_ptr<NumericEvalValue> operator*(const shared_ptr<NumericEvalValue> &b) const; shared_ptr<const NumericEvalValue> operator*(const shared_ptr<const NumericEvalValue> &b) const;
const shared_ptr<NumericEvalValue> operator/(const shared_ptr<NumericEvalValue> &b) const; shared_ptr<const NumericEvalValue> operator/(const shared_ptr<const NumericEvalValue> &b) const;
const shared_ptr<BooleanEvalValue> operator<(const shared_ptr<NumericEvalValue> &b) const; shared_ptr<const BooleanEvalValue> operator<(const shared_ptr<const NumericEvalValue> &b) const;
const shared_ptr<BooleanEvalValue> operator<=(const shared_ptr<NumericEvalValue> &b) const; shared_ptr<const BooleanEvalValue> operator<=(const shared_ptr<const NumericEvalValue> &b) const;
const shared_ptr<BooleanEvalValue> operator>(const shared_ptr<NumericEvalValue> &b) const; shared_ptr<const BooleanEvalValue> operator>(const shared_ptr<const NumericEvalValue> &b) const;
const shared_ptr<BooleanEvalValue> operator>=(const shared_ptr<NumericEvalValue> &b) const; shared_ptr<const BooleanEvalValue> operator>=(const shared_ptr<const NumericEvalValue> &b) const;
const bool operator==(EvalValue *b) const final; bool operator==(const EvalValue *b) const final;
}; };
class IntegerEvalValue : public NumericEvalValue { class IntegerEvalValue : public NumericEvalValue {
const long _value; 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."); throw EvaluationException("Attempting to retrieve float from int eval value.");
} }
@ -52,23 +58,28 @@ namespace Porygon::Evaluation {
explicit IntegerEvalValue(long value) : _value(value) { explicit IntegerEvalValue(long value) : _value(value) {
} }
inline const bool IsFloat() const final { [[nodiscard]]
inline bool IsFloat() const final {
return false; return false;
} }
inline const long EvaluateInteger() const final { [[nodiscard]]
inline long EvaluateInteger() const final {
return _value; return _value;
} }
inline const std::u16string EvaluateString() const final{ [[nodiscard]]
inline std::u16string EvaluateString() const final{
return Utilities::StringUtils::IntToString(_value); return Utilities::StringUtils::IntToString(_value);
} }
inline const shared_ptr<EvalValue> Clone() const final { [[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return make_shared<IntegerEvalValue>(_value); return make_shared<IntegerEvalValue>(_value);
} }
inline const std::size_t GetHashCode() const final { [[nodiscard]]
inline std::size_t GetHashCode() const final {
return std::hash<long>{}(_value); return std::hash<long>{}(_value);
} }
}; };
@ -76,11 +87,13 @@ namespace Porygon::Evaluation {
class FloatEvalValue : public NumericEvalValue { class FloatEvalValue : public NumericEvalValue {
const double _value; 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."); throw EvaluationException("Attempting to retrieve float from int eval value.");
} }
inline const double GetFloatValue() const final { [[nodiscard]]
inline double GetFloatValue() const final {
return _value; return _value;
} }
@ -88,19 +101,23 @@ namespace Porygon::Evaluation {
explicit FloatEvalValue(double value) : _value(value) { explicit FloatEvalValue(double value) : _value(value) {
} }
inline const bool IsFloat() const final { [[nodiscard]]
inline bool IsFloat() const final {
return true; return true;
} }
inline const double EvaluateFloat() const final { [[nodiscard]]
inline double EvaluateFloat() const final {
return _value; return _value;
} }
inline const shared_ptr<EvalValue> Clone() const final { [[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return make_shared<FloatEvalValue>(_value); return make_shared<FloatEvalValue>(_value);
} }
inline const std::size_t GetHashCode() const final { [[nodiscard]]
inline std::size_t GetHashCode() const final {
return std::hash<double>{}(_value); return std::hash<double>{}(_value);
} }
}; };

View File

@ -10,55 +10,63 @@ using namespace std;
namespace Porygon::Evaluation { namespace Porygon::Evaluation {
class NumericalTableEvalValue : public EvalValue { class NumericalTableEvalValue : public EvalValue {
const shared_ptr<vector<shared_ptr<EvalValue>>> _table; const shared_ptr<vector<shared_ptr<const EvalValue>>> _table;
const size_t _hash; const size_t _hash;
explicit NumericalTableEvalValue(shared_ptr<vector<shared_ptr<EvalValue>>> table, size_t hash) explicit NumericalTableEvalValue(shared_ptr<vector<shared_ptr<const EvalValue>>> table, size_t hash)
: _table(std::move(table)), : _table(std::move(table)),
_hash(hash) _hash(hash)
{ {
} }
public: public:
explicit NumericalTableEvalValue(shared_ptr<vector<shared_ptr<EvalValue>>> table) : explicit NumericalTableEvalValue(shared_ptr<vector<shared_ptr<const EvalValue>>> table) :
_table(std::move(table)), _table(std::move(table)),
_hash(rand()) _hash(rand())
{ {
} }
inline const TypeClass GetTypeClass() const final { [[nodiscard]]
inline TypeClass GetTypeClass() const final {
return TypeClass::Table; return TypeClass::Table;
} }
inline const size_t GetHashCode() const final { [[nodiscard]]
inline size_t GetHashCode() const final {
return _hash; return _hash;
} }
inline const bool operator==(EvalValue *b) const final { [[nodiscard]]
inline bool operator==(const EvalValue *b) const final {
return this->_hash == b->GetHashCode(); return this->_hash == b->GetHashCode();
} }
inline const shared_ptr<EvalValue> Clone() const final { [[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return shared_ptr<EvalValue>(new NumericalTableEvalValue(_table, _hash)); return shared_ptr<EvalValue>(new NumericalTableEvalValue(_table, _hash));
} }
inline const shared_ptr<EvalValue> IndexValue(EvalValue *val) const final { [[nodiscard]]
inline shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const final {
const auto index = val->EvaluateInteger() - 1; const auto index = val->EvaluateInteger() - 1;
return this->_table->at(index); return this->_table->at(index);
} }
inline const shared_ptr<EvalValue> IndexValue(uint32_t hash) const final { [[nodiscard]]
inline shared_ptr<const EvalValue> IndexValue(uint32_t hash) const final {
return this->_table->at(hash - 1); return this->_table->at(hash - 1);
} }
inline void SetIndexValue(EvalValue *key, const shared_ptr<EvalValue> &value) const final { inline void SetIndexValue(const EvalValue *key, const shared_ptr<const EvalValue> &value) const final {
auto index = key->EvaluateInteger(); auto index = key->EvaluateInteger();
this->_table->at(index - 1) = value; this->_table->at(index - 1) = value;
} }
[[nodiscard]]
Iterator * GetKeyIterator() const final; Iterator * GetKeyIterator() const final;
inline const shared_ptr<vector<shared_ptr<EvalValue>>> GetTable() const{ [[nodiscard]]
inline shared_ptr<vector<shared_ptr<const EvalValue>>> GetTable() const{
return _table; return _table;
}; };
}; };

View File

@ -20,16 +20,16 @@ namespace Porygon::Evaluation {
}; };
class EvaluationScriptFunctionOption : public GenericFunctionOption{ class EvaluationScriptFunctionOption : public GenericFunctionOption{
const std::shared_ptr<BoundBlockStatement> _innerBlock; const std::shared_ptr<const BoundBlockStatement> _innerBlock;
const std::shared_ptr<EvaluationScope> _scope; const std::shared_ptr<EvaluationScope> _scope;
public: public:
EvaluationScriptFunctionOption(shared_ptr<BoundBlockStatement> innerBlock, shared_ptr<EvaluationScope> scope) EvaluationScriptFunctionOption(shared_ptr<const BoundBlockStatement> innerBlock, shared_ptr<EvaluationScope> scope)
: _innerBlock(std::move(innerBlock)), _scope(std::move(scope)) { : _innerBlock(std::move(innerBlock)), _scope(std::move(scope)) {
} }
~EvaluationScriptFunctionOption() final = default; ~EvaluationScriptFunctionOption() final = default;
inline const std::shared_ptr<BoundBlockStatement> &GetInnerBlock() const { inline std::shared_ptr<const BoundBlockStatement> GetInnerBlock() const {
return _innerBlock; return _innerBlock;
} }
@ -40,47 +40,53 @@ namespace Porygon::Evaluation {
class GenericFunctionEvalValue : public EvalValue{ class GenericFunctionEvalValue : public EvalValue{
protected: protected:
const shared_ptr<GenericFunctionScriptType> _type; const shared_ptr<const GenericFunctionScriptType> _type;
const size_t _hash; const size_t _hash;
vector<shared_ptr<GenericFunctionOption>> _options; vector<shared_ptr<GenericFunctionOption>>* _options;
public: public:
GenericFunctionEvalValue(shared_ptr<GenericFunctionScriptType> type, size_t hash) GenericFunctionEvalValue(shared_ptr<const GenericFunctionScriptType> type, size_t hash)
: _type(move(type)), : _type(move(type)),
_hash(hash){ _hash(hash), _options(new vector<shared_ptr<GenericFunctionOption>>()){
} }
const shared_ptr<EvalValue> Clone() const final { GenericFunctionEvalValue(const GenericFunctionEvalValue& _) = delete;
GenericFunctionEvalValue() = delete;
GenericFunctionEvalValue& operator =(GenericFunctionEvalValue v) = delete;
[[nodiscard]]
shared_ptr<const EvalValue> Clone() const final {
auto t = make_shared<GenericFunctionEvalValue>(_type, _hash); auto t = make_shared<GenericFunctionEvalValue>(_type, _hash);
for (const auto& o: _options){ for (const auto& o: *_options){
t->_options.push_back(o); t->_options->push_back(o);
} }
return t; return t;
} }
inline void RegisterOption(GenericFunctionOption* option){ inline void RegisterOption(GenericFunctionOption* option) const{
_options.push_back(shared_ptr<GenericFunctionOption>(option)); _options->push_back(shared_ptr<GenericFunctionOption>(option));
} }
inline const std::shared_ptr<ScriptType> GetType() const { inline std::shared_ptr<const ScriptType> GetType() const {
return _type; return _type;
} }
inline const TypeClass GetTypeClass() const final { inline TypeClass GetTypeClass() const final {
return TypeClass::Function; return TypeClass::Function;
} }
const bool operator==(EvalValue *b) const final { bool operator==(const EvalValue *b) const final {
if (b->GetTypeClass() != TypeClass::Function) if (b->GetTypeClass() != TypeClass::Function)
return false; return false;
return this->_hash == ((GenericFunctionEvalValue *) b)->_hash; return this->_hash == ((GenericFunctionEvalValue *) b)->_hash;
}; };
inline const std::size_t GetHashCode() const final { inline std::size_t GetHashCode() const final {
return _hash; return _hash;
} }
inline const shared_ptr<GenericFunctionOption> GetOption(const size_t id) const{ [[nodiscard]]
return this->_options.at(id); inline shared_ptr<const GenericFunctionOption> GetOption(const size_t id) const{
return this->_options->at(id);
} }
}; };
} }

View File

@ -17,31 +17,35 @@ namespace Porygon::Evaluation {
_hash = Utilities::HashedString::ConstHash(_value.c_str()); _hash = Utilities::HashedString::ConstHash(_value.c_str());
} }
inline const TypeClass GetTypeClass() const final { [[nodiscard]]
inline TypeClass GetTypeClass() const final {
return TypeClass::String; return TypeClass::String;
} }
const bool operator==(EvalValue *b) const final { bool operator==(const EvalValue *b) const final {
if (b->GetTypeClass() != TypeClass::String) if (b->GetTypeClass() != TypeClass::String)
return false; return false;
return this->_hash == b->GetHashCode(); return this->_hash == b->GetHashCode();
}; };
inline const u16string EvaluateString() const final { [[nodiscard]]
inline u16string EvaluateString() const final {
return _value; return _value;
} }
inline const shared_ptr<EvalValue> Clone() const final { [[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return make_shared<StringEvalValue>(_value); return make_shared<StringEvalValue>(_value);
} }
const shared_ptr<EvalValue> IndexValue(EvalValue *val) const final { shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const final {
// Porygon is 1-indexed, so we convert to that. // Porygon is 1-indexed, so we convert to that.
auto l = val->EvaluateInteger() - 1; auto l = val->EvaluateInteger() - 1;
return make_shared<StringEvalValue>(u16string(1, _value[l])); return make_shared<const StringEvalValue>(u16string(1, _value[l]));
} }
inline const std::size_t GetHashCode() const final { [[nodiscard]]
inline std::size_t GetHashCode() const final {
return _hash; return _hash;
} }
}; };

View File

@ -4,68 +4,79 @@
#include <utility> #include <utility>
#include <map> #include <map>
#include "EvalValue.hpp" #include "EvalValue.hpp"
#include "../../Utilities/Random.hpp"
using namespace std; using namespace std;
namespace Porygon::Evaluation { namespace Porygon::Evaluation {
class TableEvalValue : public EvalValue { class TableEvalValue : public EvalValue {
const shared_ptr<map<Utilities::HashedString, shared_ptr<EvalValue>>> _table; const shared_ptr<map<Utilities::HashedString, shared_ptr<const EvalValue>>> _table;
const size_t _hash; const size_t _hash;
explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, shared_ptr<EvalValue>>> table, size_t hash) explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, shared_ptr<const EvalValue>>> table, size_t hash)
: _table(std::move(table)), : _table(std::move(table)),
_hash(hash) _hash(hash)
{ {
} }
public: public:
explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, shared_ptr<EvalValue>>> table) : explicit TableEvalValue(shared_ptr<map<Utilities::HashedString, shared_ptr<const EvalValue>>> table) :
_table(std::move(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; return TypeClass::Table;
} }
inline const size_t GetHashCode() const final { [[nodiscard]]
inline size_t GetHashCode() const final {
return _hash; return _hash;
} }
inline const bool operator==(EvalValue *b) const final { [[nodiscard]]
inline bool operator==(const EvalValue *b) const final {
return this->_hash == b->GetHashCode(); return this->_hash == b->GetHashCode();
} }
inline const shared_ptr<EvalValue> Clone() const final { [[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return shared_ptr<EvalValue>(new TableEvalValue(_table, _hash)); return shared_ptr<EvalValue>(new TableEvalValue(_table, _hash));
} }
inline const shared_ptr<EvalValue> IndexValue(EvalValue *val) const final { [[nodiscard]]
inline shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const final {
const auto stringKey = val->EvaluateString(); const auto stringKey = val->EvaluateString();
return this->_table->at(Utilities::HashedString::CreateLookup(stringKey)); return this->_table->at(Utilities::HashedString::CreateLookup(stringKey));
} }
inline const shared_ptr<EvalValue> IndexValue(uint32_t hash) const final { [[nodiscard]]
inline shared_ptr<const EvalValue> IndexValue(uint32_t hash) const final {
return this->_table->at(Utilities::HashedString::CreateLookup(hash)); return this->_table->at(Utilities::HashedString::CreateLookup(hash));
} }
inline const shared_ptr<EvalValue> IndexValue(const char *val) const { [[nodiscard]]
inline shared_ptr<const EvalValue> IndexValue(const char *val) const {
auto hash = Utilities::HashedString::ConstHash(val); auto hash = Utilities::HashedString::ConstHash(val);
return this->_table->at(Utilities::HashedString::CreateLookup(hash)); return this->_table->at(Utilities::HashedString::CreateLookup(hash));
} }
inline void SetIndexValue(EvalValue *key, const shared_ptr<EvalValue> &value) const final { inline void SetIndexValue(const EvalValue *key, const shared_ptr<const EvalValue> &value) const final {
auto hash = key->GetHashCode(); auto hash = key->GetHashCode();
this->_table->at(Utilities::HashedString::CreateLookup(hash)) = value; this->_table->at(Utilities::HashedString::CreateLookup(hash)) = value;
} }
[[nodiscard]]
Iterator * GetKeyIterator() const final; Iterator * GetKeyIterator() const final;
inline const _Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<EvalValue>>> GetTableIterator() const{ [[nodiscard]]
inline _Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<const EvalValue>>> GetTableIterator() const{
return _table->cbegin(); return _table->cbegin();
}; };
inline const _Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<EvalValue>>> GetTableIteratorEnd() const{ [[nodiscard]]
inline _Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<const EvalValue>>> GetTableIteratorEnd() const{
return _table->cend(); return _table->cend();
}; };
}; };

View File

@ -4,14 +4,14 @@
#include <memory> #include <memory>
namespace Porygon::Evaluation { namespace Porygon::Evaluation {
EvaluationScope::EvaluationScope(map<Utilities::HashedString, shared_ptr<EvalValue>> *scriptVariables, EvaluationScope::EvaluationScope(map<Utilities::HashedString, shared_ptr<const EvalValue>> *scriptVariables)
int localVariableCount) : _scriptScope(scriptVariables), : _scriptScope(scriptVariables),
_localScope(map<uint64_t, shared_ptr<EvalValue>>()) { _localScope(map<uint64_t, shared_ptr<const EvalValue>>()) {
} }
void EvaluationScope::CreateVariable(const BoundVariableKey *key, const shared_ptr<EvalValue> &value) { void EvaluationScope::CreateVariable(const BoundVariableKey *key, const shared_ptr<const EvalValue>& value) {
if (key->GetScopeId() == 0) { if (key->GetScopeId() == 0) {
_scriptScope->at(key->GetIdentifier()) = value; _scriptScope->at(*key->GetIdentifier()) = value;
} else { } else {
auto insert = _localScope.insert({key->GetHash(), value}); auto insert = _localScope.insert({key->GetHash(), value});
if (!insert.second) { if (!insert.second) {
@ -20,20 +20,20 @@ namespace Porygon::Evaluation {
} }
} }
void EvaluationScope::SetVariable(const BoundVariableKey *key, const shared_ptr<EvalValue> &value) { void EvaluationScope::SetVariable(const BoundVariableKey *key, const shared_ptr<const EvalValue> &value) {
if (key->GetScopeId() == 0) { if (key->GetScopeId() == 0) {
_scriptScope->at(key->GetIdentifier()) = value; _scriptScope->at(*key->GetIdentifier()) = value;
} else { } else {
_localScope[key->GetHash()] = value; _localScope[key->GetHash()] = value;
} }
} }
shared_ptr<EvalValue> EvaluationScope::GetVariable(const BoundVariableKey *key) { shared_ptr<const EvalValue> EvaluationScope::GetVariable(const BoundVariableKey *key) {
auto scopeId = key -> GetScopeId(); auto scopeId = key -> GetScopeId();
if (scopeId== 0) { if (scopeId== 0) {
return _scriptScope->at(key->GetIdentifier()); return _scriptScope->at(*key->GetIdentifier());
} else if(scopeId == -2){ } else if(scopeId == -2){
return StandardLibraries::StaticScope::GetVariable(key->GetIdentifier()); return StandardLibraries::StaticScope::GetVariable(*key->GetIdentifier());
} else { } else {
return _localScope[key->GetHash()]; return _localScope[key->GetHash()];
} }

View File

@ -9,18 +9,18 @@ using namespace Porygon::Binder;
namespace Porygon::Evaluation { namespace Porygon::Evaluation {
class EvaluationScope { class EvaluationScope {
map<Utilities::HashedString, shared_ptr<EvalValue>> *_scriptScope; map<Utilities::HashedString, shared_ptr<const EvalValue>> *_scriptScope;
map<uint64_t, shared_ptr<EvalValue>> _localScope; map<uint64_t, shared_ptr<const EvalValue>> _localScope;
public: public:
explicit EvaluationScope(map<Utilities::HashedString, shared_ptr<EvalValue>> *scriptVariables, int deepestScope); explicit EvaluationScope(map<Utilities::HashedString, shared_ptr<const EvalValue>> *scriptVariables);
~EvaluationScope() = default; ~EvaluationScope() = default;
void CreateVariable(const BoundVariableKey *key, const shared_ptr<EvalValue> &value); void CreateVariable(const BoundVariableKey *key, const shared_ptr<const EvalValue>&value);
void SetVariable(const BoundVariableKey *key, const shared_ptr<EvalValue> &value); void SetVariable(const BoundVariableKey *key, const shared_ptr<const EvalValue> &value);
shared_ptr<EvalValue> GetVariable(const BoundVariableKey *key); shared_ptr<const EvalValue> GetVariable(const BoundVariableKey *key);
}; };
} }

View File

@ -1,9 +1,7 @@
#include <utility>
#include <memory> #include <memory>
#include "Evaluator.hpp" #include "Evaluator.hpp"
#include "EvaluationException.hpp" #include "EvaluationException.hpp"
#include "../Script.hpp"
#include "EvaluationScope/EvaluationScope.hpp" #include "EvaluationScope/EvaluationScope.hpp"
#include "EvalValues/ScriptFunctionEvalValue.hpp" #include "EvalValues/ScriptFunctionEvalValue.hpp"
#include "EvalValues/TableEvalValue.hpp" #include "EvalValues/TableEvalValue.hpp"
@ -11,17 +9,15 @@
#include "../Binder/BoundExpressions/BoundFunctionCallExpression.hpp" #include "../Binder/BoundExpressions/BoundFunctionCallExpression.hpp"
#include "../TableScriptType.hpp" #include "../TableScriptType.hpp"
#include "../UserData/UserDataFunction.hpp" #include "../UserData/UserDataFunction.hpp"
#include "../Utilities/StringUtils.hpp"
#include "EvalValues/NumericalTableEvalValue.hpp" #include "EvalValues/NumericalTableEvalValue.hpp"
#include "../FunctionScriptType.hpp" #include "../Utilities/Random.hpp"
using namespace std; using namespace std;
using namespace Porygon::Binder; using namespace Porygon::Binder;
namespace Porygon::Evaluation { namespace Porygon::Evaluation {
EvalValue *Evaluator::Evaluate(const BoundScriptStatement *statement) { const EvalValue *Evaluator::Evaluate(const BoundScriptStatement *statement) {
this->_evaluationScope = make_shared<EvaluationScope>(this->_scriptVariables, this->_evaluationScope = make_shared<EvaluationScope>(this->_scriptVariables);
statement->GetLocalVariableCount());
EvaluateBlockStatement(statement); EvaluateBlockStatement(statement);
return this->_returnValue.get(); return this->_returnValue.get();
} }
@ -101,11 +97,11 @@ namespace Porygon::Evaluation {
auto option = new Evaluation::EvaluationScriptFunctionOption(block, this->_evaluationScope); auto option = new Evaluation::EvaluationScriptFunctionOption(block, this->_evaluationScope);
if (key->IsCreation()) { if (key->IsCreation()) {
auto value = make_shared<GenericFunctionEvalValue>(type, rand()); auto value = make_shared<GenericFunctionEvalValue>(type, Utilities::Random::Get());
value->RegisterOption(option); value->RegisterOption(option);
this->_evaluationScope->CreateVariable(key, value); this->_evaluationScope->CreateVariable(key, value);
} else { } else {
auto var = dynamic_pointer_cast<GenericFunctionEvalValue>(this -> _evaluationScope ->GetVariable(key)); auto var = dynamic_pointer_cast<const GenericFunctionEvalValue>(this -> _evaluationScope ->GetVariable(key));
var->RegisterOption(option); var->RegisterOption(option);
this->_evaluationScope->SetVariable(key, var); this->_evaluationScope->SetVariable(key, var);
} }
@ -223,7 +219,7 @@ namespace Porygon::Evaluation {
// Expressions // // Expressions //
///////////////// /////////////////
const shared_ptr<EvalValue> Evaluator::EvaluateExpression(const BoundExpression *expression) { shared_ptr<const EvalValue> Evaluator::EvaluateExpression(const BoundExpression *expression) {
auto type = expression->GetType(); auto type = expression->GetType();
switch (type->GetClass()) { switch (type->GetClass()) {
case TypeClass::Number: case TypeClass::Number:
@ -245,7 +241,7 @@ namespace Porygon::Evaluation {
} }
} }
const shared_ptr<EvalValue> Evaluator::GetVariable(const BoundVariableExpression *expression) { shared_ptr<const EvalValue> Evaluator::GetVariable(const BoundVariableExpression *expression) {
auto variable = this->_evaluationScope->GetVariable(expression->GetKey()); auto variable = this->_evaluationScope->GetVariable(expression->GetKey());
if (variable == nullptr) { if (variable == nullptr) {
throw EvaluationException("Variable not found"); throw EvaluationException("Variable not found");
@ -253,7 +249,7 @@ namespace Porygon::Evaluation {
return variable->Clone(); return variable->Clone();
} }
const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) { shared_ptr<const NumericEvalValue> Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) {
switch (expression->GetKind()) { switch (expression->GetKind()) {
case BoundExpressionKind::LiteralInteger: case BoundExpressionKind::LiteralInteger:
return make_shared<IntegerEvalValue>(((BoundLiteralIntegerExpression *) expression)->GetValue()); return make_shared<IntegerEvalValue>(((BoundLiteralIntegerExpression *) expression)->GetValue());
@ -264,21 +260,21 @@ namespace Porygon::Evaluation {
case BoundExpressionKind::Binary: case BoundExpressionKind::Binary:
return this->EvaluateIntegerBinary((BoundBinaryExpression *) expression); return this->EvaluateIntegerBinary((BoundBinaryExpression *) expression);
case BoundExpressionKind::Variable: case BoundExpressionKind::Variable:
return dynamic_pointer_cast<NumericEvalValue>( return dynamic_pointer_cast<const NumericEvalValue>(
this->GetVariable((BoundVariableExpression *) expression)); this->GetVariable((BoundVariableExpression *) expression));
case BoundExpressionKind::FunctionCall: case BoundExpressionKind::FunctionCall:
return dynamic_pointer_cast<NumericEvalValue>(this->EvaluateFunctionCallExpression(expression)); return dynamic_pointer_cast<const NumericEvalValue>(this->EvaluateFunctionCallExpression(expression));
case BoundExpressionKind::Index: case BoundExpressionKind::Index:
return dynamic_pointer_cast<NumericEvalValue>(this->EvaluateIndexExpression(expression)); return dynamic_pointer_cast<const NumericEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind::PeriodIndex: case BoundExpressionKind::PeriodIndex:
return dynamic_pointer_cast<NumericEvalValue>(this->EvaluatePeriodIndexExpression(expression)); return dynamic_pointer_cast<const NumericEvalValue>(this->EvaluatePeriodIndexExpression(expression));
default: default:
throw; throw;
} }
} }
const shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(const BoundExpression *expression) { shared_ptr<const BooleanEvalValue> Evaluator::EvaluateBoolExpression(const BoundExpression *expression) {
switch (expression->GetKind()) { switch (expression->GetKind()) {
case BoundExpressionKind::LiteralBool: case BoundExpressionKind::LiteralBool:
return make_shared<BooleanEvalValue>(((BoundLiteralBoolExpression *) expression)->GetValue()); return make_shared<BooleanEvalValue>(((BoundLiteralBoolExpression *) expression)->GetValue());
@ -287,14 +283,14 @@ namespace Porygon::Evaluation {
case BoundExpressionKind::Binary: case BoundExpressionKind::Binary:
return this->EvaluateBooleanBinary((BoundBinaryExpression *) expression); return this->EvaluateBooleanBinary((BoundBinaryExpression *) expression);
case BoundExpressionKind::Variable: case BoundExpressionKind::Variable:
return dynamic_pointer_cast<BooleanEvalValue>( return dynamic_pointer_cast<const BooleanEvalValue>(
this->GetVariable((BoundVariableExpression *) expression)); this->GetVariable((BoundVariableExpression *) expression));
case BoundExpressionKind::FunctionCall: case BoundExpressionKind::FunctionCall:
return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluateFunctionCallExpression(expression)); return dynamic_pointer_cast<const BooleanEvalValue>(this->EvaluateFunctionCallExpression(expression));
case BoundExpressionKind::Index: case BoundExpressionKind::Index:
return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluateIndexExpression(expression)); return dynamic_pointer_cast<const BooleanEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind::PeriodIndex: case BoundExpressionKind::PeriodIndex:
return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluatePeriodIndexExpression(expression)); return dynamic_pointer_cast<const BooleanEvalValue>(this->EvaluatePeriodIndexExpression(expression));
default: default:
throw; throw;
@ -302,20 +298,20 @@ namespace Porygon::Evaluation {
} }
} }
const shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(const BoundExpression *expression) { shared_ptr<const StringEvalValue> Evaluator::EvaluateStringExpression(const BoundExpression *expression) {
switch (expression->GetKind()) { switch (expression->GetKind()) {
case BoundExpressionKind::LiteralString: case BoundExpressionKind::LiteralString:
return make_shared<StringEvalValue>(((BoundLiteralStringExpression *) expression)->GetValue()); return make_shared<StringEvalValue>(*((BoundLiteralStringExpression *) expression)->GetValue());
case BoundExpressionKind::Binary: case BoundExpressionKind::Binary:
return this->EvaluateStringBinary((BoundBinaryExpression *) expression); return this->EvaluateStringBinary((BoundBinaryExpression *) expression);
case BoundExpressionKind::Variable: case BoundExpressionKind::Variable:
return dynamic_pointer_cast<StringEvalValue>(this->GetVariable((BoundVariableExpression *) expression)); return dynamic_pointer_cast<const StringEvalValue>(this->GetVariable((BoundVariableExpression *) expression));
case BoundExpressionKind::FunctionCall: case BoundExpressionKind::FunctionCall:
return dynamic_pointer_cast<StringEvalValue>(this->EvaluateFunctionCallExpression(expression)); return dynamic_pointer_cast<const StringEvalValue>(this->EvaluateFunctionCallExpression(expression));
case BoundExpressionKind::Index: case BoundExpressionKind::Index:
return dynamic_pointer_cast<StringEvalValue>(this->EvaluateIndexExpression(expression)); return dynamic_pointer_cast<const StringEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind::PeriodIndex: case BoundExpressionKind::PeriodIndex:
return dynamic_pointer_cast<StringEvalValue>(this->EvaluatePeriodIndexExpression(expression)); return dynamic_pointer_cast<const StringEvalValue>(this->EvaluatePeriodIndexExpression(expression));
default: default:
throw; throw;
@ -323,7 +319,7 @@ namespace Porygon::Evaluation {
} }
} }
const shared_ptr<EvalValue> Evaluator::EvaluateFunctionExpression(const BoundExpression *expression) { shared_ptr<const EvalValue> Evaluator::EvaluateFunctionExpression(const BoundExpression *expression) {
switch (expression->GetKind()) { switch (expression->GetKind()) {
case BoundExpressionKind::Variable: case BoundExpressionKind::Variable:
return this->GetVariable((BoundVariableExpression *) expression); return this->GetVariable((BoundVariableExpression *) expression);
@ -336,7 +332,7 @@ namespace Porygon::Evaluation {
} }
} }
const shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression *expression) { shared_ptr<const EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression *expression) {
switch (expression->GetKind()) { switch (expression->GetKind()) {
case BoundExpressionKind::FunctionCall: case BoundExpressionKind::FunctionCall:
return this->EvaluateFunctionCallExpression(expression); return this->EvaluateFunctionCallExpression(expression);
@ -345,7 +341,7 @@ namespace Porygon::Evaluation {
} }
} }
const shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(const BoundExpression *expression) { shared_ptr<const EvalValue> Evaluator::EvaluateTableExpression(const BoundExpression *expression) {
switch (expression->GetKind()) { switch (expression->GetKind()) {
case BoundExpressionKind::FunctionCall: case BoundExpressionKind::FunctionCall:
return this->EvaluateFunctionCallExpression(expression); return this->EvaluateFunctionCallExpression(expression);
@ -365,19 +361,19 @@ namespace Porygon::Evaluation {
} }
const shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(const BoundExpression *expression) { shared_ptr<const EvalValue> Evaluator::EvaluateFunctionCallExpression(const BoundExpression *expression) {
auto functionCall = (BoundFunctionCallExpression *) expression; auto functionCall = (BoundFunctionCallExpression *) expression;
auto function = dynamic_pointer_cast<GenericFunctionEvalValue>( auto function = dynamic_pointer_cast<const GenericFunctionEvalValue>(
this->EvaluateExpression(functionCall->GetFunctionExpression())); this->EvaluateExpression(functionCall->GetFunctionExpression()));
auto boundParameters = functionCall->GetParameters(); auto boundParameters = functionCall->GetParameters();
auto parameters = vector<shared_ptr<EvalValue>>(boundParameters->size()); auto parameters = vector<shared_ptr<const EvalValue>>(boundParameters->size());
for (int i = 0; i < boundParameters->size(); i++) { for (size_t i = 0; i < boundParameters->size(); i++) {
parameters[i] = this->EvaluateExpression(boundParameters->at(i)); parameters[i] = this->EvaluateExpression(boundParameters->at(i));
} }
auto type = std::dynamic_pointer_cast<GenericFunctionScriptType>(function->GetType()); auto type = std::dynamic_pointer_cast<const GenericFunctionScriptType>(function->GetType());
auto func = dynamic_pointer_cast<GenericFunctionEvalValue>(function); auto func = dynamic_pointer_cast<const GenericFunctionEvalValue>(function);
auto option = functionCall ->GetFunctionOption(); auto option = functionCall ->GetFunctionOption();
auto opt = func->GetOption(option->GetOptionId()); auto opt = func->GetOption(option->GetOptionId());
if (option -> IsScriptFunction()){ if (option -> IsScriptFunction()){
@ -385,7 +381,7 @@ namespace Porygon::Evaluation {
auto scriptFunctionType = dynamic_cast<const ScriptFunctionOption*>(option); auto scriptFunctionType = dynamic_cast<const ScriptFunctionOption*>(option);
auto parameterKeys = scriptFunctionType->GetParameterKeys(); auto parameterKeys = scriptFunctionType->GetParameterKeys();
auto originalScope = this->_evaluationScope; auto originalScope = this->_evaluationScope;
auto scriptOption = dynamic_pointer_cast<EvaluationScriptFunctionOption>(opt); auto scriptOption = dynamic_pointer_cast<const EvaluationScriptFunctionOption>(opt);
this->_evaluationScope = scriptOption->GetScope(); this->_evaluationScope = scriptOption->GetScope();
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++) { for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++) {
@ -401,25 +397,25 @@ namespace Porygon::Evaluation {
this->_returnValue = nullptr; this->_returnValue = nullptr;
return r; return r;
} else{ } else{
auto scriptOption = dynamic_pointer_cast<UserData::UserDataFunction>(opt); auto scriptOption = dynamic_pointer_cast<const UserData::UserDataFunction>(opt);
EvalValue* arr[parameters.size()]; const EvalValue* arr[parameters.size()];
for (int i = 0; i < parameters.size(); i++){ for (size_t i = 0; i < parameters.size(); i++){
arr[i] = parameters[i].get(); arr[i] = parameters[i].get();
} }
return shared_ptr<EvalValue>(scriptOption -> Call(arr, parameters.size())); return shared_ptr<const EvalValue>(scriptOption -> Call(arr, parameters.size()));
} }
} }
const shared_ptr<EvalValue> Evaluator::EvaluateFunction(const GenericFunctionEvalValue *function, shared_ptr<const EvalValue> Evaluator::EvaluateFunction(const GenericFunctionEvalValue *function,
const vector<EvalValue *> &parameters) { const vector<EvalValue *> &parameters) {
auto type = std::dynamic_pointer_cast<GenericFunctionScriptType>(function->GetType()); auto type = std::dynamic_pointer_cast<const GenericFunctionScriptType>(function->GetType());
auto option = dynamic_cast<const ScriptFunctionOption*>(type->GetFirstOption()); auto option = dynamic_cast<const ScriptFunctionOption*>(type->GetFirstOption());
auto parameterTypes = option->GetParameterTypes(); auto parameterTypes = option->GetParameterTypes();
auto parameterKeys = option->GetParameterKeys(); auto parameterKeys = option->GetParameterKeys();
auto originalScope = this->_evaluationScope; auto originalScope = this->_evaluationScope;
auto scriptOption = dynamic_pointer_cast<EvaluationScriptFunctionOption>(function->GetOption(0)); auto scriptOption = dynamic_pointer_cast<const EvaluationScriptFunctionOption>(function->GetOption(0));
this->_evaluationScope = scriptOption->GetScope(); this->_evaluationScope = scriptOption->GetScope();
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++) { for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++) {
@ -435,41 +431,41 @@ namespace Porygon::Evaluation {
return r; return r;
} }
const shared_ptr<EvalValue> Evaluator::EvaluateIndexExpression(const BoundExpression *expression) { shared_ptr<const EvalValue> Evaluator::EvaluateIndexExpression(const BoundExpression *expression) {
auto indexExpression = (BoundIndexExpression *) expression; auto indexExpression = (BoundIndexExpression *) expression;
auto index = this->EvaluateExpression(indexExpression->GetIndexExpression()); auto index = this->EvaluateExpression(indexExpression->GetIndexExpression());
auto indexable = this->EvaluateExpression(indexExpression->GetIndexableExpression()); auto indexable = this->EvaluateExpression(indexExpression->GetIndexableExpression());
return indexable->IndexValue(index.get())->Clone(); return indexable->IndexValue(index.get());
} }
const shared_ptr<EvalValue> Evaluator::EvaluatePeriodIndexExpression(const BoundExpression *expression) { shared_ptr<const EvalValue> Evaluator::EvaluatePeriodIndexExpression(const BoundExpression *expression) {
auto indexExpression = (BoundPeriodIndexExpression *) expression; auto indexExpression = (BoundPeriodIndexExpression *) expression;
auto index = indexExpression->GetIndex().GetHash(); auto index = indexExpression->GetIndex()->GetHash();
auto indexable = this->EvaluateExpression(indexExpression->GetIndexableExpression()); auto indexable = this->EvaluateExpression(indexExpression->GetIndexableExpression());
return indexable->IndexValue(index)->Clone(); return indexable->IndexValue(index)->Clone();
} }
const shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) { shared_ptr<const EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
auto tableExpression = (BoundNumericalTableExpression *) expression; auto tableExpression = (BoundNumericalTableExpression *) expression;
auto valueExpressions = tableExpression->GetExpressions(); auto valueExpressions = tableExpression->GetExpressions();
auto values = new vector<shared_ptr<EvalValue>>(valueExpressions->size()); auto values = new vector<shared_ptr<const EvalValue>>(valueExpressions->size());
for (int i = 0; i < valueExpressions->size(); i++) { for (size_t i = 0; i < valueExpressions->size(); i++) {
auto val = this->EvaluateExpression(valueExpressions->at(i)); auto val = this->EvaluateExpression(valueExpressions->at(i));
values->at(i) = val; values->at(i) = val;
} }
auto valuesPointer = shared_ptr<vector<shared_ptr<EvalValue>>>(values); auto valuesPointer = shared_ptr<vector<shared_ptr<const EvalValue>>>(values);
return make_shared<NumericalTableEvalValue>(valuesPointer); return make_shared<NumericalTableEvalValue>(valuesPointer);
} }
const shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) { shared_ptr<const EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) {
auto tableExpression = (BoundTableExpression *) expression; auto tableExpression = (BoundTableExpression *) expression;
auto type = dynamic_pointer_cast<TableScriptType>(tableExpression->GetType()); auto type = dynamic_pointer_cast<const TableScriptType>(tableExpression->GetType());
auto declaredVars = type->GetValues(); auto declaredVars = type->GetValues();
auto variables = make_shared<map<Utilities::HashedString, shared_ptr<EvalValue>>>(); auto variables = make_shared<map<Utilities::HashedString, shared_ptr<const EvalValue>>>();
for (const auto& i : *declaredVars) { for (const auto& i : *declaredVars) {
variables->insert({i.first, nullptr}); variables->insert({i.first, nullptr});
} }
auto evaluator = make_shared<EvaluationScope>(variables.get(), type->GetLocalVariableCount()); auto evaluator = make_shared<EvaluationScope>(variables.get());
auto currentEvaluator = this->_evaluationScope; auto currentEvaluator = this->_evaluationScope;
this->_evaluationScope = evaluator; this->_evaluationScope = evaluator;
this->EvaluateBlockStatement(tableExpression->GetBlock()); this->EvaluateBlockStatement(tableExpression->GetBlock());
@ -477,7 +473,7 @@ namespace Porygon::Evaluation {
return make_shared<TableEvalValue>(variables); return make_shared<TableEvalValue>(variables);
} }
const shared_ptr<EvalValue> Evaluator::EvaluateUserDataExpression(const BoundExpression *expression) { shared_ptr<const EvalValue> Evaluator::EvaluateUserDataExpression(const BoundExpression *expression) {
switch (expression->GetKind()) { switch (expression->GetKind()) {
case BoundExpressionKind::Variable: case BoundExpressionKind::Variable:
return this->GetVariable((BoundVariableExpression *) expression); return this->GetVariable((BoundVariableExpression *) expression);

View File

@ -15,11 +15,11 @@ using namespace std;
namespace Porygon::Evaluation{ namespace Porygon::Evaluation{
class Evaluator { class Evaluator {
shared_ptr<EvalValue> _returnValue; shared_ptr<const EvalValue> _returnValue;
map<Utilities::HashedString, shared_ptr<EvalValue>>* _scriptVariables; map<Utilities::HashedString, shared_ptr<const EvalValue>>* _scriptVariables;
bool _hasReturned; bool _hasReturned;
bool _hasBroken; bool _hasBroken;
shared_ptr<EvalValue> _lastValue; shared_ptr<const EvalValue> _lastValue;
//Porygon::Script* _scriptData; //Porygon::Script* _scriptData;
shared_ptr<EvaluationScope> _evaluationScope; shared_ptr<EvaluationScope> _evaluationScope;
@ -36,39 +36,39 @@ namespace Porygon::Evaluation{
void EvaluateGenericForStatement(const BoundGenericForStatement *statement); void EvaluateGenericForStatement(const BoundGenericForStatement *statement);
void EvaluateWhileStatement(const BoundWhileStatement *statement); void EvaluateWhileStatement(const BoundWhileStatement *statement);
const shared_ptr<EvalValue> EvaluateExpression(const BoundExpression *expression); shared_ptr<const EvalValue> EvaluateExpression(const BoundExpression *expression);
const shared_ptr<NumericEvalValue> EvaluateIntegerExpression(const BoundExpression *expression); shared_ptr<const NumericEvalValue> EvaluateIntegerExpression(const BoundExpression *expression);
const shared_ptr<BooleanEvalValue> EvaluateBoolExpression(const BoundExpression *expression); shared_ptr<const BooleanEvalValue> EvaluateBoolExpression(const BoundExpression *expression);
const shared_ptr<StringEvalValue> EvaluateStringExpression(const BoundExpression *expression); shared_ptr<const StringEvalValue> EvaluateStringExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluateFunctionExpression(const BoundExpression *expression); shared_ptr<const EvalValue> EvaluateFunctionExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluateNilExpression(const BoundExpression *expression); shared_ptr<const EvalValue> EvaluateNilExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluateTableExpression(const BoundExpression *expression); shared_ptr<const EvalValue> EvaluateTableExpression(const BoundExpression *expression);
const shared_ptr<NumericEvalValue> EvaluateIntegerBinary(const BoundBinaryExpression *expression); shared_ptr<const NumericEvalValue> EvaluateIntegerBinary(const BoundBinaryExpression *expression);
const shared_ptr<BooleanEvalValue> EvaluateBooleanBinary(const BoundBinaryExpression *expression); shared_ptr<const BooleanEvalValue> EvaluateBooleanBinary(const BoundBinaryExpression *expression);
const shared_ptr<StringEvalValue> EvaluateStringBinary(const BoundBinaryExpression *expression); shared_ptr<const StringEvalValue> EvaluateStringBinary(const BoundBinaryExpression *expression);
const shared_ptr<NumericEvalValue> EvaluateIntegerUnary(const BoundUnaryExpression *expression); shared_ptr<const NumericEvalValue> EvaluateIntegerUnary(const BoundUnaryExpression *expression);
const shared_ptr<BooleanEvalValue> EvaluateBooleanUnary(const BoundUnaryExpression *expression); shared_ptr<const BooleanEvalValue> EvaluateBooleanUnary(const BoundUnaryExpression *expression);
const shared_ptr<EvalValue> EvaluateFunctionCallExpression(const BoundExpression *expression); shared_ptr<const EvalValue> EvaluateFunctionCallExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluateIndexExpression(const BoundExpression *expression); shared_ptr<const EvalValue> EvaluateIndexExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluatePeriodIndexExpression(const BoundExpression *expression); shared_ptr<const EvalValue> EvaluatePeriodIndexExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluateNumericTableExpression(const BoundExpression *expression); shared_ptr<const EvalValue> EvaluateNumericTableExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluateComplexTableExpression(const BoundExpression *expression); shared_ptr<const EvalValue> EvaluateUserDataExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> EvaluateUserDataExpression(const BoundExpression *expression); shared_ptr<const EvalValue> EvaluateComplexTableExpression(const BoundExpression *expression);
const shared_ptr<EvalValue> GetVariable(const BoundVariableExpression *expression); shared_ptr<const EvalValue> GetVariable(const BoundVariableExpression *expression);
public: public:
explicit Evaluator(map<Utilities::HashedString, shared_ptr<EvalValue>>* scriptVariables) explicit Evaluator(map<Utilities::HashedString, shared_ptr<const EvalValue>>* scriptVariables)
: _scriptVariables(scriptVariables), _hasReturned(false), _hasBroken(false), _returnValue(nullptr), : _scriptVariables(scriptVariables), _hasReturned(false), _hasBroken(false), _returnValue(nullptr),
_evaluationScope(nullptr){ _evaluationScope(nullptr){
} }
EvalValue* Evaluate(const BoundScriptStatement* statement); const EvalValue* Evaluate(const BoundScriptStatement* statement);
const shared_ptr<EvalValue> EvaluateFunction(const GenericFunctionEvalValue *function, shared_ptr<const EvalValue> EvaluateFunction(const GenericFunctionEvalValue *function,
const vector<EvalValue *> &parameters); const vector<EvalValue *> &parameters);
EvalValue* GetLastValue(){ inline const EvalValue* GetLastValue(){
return _lastValue.get(); return _lastValue.get();
} }

View File

@ -9,7 +9,7 @@
namespace Porygon::Evaluation{ namespace Porygon::Evaluation{
class NumericalKeyIterator : public Iterator{ class NumericalKeyIterator : public Iterator{
const shared_ptr<vector<shared_ptr<EvalValue>>> _vec; const shared_ptr<vector<shared_ptr<const EvalValue>>> _vec;
const size_t _size; const size_t _size;
long _position = 0; long _position = 0;
public: public:

View File

@ -8,8 +8,8 @@
namespace Porygon::Evaluation{ namespace Porygon::Evaluation{
class TableKeyIterator : public Iterator{ class TableKeyIterator : public Iterator{
_Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<EvalValue>>> _iterator; _Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<const EvalValue>>> _iterator;
_Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<EvalValue>>> _end; _Rb_tree_const_iterator<pair<const Utilities::HashedString, shared_ptr<const EvalValue>>> _end;
bool _hasStarted = false; bool _hasStarted = false;
public: public:
explicit TableKeyIterator(const TableEvalValue* table) explicit TableKeyIterator(const TableEvalValue* table)

View File

@ -5,7 +5,7 @@
#include "../Script.hpp" #include "../Script.hpp"
namespace Porygon::Evaluation { namespace Porygon::Evaluation {
const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerUnary(const BoundUnaryExpression *expression) { shared_ptr<const NumericEvalValue> Evaluator::EvaluateIntegerUnary(const BoundUnaryExpression *expression) {
switch (expression->GetOperation()) { switch (expression->GetOperation()) {
case BoundUnaryOperation::Negation: { case BoundUnaryOperation::Negation: {
auto operandValue = EvaluateIntegerExpression(expression->GetOperand()); auto operandValue = EvaluateIntegerExpression(expression->GetOperand());
@ -22,7 +22,7 @@ namespace Porygon::Evaluation {
} }
} }
const shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanUnary(const BoundUnaryExpression *expression) { shared_ptr<const BooleanEvalValue> Evaluator::EvaluateBooleanUnary(const BoundUnaryExpression *expression) {
switch (expression->GetOperation()) { switch (expression->GetOperation()) {
case BoundUnaryOperation::LogicalNegation: { case BoundUnaryOperation::LogicalNegation: {
auto val = EvaluateBoolExpression(expression->GetOperand()); auto val = EvaluateBoolExpression(expression->GetOperand());

View File

@ -7,7 +7,7 @@
namespace Porygon { namespace Porygon {
class GenericFunctionOption{ class GenericFunctionOption{
shared_ptr<ScriptType> _returnType; shared_ptr<const ScriptType> _returnType;
vector<shared_ptr<ScriptType>> _parameterTypes; vector<shared_ptr<ScriptType>> _parameterTypes;
size_t _option = 0; size_t _option = 0;
public: public:
@ -17,7 +17,7 @@ namespace Porygon {
virtual ~GenericFunctionOption() = default; virtual ~GenericFunctionOption() = default;
inline const shared_ptr<ScriptType> GetReturnType() const { [[nodiscard]] inline shared_ptr<const ScriptType> GetReturnType() const {
return _returnType; return _returnType;
} }
@ -25,19 +25,19 @@ namespace Porygon {
_option = v; _option = v;
} }
inline void SetReturnType(shared_ptr<ScriptType> t) { inline void SetReturnType(const shared_ptr<const ScriptType>& t) {
_returnType = move(t); _returnType = t;
} }
inline const vector<shared_ptr<ScriptType>> GetParameterTypes() const { [[nodiscard]] inline vector<shared_ptr<ScriptType>> GetParameterTypes() const {
return _parameterTypes; return _parameterTypes;
} }
const bool IsValid(const vector<shared_ptr<ScriptType>>& parameters){ bool IsValid(const vector<shared_ptr<const ScriptType>>& parameters){
if (parameters.size() != _parameterTypes.size()){ if (parameters.size() != _parameterTypes.size()){
return false; 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())){ if (parameters[i]->operator!=(_parameterTypes[i].get())){
return false; return false;
} }
@ -45,15 +45,15 @@ namespace Porygon {
return true; return true;
} }
inline const size_t GetOptionId() const{ [[nodiscard]] inline size_t GetOptionId() const{
return _option; return _option;
} }
virtual const bool IsScriptFunction() const = 0; [[nodiscard]] virtual bool IsScriptFunction() const = 0;
}; };
class GenericFunctionScriptType : public Porygon::ScriptType { class GenericFunctionScriptType : public Porygon::ScriptType {
vector<GenericFunctionOption *> _options; vector<GenericFunctionOption *>* _options = new vector<GenericFunctionOption *>;
public: public:
GenericFunctionScriptType() GenericFunctionScriptType()
: ScriptType(Porygon::TypeClass::Function){}; : ScriptType(Porygon::TypeClass::Function){};
@ -64,18 +64,18 @@ namespace Porygon {
}; };
~GenericFunctionScriptType() final{ ~GenericFunctionScriptType() final{
for (auto o: _options){ for (auto o: *_options){
delete o; delete o;
} }
} }
void RegisterFunctionOption (GenericFunctionOption * opt){ void RegisterFunctionOption (GenericFunctionOption * opt) const{
opt->SetOption(_options.size()); opt->SetOption(_options->size());
_options.push_back(opt); _options->push_back(opt);
} }
GenericFunctionOption* GetFunctionOption(const vector<shared_ptr<ScriptType>>& parameters){ GenericFunctionOption* GetFunctionOption(const vector<shared_ptr<const ScriptType>>& parameters) const{
for (auto o: _options){ for (auto o: *_options){
if (o->IsValid(parameters)){ if (o->IsValid(parameters)){
return o; return o;
} }
@ -83,24 +83,24 @@ namespace Porygon {
return nullptr; return nullptr;
} }
inline GenericFunctionOption* GetFirstOption(){ [[nodiscard]] inline GenericFunctionOption* GetFirstOption() const{
return _options[0]; return _options->at(0);
} }
}; };
class ScriptFunctionOption : public GenericFunctionOption { class ScriptFunctionOption : public GenericFunctionOption {
vector<shared_ptr<Porygon::Binder::BoundVariableKey>> _parameterKeys; vector<shared_ptr<const Porygon::Binder::BoundVariableKey>> _parameterKeys;
public: public:
ScriptFunctionOption(shared_ptr<ScriptType> returnType, vector<shared_ptr<ScriptType>> parameterTypes, ScriptFunctionOption(shared_ptr<ScriptType> returnType, vector<shared_ptr<ScriptType>> parameterTypes,
vector<shared_ptr<Porygon::Binder::BoundVariableKey>> parameterKeys) vector<shared_ptr<const Porygon::Binder::BoundVariableKey>> parameterKeys)
: GenericFunctionOption(move(returnType), std::move(parameterTypes)), _parameterKeys(move(parameterKeys)) { : GenericFunctionOption(move(returnType), std::move(parameterTypes)), _parameterKeys(move(parameterKeys)) {
} }
inline const vector<shared_ptr<Porygon::Binder::BoundVariableKey>> GetParameterKeys() const { [[nodiscard]] inline vector<shared_ptr<const Porygon::Binder::BoundVariableKey>> GetParameterKeys() const {
return _parameterKeys; return _parameterKeys;
} }
inline const bool IsScriptFunction() const final { [[nodiscard]] inline bool IsScriptFunction() const final {
return true; return true;
} }
}; };

View File

@ -1,4 +1,3 @@
#include <utility>
#include <cmath> #include <cmath>
#include <unordered_map> #include <unordered_map>
#include <sstream> #include <sstream>
@ -175,7 +174,7 @@ namespace Porygon::Parser {
this->Next(); this->Next();
has_point = true; has_point = true;
decimal_index = 0; decimal_index = 0;
float_value = int_value; float_value = (double) int_value;
length++; length++;
continue; continue;
default: default:
@ -296,7 +295,7 @@ namespace Porygon::Parser {
u16string s = this->_scriptString.substr(start + 1, end - start); u16string s = this->_scriptString.substr(start + 1, end - start);
std::basic_ostringstream<char16_t> stream; std::basic_ostringstream<char16_t> stream;
for (int i = 0; i < s.size(); i++) { for (size_t i = 0; i < s.size(); i++) {
c = s[i]; c = s[i];
if (c == '\\') { if (c == '\\') {
i++; i++;

View File

@ -1,4 +1,3 @@
#include <utility>
#include <algorithm> #include <algorithm>
#include "Parser.hpp" #include "Parser.hpp"
#include "ParsedStatements/ParsedStatement.hpp" #include "ParsedStatements/ParsedStatement.hpp"

View File

@ -1,6 +1,3 @@
#include <utility>
#ifndef PORYGONLANG_PARSER_HPP #ifndef PORYGONLANG_PARSER_HPP
#define PORYGONLANG_PARSER_HPP #define PORYGONLANG_PARSER_HPP

View File

@ -1,6 +1,3 @@
#include <utility>
#ifndef PORYGONLANG_TOKEN_HPP #ifndef PORYGONLANG_TOKEN_HPP
#define PORYGONLANG_TOKEN_HPP #define PORYGONLANG_TOKEN_HPP

View File

@ -6,19 +6,19 @@
namespace Porygon::Parser { namespace Porygon::Parser {
class TypedVariableIdentifier { class TypedVariableIdentifier {
HashedString _type; const HashedString _type;
HashedString _identifier; const HashedString _identifier;
public: public:
TypedVariableIdentifier(const HashedString& type, const HashedString& identifier) TypedVariableIdentifier(const HashedString& type, const HashedString& identifier)
: _type(type), _identifier(identifier) { : _type(type), _identifier(identifier) {
} }
inline HashedString GetType() { inline const HashedString* GetType() {
return _type; return &_type;
} }
inline HashedString GetIdentifier() { inline const HashedString* GetIdentifier() {
return _identifier; return &_identifier;
} }
}; };
} }

View File

@ -1,10 +1,7 @@
#include <utility>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <iterator> #include <iterator>
#include <locale> #include <locale>
#include <unordered_map>
#include <codecvt> #include <codecvt>
#include "Script.hpp" #include "Script.hpp"
#include "Parser/Lexer.hpp" #include "Parser/Lexer.hpp"
@ -27,13 +24,13 @@ Porygon::Script *Porygon::Script::Create(const string &script) {
Porygon::Script::Script(const u16string& s) Porygon::Script::Script(const u16string& s)
: Diagnostics(make_shared<Diagnostics::DiagnosticsHolder>(s)), : Diagnostics(make_shared<Diagnostics::DiagnosticsHolder>(s)),
_boundScript(nullptr), _boundScript(nullptr),
_scriptVariables(new map<Utilities::HashedString, shared_ptr<EvalValue>>()) _scriptVariables(new map<Utilities::HashedString, shared_ptr<const EvalValue>>())
{ {
_evaluator = new Evaluator(this -> _scriptVariables); _evaluator = new Evaluator(this -> _scriptVariables);
this -> Parse(s); this -> Parse(s);
} }
EvalValue* Porygon::Script::Evaluate() { const EvalValue* Porygon::Script::Evaluate() {
return _evaluator->Evaluate(_boundScript.get()); return _evaluator->Evaluate(_boundScript.get());
} }
@ -65,7 +62,7 @@ void Porygon::Script::Parse(const u16string& script) {
delete parseResult; delete parseResult;
} }
EvalValue *Porygon::Script::GetVariable(const u16string &key) { const EvalValue *Porygon::Script::GetVariable(const u16string &key) {
return _scriptVariables -> at(HashedString::CreateLookup(key)).get(); return _scriptVariables -> at(HashedString::CreateLookup(key)).get();
} }
@ -74,7 +71,7 @@ bool Porygon::Script::HasVariable(const u16string &key) {
return f != _scriptVariables->end(); return f != _scriptVariables->end();
} }
EvalValue *Porygon::Script::GetLastValue() { const EvalValue *Porygon::Script::GetLastValue() {
return _evaluator->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; return f != _scriptVariables->end() && f.operator->()->second->GetTypeClass() == TypeClass ::Function;
} }
shared_ptr<EvalValue> Porygon::Script::CallFunction(const u16string &key, const vector<EvalValue *>& variables) { shared_ptr<const EvalValue> Porygon::Script::CallFunction(const u16string &key, const vector<EvalValue *>& variables) {
auto var = (GenericFunctionEvalValue*)GetVariable(key); auto var = (GenericFunctionEvalValue*)GetVariable(key);
return this->_evaluator->EvaluateFunction(var, variables); return this->_evaluator->EvaluateFunction(var, variables);
} }
@ -101,7 +98,7 @@ Porygon::Script::Script(shared_ptr<BoundScriptStatement> boundScript,
shared_ptr<Porygon::Diagnostics::DiagnosticsHolder> diagnostics) shared_ptr<Porygon::Diagnostics::DiagnosticsHolder> diagnostics)
: _boundScript(std::move(boundScript)), : _boundScript(std::move(boundScript)),
Diagnostics(std::move(diagnostics)), Diagnostics(std::move(diagnostics)),
_scriptVariables(new map<Utilities::HashedString, shared_ptr<EvalValue>>()) _scriptVariables(new map<Utilities::HashedString, shared_ptr<const EvalValue>>())
{ {
_evaluator = new Evaluator(_scriptVariables); _evaluator = new Evaluator(_scriptVariables);
} }
@ -116,7 +113,7 @@ extern "C" {
script->Evaluate(); script->Evaluate();
} }
EvalValue* GetLastValue(Porygon::Script* script){ const EvalValue* GetLastValue(Porygon::Script* script){
return script->GetLastValue(); return script->GetLastValue();
} }
@ -124,7 +121,7 @@ extern "C" {
return script->HasVariable(key); 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); return script->GetVariable(key);
} }
@ -132,7 +129,7 @@ extern "C" {
return script->HasFunction(key); 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<EvalValue*> v(parameters, parameters + parameterCount); std::vector<EvalValue*> v(parameters, parameters + parameterCount);
return script->CallFunction(key, v).get(); return script->CallFunction(key, v).get();
} }

View File

@ -17,9 +17,9 @@ using namespace Porygon::Evaluation;
namespace Porygon{ namespace Porygon{
class Script { class Script {
Evaluator* _evaluator; Evaluator* _evaluator;
map<Utilities::HashedString, shared_ptr<EvalValue>>* _scriptVariables; map<Utilities::HashedString, shared_ptr<const EvalValue>>* _scriptVariables;
shared_ptr<Binder::BoundScriptStatement> _boundScript; shared_ptr<Binder::BoundScriptStatement> _boundScript;
shared_ptr<ScriptType> _returnType; shared_ptr<const ScriptType> _returnType;
explicit Script(const u16string&); explicit Script(const u16string&);
Script(shared_ptr<BoundScriptStatement> boundScript, shared_ptr<Diagnostics::DiagnosticsHolder> diagnostics); Script(shared_ptr<BoundScriptStatement> boundScript, shared_ptr<Diagnostics::DiagnosticsHolder> diagnostics);
@ -33,22 +33,22 @@ namespace Porygon{
~Script(); ~Script();
inline shared_ptr<ScriptType> GetReturnType(){ inline shared_ptr<const ScriptType> GetReturnType(){
return _returnType; return _returnType;
} }
inline void SetReturnType(shared_ptr<ScriptType> t){ inline void SetReturnType(const shared_ptr<const ScriptType>& t){
_returnType = std::move(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); bool HasVariable(const u16string& key);
shared_ptr<EvalValue> CallFunction(const u16string& key, const vector<EvalValue*>& variables); shared_ptr<const EvalValue> CallFunction(const u16string& key, const vector<EvalValue*>& variables);
bool HasFunction(const u16string& key); bool HasFunction(const u16string& key);
}; };

View File

@ -2,11 +2,11 @@
#include "UserData/UserDataFunctionType.hpp" #include "UserData/UserDataFunctionType.hpp"
namespace Porygon{ namespace Porygon{
inline const bool ScriptType::CanBeIndexedWith(ScriptType *indexer) const{ inline bool ScriptType::CanBeIndexedWith(const ScriptType *) const{
return false; return false;
} }
const shared_ptr<ScriptType> ScriptType::GetIndexedType(ScriptType*) const{ shared_ptr<const ScriptType> ScriptType::GetIndexedType(const ScriptType*) const{
if (_class == TypeClass::String){ if (_class == TypeClass::String){
return make_shared<ScriptType>(TypeClass::String); return make_shared<ScriptType>(TypeClass::String);
} }

View File

@ -41,31 +41,36 @@ namespace Porygon{
return _class == b._class; return _class == b._class;
}; };
virtual bool operator ==(ScriptType* b) const{ virtual bool operator ==(const ScriptType* b) const{
return _class == b->_class; return _class == b->_class;
}; };
virtual bool operator !=(const ScriptType& b) const{ virtual bool operator !=(const ScriptType& b) const{
return ! (operator==(b)); return ! (operator==(b));
} }
virtual bool operator !=(ScriptType* b) const{ virtual bool operator !=(const ScriptType* b) const{
return ! (operator==(b)); return ! (operator==(b));
} }
virtual const bool CanBeIndexedWith(ScriptType* indexer) const; virtual bool CanBeIndexedWith(const ScriptType* indexer) const;
virtual const bool CanBeIndexedWithIdentifier(uint32_t hash) const{ [[nodiscard]]
virtual bool CanBeIndexedWithIdentifier(uint32_t hash) const{
return false; return false;
} }
virtual const shared_ptr<ScriptType> GetIndexedType(ScriptType* indexer) const; [[nodiscard]]
virtual const shared_ptr<ScriptType> GetIndexedType(uint32_t hash) const{ virtual shared_ptr<const ScriptType> GetIndexedType(const ScriptType* indexer) const;
[[nodiscard]]
virtual shared_ptr<const ScriptType> GetIndexedType(uint32_t hash) const{
throw "This type told the binder it can be indexed, but it does not implement the resulting type."; 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; return false;
} }
virtual shared_ptr<ScriptType> GetIteratorKeyType() const{ [[nodiscard]]
virtual shared_ptr<const ScriptType> GetIteratorKeyType() const{
throw "This type told the binder it can be iterated, but it does not implement the resulting type."; 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; _isFloat = isFloat;
} }
inline const bool IsAwareOfFloat() const{ [[nodiscard]] inline bool IsAwareOfFloat() const{
return _awareOfFloat; return _awareOfFloat;
} }
inline const bool IsFloat() const{ [[nodiscard]] inline bool IsFloat() const{
return _isFloat; return _isFloat;
} }
}; };
@ -99,49 +104,54 @@ namespace Porygon{
_hashValue = hashValue; _hashValue = hashValue;
} }
const bool CanBeIndexedWith(ScriptType* indexer) const final{ [[nodiscard]]
bool CanBeIndexedWith(const ScriptType* indexer) const final{
if (indexer -> GetClass() != TypeClass::Number) if (indexer -> GetClass() != TypeClass::Number)
return false; return false;
auto num = dynamic_cast<NumericScriptType*>(indexer); auto num = dynamic_cast<const NumericScriptType*>(indexer);
return !(num->IsAwareOfFloat() && num->IsFloat()); return !(num->IsAwareOfFloat() && num->IsFloat());
} }
inline const shared_ptr<ScriptType> GetIndexedType(ScriptType* indexer) const final{ inline shared_ptr<const ScriptType> GetIndexedType(const ScriptType* indexer) const final{
return make_shared<StringScriptType>(false, 0); return make_shared<StringScriptType>(false, 0);
} }
inline const bool IsKnownAtBind() const{ [[nodiscard]]
inline bool IsKnownAtBind() const{
return _isKnownAtBind; return _isKnownAtBind;
} }
inline const uint32_t GetHashValue() const{ [[nodiscard]]
inline uint32_t GetHashValue() const{
return _hashValue; return _hashValue;
} }
}; };
class NumericalTableScriptType : public ScriptType{ class NumericalTableScriptType : public ScriptType{
shared_ptr<ScriptType> _valueType; shared_ptr<const ScriptType> _valueType;
// Consider adding a check whether the table actually contains a type if every key is static. // Consider adding a check whether the table actually contains a type if every key is static.
public: public:
explicit NumericalTableScriptType(shared_ptr<ScriptType> valueType) explicit NumericalTableScriptType(shared_ptr<const ScriptType> valueType)
: ScriptType(TypeClass::Table), _valueType(std::move(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) if (indexer -> GetClass() != TypeClass::Number)
return false; return false;
auto num = dynamic_cast<NumericScriptType*>(indexer); auto num = dynamic_cast<const NumericScriptType*>(indexer);
return !(num->IsAwareOfFloat() && num->IsFloat()); return !(num->IsAwareOfFloat() && num->IsFloat());
} }
inline const shared_ptr<ScriptType> GetIndexedType(ScriptType* indexer) const final{ inline shared_ptr<const ScriptType> GetIndexedType(const ScriptType* indexer) const final{
return _valueType; return _valueType;
} }
inline const bool CanBeIterated() const final{ [[nodiscard]]
inline bool CanBeIterated() const final{
return true; return true;
} }
inline shared_ptr<ScriptType> GetIteratorKeyType() const final{ [[nodiscard]]
inline shared_ptr<const ScriptType> GetIteratorKeyType() const final{
return make_shared<NumericScriptType>(true, false); return make_shared<NumericScriptType>(true, false);
} }
}; };

View File

@ -15,13 +15,13 @@
namespace Porygon::StandardLibraries{ namespace Porygon::StandardLibraries{
class BasicLibrary{ 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 message = parameters[0]->EvaluateString();
auto conv = Utilities::StringUtils::FromUTF8(message); auto conv = Utilities::StringUtils::FromUTF8(message);
throw Evaluation::EvaluationException(conv); 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(); auto assertion = parameters[0]->EvaluateBool();
if (!assertion){ if (!assertion){
if (parameterCount >= 2){ if (parameterCount >= 2){
@ -34,13 +34,13 @@ namespace Porygon::StandardLibraries{
return new Evaluation::BooleanEvalValue(true); 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(); auto message = parameters[0]->EvaluateString();
GlobalScriptOptions::Print(message.c_str()); GlobalScriptOptions::Print(message.c_str());
return new Evaluation::NilEvalValue(); 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 parameter = parameters[0]->EvaluateString();
auto parsed = Utilities::StringUtils::ParseInteger(parameter); auto parsed = Utilities::StringUtils::ParseInteger(parameter);
return new Evaluation::IntegerEvalValue(parsed); return new Evaluation::IntegerEvalValue(parsed);
@ -75,10 +75,10 @@ namespace Porygon::StandardLibraries{
static shared_ptr<Evaluation::EvalValue> GetFuncEvalValue( static shared_ptr<Evaluation::EvalValue> GetFuncEvalValue(
Evaluation::EvalValue* (*func)(void* obj, Evaluation::EvalValue* parameters[], int parameterCount), const Evaluation::EvalValue* (*func)(void* obj, const Evaluation::EvalValue* parameters[], int parameterCount),
shared_ptr<GenericFunctionScriptType> type, size_t optionLength){ const shared_ptr<GenericFunctionScriptType>& type, size_t optionLength){
auto f = make_shared<Evaluation::GenericFunctionEvalValue>(type, rand()); auto f = make_shared<Evaluation::GenericFunctionEvalValue>(type, rand());
for (int i = 0; i < optionLength; i++){ for (size_t i = 0; i < optionLength; i++){
auto funcOption = new UserData::UserDataFunction(func, nullptr); auto funcOption = new UserData::UserDataFunction(func, nullptr);
f->RegisterOption(funcOption); f->RegisterOption(funcOption);
} }

View File

@ -22,33 +22,30 @@ namespace Porygon{
delete _values; delete _values;
} }
inline const bool CanBeIndexedWith(ScriptType* indexer) const final{ [[nodiscard]]
inline bool CanBeIndexedWith(const ScriptType* indexer) const final{
return indexer->GetClass() == TypeClass ::String; return indexer->GetClass() == TypeClass ::String;
} }
const bool CanBeIndexedWithIdentifier(uint32_t hash) const final{ [[nodiscard]] bool CanBeIndexedWithIdentifier(uint32_t hash) const final{
return true; return true;
} }
const shared_ptr<ScriptType> GetIndexedType(ScriptType* indexer) const final{ shared_ptr<const ScriptType> GetIndexedType(const ScriptType* indexer) const final{
auto stringKey = dynamic_cast<StringScriptType*>(indexer); auto stringKey = dynamic_cast<const StringScriptType*>(indexer);
if (stringKey->IsKnownAtBind()){ if (stringKey->IsKnownAtBind()){
return _values-> at(Utilities::HashedString::CreateLookup(stringKey->GetHashValue()))->GetType(); return _values-> at(Utilities::HashedString::CreateLookup(stringKey->GetHashValue()))->GetType();
} }
throw "TODO: indexing with dynamic keys"; throw "TODO: indexing with dynamic keys";
} }
inline const shared_ptr<ScriptType> GetIndexedType(uint32_t hash) const final{ [[nodiscard]] inline shared_ptr<const ScriptType> GetIndexedType(uint32_t hash) const final{
return _values-> at(Utilities::HashedString::CreateLookup(hash))->GetType(); return _values-> at(Utilities::HashedString::CreateLookup(hash))->GetType();
} }
inline const map<Utilities::HashedString, BoundVariable*>* GetValues() const{ [[nodiscard]] inline const map<Utilities::HashedString, BoundVariable*>* GetValues() const{
return _values; return _values;
} }
inline const int GetLocalVariableCount() const{
return _localVariableCount;
}
}; };
} }

View File

@ -1,5 +1,3 @@
#include <utility>
#ifndef PORYGONLANG_USERDATA_HPP #ifndef PORYGONLANG_USERDATA_HPP
#define PORYGONLANG_USERDATA_HPP #define PORYGONLANG_USERDATA_HPP
@ -52,10 +50,12 @@ namespace Porygon::UserData {
delete _concatenation; delete _concatenation;
} }
[[nodiscard]]
inline bool ContainsField(uint32_t fieldId) const{ inline bool ContainsField(uint32_t fieldId) const{
return _fields.find(fieldId) != _fields.end(); return _fields.find(fieldId) != _fields.end();
} }
[[nodiscard]]
inline UserDataField *GetField(uint32_t fieldId) const { inline UserDataField *GetField(uint32_t fieldId) const {
return _fields.at(fieldId); return _fields.at(fieldId);
} }
@ -64,10 +64,12 @@ namespace Porygon::UserData {
_fields.insert({fieldId, field}); _fields.insert({fieldId, field});
} }
[[nodiscard]]
inline int32_t GetFieldCount() const{ inline int32_t GetFieldCount() const{
return _fields.size(); return _fields.size();
} }
[[nodiscard]]
UserDataBinaryOperation* GetBinaryOperation(Binder::BoundBinaryOperation op){ UserDataBinaryOperation* GetBinaryOperation(Binder::BoundBinaryOperation op){
switch (op){ switch (op){

View File

@ -4,7 +4,9 @@
namespace Porygon::UserData { namespace Porygon::UserData {
extern "C" { extern "C" {
UserDataField * 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); return new UserDataField(type, getter, setter);
} }
} }

View File

@ -8,31 +8,35 @@
namespace Porygon::UserData{ namespace Porygon::UserData{
class UserDataField { class UserDataField {
shared_ptr<ScriptType> _type; shared_ptr<const ScriptType> _type;
Evaluation::EvalValue* (*_get)(void* obj); const Evaluation::EvalValue* (*_get)(void* obj);
void (*_set)(void* obj, Evaluation::EvalValue* val); void (*_set)(void* obj, const Evaluation::EvalValue* val);
public: public:
UserDataField(ScriptType* type, Evaluation::EvalValue* (*getter)(void* obj), void (*setter)(void* obj, Evaluation::EvalValue* val)) UserDataField(const ScriptType* type, const Evaluation::EvalValue* (*getter)(void* obj), void (*setter)(void* obj, const Evaluation::EvalValue* val))
: _type(shared_ptr<ScriptType>(type)), _get(getter), _set(setter){ : _type(shared_ptr<const ScriptType>(type)), _get(getter), _set(setter){
} }
inline shared_ptr<ScriptType> GetType(){ [[nodiscard]]
inline shared_ptr<const ScriptType> GetType(){
return _type; return _type;
} }
[[nodiscard]]
inline bool HasGetter(){ inline bool HasGetter(){
return _get != nullptr; return _get != nullptr;
} }
inline Evaluation::EvalValue* Get(void* obj){ [[nodiscard]]
inline const Evaluation::EvalValue* Get(void* obj){
return this ->_get(obj); return this ->_get(obj);
} }
[[nodiscard]]
inline bool HasSetter(){ inline bool HasSetter(){
return _set != nullptr; return _set != nullptr;
} }
inline void Set(void* obj, Evaluation::EvalValue* val){ inline void Set(void* obj, const Evaluation::EvalValue* val){
this->_set(obj, val); this->_set(obj, val);
} }
}; };

View File

@ -5,7 +5,7 @@ using namespace Porygon::Evaluation;
namespace Porygon::UserData{ namespace Porygon::UserData{
extern "C" { 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 opt = new UserDataFunction(func, obj);
auto t = new GenericFunctionEvalValue(make_shared<GenericFunctionScriptType>(), rand()); auto t = new GenericFunctionEvalValue(make_shared<GenericFunctionScriptType>(), rand());
t->RegisterOption(opt); t->RegisterOption(opt);

View File

@ -1,33 +1,31 @@
#ifndef PORYGONLANG_USERDATAFUNCTION_HPP #ifndef PORYGONLANG_USERDATAFUNCTION_HPP
#define PORYGONLANG_USERDATAFUNCTION_HPP #define PORYGONLANG_USERDATAFUNCTION_HPP
#include <utility>
#include "../Evaluator/EvalValues/ScriptFunctionEvalValue.hpp" #include "../Evaluator/EvalValues/ScriptFunctionEvalValue.hpp"
#include "UserDataFunctionType.hpp" #include "UserDataFunctionType.hpp"
#include "../FunctionScriptType.hpp" #include "../FunctionScriptType.hpp"
namespace Porygon::UserData{ namespace Porygon::UserData{
class UserDataFunction : public Evaluation::GenericFunctionOption { 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; void *_obj;
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,
shared_ptr<GenericFunctionScriptType> type, size_t hash) : GenericFunctionOption(){ const shared_ptr<GenericFunctionScriptType>& type, size_t hash) : GenericFunctionOption(){
_call = call; _call = call;
_obj = obj; _obj = obj;
} }
public: 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(){ GenericFunctionOption(){
_call = call; _call = call;
_obj = obj; _obj = obj;
} }
~UserDataFunction() final{ ~UserDataFunction() final = default;
} [[nodiscard]]
inline const Evaluation::EvalValue* Call(const Evaluation::EvalValue* parameters[], int parameterCount) const{
inline Evaluation::EvalValue* Call(Evaluation::EvalValue* parameters[], int parameterCount){
return _call(_obj, parameters, parameterCount); return _call(_obj, parameters, parameterCount);
} }
}; };

View File

@ -15,7 +15,7 @@ namespace Porygon::UserData{
static UserDataFunctionOption* FromRawPointers(ScriptType* returnType, vector<ScriptType*> parameterTypes){ static UserDataFunctionOption* FromRawPointers(ScriptType* returnType, vector<ScriptType*> parameterTypes){
auto rt = shared_ptr<ScriptType>(returnType); auto rt = shared_ptr<ScriptType>(returnType);
auto p = vector<shared_ptr<ScriptType>>(parameterTypes.size()); auto p = vector<shared_ptr<ScriptType>>(parameterTypes.size());
for (int i = 0; i < parameterTypes.size(); i++){ for (size_t i = 0; i < parameterTypes.size(); i++){
p[i] = shared_ptr<ScriptType>(parameterTypes[i]); p[i] = shared_ptr<ScriptType>(parameterTypes[i]);
} }
return new UserDataFunctionOption(rt, p); return new UserDataFunctionOption(rt, p);
@ -26,10 +26,7 @@ namespace Porygon::UserData{
return new UserDataFunctionOption(rt, {}); return new UserDataFunctionOption(rt, {});
} }
[[nodiscard]] inline bool IsScriptFunction() const final{
inline const bool IsScriptFunction() const final{
return false; return false;
} }
}; };

View File

@ -9,8 +9,8 @@ namespace Porygon::UserData {
class UserDataBinaryOperation { class UserDataBinaryOperation {
void* _parent; void* _parent;
Evaluation::EvalValue *(*_func)(void *obj, Evaluation::EvalValue *b); Evaluation::EvalValue *(*_func)(void *obj, Evaluation::EvalValue *b);
const shared_ptr<ScriptType> _secondParameter; const shared_ptr<const ScriptType> _secondParameter;
const shared_ptr<ScriptType> _returnType; const shared_ptr<const ScriptType> _returnType;
public: public:
UserDataBinaryOperation(void *parent, UserDataBinaryOperation(void *parent,
Evaluation::EvalValue *(*func)(void *, Evaluation::EvalValue *), Evaluation::EvalValue *(*func)(void *, Evaluation::EvalValue *),
@ -23,11 +23,13 @@ namespace Porygon::UserData {
return _func(_parent, b); return _func(_parent, b);
} }
inline const shared_ptr<ScriptType> GetSecondParameterType() const{ [[nodiscard]]
inline shared_ptr<const ScriptType> GetSecondParameterType() const{
return _secondParameter; return _secondParameter;
} }
inline const shared_ptr<ScriptType> GetReturnType() const{ [[nodiscard]]
inline shared_ptr<const ScriptType> GetReturnType() const{
return _returnType; return _returnType;
} }
}; };

View File

@ -20,33 +20,33 @@ namespace Porygon::UserData {
_userData = ud; _userData = ud;
} }
const bool CanBeIndexedWith(ScriptType *indexer) const final { bool CanBeIndexedWith(const ScriptType *indexer) const final {
if (indexer->GetClass() != TypeClass::String) { if (indexer->GetClass() != TypeClass::String) {
return false; return false;
} }
auto str = dynamic_cast<StringScriptType*>(indexer); auto str = dynamic_cast<const StringScriptType*>(indexer);
if (!str->IsKnownAtBind()) if (!str->IsKnownAtBind())
return false; return false;
return _userData->ContainsField(str->GetHashValue()); 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); return _userData -> ContainsField(hash);
} }
inline UserDataField *GetField(uint32_t id) { [[nodiscard]] inline UserDataField *GetField(uint32_t id) const{
return _userData->GetField(id); return _userData->GetField(id);
} }
const shared_ptr<ScriptType> GetIndexedType(ScriptType *indexer) const final { shared_ptr<const ScriptType> GetIndexedType(const ScriptType *indexer) const final {
auto stringKey = dynamic_cast<StringScriptType*>(indexer); auto stringKey = dynamic_cast<const StringScriptType*>(indexer);
if (stringKey->IsKnownAtBind()) { if (stringKey->IsKnownAtBind()) {
return _userData->GetField(stringKey->GetHashValue())->GetType(); return _userData->GetField(stringKey->GetHashValue())->GetType();
} }
throw "TODO: indexing with dynamic keys"; throw "TODO: indexing with dynamic keys";
} }
inline const shared_ptr<ScriptType> GetIndexedType(uint32_t hash) const final { [[nodiscard]] inline shared_ptr<const ScriptType> GetIndexedType(uint32_t hash) const final {
return _userData->GetField(hash)->GetType(); return _userData->GetField(hash)->GetType();
} }
}; };

View File

@ -33,8 +33,8 @@
{ \ { \
Porygon::Utilities::HashedString::ConstHash(#fieldName), \ Porygon::Utilities::HashedString::ConstHash(#fieldName), \
new Porygon::UserData::UserDataField(fieldType, \ new Porygon::UserData::UserDataField(fieldType, \
[](void* obj) -> Porygon::Evaluation::EvalValue* { return new getterHelper;}, \ [](void* obj) -> const Porygon::Evaluation::EvalValue* { return new getterHelper;}, \
[](void* obj, Porygon::Evaluation::EvalValue* val) { ((T_USERDATA*)obj)->fieldName = setterHelper;} \ [](void* obj, const Porygon::Evaluation::EvalValue* val) { ((T_USERDATA*)obj)->fieldName = setterHelper;} \
) \ ) \
}, \ }, \
@ -49,7 +49,7 @@
#define PORYGON_INTEGER_FIELD(fieldName) \ #define PORYGON_INTEGER_FIELD(fieldName) \
PORYGON_FIELD(fieldName, PORYGON_INTEGER_TYPE, \ 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) \ #define PORYGON_READONLY_INTEGER_FIELD(fieldName) \
PORYGON_READONLY_FIELD(fieldName, PORYGON_INTEGER_TYPE, \ PORYGON_READONLY_FIELD(fieldName, PORYGON_INTEGER_TYPE, \
@ -72,11 +72,11 @@
Porygon::UserData::UserDataFunctionOption::FromRawPointers(returnType, {__VA_ARGS__} )), \ 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<GenericFunctionScriptType>(), rand()); \ auto t = new Porygon::Evaluation::GenericFunctionEvalValue(make_shared<GenericFunctionScriptType>(), rand()); \
t->RegisterOption(new Porygon::UserData::UserDataFunction( \ t->RegisterOption(new Porygon::UserData::UserDataFunction( \
[](void* obj, Porygon::Evaluation::EvalValue* par[], int parameterCount) \ [](void* obj, const Porygon::Evaluation::EvalValue* par[], int parameterCount) \
-> Porygon::Evaluation::EvalValue*{return ((T_USERDATA*)obj)->invoke__##fieldName(obj, par, parameterCount);}, \ -> const Porygon::Evaluation::EvalValue*{return ((const T_USERDATA*)obj)->invoke__##fieldName(obj, par, parameterCount);}, \
obj)); \ obj)); \
return t;}, \ return t;}, \
nullptr) \ nullptr) \
@ -96,7 +96,7 @@
\returns An invokable function. \returns An invokable function.
*/ */
#define PORYGON_PREPARE_FUNCTION(userDataTypeName, fieldName, returnType, ...) \ #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( \ return Porygon::Evaluation::EvalValueHelper::Create(((userDataTypeName*)obj)->fieldName( \
__VA_ARGS__ \ __VA_ARGS__ \
));} ));}

View File

@ -22,36 +22,40 @@ namespace Porygon::UserData {
_obj = obj; _obj = obj;
} }
inline const TypeClass GetTypeClass() const final { [[nodiscard]]
inline TypeClass GetTypeClass() const final {
return TypeClass::UserData; return TypeClass::UserData;
} }
const bool operator==(EvalValue *b) const final { bool operator==(const EvalValue *b) const final {
if (b->GetTypeClass() != TypeClass::UserData) if (b->GetTypeClass() != TypeClass::UserData)
return false; return false;
return _obj == ((UserDataValue *) b)->_obj; return _obj == ((UserDataValue *) b)->_obj;
} }
inline const shared_ptr<EvalValue> Clone() const final { [[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return make_shared<UserDataValue>(_userData, _obj); return make_shared<UserDataValue>(_userData, _obj);
} }
inline const std::size_t GetHashCode() const final { [[nodiscard]]
inline std::size_t GetHashCode() const final {
return reinterpret_cast<intptr_t>(_obj); return reinterpret_cast<intptr_t>(_obj);
} }
const shared_ptr<EvalValue> IndexValue(EvalValue *val) const final { shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const final {
auto fieldId = val->GetHashCode(); auto fieldId = val->GetHashCode();
auto field = _userData->GetField(fieldId); auto field = _userData->GetField(fieldId);
return shared_ptr<EvalValue>(field->Get(_obj)); return shared_ptr<const EvalValue>(field->Get(_obj));
} }
inline const shared_ptr<EvalValue> IndexValue(uint32_t hash) const final { [[nodiscard]]
inline shared_ptr<const EvalValue> IndexValue(uint32_t hash) const final {
auto field = _userData->GetField(hash); auto field = _userData->GetField(hash);
return shared_ptr<EvalValue>(field->Get(_obj)); return shared_ptr<const EvalValue>(field->Get(_obj));
} }
void SetIndexValue(EvalValue *key, const shared_ptr<EvalValue> &value) const final { void SetIndexValue(const EvalValue *key, const shared_ptr<const EvalValue> &value) const final {
auto fieldId = key->GetHashCode(); auto fieldId = key->GetHashCode();
auto field = _userData->GetField(fieldId); auto field = _userData->GetField(fieldId);
field->Set(_obj, value.get()); field->Set(_obj, value.get());

3
src/Utilities/Random.cpp Normal file
View File

@ -0,0 +1,3 @@
#include "Random.hpp"
std::mt19937_64 Porygon::Utilities::Random::_rng;

20
src/Utilities/Random.hpp Normal file
View File

@ -0,0 +1,20 @@
#ifndef PORYGONLANG_RANDOM_HPP
#define PORYGONLANG_RANDOM_HPP
#include <random>
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

View File

@ -277,10 +277,10 @@ TEST_CASE( "Parse function declaration", "[parser]" ){
auto functionDeclaration = (ParsedFunctionDeclarationStatement*)firstStatement; auto functionDeclaration = (ParsedFunctionDeclarationStatement*)firstStatement;
REQUIRE(functionDeclaration->GetIdentifier() == HashedString::CreateLookup(u"foo")); REQUIRE(functionDeclaration->GetIdentifier() == HashedString::CreateLookup(u"foo"));
auto parameters = functionDeclaration->GetParameters(); auto parameters = functionDeclaration->GetParameters();
CHECK(parameters -> at(0) ->GetType() == HashedString::CreateLookup(u"number")); CHECK(*parameters -> at(0) ->GetType() == HashedString::CreateLookup(u"number"));
CHECK(parameters -> at(0) ->GetIdentifier() == HashedString::CreateLookup(u"bar")); CHECK(*parameters -> at(0) ->GetIdentifier() == HashedString::CreateLookup(u"bar"));
CHECK(parameters -> at(1) ->GetType() == HashedString::CreateLookup(u"number")); CHECK(*parameters -> at(1) ->GetType() == HashedString::CreateLookup(u"number"));
CHECK(parameters -> at(1) ->GetIdentifier() == HashedString::CreateLookup(u"par")); CHECK(*parameters -> at(1) ->GetIdentifier() == HashedString::CreateLookup(u"par"));
for (auto t : v){ for (auto t : v){
delete t; delete t;