From acc687f213316739a81bb8f66b03d438d841840d Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 7 Sep 2019 11:16:12 +0200 Subject: [PATCH] Cleanup of main script type class --- src/Evaluator/EvalValues/NumericEvalValue.hpp | 1 - src/ScriptTypes/ScriptType.cpp | 58 ++++++++++++++ src/ScriptTypes/ScriptType.hpp | 75 ++++--------------- 3 files changed, 71 insertions(+), 63 deletions(-) diff --git a/src/Evaluator/EvalValues/NumericEvalValue.hpp b/src/Evaluator/EvalValues/NumericEvalValue.hpp index a8c7833..3cbf698 100644 --- a/src/Evaluator/EvalValues/NumericEvalValue.hpp +++ b/src/Evaluator/EvalValues/NumericEvalValue.hpp @@ -10,7 +10,6 @@ namespace Porygon::Evaluation { class NumericEvalValue : public EvalValue { std::variant _intValue, _floatValue; - bool _isFloat; public: diff --git a/src/ScriptTypes/ScriptType.cpp b/src/ScriptTypes/ScriptType.cpp index c391cfa..3cfbd39 100644 --- a/src/ScriptTypes/ScriptType.cpp +++ b/src/ScriptTypes/ScriptType.cpp @@ -17,6 +17,64 @@ namespace Porygon{ return make_shared(TypeClass::Error); } + bool ScriptType::operator==(const shared_ptr &b) const { + if (_class == TypeClass::Nil){ + auto bClass = b->_class; + if (bClass == TypeClass::UserData || bClass == TypeClass::String || bClass == TypeClass::All) + return true; + } + return _class == b->_class; + } + + bool ScriptType::operator!=(const shared_ptr &b) const { + return ! (operator==(b)); + } + + bool ScriptType::CanBeIndexedWithIdentifier(uint32_t hash) const { + return false; + } + + shared_ptr ScriptType::GetIndexedType(uint32_t hash) const { + throw "This type told the binder it can be indexed, but it does not implement the resulting type."; + } + + bool ScriptType::CanBeIterated() const { + return false; + } + + shared_ptr ScriptType::GetIteratorKeyType() const { + throw "This type told the binder it can be iterated, but it does not implement the resulting type."; + } + + CastResult ScriptType::CastableTo(const shared_ptr &castType, bool explicitCast) const { + if (_class == TypeClass::All){ + return CastResult ::UncheckedCast; + } + if (explicitCast) + return CastResult::InvalidCast; + return CastResult::InvalidCast; + } + + std::string ScriptType::ToString(TypeClass c) { + switch (c){ + + case TypeClass::Error: return "error"; + case TypeClass::Nil: return "nil"; + case TypeClass::Number: return "number"; + case TypeClass::Bool: return "bool"; + case TypeClass::String: return "string"; + case TypeClass::Function: return "function"; + case TypeClass::UserData: return "userdata"; + case TypeClass::Table: return "table"; + case TypeClass::All: return "all"; + } + throw exception(); + } + + std::string ScriptType::ToString() const { + return ToString(this->_class); + } + extern "C"{ ScriptType* CreateScriptType(Porygon::TypeClass t){ return new ScriptType(t); diff --git a/src/ScriptTypes/ScriptType.hpp b/src/ScriptTypes/ScriptType.hpp index 3d65971..84e1874 100644 --- a/src/ScriptTypes/ScriptType.hpp +++ b/src/ScriptTypes/ScriptType.hpp @@ -26,85 +26,36 @@ namespace Porygon{ All, }; - class ScriptType{ TypeClass _class; public: + static shared_ptr BoolType; + static shared_ptr NilType; + explicit ScriptType(TypeClass c){ _class = c; } - - static shared_ptr BoolType; - static shared_ptr NilType; virtual ~ScriptType() = default; [[nodiscard]] inline TypeClass GetClass() const{ return _class; } - virtual bool operator ==(const shared_ptr& b) const{ - if (_class == TypeClass::Nil){ - auto bClass = b->_class; - if (bClass == TypeClass::UserData || bClass == TypeClass::String || bClass == TypeClass::All) - return true; - } - return _class == b->_class; - } - - virtual bool operator !=(const shared_ptr& b) const{ - return ! (operator==(b)); - } + virtual bool operator ==(const shared_ptr& b) const; + virtual bool operator !=(const shared_ptr& b) const; virtual bool CanBeIndexedWith(const ScriptType* indexer) const; - [[nodiscard]] - virtual bool CanBeIndexedWithIdentifier(uint32_t hash) const{ - return false; - } + [[nodiscard]] virtual bool CanBeIndexedWithIdentifier(uint32_t hash) const; + [[nodiscard]] virtual shared_ptr GetIndexedType(const ScriptType* indexer) const; + [[nodiscard]] virtual shared_ptr GetIndexedType(uint32_t hash) const; - [[nodiscard]] - virtual shared_ptr GetIndexedType(const ScriptType* indexer) const; - [[nodiscard]] - virtual shared_ptr 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; + [[nodiscard]] virtual shared_ptr GetIteratorKeyType() const; - [[nodiscard]] - virtual bool CanBeIterated() const{ - return false; - } - [[nodiscard]] - virtual shared_ptr GetIteratorKeyType() const{ - throw "This type told the binder it can be iterated, but it does not implement the resulting type."; - } + [[nodiscard]] virtual CastResult CastableTo(const shared_ptr& castType, bool explicitCast) const; - [[nodiscard]] virtual CastResult CastableTo(const shared_ptr& castType, bool explicitCast) const{ - if (_class == TypeClass::All){ - return CastResult ::UncheckedCast; - } - if (explicitCast) - return CastResult::InvalidCast; - return CastResult::InvalidCast; - } - - static std::string ToString(TypeClass c){ - switch (c){ - - case TypeClass::Error: return "error"; - case TypeClass::Nil: return "nil"; - case TypeClass::Number: return "number"; - case TypeClass::Bool: return "bool"; - case TypeClass::String: return "string"; - case TypeClass::Function: return "function"; - case TypeClass::UserData: return "userdata"; - case TypeClass::Table: return "table"; - case TypeClass::All: return "all"; - } - throw exception(); - } - - [[nodiscard]] virtual std::string ToString() const{ - return ToString(this->_class); - } + static std::string ToString(TypeClass c); + [[nodiscard]] virtual std::string ToString() const; }; class NumericScriptType : public ScriptType{