PorygonLang/src/ScriptType.hpp

149 lines
3.5 KiB
C++
Raw Normal View History

#ifndef PORYGONLANG_SCRIPTTYPE_HPP
#define PORYGONLANG_SCRIPTTYPE_HPP
#include <utility>
#include <vector>
#include <memory>
#include "Binder/BoundVariables/BoundVariableKey.hpp"
2019-06-12 13:19:28 +00:00
#include "Utilities/HashedString.hpp"
using namespace std;
enum class TypeClass{
Error,
Nil,
Number,
Bool,
String,
Function,
UserData,
Table,
};
class ScriptType{
TypeClass _class;
public:
explicit ScriptType(TypeClass c){
_class = c;
}
2019-05-28 16:50:23 +00:00
virtual ~ScriptType() = default;
const TypeClass GetClass(){
return _class;
}
2019-05-28 16:50:23 +00:00
virtual bool operator ==(const ScriptType& b){
2019-05-28 16:50:23 +00:00
return _class == b._class;
};
virtual bool operator ==(ScriptType* b){
return _class == b->_class;
};
virtual bool operator !=(const ScriptType& b){
return ! (operator==(b));
}
virtual bool operator !=(ScriptType* b){
2019-05-28 16:50:23 +00:00
return ! (operator==(b));
}
virtual bool CanBeIndexedWith(ScriptType* indexer);
2019-06-09 18:15:09 +00:00
virtual shared_ptr<ScriptType> GetIndexedType(ScriptType* indexer);
};
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;
}
2019-05-21 20:15:51 +00:00
bool IsAwareOfFloat(){
return _awareOfFloat;
}
bool IsFloat(){
return _isFloat;
}
};
2019-06-12 13:19:28 +00:00
class StringScriptType : public ScriptType{
bool _isKnownAtBind;
uint32_t _hashValue;
2019-06-12 13:19:28 +00:00
public:
explicit StringScriptType(bool knownAtBind, uint32_t hashValue): ScriptType(TypeClass::String){
2019-06-12 13:19:28 +00:00
_isKnownAtBind = knownAtBind;
_hashValue = hashValue;
}
bool IsKnownAtBind(){
return _isKnownAtBind;
}
uint32_t GetHashValue(){
2019-06-12 13:19:28 +00:00
return _hashValue;
}
};
class FunctionScriptType : public ScriptType{
shared_ptr<ScriptType> _returnType;
vector<shared_ptr<ScriptType>> _parameterTypes;
vector<shared_ptr<BoundVariableKey>> _parameterKeys;
int _scopeIndex;
public:
FunctionScriptType(std::shared_ptr<ScriptType> returnType, vector<shared_ptr<ScriptType>> parameterTypes,
vector<shared_ptr<BoundVariableKey>> parameterKeys, int scopeIndex)
: ScriptType(TypeClass::Function){
_returnType = std::move(returnType);
_parameterTypes = std::move(parameterTypes);
_parameterKeys = std::move(parameterKeys);
_scopeIndex = scopeIndex;
}
shared_ptr<ScriptType> GetReturnType(){
return _returnType;
}
void SetReturnType(shared_ptr<ScriptType> t){
_returnType = t;
}
vector<shared_ptr<ScriptType>> GetParameterTypes(){
return _parameterTypes;
}
vector<shared_ptr<BoundVariableKey>> GetParameterKeys(){
return _parameterKeys;
}
int GetScopeIndex(){
return _scopeIndex;
}
};
2019-06-12 13:19:28 +00:00
class NumericalTableScriptType : public ScriptType{
2019-06-09 18:15:09 +00:00
shared_ptr<ScriptType> _valueType;
// Consider adding a check whether the table actually contains a type if every key is static.
public:
2019-06-12 13:19:28 +00:00
explicit NumericalTableScriptType(shared_ptr<ScriptType> valueType) : ScriptType(TypeClass::Table){
2019-06-09 18:15:09 +00:00
_valueType = std::move(valueType);
}
bool CanBeIndexedWith(ScriptType* indexer) final{
2019-06-12 13:19:28 +00:00
return indexer->GetClass() == TypeClass ::Number;
2019-06-09 18:15:09 +00:00
}
shared_ptr<ScriptType> GetIndexedType(ScriptType* indexer) final{
return _valueType;
}
};
2019-06-12 13:19:28 +00:00
#endif //PORYGONLANG_SCRIPTTYPE_HPP