Added namespaces to most classes, general cleanup
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2019-06-17 18:35:12 +02:00
parent 21d3329c55
commit fde102d954
66 changed files with 4301 additions and 3909 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -8,36 +8,54 @@
#include "../Parser/ParsedExpressions/ParsedTableExpression.hpp"
using namespace std;
using namespace Porygon::Parser;
class Binder {
Script* _scriptData;
BoundScope* _scope;
shared_ptr<FunctionScriptType> _currentFunction;
namespace Porygon::Binder {
class Binder {
Porygon::Script *_scriptData;
BoundScope *_scope;
shared_ptr<FunctionScriptType> _currentFunction;
~Binder();
~Binder();
BoundStatement *BindStatement(const ParsedStatement *statement);
BoundStatement *BindBlockStatement(const ParsedStatement *statement);
BoundStatement *BindExpressionStatement(const ParsedStatement *statement);
BoundStatement *BindAssignmentStatement(const ParsedStatement *statement);
BoundStatement *BindIndexAssignmentStatement(const ParsedStatement *statement);
BoundStatement *BindFunctionDeclarationStatement(const ParsedStatement * statement);
BoundStatement *BindReturnStatement(const ParsedStatement *statement);
BoundStatement *BindConditionalStatement(const ParsedStatement *statement);
BoundStatement *BindStatement(const ParsedStatement *statement);
BoundExpression *BindExpression(const ParsedExpression *expression);
BoundExpression *BindVariableExpression(const VariableExpression *expression);
BoundExpression *BindBinaryOperator(const BinaryExpression *expression);
BoundExpression *BindUnaryOperator(const UnaryExpression *expression);
BoundExpression *BindFunctionCall(const FunctionCallExpression *expression);
BoundExpression *BindIndexExpression(const IndexExpression *expression, bool setter);
BoundExpression *BindNumericalTableExpression(const ParsedNumericalTableExpression *expression);
BoundExpression *BindTableExpression(const ParsedTableExpression * expression);
public:
static BoundScriptStatement* Bind(Script* script, const ParsedScriptStatement* s, BoundScope* scriptScope);
BoundStatement *BindBlockStatement(const ParsedStatement *statement);
BoundExpression *BindPeriodIndexExpression(const PeriodIndexExpression *expression, bool setter);
};
BoundStatement *BindExpressionStatement(const ParsedStatement *statement);
BoundStatement *BindAssignmentStatement(const ParsedStatement *statement);
BoundStatement *BindIndexAssignmentStatement(const ParsedStatement *statement);
BoundStatement *BindFunctionDeclarationStatement(const ParsedStatement *statement);
BoundStatement *BindReturnStatement(const ParsedStatement *statement);
BoundStatement *BindConditionalStatement(const ParsedStatement *statement);
BoundExpression *BindExpression(const ParsedExpression *expression);
BoundExpression *BindVariableExpression(const VariableExpression *expression);
BoundExpression *BindBinaryOperator(const BinaryExpression *expression);
BoundExpression *BindUnaryOperator(const UnaryExpression *expression);
BoundExpression *BindFunctionCall(const FunctionCallExpression *expression);
BoundExpression *BindIndexExpression(const IndexExpression *expression, bool setter);
BoundExpression *BindNumericalTableExpression(const ParsedNumericalTableExpression *expression);
BoundExpression *BindTableExpression(const ParsedTableExpression *expression);
public:
static BoundScriptStatement *
Bind(Porygon::Script *script, const ParsedScriptStatement *s, BoundScope *scriptScope);
BoundExpression *BindPeriodIndexExpression(const PeriodIndexExpression *expression, bool setter);
};
}
#endif //PORYGONLANG_BINDER_HPP

View File

