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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,9 +1,9 @@
#include <utility>
#ifndef PORYGONLANG_BOUNDVARIABLE_HPP
#define PORYGONLANG_BOUNDVARIABLE_HPP
#include <utility>
#include <memory>
#include "../../ScriptType.hpp"
@ -11,12 +11,12 @@ using namespace std;
namespace Porygon::Binder {
class BoundVariable {
std::shared_ptr<ScriptType> _type;
std::shared_ptr<const ScriptType> _type;
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;
}
};

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -35,13 +35,13 @@ namespace Porygon::Diagnostics {
_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();
@ -52,6 +52,7 @@ namespace Porygon::Diagnostics {
Diagnostic *GetDiagnosticAt(int position);
size_t GetLineFromPosition(size_t i);
[[nodiscard]]
inline size_t GetStartPositionForLine(size_t i){
return _lineStarts[i];
}

View File

@ -8,7 +8,7 @@
using namespace Porygon::Binder;
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 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()) {
case BoundBinaryOperation::Equality: {
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)
throw;
std::basic_ostringstream<char16_t> stringStream;

View File

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

View File

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

View File

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

View File

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

View File

@ -2,74 +2,74 @@
#include "NumericEvalValue.hpp"
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 (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetFloatValue() + b->GetFloatValue());
} else {
return make_shared<FloatEvalValue>(this->GetFloatValue() + b->GetIntegerValue());
return make_shared<FloatEvalValue>(this->GetFloatValue() + (double) b->GetIntegerValue());
}
} else {
if (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetIntegerValue() + b->GetFloatValue());
return make_shared<FloatEvalValue>((double) this->GetIntegerValue() + b->GetFloatValue());
} else {
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 (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetFloatValue() - b->GetFloatValue());
} else {
return make_shared<FloatEvalValue>(this->GetFloatValue() - b->GetIntegerValue());
return make_shared<FloatEvalValue>(this->GetFloatValue() - (double) b->GetIntegerValue());
}
} else {
if (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetIntegerValue() - b->GetFloatValue());
return make_shared<FloatEvalValue>((double) this->GetIntegerValue() - b->GetFloatValue());
} else {
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 (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetFloatValue() * b->GetFloatValue());
} else {
return make_shared<FloatEvalValue>(this->GetFloatValue() * b->GetIntegerValue());
return make_shared<FloatEvalValue>(this->GetFloatValue() * (double) b->GetIntegerValue());
}
} else {
if (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetIntegerValue() * b->GetFloatValue());
return make_shared<FloatEvalValue>((double) this->GetIntegerValue() * b->GetFloatValue());
} else {
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 (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetFloatValue() / b->GetFloatValue());
} else {
return make_shared<FloatEvalValue>(this->GetFloatValue() / b->GetIntegerValue());
return make_shared<FloatEvalValue>(this->GetFloatValue() / (double) b->GetIntegerValue());
}
} else {
if (b->IsFloat()) {
return make_shared<FloatEvalValue>(this->GetIntegerValue() / b->GetFloatValue());
return make_shared<FloatEvalValue>((double) this->GetIntegerValue() / b->GetFloatValue());
} else {
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)
return false;
auto numVal = dynamic_cast<NumericEvalValue*>(b);
auto numVal = dynamic_cast<const NumericEvalValue*>(b);
if (this->IsFloat() != numVal->IsFloat())
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 (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetFloatValue() < b->GetFloatValue());
} else {
return make_shared<BooleanEvalValue>(this->GetFloatValue() < b->GetIntegerValue());
return make_shared<BooleanEvalValue>(this->GetFloatValue() < (double) b->GetIntegerValue());
}
} else {
if (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetIntegerValue() < b->GetFloatValue());
return make_shared<BooleanEvalValue>((double) this->GetIntegerValue() < b->GetFloatValue());
} else {
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 (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetFloatValue() <= b->GetFloatValue());
} else {
return make_shared<BooleanEvalValue>(this->GetFloatValue() <= b->GetIntegerValue());
return make_shared<BooleanEvalValue>(this->GetFloatValue() <=(double) b->GetIntegerValue());
}
} else {
if (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetIntegerValue() <= b->GetFloatValue());
return make_shared<BooleanEvalValue>((double) this->GetIntegerValue() <= b->GetFloatValue());
} else {
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 (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetFloatValue() > b->GetFloatValue());
} else {
return make_shared<BooleanEvalValue>(this->GetFloatValue() > b->GetIntegerValue());
return make_shared<BooleanEvalValue>(this->GetFloatValue() > (double) b->GetIntegerValue());
}
} else {
if (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetIntegerValue() > b->GetFloatValue());
return make_shared<BooleanEvalValue>((double) this->GetIntegerValue() > b->GetFloatValue());
} else {
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 (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetFloatValue() >= b->GetFloatValue());
} else {
return make_shared<BooleanEvalValue>(this->GetFloatValue() >= b->GetIntegerValue());
return make_shared<BooleanEvalValue>(this->GetFloatValue() >= (double) b->GetIntegerValue());
}
} else {
if (b->IsFloat()) {
return make_shared<BooleanEvalValue>(this->GetIntegerValue() >= b->GetFloatValue());
return make_shared<BooleanEvalValue>((double) this->GetIntegerValue() >= b->GetFloatValue());
} else {
return make_shared<BooleanEvalValue>(this->GetIntegerValue() >= b->GetIntegerValue());
}

View File

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

View File

@ -10,55 +10,63 @@ using namespace std;
namespace Porygon::Evaluation {
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;
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)),
_hash(hash)
{
}
public:
explicit NumericalTableEvalValue(shared_ptr<vector<shared_ptr<EvalValue>>> table) :
explicit NumericalTableEvalValue(shared_ptr<vector<shared_ptr<const EvalValue>>> table) :
_table(std::move(table)),
_hash(rand())
{
}
inline const TypeClass GetTypeClass() const final {
[[nodiscard]]
inline TypeClass GetTypeClass() const final {
return TypeClass::Table;
}
inline const size_t GetHashCode() const final {
[[nodiscard]]
inline size_t GetHashCode() const final {
return _hash;
}
inline const bool operator==(EvalValue *b) const final {
[[nodiscard]]
inline bool operator==(const EvalValue *b) const final {
return this->_hash == b->GetHashCode();
}
inline const shared_ptr<EvalValue> Clone() const final {
[[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
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;
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);
}
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();
this->_table->at(index - 1) = value;
}
[[nodiscard]]
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;
};
};

View File

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

View File

@ -4,68 +4,79 @@
#include <utility>
#include <map>
#include "EvalValue.hpp"
#include "../../Utilities/Random.hpp"
using namespace std;
namespace Porygon::Evaluation {
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;
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)),
_hash(hash)
{
}
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)),
_hash(rand())
_hash(Utilities::Random::Get())
{
}
inline const TypeClass GetTypeClass() const final {
[[nodiscard]]
inline TypeClass GetTypeClass() const final {
return TypeClass::Table;
}
inline const size_t GetHashCode() const final {
[[nodiscard]]
inline size_t GetHashCode() const final {
return _hash;
}
inline const bool operator==(EvalValue *b) const final {
[[nodiscard]]
inline bool operator==(const EvalValue *b) const final {
return this->_hash == b->GetHashCode();
}
inline const shared_ptr<EvalValue> Clone() const final {
[[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
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();
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));
}
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);
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();
this->_table->at(Utilities::HashedString::CreateLookup(hash)) = value;
}
[[nodiscard]]
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();
};
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();
};
};

