Moved ScriptType classes into separate directory
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
1
src/ScriptTypes/FunctionScriptType.cpp
Normal file
1
src/ScriptTypes/FunctionScriptType.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include "FunctionScriptType.hpp"
|
||||
112
src/ScriptTypes/FunctionScriptType.hpp
Normal file
112
src/ScriptTypes/FunctionScriptType.hpp
Normal file
@@ -0,0 +1,112 @@
|
||||
|
||||
#ifndef PORYGONLANG_FUNCTIONSCRIPTTYPE_HPP
|
||||
#define PORYGONLANG_FUNCTIONSCRIPTTYPE_HPP
|
||||
|
||||
#include <utility>
|
||||
#include "ScriptType.hpp"
|
||||
|
||||
namespace Porygon {
|
||||
class GenericFunctionOption{
|
||||
shared_ptr<const ScriptType> _returnType;
|
||||
vector<shared_ptr<ScriptType>> _parameterTypes;
|
||||
size_t _option = 0;
|
||||
public:
|
||||
GenericFunctionOption(shared_ptr<ScriptType> returnType, vector<shared_ptr<ScriptType>> parameterTypes)
|
||||
: _returnType(std::move(returnType)), _parameterTypes(std::move(parameterTypes)){
|
||||
}
|
||||
|
||||
virtual ~GenericFunctionOption() = default;
|
||||
|
||||
[[nodiscard]] inline shared_ptr<const ScriptType> GetReturnType() const {
|
||||
return _returnType;
|
||||
}
|
||||
|
||||
inline void SetOption(size_t v){
|
||||
_option = v;
|
||||
}
|
||||
|
||||
inline void SetReturnType(const shared_ptr<const ScriptType>& t) {
|
||||
_returnType = t;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline vector<shared_ptr<ScriptType>> GetParameterTypes() const {
|
||||
return _parameterTypes;
|
||||
}
|
||||
|
||||
bool IsValid(const vector<shared_ptr<const ScriptType>>& parameters){
|
||||
if (parameters.size() != _parameterTypes.size()){
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < parameters.size(); i++){
|
||||
if (parameters[i]->operator!=(_parameterTypes[i].get())){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline size_t GetOptionId() const{
|
||||
return _option;
|
||||
}
|
||||
|
||||
[[nodiscard]] virtual bool IsScriptFunction() const = 0;
|
||||
};
|
||||
|
||||
class GenericFunctionScriptType : public Porygon::ScriptType {
|
||||
vector<GenericFunctionOption *>* _options = new vector<GenericFunctionOption *>;
|
||||
public:
|
||||
GenericFunctionScriptType()
|
||||
: ScriptType(Porygon::TypeClass::Function){};
|
||||
|
||||
explicit GenericFunctionScriptType(GenericFunctionOption * option)
|
||||
: ScriptType(Porygon::TypeClass::Function){
|
||||
this -> RegisterFunctionOption(option);
|
||||
};
|
||||
|
||||
~GenericFunctionScriptType() final{
|
||||
for (auto o: *_options){
|
||||
delete o;
|
||||
}
|
||||
delete _options;
|
||||
}
|
||||
|
||||
void RegisterFunctionOption (GenericFunctionOption * opt) const{
|
||||
opt->SetOption(_options->size());
|
||||
_options->push_back(opt);
|
||||
}
|
||||
|
||||
GenericFunctionOption* GetFunctionOption(const vector<shared_ptr<const ScriptType>>& parameters) const{
|
||||
for (auto o: *_options){
|
||||
if (o->IsValid(parameters)){
|
||||
return o;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline GenericFunctionOption* GetFirstOption() const{
|
||||
return _options->at(0);
|
||||
}
|
||||
};
|
||||
|
||||
class ScriptFunctionOption : public GenericFunctionOption {
|
||||
vector<shared_ptr<const Porygon::Binder::BoundVariableKey>> _parameterKeys;
|
||||
public:
|
||||
ScriptFunctionOption(shared_ptr<ScriptType> returnType, vector<shared_ptr<ScriptType>> parameterTypes,
|
||||
vector<shared_ptr<const Porygon::Binder::BoundVariableKey>> parameterKeys)
|
||||
: GenericFunctionOption(move(returnType), std::move(parameterTypes)), _parameterKeys(move(parameterKeys)) {
|
||||
}
|
||||
|
||||
[[nodiscard]] inline vector<shared_ptr<const Porygon::Binder::BoundVariableKey>> GetParameterKeys() const {
|
||||
return _parameterKeys;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool IsScriptFunction() const final {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#include "ScriptType.hpp"
|
||||
|
||||
#endif //PORYGONLANG_FUNCTIONSCRIPTTYPE_HPP
|
||||
41
src/ScriptTypes/ScriptType.cpp
Normal file
41
src/ScriptTypes/ScriptType.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "../Script.hpp"
|
||||
#include "../UserData/UserDataFunctionType.hpp"
|
||||
|
||||
namespace Porygon{
|
||||
inline bool ScriptType::CanBeIndexedWith(const ScriptType *) const{
|
||||
return false;
|
||||
}
|
||||
|
||||
shared_ptr<const ScriptType> ScriptType::GetIndexedType(const ScriptType*) const{
|
||||
if (_class == TypeClass::String){
|
||||
return make_shared<ScriptType>(TypeClass::String);
|
||||
}
|
||||
return make_shared<ScriptType>(TypeClass::Error);
|
||||
}
|
||||
|
||||
extern "C"{
|
||||
ScriptType* CreateScriptType(Porygon::TypeClass t){
|
||||
return new ScriptType(t);
|
||||
}
|
||||
|
||||
ScriptType* CreateNumericScriptType(bool isAware, bool isFloat){
|
||||
return new NumericScriptType(isAware, isFloat);
|
||||
}
|
||||
|
||||
ScriptType* CreateStringScriptType(bool knownAtBind, uint32_t hash){
|
||||
return new StringScriptType(knownAtBind, hash);
|
||||
}
|
||||
|
||||
ScriptType* CreateUserDataFunctionScriptType(ScriptType* returnType, ScriptType* parameters[], size_t parameterCount){
|
||||
vector<shared_ptr<ScriptType>> vector(parameterCount);
|
||||
for (int i = 0; i < parameterCount; i++){
|
||||
vector[i] = shared_ptr<ScriptType>(parameters[i]);
|
||||
}
|
||||
auto option = new UserData::UserDataFunctionOption(shared_ptr<ScriptType>(returnType), vector);
|
||||
auto type = new GenericFunctionScriptType();
|
||||
type->RegisterFunctionOption(option);
|
||||
return type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
161
src/ScriptTypes/ScriptType.hpp
Normal file
161
src/ScriptTypes/ScriptType.hpp
Normal file
@@ -0,0 +1,161 @@
|
||||
#include <utility>
|
||||
|
||||
|
||||
#ifndef PORYGONLANG_SCRIPTTYPE_HPP
|
||||
#define PORYGONLANG_SCRIPTTYPE_HPP
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "../Binder/BoundVariables/BoundVariableKey.hpp"
|
||||
#include "../Utilities/HashedString.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace Porygon{
|
||||
enum class TypeClass{
|
||||
Error,
|
||||
Nil,
|
||||
Number,
|
||||
Bool,
|
||||
String,
|
||||
Function,
|
||||
UserData,
|
||||
Table,
|
||||
};
|
||||
|
||||
class ScriptType{
|
||||
TypeClass _class;
|
||||
public:
|
||||
explicit ScriptType(TypeClass c){
|
||||
_class = c;
|
||||
}
|
||||
|
||||
virtual ~ScriptType() = default;
|
||||
|
||||
const TypeClass GetClass() const{
|
||||
return _class;
|
||||
}
|
||||
|
||||
virtual bool operator ==(const ScriptType& b) const{
|
||||
return _class == b._class;
|
||||
};
|
||||
|
||||
virtual bool operator ==(const ScriptType* b) const{
|
||||
return _class == b->_class;
|
||||
};
|
||||
|
||||
virtual bool operator !=(const ScriptType& b) const{
|
||||
return ! (operator==(b));
|
||||
}
|
||||
virtual bool operator !=(const ScriptType* b) const{
|
||||
return ! (operator==(b));
|
||||
}
|
||||
|
||||
virtual bool CanBeIndexedWith(const ScriptType* indexer) const;
|
||||
[[nodiscard]]
|
||||
virtual bool CanBeIndexedWithIdentifier(uint32_t hash) const{
|
||||
return false;
|
||||
}
|
||||
|
||||
[[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.";
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
virtual bool CanBeIterated() const{
|
||||
return false;
|
||||
}
|
||||
[[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.";
|
||||
}
|
||||
};
|
||||
|
||||
class NumericScriptType : public ScriptType{
|
||||
// Are we aware of whether this is a float or not?
|
||||
bool _awareOfFloat;
|
||||
// Is this value a float?
|
||||
bool _isFloat;
|
||||
public:
|
||||
explicit NumericScriptType(bool floatAware, bool isFloat) : ScriptType(TypeClass::Number){
|
||||
_awareOfFloat = floatAware;
|
||||
_isFloat = isFloat;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool IsAwareOfFloat() const{
|
||||
return _awareOfFloat;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline bool IsFloat() const{
|
||||
return _isFloat;
|
||||
}
|
||||
};
|
||||
|
||||
class StringScriptType : public ScriptType{
|
||||
bool _isKnownAtBind;
|
||||
uint32_t _hashValue;
|
||||
public:
|
||||
explicit StringScriptType(bool knownAtBind, uint32_t hashValue): ScriptType(TypeClass::String){
|
||||
_isKnownAtBind = knownAtBind;
|
||||
_hashValue = hashValue;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool CanBeIndexedWith(const ScriptType* indexer) const final{
|
||||
if (indexer -> GetClass() != TypeClass::Number)
|
||||
return false;
|
||||
auto num = dynamic_cast<const NumericScriptType*>(indexer);
|
||||
return !(num->IsAwareOfFloat() && num->IsFloat());
|
||||
}
|
||||
|
||||
inline shared_ptr<const ScriptType> GetIndexedType(const ScriptType* indexer) const final{
|
||||
return make_shared<StringScriptType>(false, 0);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
inline bool IsKnownAtBind() const{
|
||||
return _isKnownAtBind;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
inline uint32_t GetHashValue() const{
|
||||
return _hashValue;
|
||||
}
|
||||
};
|
||||
|
||||
class NumericalTableScriptType : public ScriptType{
|
||||
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<const ScriptType> valueType)
|
||||
: ScriptType(TypeClass::Table), _valueType(std::move(valueType)){
|
||||
}
|
||||
|
||||
bool CanBeIndexedWith(const ScriptType* indexer) const final{
|
||||
if (indexer -> GetClass() != TypeClass::Number)
|
||||
return false;
|
||||
auto num = dynamic_cast<const NumericScriptType*>(indexer);
|
||||
return !(num->IsAwareOfFloat() && num->IsFloat());
|
||||
}
|
||||
|
||||
inline shared_ptr<const ScriptType> GetIndexedType(const ScriptType* indexer) const final{
|
||||
return _valueType;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
inline bool CanBeIterated() const final{
|
||||
return true;
|
||||
}
|
||||
[[nodiscard]]
|
||||
inline shared_ptr<const ScriptType> GetIteratorKeyType() const final{
|
||||
return make_shared<NumericScriptType>(true, false);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif //PORYGONLANG_SCRIPTTYPE_HPP
|
||||
54
src/ScriptTypes/TableScriptType.hpp
Normal file
54
src/ScriptTypes/TableScriptType.hpp
Normal file
@@ -0,0 +1,54 @@
|
||||
|
||||
#ifndef PORYGONLANG_TABLESCRIPTTYPE_HPP
|
||||
#define PORYGONLANG_TABLESCRIPTTYPE_HPP
|
||||
#include <unordered_map>
|
||||
#include "../Binder/BoundVariables/BoundVariable.hpp"
|
||||
|
||||
namespace Porygon{
|
||||
class TableScriptType : public ScriptType{
|
||||
const map<Utilities::HashedString, BoundVariable*>* _values;
|
||||
const int _localVariableCount;
|
||||
public:
|
||||
explicit TableScriptType(map<Utilities::HashedString, BoundVariable*>* values, int localVariableCount)
|
||||
: ScriptType(TypeClass::Table),
|
||||
_values(values),
|
||||
_localVariableCount(localVariableCount)
|
||||
{}
|
||||
|
||||
~TableScriptType() final{
|
||||
for (auto i : *_values){
|
||||
delete i.second;
|
||||
}
|
||||
delete _values;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
inline bool CanBeIndexedWith(const ScriptType* indexer) const final{
|
||||
return indexer->GetClass() == TypeClass ::String;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool CanBeIndexedWithIdentifier(uint32_t hash) const final{
|
||||
return true;
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
[[nodiscard]] inline shared_ptr<const ScriptType> GetIndexedType(uint32_t hash) const final{
|
||||
return _values-> at(Utilities::HashedString::CreateLookup(hash))->GetType();
|
||||
}
|
||||
|
||||
[[nodiscard]] inline const map<Utilities::HashedString, BoundVariable*>* GetValues() const{
|
||||
return _values;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#include "ScriptType.hpp"
|
||||
|
||||
#endif //PORYGONLANG_TABLESCRIPTTYPE_HPP
|
||||
Reference in New Issue
Block a user