@@ -10,322 +10,330 @@
using namespace std;
enum class BoundExpressionKind{
Bad,
namespace Porygon::Binder {
enum class BoundExpressionKind {
Bad,
LiteralInteger,
LiteralFloat,
LiteralString,
LiteralBool,
Variable,
LiteralInteger,
LiteralFloat,
LiteralString,
LiteralBool,
Variable,
Unary,
Binary,
FunctionCall,
Index,
PeriodIndex,
NumericalTable,
Table,
};
class BoundExpression{
const unsigned int _start;
const unsigned int _length;
const shared_ptr<ScriptType> _type;
public:
BoundExpression(unsigned int start, unsigned int length, shared_ptr<ScriptType> type)
: _start(start),
_length(length),
_type(std::move(type))
{
}
virtual ~BoundExpression() = default;
virtual const BoundExpressionKind GetKind() const = 0;
virtual const std::shared_ptr<ScriptType>& GetType() const{
return _type;
Unary,
Binary,
FunctionCall,
Index,
PeriodIndex,
NumericalTable,
Table,
};
const unsigned int GetStartPosition() const{
return _start;
}
const unsigned int GetLength() const{
return _length;
}
const unsigned int GetEndPosition() const{
return _start + _length - 1;
}
};
class BoundBadExpression : public BoundExpression{
public:
BoundBadExpression(unsigned int start, unsigned int length) : BoundExpression(start, length, make_shared<ScriptType>(TypeClass::Error)){}
const BoundExpressionKind GetKind() const final{
return BoundExpressionKind ::Bad;
}
};
class BoundLiteralIntegerExpression : public BoundExpression{
const long _value;
public:
BoundLiteralIntegerExpression(long value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<NumericScriptType>(true, false)),
_value(value)
{
}
const BoundExpressionKind GetKind() const final{
return BoundExpressionKind ::LiteralInteger;
}
const long GetValue() const{
return _value;
}
};
class BoundLiteralFloatExpression : public BoundExpression{
const double _value;
public:
BoundLiteralFloatExpression(double value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<NumericScriptType>(true, true)),
_value(value)
{
}
const BoundExpressionKind GetKind() const final{
return BoundExpressionKind ::LiteralFloat;
}
const double GetValue() const{
return _value;
}
};
class BoundLiteralStringExpression : public BoundExpression{
const u16string _value;
public:
BoundLiteralStringExpression(const u16string& value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<StringScriptType>(true, HashedString::ConstHash(value.c_str()))),
_value(value)
{
}
const BoundExpressionKind GetKind() const final{
return BoundExpressionKind ::LiteralString;
}
const u16string GetValue() const{
return _value;
}
};
class BoundLiteralBoolExpression : public BoundExpression{
const bool _value;
public:
BoundLiteralBoolExpression(bool value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<ScriptType>(TypeClass::Bool)),
_value(value)
{
}
const BoundExpressionKind GetKind() const final{
return BoundExpressionKind ::LiteralBool;
}
const bool GetValue() const{
return _value;
}
};
class BoundVariableExpression : public BoundExpression{
const BoundVariableKey* _key;
public:
BoundVariableExpression(BoundVariableKey* key, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(type)),
_key(key)
{
}
~BoundVariableExpression() override{
delete _key;
}
const BoundExpressionKind GetKind() const final{
return BoundExpressionKind ::Variable;
}
const BoundVariableKey* GetKey() const{
return _key;
}
};
class BoundBinaryExpression : public BoundExpression {
const BoundExpression* _left;
const BoundExpression* _right;
const BoundBinaryOperation _operation;
public:
BoundBinaryExpression(BoundExpression* left, BoundExpression* right, BoundBinaryOperation op, shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)),
_left(left),
_right(right),
_operation(op)
{
}
~BoundBinaryExpression() final{
delete _left;
delete _right;
}
const BoundExpressionKind GetKind() const final{
return BoundExpressionKind ::Binary;
}
const BoundExpression* GetLeft() const{
return _left;
}
const BoundExpression* GetRight() const{
return _right;
}
const BoundBinaryOperation GetOperation() const{
return _operation;
}
};
class BoundUnaryExpression : public BoundExpression {
const BoundExpression* _operand;
const BoundUnaryOperation _operation;
public:
BoundUnaryExpression(BoundExpression* operand, BoundUnaryOperation op, shared_ptr<ScriptType> result, unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)),
_operand(operand),
_operation(op)
{
}
~BoundUnaryExpression() final{
delete _operand;
}
const BoundExpressionKind GetKind() const final{
return BoundExpressionKind ::Unary;
}
const BoundExpression* GetOperand() const{
return _operand;
}
const BoundUnaryOperation GetOperation() const{
return _operation;
}
};
class BoundFunctionCallExpression : public BoundExpression {
const BoundExpression* _functionExpression;
const vector<BoundExpression*> _parameters;
public:
BoundFunctionCallExpression(BoundExpression *functionExpression, vector<BoundExpression *> parameters, shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)), _functionExpression(functionExpression), _parameters(std::move(parameters)) {}
~BoundFunctionCallExpression() final{
delete _functionExpression;
for (auto p : _parameters){
delete p;
class BoundExpression {
const unsigned int _start;
const unsigned int _length;
const shared_ptr<ScriptType> _type;
public:
BoundExpression(unsigned int start, unsigned int length, shared_ptr<ScriptType> type)
: _start(start),
_length(length),
_type(std::move(type)) {
}
}
const BoundExpressionKind GetKind() const final{
return BoundExpressionKind ::FunctionCall;
}
virtual ~BoundExpression() = default;
const BoundExpression* GetFunctionExpression() const{
return _functionExpression;
}
virtual const BoundExpressionKind GetKind() const = 0;
const vector<BoundExpression*>* GetParameters() const{
return &_parameters;
}
};
virtual const std::shared_ptr<ScriptType> &GetType() const {
return _type;
};
class BoundIndexExpression : public BoundExpression {
const BoundExpression* _indexableExpression;
const BoundExpression* _indexExpression;
public:
BoundIndexExpression(BoundExpression* indexableExpression, BoundExpression* indexExpression, shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression), _indexExpression(indexExpression) {}
~BoundIndexExpression() final{
delete _indexableExpression;
delete _indexExpression;
}
const BoundExpressionKind GetKind() const final{
return BoundExpressionKind ::Index;
}
const BoundExpression* GetIndexableExpression() const{
return _indexableExpression;
}
const BoundExpression* GetIndexExpression() const{
return _indexExpression;
}
};
class BoundPeriodIndexExpression : public BoundExpression {
const BoundExpression* _indexableExpression;
const HashedString _index;
public:
BoundPeriodIndexExpression(BoundExpression* indexableExpression, HashedString index, shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression), _index(index) {}
~BoundPeriodIndexExpression() final{
delete _indexableExpression;
}
const BoundExpressionKind GetKind() const final{
return BoundExpressionKind ::PeriodIndex;
}
const BoundExpression* GetIndexableExpression() const{
return _indexableExpression;
}
const HashedString GetIndex() const{
return _index;
}
};
class BoundNumericalTableExpression : public BoundExpression{
const vector<const BoundExpression*> _expressions;
public:
BoundNumericalTableExpression(vector<const BoundExpression*> expressions, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(type)),
_expressions(std::move(expressions))
{}
~BoundNumericalTableExpression() final{
for (auto e: _expressions){
delete e;
const unsigned int GetStartPosition() const {
return _start;
}
}
const BoundExpressionKind GetKind() const final{
return BoundExpressionKind ::NumericalTable;
}
const unsigned int GetLength() const {
return _length;
}
const vector<const BoundExpression*>* GetExpressions() const{
return &_expressions;
}
};
const unsigned int GetEndPosition() const {
return _start + _length - 1;
}
};
class BoundBadExpression : public BoundExpression {
public:
BoundBadExpression(unsigned int start, unsigned int length) : BoundExpression(start, length,
make_shared<ScriptType>(
TypeClass::Error)) {}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind::Bad;
}
};
class BoundLiteralIntegerExpression : public BoundExpression {
const long _value;
public:
BoundLiteralIntegerExpression(long value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<NumericScriptType>(true, false)),
_value(value) {
}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind::LiteralInteger;
}
const long GetValue() const {
return _value;
}
};
class BoundLiteralFloatExpression : public BoundExpression {
const double _value;
public:
BoundLiteralFloatExpression(double value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<NumericScriptType>(true, true)),
_value(value) {
}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind::LiteralFloat;
}
const double GetValue() const {
return _value;
}
};
class BoundLiteralStringExpression : public BoundExpression {
const u16string _value;
public:
BoundLiteralStringExpression(const u16string &value, unsigned int start, unsigned int length)
: BoundExpression(start, length,
make_shared<StringScriptType>(true, Utilities::HashedString::ConstHash(value.c_str()))),
_value(value) {
}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind::LiteralString;
}
const u16string GetValue() const {
return _value;
}
};
class BoundLiteralBoolExpression : public BoundExpression {
const bool _value;
public:
BoundLiteralBoolExpression(bool value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<ScriptType>(TypeClass::Bool)),
_value(value) {
}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind::LiteralBool;
}
const bool GetValue() const {
return _value;
}
};
class BoundVariableExpression : public BoundExpression {
const BoundVariableKey *_key;
public:
BoundVariableExpression(BoundVariableKey *key, shared_ptr<ScriptType> type, unsigned int start,
unsigned int length)
: BoundExpression(start, length, std::move(type)),
_key(key) {
}
~BoundVariableExpression() override {
delete _key;
}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind::Variable;
}
const BoundVariableKey *GetKey() const {
return _key;
}
};
class BoundBinaryExpression : public BoundExpression {
const BoundExpression *_left;
const BoundExpression *_right;
const BoundBinaryOperation _operation;
public:
BoundBinaryExpression(BoundExpression *left, BoundExpression *right, BoundBinaryOperation op,
shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)),
_left(left),
_right(right),
_operation(op) {
}
~BoundBinaryExpression() final {
delete _left;
delete _right;
}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind::Binary;
}
const BoundExpression *GetLeft() const {
return _left;
}
const BoundExpression *GetRight() const {
return _right;
}
const BoundBinaryOperation GetOperation() const {
return _operation;
}
};
class BoundUnaryExpression : public BoundExpression {
const BoundExpression *_operand;
const BoundUnaryOperation _operation;
public:
BoundUnaryExpression(BoundExpression *operand, BoundUnaryOperation op, shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)),
_operand(operand),
_operation(op) {
}
~BoundUnaryExpression() final {
delete _operand;
}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind::Unary;
}
const BoundExpression *GetOperand() const {
return _operand;
}
const BoundUnaryOperation GetOperation() const {
return _operation;
}
};
class BoundFunctionCallExpression : public BoundExpression {
const BoundExpression *_functionExpression;
const vector<BoundExpression *> _parameters;
public:
BoundFunctionCallExpression(BoundExpression *functionExpression, vector<BoundExpression *> parameters,
shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)), _functionExpression(functionExpression),
_parameters(std::move(parameters)) {}
~BoundFunctionCallExpression() final {
delete _functionExpression;
for (auto p : _parameters) {
delete p;
}
}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind::FunctionCall;
}
const BoundExpression *GetFunctionExpression() const {
return _functionExpression;
}
const vector<BoundExpression *> *GetParameters() const {
return &_parameters;
}
};
class BoundIndexExpression : public BoundExpression {
const BoundExpression *_indexableExpression;
const BoundExpression *_indexExpression;
public:
BoundIndexExpression(BoundExpression *indexableExpression, BoundExpression *indexExpression,
shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression),
_indexExpression(indexExpression) {}
~BoundIndexExpression() final {
delete _indexableExpression;
delete _indexExpression;
}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind::Index;
}
const BoundExpression *GetIndexableExpression() const {
return _indexableExpression;
}
const BoundExpression *GetIndexExpression() const {
return _indexExpression;
}
};
class BoundPeriodIndexExpression : public BoundExpression {
const BoundExpression *_indexableExpression;
const Utilities::HashedString _index;
public:
BoundPeriodIndexExpression(BoundExpression *indexableExpression, Utilities::HashedString index,
shared_ptr<ScriptType> result,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(result)), _indexableExpression(indexableExpression),
_index(index) {}
~BoundPeriodIndexExpression() final {
delete _indexableExpression;
}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind::PeriodIndex;
}
const BoundExpression *GetIndexableExpression() const {
return _indexableExpression;
}
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,
unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(type)),
_expressions(std::move(expressions)) {}
~BoundNumericalTableExpression() final {
for (auto e: _expressions) {
delete e;
}
}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind::NumericalTable;
}
const vector<const BoundExpression *> *GetExpressions() const {
return &_expressions;
}
};
}
#endif //PORYGONLANG_BOUNDEXPRESSION_HPP

