PorygonLang/src/Evaluator/EvalValues/StringEvalValue.hpp

51 lines
1.2 KiB
C++
Raw Normal View History

#ifndef PORYGONLANG_STRINGEVALVALUE_HPP
#define PORYGONLANG_STRINGEVALVALUE_HPP
#include <string>
#include "EvalValue.hpp"
using namespace std;
class StringEvalValue : public EvalValue{
string _value;
2019-06-09 18:15:09 +00:00
size_t _hash;
std::shared_ptr<ScriptType> _type;
public:
explicit StringEvalValue(string s){
_value = move(s);
_type = std::make_shared<ScriptType>(TypeClass::String);
2019-06-09 18:15:09 +00:00
_hash = std::hash<string>{}(_value);
}
std::shared_ptr<ScriptType> GetType() final{
return _type;
};
bool operator ==(EvalValue* b) final{
if (b->GetType()->GetClass() != TypeClass::String)
return false;
2019-06-09 18:15:09 +00:00
return this->_hash == b->GetHashCode();
};
string* EvaluateString() final{
return &_value;
}
shared_ptr<EvalValue> Clone() final{
return make_shared<StringEvalValue>(_value);
}
2019-06-09 18:15:09 +00:00
shared_ptr<EvalValue> IndexValue(EvalValue* val) final{
// Porygon is 1-indexed, so we convert to that.
auto l = val->EvaluateInteger() - 1;
2019-06-09 18:15:09 +00:00
return make_shared<StringEvalValue>(string(1, _value[l]));
}
std::size_t GetHashCode() final{
return _hash;
}
};
#endif //PORYGONLANG_STRINGEVALVALUE_HPP