View File

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

View File

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

View File

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

View File

@ -9,7 +9,7 @@
namespace Porygon::Evaluation{
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;
long _position = 0;
public:

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,7 +4,9 @@
namespace Porygon::UserData {
extern "C" {
UserDataField *
CreateUserDataField(ScriptType *type, Evaluation::EvalValue *(*getter)(void *obj), void (*setter)(void *obj, Evaluation::EvalValue *val)) {
CreateUserDataField(ScriptType *type,
const Evaluation::EvalValue *(*getter)(void *),
void (*setter)(void *, const Evaluation::EvalValue *)) {
return new UserDataField(type, getter, setter);
}
}

View File

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

View File

@ -5,7 +5,7 @@ using namespace Porygon::Evaluation;
namespace Porygon::UserData{
extern "C" {
EvalValue * CreateFunctionEvalValue(Evaluation::EvalValue* (*func)(void* , EvalValue* [], int ), void* obj) {
const EvalValue * CreateFunctionEvalValue(const Evaluation::EvalValue* (*func)(void* , const EvalValue* [], int ), void* obj) {
auto opt = new UserDataFunction(func, obj);
auto t = new GenericFunctionEvalValue(make_shared<GenericFunctionScriptType>(), rand());
t->RegisterOption(opt);

View File

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

View File

@ -15,7 +15,7 @@ namespace Porygon::UserData{
static UserDataFunctionOption* FromRawPointers(ScriptType* returnType, vector<ScriptType*> parameterTypes){
auto rt = shared_ptr<ScriptType>(returnType);
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]);
}
return new UserDataFunctionOption(rt, p);
@ -26,10 +26,7 @@ namespace Porygon::UserData{
return new UserDataFunctionOption(rt, {});
}
inline const bool IsScriptFunction() const final{
[[nodiscard]] inline bool IsScriptFunction() const final{
return false;
}
};

View File

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

View File

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

View File

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

View File

@ -22,36 +22,40 @@ namespace Porygon::UserData {
_obj = obj;
}
inline const TypeClass GetTypeClass() const final {
[[nodiscard]]
inline TypeClass GetTypeClass() const final {
return TypeClass::UserData;
}
const bool operator==(EvalValue *b) const final {
bool operator==(const EvalValue *b) const final {
if (b->GetTypeClass() != TypeClass::UserData)
return false;
return _obj == ((UserDataValue *) b)->_obj;
}
inline const shared_ptr<EvalValue> Clone() const final {
[[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
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);
}
const shared_ptr<EvalValue> IndexValue(EvalValue *val) const final {
shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const final {
auto fieldId = val->GetHashCode();
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);
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 field = _userData->GetField(fieldId);
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;
REQUIRE(functionDeclaration->GetIdentifier() == HashedString::CreateLookup(u"foo"));
auto parameters = functionDeclaration->GetParameters();
CHECK(parameters -> at(0) ->GetType() == HashedString::CreateLookup(u"number"));
CHECK(parameters -> at(0) ->GetIdentifier() == HashedString::CreateLookup(u"bar"));
CHECK(parameters -> at(1) ->GetType() == HashedString::CreateLookup(u"number"));
CHECK(parameters -> at(1) ->GetIdentifier() == HashedString::CreateLookup(u"par"));
CHECK(*parameters -> at(0) ->GetType() == HashedString::CreateLookup(u"number"));
CHECK(*parameters -> at(0) ->GetIdentifier() == HashedString::CreateLookup(u"bar"));
CHECK(*parameters -> at(1) ->GetType() == HashedString::CreateLookup(u"number"));
CHECK(*parameters -> at(1) ->GetIdentifier() == HashedString::CreateLookup(u"par"));
for (auto t : v){
delete t;