View File

@@ -6,26 +6,29 @@
#include "../BoundStatements/BoundStatement.hpp"
class BoundTableExpression : public BoundExpression{
const BoundBlockStatement* _block;
public:
BoundTableExpression(BoundBlockStatement* block, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(type)){
_block = block;
}
namespace Porygon::Binder {
class BoundTableExpression : public BoundExpression {
const BoundBlockStatement *_block;
public:
BoundTableExpression(BoundBlockStatement *block, shared_ptr<ScriptType> type, unsigned int start,
unsigned int length)
: BoundExpression(start, length, std::move(type)) {
_block = block;
}
~BoundTableExpression() final{
delete _block;
}
~BoundTableExpression() final {
delete _block;
}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind ::Table;
}
const BoundExpressionKind GetKind() const final {
return BoundExpressionKind::Table;
}
const BoundBlockStatement* GetBlock() const {
return _block;
}
};
const BoundBlockStatement *GetBlock() const {
return _block;
}
};
}
#include "BoundExpression.hpp"

View File

@@ -2,26 +2,28 @@
#ifndef PORYGONLANG_BOUNDOPERATORS_HPP
#define PORYGONLANG_BOUNDOPERATORS_HPP
enum class BoundBinaryOperation{
Addition,
Subtraction,
Multiplication,
Division,
Equality,
Inequality,
LessThan,
LessThanEquals,
GreaterThan,
GreaterThanEquals,
namespace Porygon::Binder {
enum class BoundBinaryOperation {
Addition,
Subtraction,
Multiplication,
Division,
Equality,
Inequality,
LessThan,
LessThanEquals,
GreaterThan,
GreaterThanEquals,
LogicalAnd,
LogicalOr,
Concatenation
};
LogicalAnd,
LogicalOr,
Concatenation
};
enum class BoundUnaryOperation{
Negation,
LogicalNegation,
};
enum class BoundUnaryOperation {
Negation,
LogicalNegation,
};
}
#endif //PORYGONLANG_BOUNDOPERATORS_HPP

