2019-06-01 17:20:31 +00:00
|
|
|
|
2019-05-21 18:59:26 +00:00
|
|
|
#ifndef PORYGONLANG_SCRIPTTYPE_HPP
|
|
|
|
#define PORYGONLANG_SCRIPTTYPE_HPP
|
|
|
|
|
2019-06-01 10:33:52 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2019-06-01 11:43:25 +00:00
|
|
|
#include <memory>
|
|
|
|
#include "Binder/BoundVariables/BoundVariableKey.hpp"
|
2019-06-12 13:19:28 +00:00
|
|
|
#include "Utilities/HashedString.hpp"
|
2019-06-01 10:33:52 +00:00
|
|
|
|
2019-06-01 17:20:31 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
namespace Porygon{
|
|
|
|
enum class TypeClass{
|
|
|
|
Error,
|
|
|
|
Nil,
|
|
|
|
Number,
|
|
|
|
Bool,
|
|
|
|
String,
|
|
|
|
Function,
|
|
|
|
UserData,
|
|
|
|
Table,
|
2019-05-28 16:50:23 +00:00
|
|
|
};
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
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 ==(ScriptType* b) const{
|
|
|
|
return _class == b->_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual bool operator !=(const ScriptType& b) const{
|
|
|
|
return ! (operator==(b));
|
|
|
|
}
|
|
|
|
virtual bool operator !=(ScriptType* b) const{
|
|
|
|
return ! (operator==(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const bool CanBeIndexedWith(ScriptType* indexer) const;
|
|
|
|
virtual const 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{
|
2019-06-26 14:19:34 +00:00
|
|
|
throw "This type told the binder it can be indexed, but it does not implement the resulting type.";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const bool CanBeIterated() const{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
virtual shared_ptr<ScriptType> GetIteratorKeyType() const{
|
|
|
|
throw "This type told the binder it can be iterated, but it does not implement the resulting type.";
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
2019-06-01 17:20:31 +00:00
|
|
|
};
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool IsAwareOfFloat() const{
|
|
|
|
return _awareOfFloat;
|
|
|
|
}
|
|
|
|
|
|
|
|
const 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool IsKnownAtBind() const{
|
|
|
|
return _isKnownAtBind;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t GetHashValue() const{
|
|
|
|
return _hashValue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class NumericalTableScriptType : public ScriptType{
|
|
|
|
shared_ptr<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) : ScriptType(TypeClass::Table){
|
|
|
|
_valueType = std::move(valueType);
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool CanBeIndexedWith(ScriptType* indexer) const final{
|
2019-06-26 14:19:34 +00:00
|
|
|
if (indexer -> GetClass() != TypeClass::Number)
|
|
|
|
return false;
|
|
|
|
auto num =(NumericScriptType*)indexer;
|
|
|
|
return !(num->IsAwareOfFloat() && num->IsFloat());
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const shared_ptr<ScriptType> GetIndexedType(ScriptType* indexer) const final{
|
|
|
|
return _valueType;
|
|
|
|
}
|
2019-06-26 14:19:34 +00:00
|
|
|
|
|
|
|
const bool CanBeIterated() const final{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
shared_ptr<ScriptType> GetIteratorKeyType() const final{
|
2019-06-27 12:02:31 +00:00
|
|
|
return make_shared<NumericScriptType>(true, false);
|
2019-06-26 14:19:34 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
};
|
|
|
|
}
|
2019-06-09 18:15:09 +00:00
|
|
|
|
2019-06-12 13:19:28 +00:00
|
|
|
|
2019-05-21 18:59:26 +00:00
|
|
|
#endif //PORYGONLANG_SCRIPTTYPE_HPP
|