PorygonLang/src/Evaluator/EvalValues/StringEvalValue.hpp

55 lines
1.5 KiB
C++
Raw Normal View History

#ifndef PORYGONLANG_STRINGEVALVALUE_HPP
#define PORYGONLANG_STRINGEVALVALUE_HPP
#include <string>
#include "EvalValue.hpp"
2019-06-12 13:19:28 +00:00
#include "../../Utilities/HashedString.hpp"
using namespace std;
namespace Porygon::Evaluation {
class StringEvalValue : public EvalValue {
u16string _value;
size_t _hash;
public:
2019-07-04 16:24:49 +00:00
explicit StringEvalValue(u16string s) : _value(move(s)){
_hash = Utilities::HashedString::ConstHash(_value.c_str());
}
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline TypeClass GetTypeClass() const final {
return TypeClass::String;
}
2019-07-25 15:23:54 +00:00
bool operator==(const EvalValue *b) const final {
if (b->GetTypeClass() != TypeClass::String)
return false;
return this->_hash == b->GetHashCode();
};
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline u16string EvaluateString() const final {
return _value;
}
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return make_shared<StringEvalValue>(_value);
}
2019-07-25 15:23:54 +00:00
shared_ptr<const EvalValue> IndexValue(const EvalValue *val) const final {
// Porygon is 1-indexed, so we convert to that.
auto l = val->EvaluateInteger() - 1;
2019-07-25 15:23:54 +00:00
return make_shared<const StringEvalValue>(u16string(1, _value[l]));
}
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline std::size_t GetHashCode() const final {
return _hash;
}
};
}
#endif //PORYGONLANG_STRINGEVALVALUE_HPP