View File

@@ -7,36 +7,38 @@
#include <memory>
#include "BoundStatement.hpp"
class BoundFunctionDeclarationStatement : public BoundStatement{
const BoundVariableKey* _key;
const std::shared_ptr<BoundBlockStatement> _block;
const std::shared_ptr<FunctionScriptType> _type;
public:
BoundFunctionDeclarationStatement(std::shared_ptr<FunctionScriptType> type, BoundVariableKey* key, BoundBlockStatement* block)
:_key(key), _block(block), _type(std::move(type))
{
}
namespace Porygon::Binder {
class BoundFunctionDeclarationStatement : public BoundStatement {
const BoundVariableKey *_key;
const std::shared_ptr<BoundBlockStatement> _block;
const std::shared_ptr<FunctionScriptType> _type;
public:
BoundFunctionDeclarationStatement(std::shared_ptr<FunctionScriptType> type, BoundVariableKey *key,
BoundBlockStatement *block)
: _key(key), _block(block), _type(std::move(type)) {
}
~BoundFunctionDeclarationStatement() final{
delete _key;
}
~BoundFunctionDeclarationStatement() final {
delete _key;
}
const BoundStatementKind GetKind() const final{
return BoundStatementKind ::FunctionDeclaration;
}
const BoundStatementKind GetKind() const final {
return BoundStatementKind::FunctionDeclaration;
}
const BoundVariableKey* GetKey() const{
return _key;
}
const BoundVariableKey *GetKey() const {
return _key;
}
const std::shared_ptr<BoundBlockStatement> GetBlock() const{
return _block;
}
const std::shared_ptr<BoundBlockStatement> GetBlock() const {
return _block;
}
const std::shared_ptr<FunctionScriptType> GetType() const{
return _type;
}
};
const std::shared_ptr<FunctionScriptType> GetType() const {
return _type;
}
};
}
#include "BoundStatement.hpp"

