Cleanup of main script type class

This commit is contained in:
Deukhoofd 2019-09-07 11:16:12 +02:00
parent 5d700aa0e9
commit acc687f213
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
3 changed files with 71 additions and 63 deletions

View File

@ -10,7 +10,6 @@
namespace Porygon::Evaluation {
class NumericEvalValue : public EvalValue {
std::variant<int64_t , double> _intValue, _floatValue;
bool _isFloat;
public:

View File

@ -17,6 +17,64 @@ namespace Porygon{
return make_shared<ScriptType>(TypeClass::Error);
}
bool ScriptType::operator==(const shared_ptr<const ScriptType> &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<const ScriptType> &b) const {
return ! (operator==(b));
}
bool ScriptType::CanBeIndexedWithIdentifier(uint32_t hash) const {
return false;
}
shared_ptr<const ScriptType> 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<const ScriptType> 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<const ScriptType> &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);

View File

@ -26,85 +26,36 @@ namespace Porygon{
All,
};
class ScriptType{
TypeClass _class;
public:
static shared_ptr<const ScriptType> BoolType;
static shared_ptr<const ScriptType> NilType;
explicit ScriptType(TypeClass c){
_class = c;
}
static shared_ptr<const ScriptType> BoolType;
static shared_ptr<const ScriptType> NilType;
virtual ~ScriptType() = default;
[[nodiscard]] inline TypeClass GetClass() const{
return _class;
}
virtual bool operator ==(const shared_ptr<const ScriptType>& 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<const ScriptType>& b) const{
return ! (operator==(b));
}
virtual bool operator ==(const shared_ptr<const ScriptType>& b) const;
virtual bool operator !=(const shared_ptr<const ScriptType>& 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<const ScriptType> GetIndexedType(const ScriptType* indexer) const;
[[nodiscard]] virtual shared_ptr<const 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.";
}
[[nodiscard]] virtual bool CanBeIterated() const;
[[nodiscard]] virtual shared_ptr<const ScriptType> GetIteratorKeyType() const;
[[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.";
}
[[nodiscard]] virtual CastResult CastableTo(const shared_ptr<const ScriptType>& castType, bool explicitCast) const;
[[nodiscard]] virtual CastResult CastableTo(const shared_ptr<const ScriptType>& 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{