2019-05-25 14:15:20 +00:00
|
|
|
|
|
|
|
#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"
|
2019-05-25 14:15:20 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
namespace Porygon::Evaluation {
|
|
|
|
class StringEvalValue : public EvalValue {
|
2019-07-28 10:33:14 +00:00
|
|
|
shared_ptr<u16string> _value;
|
2019-06-17 16:35:12 +00:00
|
|
|
size_t _hash;
|
2019-07-28 10:33:14 +00:00
|
|
|
explicit StringEvalValue(shared_ptr<u16string> s) : _value(move(s)){
|
|
|
|
_hash = Utilities::HashedString::ConstHash(_value->c_str());
|
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
public:
|
2019-07-28 10:33:14 +00:00
|
|
|
explicit StringEvalValue(const u16string& s) : _value(make_shared<u16string>(s)){
|
|
|
|
_hash = Utilities::HashedString::ConstHash(_value->c_str());
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:33:14 +00:00
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
inline TypeClass GetTypeClass() const final {
|
2019-06-17 16:35:12 +00:00
|
|
|
return TypeClass::String;
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
bool operator==(const EvalValue *b) const final {
|
2019-06-17 16:35:12 +00:00
|
|
|
if (b->GetTypeClass() != TypeClass::String)
|
|
|
|
return false;
|
|
|
|
return this->_hash == b->GetHashCode();
|
|
|
|
};
|
|
|
|
|
2019-09-07 11:28:25 +00:00
|
|
|
bool operator!=(const EvalValue *b) const override {
|
|
|
|
return !operator==(b);
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
inline u16string EvaluateString() const final {
|
2019-07-28 10:33:14 +00:00
|
|
|
return *_value.get();
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
2019-07-27 15:59:42 +00:00
|
|
|
inline EvalValue* Clone() const final {
|
|
|
|
return new StringEvalValue(_value);
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-27 15:59:42 +00:00
|
|
|
EvalValue* IndexValue(const EvalValue *val) const final {
|
2019-06-17 16:35:12 +00:00
|
|
|
// Porygon is 1-indexed, so we convert to that.
|
|
|
|
auto l = val->EvaluateInteger() - 1;
|
2019-07-28 10:33:14 +00:00
|
|
|
return new StringEvalValue(u16string(1, (*_value)[l]));
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
inline std::size_t GetHashCode() const final {
|
2019-06-17 16:35:12 +00:00
|
|
|
return _hash;
|
|
|
|
}
|
2019-07-27 15:59:42 +00:00
|
|
|
|
|
|
|
[[nodiscard]]
|
|
|
|
inline EvalValue* BinaryOperation(Binder::BoundBinaryOperation operation, const EvalValue* b) const final{
|
|
|
|
if (operation != Binder::BoundBinaryOperation::Concatenation){
|
|
|
|
throw EvaluationException("Binary operation not supported for strings.");
|
|
|
|
}
|
|
|
|
std::basic_ostringstream<char16_t> stringStream;
|
2019-07-28 10:33:14 +00:00
|
|
|
stringStream << *_value;
|
2019-07-27 15:59:42 +00:00
|
|
|
stringStream << b->EvaluateString();
|
2019-07-28 10:33:14 +00:00
|
|
|
auto str = stringStream.str();
|
|
|
|
return new StringEvalValue(str);
|
2019-07-27 15:59:42 +00:00
|
|
|
}
|
2019-05-25 14:15:20 +00:00
|
|
|
};
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
2019-05-25 14:15:20 +00:00
|
|
|
|
|
|
|
#endif //PORYGONLANG_STRINGEVALVALUE_HPP
|