View File

@@ -12,198 +12,197 @@
using namespace std;
enum class BoundStatementKind{
Bad,
Script,
Block,
Expression,
Assignment,
IndexAssignment,
FunctionDeclaration,
Return,
Conditional,
};
namespace Porygon::Binder {
enum class BoundStatementKind {
Bad,
Script,
Block,
Expression,
Assignment,
IndexAssignment,
FunctionDeclaration,
Return,
Conditional,
};
class BoundStatement{
public:
virtual const BoundStatementKind GetKind() const = 0;
virtual ~BoundStatement() = default;
};
class BoundStatement {
public:
virtual const BoundStatementKind GetKind() const = 0;
class BoundBadStatement : public BoundStatement{
public:
const BoundStatementKind GetKind() const final{
return BoundStatementKind ::Bad;
}
};
virtual ~BoundStatement() = default;
};
class BoundBlockStatement : public BoundStatement{
const vector<BoundStatement*> _statements;
public:
explicit BoundBlockStatement(vector<BoundStatement*> statements)
: _statements(std::move(statements))
{
}
~BoundBlockStatement() override {
for (auto s : _statements){
delete s;
class BoundBadStatement : public BoundStatement {
public:
const BoundStatementKind GetKind() const final {
return BoundStatementKind::Bad;
}
}
};
const BoundStatementKind GetKind() const override {
return BoundStatementKind ::Block;
}
class BoundBlockStatement : public BoundStatement {
const vector<BoundStatement *> _statements;
public:
explicit BoundBlockStatement(vector<BoundStatement *> statements)
: _statements(std::move(statements)) {
}
const vector<BoundStatement*>* GetStatements() const{
return &_statements;
}
};
~BoundBlockStatement() override {
for (auto s : _statements) {
delete s;
}
}
class BoundScriptStatement : public BoundBlockStatement{
const int _localVariableCount;
public:
explicit BoundScriptStatement(vector<BoundStatement*> statements, int localVariableCount)
: BoundBlockStatement(std::move(statements)),
_localVariableCount(localVariableCount)
{
}
const BoundStatementKind GetKind() const override {
return BoundStatementKind::Block;
}
const BoundStatementKind GetKind() const final{
return BoundStatementKind ::Script;
}
const vector<BoundStatement *> *GetStatements() const {
return &_statements;
}
};
const int GetLocalVariableCount() const{
return _localVariableCount;
}
};
class BoundScriptStatement : public BoundBlockStatement {
const int _localVariableCount;
public:
explicit BoundScriptStatement(vector<BoundStatement *> statements, int localVariableCount)
: BoundBlockStatement(std::move(statements)),
_localVariableCount(localVariableCount) {
}
class BoundExpressionStatement : public BoundStatement{
const BoundExpression* _expression;
public:
explicit BoundExpressionStatement(BoundExpression* expression)
: _expression(expression)
{
_expression = expression;
}
~BoundExpressionStatement() final{
delete _expression;
}
const BoundStatementKind GetKind() const final {
return BoundStatementKind::Script;
}
const BoundStatementKind GetKind() const final{
return BoundStatementKind ::Expression;
}
const int GetLocalVariableCount() const {
return _localVariableCount;
}
};
const BoundExpression* GetExpression() const{
return _expression;
}
};
class BoundExpressionStatement : public BoundStatement {
const BoundExpression *_expression;
public:
explicit BoundExpressionStatement(BoundExpression *expression)
: _expression(expression) {
_expression = expression;
}
class BoundAssignmentStatement : public BoundStatement{
const BoundVariableKey* _key;
const BoundExpression* _expression;
public:
BoundAssignmentStatement(BoundVariableKey* key, BoundExpression* expression)
: _key(key), _expression(expression)
{
}
~BoundExpressionStatement() final {
delete _expression;
}
~BoundAssignmentStatement() final{
delete _key;
delete _expression;
}
const BoundStatementKind GetKind() const final {
return BoundStatementKind::Expression;
}
const BoundStatementKind GetKind() const final{
return BoundStatementKind ::Assignment;
}
const BoundExpression *GetExpression() const {
return _expression;
}
};
const BoundVariableKey* GetKey() const {
return _key;
}
class BoundAssignmentStatement : public BoundStatement {
const BoundVariableKey *_key;
const BoundExpression *_expression;
public:
BoundAssignmentStatement(BoundVariableKey *key, BoundExpression *expression)
: _key(key), _expression(expression) {
}
const BoundExpression* GetExpression() const {
return _expression;
}
};
~BoundAssignmentStatement() final {
delete _key;
delete _expression;
}
class BoundIndexAssignmentStatement : public BoundStatement{
const BoundExpression* _indexExpression;
const BoundExpression* _valueExpression;
public:
BoundIndexAssignmentStatement(const BoundExpression* index, BoundExpression* value)
: _indexExpression(index), _valueExpression(value)
{
}
const BoundStatementKind GetKind() const final {
return BoundStatementKind::Assignment;
}
~BoundIndexAssignmentStatement() final{
delete _indexExpression;
delete _valueExpression;
}
const BoundVariableKey *GetKey() const {
return _key;
}
const BoundStatementKind GetKind() const final{
return BoundStatementKind ::IndexAssignment;
}
const BoundExpression *GetExpression() const {
return _expression;
}
};
const BoundExpression* GetIndexExpression() const {
return _indexExpression;
}
class BoundIndexAssignmentStatement : public BoundStatement {
const BoundExpression *_indexExpression;
const BoundExpression *_valueExpression;
public:
BoundIndexAssignmentStatement(const BoundExpression *index, BoundExpression *value)
: _indexExpression(index), _valueExpression(value) {
}
const BoundExpression* GetValueExpression() const {
return _valueExpression;
}
};
~BoundIndexAssignmentStatement() final {
delete _indexExpression;
delete _valueExpression;
}
class BoundReturnStatement : public BoundStatement{
const BoundExpression* _expression;
public:
explicit BoundReturnStatement(BoundExpression* expression)
: _expression(expression)
{
}
~BoundReturnStatement() final{
delete _expression;
}
const BoundStatementKind GetKind() const final {
return BoundStatementKind::IndexAssignment;
}
const BoundStatementKind GetKind() const final{
return BoundStatementKind ::Return;
}
const BoundExpression *GetIndexExpression() const {
return _indexExpression;
}
const BoundExpression* GetExpression() const{
return _expression;
}
};
const BoundExpression *GetValueExpression() const {
return _valueExpression;
}
};
class BoundConditionalStatement : public BoundStatement{
const BoundExpression* _condition;
const BoundStatement* _block;
const BoundStatement* _elseStatement;
public:
explicit BoundConditionalStatement(BoundExpression* condition, BoundStatement* block, BoundStatement* next)
:_condition(condition), _block(block), _elseStatement(next)
{
}
class BoundReturnStatement : public BoundStatement {
const BoundExpression *_expression;
public:
explicit BoundReturnStatement(BoundExpression *expression)
: _expression(expression) {
}
~BoundConditionalStatement() final{
delete _condition;
delete _block;
delete _elseStatement;
}
~BoundReturnStatement() final {
delete _expression;
}
const BoundStatementKind GetKind() const final{
return BoundStatementKind ::Conditional;
}
const BoundStatementKind GetKind() const final {
return BoundStatementKind::Return;
}
const BoundExpression* GetCondition() const{
return _condition;
}
const BoundExpression *GetExpression() const {
return _expression;
}
};
const BoundStatement* GetBlock() const{
return _block;
}
class BoundConditionalStatement : public BoundStatement {
const BoundExpression *_condition;
const BoundStatement *_block;
const BoundStatement *_elseStatement;
public:
explicit BoundConditionalStatement(BoundExpression *condition, BoundStatement *block, BoundStatement *next)
: _condition(condition), _block(block), _elseStatement(next) {
}
const BoundStatement* GetElseStatement() const{
return _elseStatement;
}
};
~BoundConditionalStatement() final {
delete _condition;
delete _block;
delete _elseStatement;
}
const BoundStatementKind GetKind() const final {
return BoundStatementKind::Conditional;
}
const BoundExpression *GetCondition() const {
return _condition;
}
const BoundStatement *GetBlock() const {
return _block;
}
const BoundStatement *GetElseStatement() const {
return _elseStatement;
}
};
}
#include "BoundFunctionDeclarationStatement.hpp"

View File

@@ -3,95 +3,98 @@
#include "BoundScope.hpp"
BoundScope::BoundScope(unordered_map<uint32_t, BoundVariable *> *tableScope) {
_tableScope = tableScope;
_currentScope = 1;
_lastCreatedScope = 1;
auto localUpmostScope = new unordered_map<uint32_t, BoundVariable*>();
_localScope.push_back(localUpmostScope);
}
namespace Porygon::Binder {
BoundScope::BoundScope(unordered_map<uint32_t, BoundVariable *> *tableScope) {
_tableScope = tableScope;
_currentScope = 1;
_lastCreatedScope = 1;
auto localUpmostScope = new unordered_map<uint32_t, BoundVariable *>();
_localScope.push_back(localUpmostScope);
}
BoundScope::~BoundScope() {
for (auto scope : _localScope){
for (auto v : *scope){
BoundScope::~BoundScope() {
for (auto scope : _localScope) {
for (auto v : *scope) {
delete v.second;
}
delete scope;
}
}
void BoundScope::GoInnerScope() {
_lastCreatedScope++;
_currentScope = _lastCreatedScope;
if (_localScope.size() < _currentScope) {
auto innerScope = new unordered_map<uint32_t, BoundVariable *>();
_localScope.push_back(innerScope);
}
}
void BoundScope::GoOuterScope() {
auto scope = _localScope[_currentScope - 1];
for (auto v : *scope) {
delete v.second;
}
delete scope;
scope->clear();
_currentScope--;
}
}
void BoundScope::GoInnerScope() {
_lastCreatedScope++;
_currentScope = _lastCreatedScope;
if (_localScope.size() < _currentScope){
auto innerScope = new unordered_map<uint32_t, BoundVariable*>();
_localScope.push_back(innerScope);
int BoundScope::Exists(int key) {
auto found = this->_tableScope->find(key);
if (found != _tableScope->end()) {
return 0;
}
for (int i = _currentScope - 1; i >= 0; i--) {
auto scope = _localScope.at(i);
found = scope->find(key);
if (found != scope->end()) {
return i + 1;
}
}
return -1;
}
}
void BoundScope::GoOuterScope() {
auto scope = _localScope[_currentScope - 1];
for (auto v : *scope){
delete v.second;
}
scope->clear();
_currentScope--;
}
int BoundScope::Exists(int key) {
auto found = this -> _tableScope -> find(key);
if (found != _tableScope -> end()){
return 0;
}
for (int i = _currentScope - 1; i >= 0; i--){
auto scope = _localScope.at(i);
found = scope -> find(key);
if (found != scope -> end()){
return i + 1;
BoundVariable *BoundScope::GetVariable(uint32_t scope, uint32_t identifier) {
if (scope == 0) {
auto find = this->_tableScope->find(identifier);
if (find != _tableScope->end()) {
return find->second;
}
return nullptr;
} else {
auto s = this->_localScope.at(scope - 1);
auto find = s->find(identifier);
if (find != s->end()) {
return find->second;
}
return nullptr;
}
}
return -1;
}
BoundVariable *BoundScope::GetVariable(uint32_t scope, uint32_t identifier) {
if (scope == 0){
auto find = this -> _tableScope->find(identifier);
if (find != _tableScope->end()){
return find -> second;
VariableAssignment BoundScope::CreateExplicitLocal(uint32_t identifier, std::shared_ptr<ScriptType> type) {
auto scope = this->_localScope.at(this->_currentScope - 1);
if (scope->find(identifier) != scope->end()) {
return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr);
}
return nullptr;
} else{
auto s = this->_localScope.at(scope - 1);
auto find = s -> find(identifier);
if (find != s -> end()){
return find -> second;
}
return nullptr;
scope->insert({identifier, new BoundVariable(std::move(type))});
return VariableAssignment(VariableAssignmentResult::Ok,
new BoundVariableKey(identifier, this->_currentScope, true));
}
}
VariableAssignment BoundScope::CreateExplicitLocal(uint32_t identifier, std::shared_ptr<ScriptType> type) {
auto scope = this->_localScope.at(this->_currentScope - 1);
if (scope -> find(identifier) != scope -> end()){
return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr);
}
scope -> insert({identifier, new BoundVariable(std::move(type))});
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, this->_currentScope, true));
}
VariableAssignment BoundScope::AssignVariable(uint32_t identifier, const std::shared_ptr<ScriptType>& type) {
int exists = this->Exists(identifier);
if (exists == -1){
// Creation
_tableScope->insert({identifier, new BoundVariable(type)});
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, 0, true));
} else{
// Assigning
auto var = this->GetVariable(exists, identifier);
if (var->GetType().get()->operator!=(type.get())){
return VariableAssignment(VariableAssignmentResult::VariableDefinedWithDifferentType, nullptr);
VariableAssignment BoundScope::AssignVariable(uint32_t identifier, const std::shared_ptr<ScriptType> &type) {
int exists = this->Exists(identifier);
if (exists == -1) {
// Creation
_tableScope->insert({identifier, new BoundVariable(type)});
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, 0, true));
} else {
// Assigning
auto var = this->GetVariable(exists, identifier);
if (var->GetType().get()->operator!=(type.get())) {
return VariableAssignment(VariableAssignmentResult::VariableDefinedWithDifferentType, nullptr);
}
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, exists, false));
}
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, exists, false));
}
}

View File

@@ -13,31 +13,38 @@
using namespace std;
class BoundScope {
unordered_map<uint32_t, BoundVariable*>* _tableScope;
vector<unordered_map<uint32_t, BoundVariable*>*> _localScope;
int _currentScope;
int _lastCreatedScope;
public:
explicit BoundScope(unordered_map<uint32_t, BoundVariable*> *tableScope);
~BoundScope();
namespace Porygon::Binder {
class BoundScope {
unordered_map<uint32_t, BoundVariable *> *_tableScope;
vector<unordered_map<uint32_t, BoundVariable *> *> _localScope;
int _currentScope;
int _lastCreatedScope;
public:
explicit BoundScope(unordered_map<uint32_t, BoundVariable *> *tableScope);
void GoInnerScope();
void GoOuterScope();
~BoundScope();
int Exists(int key);
BoundVariable* GetVariable(uint32_t scope, uint32_t identifier);
VariableAssignment CreateExplicitLocal(uint32_t identifier, std::shared_ptr<ScriptType> type);
VariableAssignment AssignVariable(uint32_t identifier, const std::shared_ptr<ScriptType>& type);
void GoInnerScope();
size_t GetLocalVariableCount(){
return _localScope.size();
}
void GoOuterScope();
int GetCurrentScope(){
return _currentScope;
}
};
int Exists(int key);
BoundVariable *GetVariable(uint32_t scope, uint32_t identifier);
VariableAssignment CreateExplicitLocal(uint32_t identifier, std::shared_ptr<ScriptType> type);
VariableAssignment AssignVariable(uint32_t identifier, const std::shared_ptr<ScriptType> &type);
size_t GetLocalVariableCount() {
return _localScope.size();
}
int GetCurrentScope() {
return _currentScope;
}
};
}
#endif //PORYGONLANG_BOUNDSCOPE_HPP

View File

@@ -9,15 +9,17 @@
using namespace std;
class BoundVariable{
std::shared_ptr<ScriptType> _type;
public:
explicit BoundVariable(std::shared_ptr<ScriptType> type) : _type(std::move(type)){
}
namespace Porygon::Binder {
class BoundVariable {
std::shared_ptr<ScriptType> _type;
public:
explicit BoundVariable(std::shared_ptr<ScriptType> type) : _type(std::move(type)) {
}
std::shared_ptr<ScriptType> GetType(){
return _type;
}
};
std::shared_ptr<ScriptType> GetType() {
return _type;
}
};
}
#endif //PORYGONLANG_BOUNDVARIABLE_HPP

View File

@@ -4,42 +4,43 @@
#include <string>
class BoundVariableKey{
const int _identifier;
const unsigned int _scopeId;
const bool _isCreation;
const uint64_t _hash;
namespace Porygon::Binder {
class BoundVariableKey {
const int _identifier;
const unsigned int _scopeId;
const bool _isCreation;
const uint64_t _hash;
static uint64_t KnuthsHash(unsigned int i1, unsigned int i2)
{
uint64_t ret = i1;
ret *= 2654435761U;
return ret ^ i2;
}
public:
BoundVariableKey(int id, unsigned int scope, bool creation)
: _identifier(id),
_scopeId(scope),
_isCreation(creation),
_hash(KnuthsHash(id, scope))
{}
static uint64_t KnuthsHash(unsigned int i1, unsigned int i2) {
uint64_t ret = i1;
ret *= 2654435761U;
return ret ^ i2;
}
public:
BoundVariableKey(int id, unsigned int scope, bool creation)
: _identifier(id),
_scopeId(scope),
_isCreation(creation),
_hash(KnuthsHash(id, scope)) {}
const int GetIdentifier() const{
return _identifier;
}
const int GetIdentifier() const {
return _identifier;
}
const unsigned int GetScopeId() const{
return _scopeId;
}
const unsigned int GetScopeId() const {
return _scopeId;
}
const bool IsCreation() const{
return _isCreation;
}
const bool IsCreation() const {
return _isCreation;
}
const uint64_t GetHash() const{
return _hash;
}
};
const uint64_t GetHash() const {
return _hash;
}
};
}
#endif //PORYGONLANG_BOUNDVARIABLEKEY_HPP

View File

@@ -4,28 +4,30 @@
#include "BoundVariableKey.hpp"
enum class VariableAssignmentResult{
Ok,
ExplicitLocalVariableExists,
VariableDefinedWithDifferentType
};
namespace Porygon::Binder {
enum class VariableAssignmentResult {
Ok,
ExplicitLocalVariableExists,
VariableDefinedWithDifferentType
};
class VariableAssignment{
VariableAssignmentResult _result;
BoundVariableKey* _key;
public:
VariableAssignment(VariableAssignmentResult result, BoundVariableKey *key) {
_result = result;
_key = key;
}
class VariableAssignment {
VariableAssignmentResult _result;
BoundVariableKey *_key;
public:
VariableAssignment(VariableAssignmentResult result, BoundVariableKey *key) {
_result = result;
_key = key;
}
VariableAssignmentResult GetResult(){
return _result;
}
VariableAssignmentResult GetResult() {
return _result;
}
BoundVariableKey* GetKey(){
return _key;
}
};
BoundVariableKey *GetKey() {
return _key;
}
};
}
#endif //PORYGONLANG_VARIABLEASSIGMENTRESULT_HPP