2022-03-31 21:20:41 +00:00
|
|
|
#include "value.h"
|
|
|
|
|
|
|
|
#include <string>
|
2022-06-27 09:39:13 +00:00
|
|
|
#include <stdexcept>
|
2022-03-31 21:20:41 +00:00
|
|
|
|
|
|
|
namespace domain {
|
|
|
|
|
2022-05-27 06:18:17 +00:00
|
|
|
std::string Value::string() {
|
|
|
|
switch (type) {
|
|
|
|
case INT:
|
|
|
|
return std::to_string(intValue);
|
|
|
|
case BOOL:
|
|
|
|
return boolValue ? "true" : "false";
|
|
|
|
case STRING:
|
|
|
|
return stringValue;
|
|
|
|
case NIL:
|
|
|
|
return "null";
|
|
|
|
case UNDEFINED:
|
|
|
|
return "undefined";
|
|
|
|
case ADDRESS_SPACE:
|
|
|
|
return "AddressSpace::" + addressSpace->getName();
|
|
|
|
case WIRE_REFERENCE:
|
|
|
|
return "Wire::" + reference;
|
|
|
|
case ADDRESS_SPACE_REFERENCE:
|
|
|
|
return "AddressSpace::" + reference;
|
|
|
|
case ATTRIBUTE_REFERENCE:
|
|
|
|
return "Attribute::" + reference;
|
|
|
|
case MEMORY_REFERENCE:
|
|
|
|
if (memoryReference.has_value()) {
|
|
|
|
return "Memory::" + *memoryReference;
|
|
|
|
} else {
|
|
|
|
return "Memory::null";
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Value::ValueType Value::getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::isType(Value::ValueType _type) {
|
|
|
|
return type == _type;
|
|
|
|
}
|
|
|
|
|
2022-06-27 09:39:13 +00:00
|
|
|
std::string Value::getTypename() {
|
|
|
|
switch (type) {
|
|
|
|
case Value::BOOL:
|
|
|
|
return "bool";
|
|
|
|
case Value::INT:
|
|
|
|
return "int";
|
|
|
|
case Value::STRING:
|
|
|
|
return "string";
|
|
|
|
case Value::ADDRESS_SPACE:
|
|
|
|
case Value::ADDRESS_SPACE_REFERENCE:
|
|
|
|
return "address space";
|
|
|
|
case Value::WIRE_REFERENCE:
|
|
|
|
return "wire reference";
|
|
|
|
case Value::ATTRIBUTE_REFERENCE:
|
|
|
|
return "attribute";
|
|
|
|
case Value::NIL:
|
|
|
|
return "null";
|
|
|
|
case Value::MEMORY:
|
|
|
|
case Value::MEMORY_REFERENCE:
|
|
|
|
return "memory";
|
|
|
|
case Value::UNDEFINED:
|
|
|
|
return "undefined";
|
|
|
|
}
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
|
2022-05-27 06:18:17 +00:00
|
|
|
long long Value::asInt() {
|
|
|
|
if (isType(Value::INT)) {
|
|
|
|
return intValue;
|
|
|
|
}
|
2022-06-27 09:39:13 +00:00
|
|
|
throw std::runtime_error("expected 'int' but value contains '" + getTypename() + "'");
|
2022-05-27 06:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Value::asString() {
|
|
|
|
if (isType(Value::STRING)) {
|
2022-04-10 12:23:18 +00:00
|
|
|
return stringValue;
|
2022-05-27 06:18:17 +00:00
|
|
|
}
|
2022-06-27 09:39:13 +00:00
|
|
|
throw std::runtime_error("expected 'string' but value contains '" + getTypename() + "'");
|
2022-05-27 06:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Value::asBool() {
|
|
|
|
if (isType(Value::BOOL)) {
|
|
|
|
return boolValue;
|
|
|
|
}
|
2022-06-27 09:39:13 +00:00
|
|
|
throw std::runtime_error("expected 'bool' but value contains '" + getTypename() + "'");
|
2022-05-27 06:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AddressSpace Value::asAddressSpace() {
|
|
|
|
if (isType(Value::ADDRESS_SPACE)) {
|
|
|
|
return *addressSpace;
|
|
|
|
}
|
2022-06-27 09:39:13 +00:00
|
|
|
throw std::runtime_error("expected 'address space' but value contains '" + getTypename() + "'");
|
2022-05-27 06:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Value::asReference() {
|
|
|
|
if (isType(Value::WIRE_REFERENCE) || isType(Value::ADDRESS_SPACE_REFERENCE) ||
|
|
|
|
isType(Value::ATTRIBUTE_REFERENCE) || isType(Value::UNDEFINED)) {
|
|
|
|
return reference;
|
|
|
|
}
|
2022-06-27 09:39:13 +00:00
|
|
|
throw std::runtime_error("expected 'reference' but value contains '" + getTypename() + "'");
|
2022-05-27 06:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::string> Value::asMemoryReference() {
|
|
|
|
return memoryReference;
|
|
|
|
}
|
2022-05-15 21:55:03 +00:00
|
|
|
|
|
|
|
domain::ComponentInstance *Value::asMemory() {
|
|
|
|
return memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-27 06:18:17 +00:00
|
|
|
void Value::setInt(long long value) {
|
|
|
|
if (isType(Value::INT)) {
|
|
|
|
this->intValue = value;
|
2022-06-19 18:10:44 +00:00
|
|
|
} else {
|
2022-06-27 09:39:13 +00:00
|
|
|
throw std::runtime_error("expected to set 'int' but value contains '" + getTypename() + "'");
|
2022-05-27 06:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Value::setString(std::string value) {
|
|
|
|
if (isType(Value::STRING)) {
|
|
|
|
this->stringValue = value;
|
2022-06-19 18:10:44 +00:00
|
|
|
} else {
|
2022-06-27 09:39:13 +00:00
|
|
|
throw std::runtime_error("expected to set 'string' but value contains '" + getTypename() + "'");
|
2022-05-27 06:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Value::setBool(bool value) {
|
|
|
|
if (isType(Value::BOOL)) {
|
|
|
|
this->boolValue = value;
|
2022-06-19 18:10:44 +00:00
|
|
|
} else {
|
2022-06-27 09:39:13 +00:00
|
|
|
throw std::runtime_error("expected to set 'bool' but value contains '" + getTypename() + "'");
|
2022-05-27 06:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Value::setReference(std::string value) {
|
|
|
|
if (isType(Value::WIRE_REFERENCE)) {
|
|
|
|
this->reference = value;
|
2022-06-19 18:10:44 +00:00
|
|
|
} else {
|
2022-06-27 09:39:13 +00:00
|
|
|
throw std::runtime_error("expected to set 'wire' but value contains '" + getTypename() + "'");
|
2022-05-27 06:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Value Value::fromInt(long long value) {
|
|
|
|
Value val;
|
|
|
|
val.type = Value::INT;
|
|
|
|
val.intValue = value;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Value::fromString(std::string value) {
|
|
|
|
Value val;
|
|
|
|
val.type = Value::STRING;
|
|
|
|
val.stringValue = value;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Value::fromBool(bool value) {
|
|
|
|
Value val;
|
|
|
|
val.type = Value::BOOL;
|
|
|
|
val.boolValue = value;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Value::fromAddressSpace(AddressSpace addressSpace) {
|
|
|
|
Value val;
|
|
|
|
val.type = Value::ADDRESS_SPACE;
|
|
|
|
val.addressSpace = addressSpace;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Value::fromReference(std::string value, Value::ValueType type) {
|
|
|
|
Value val;
|
|
|
|
val.type = type;
|
|
|
|
val.reference = value;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Value::fromNull() {
|
|
|
|
Value val;
|
|
|
|
val.type = Value::NIL;
|
|
|
|
return val;
|
2022-04-24 20:21:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 21:55:03 +00:00
|
|
|
Value Value::fromMemoryReference(std::optional<std::string> value) {
|
|
|
|
Value val;
|
|
|
|
val.type = MEMORY_REFERENCE;
|
|
|
|
val.memoryReference = value;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2022-05-27 06:18:17 +00:00
|
|
|
std::string Value::stringify() {
|
|
|
|
switch (type) {
|
|
|
|
case INT:
|
|
|
|
return std::to_string(intValue);
|
|
|
|
case BOOL:
|
|
|
|
return boolValue ? "true" : "false";
|
|
|
|
case STRING:
|
|
|
|
return "\"" + stringValue + "\"";
|
|
|
|
case NIL:
|
|
|
|
return "null";
|
|
|
|
case WIRE_REFERENCE:
|
|
|
|
case ADDRESS_SPACE_REFERENCE:
|
|
|
|
case ATTRIBUTE_REFERENCE:
|
|
|
|
return reference;
|
|
|
|
case MEMORY_REFERENCE:
|
|
|
|
if (memoryReference.has_value()) {
|
|
|
|
return memoryReference.value();
|
|
|
|
} else {
|
|
|
|
return "null";
|
|
|
|
}
|
|
|
|
default:
|
2022-06-27 09:39:13 +00:00
|
|
|
throw std::runtime_error("unknown type couldn't stringify '" + getTypename() + "'");
|
2022-05-27 06:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Value::ofType(Value::ValueType type) {
|
2022-05-15 21:55:03 +00:00
|
|
|
Value val;
|
2022-05-27 06:18:17 +00:00
|
|
|
val.type = type;
|
2022-05-15 21:55:03 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2022-06-13 22:53:46 +00:00
|
|
|
bool Value::equals(Value value) {
|
|
|
|
if (value.getType() == type) {
|
|
|
|
switch (type) {
|
|
|
|
case INT:
|
|
|
|
return value.asInt() == intValue;
|
|
|
|
case STRING:
|
|
|
|
return value.asString() == stringValue;
|
|
|
|
case NIL:
|
|
|
|
case UNDEFINED:
|
|
|
|
return true;
|
|
|
|
case WIRE_REFERENCE:
|
|
|
|
case ATTRIBUTE_REFERENCE:
|
|
|
|
case ADDRESS_SPACE_REFERENCE:
|
|
|
|
return value.asReference() == reference;
|
|
|
|
case MEMORY_REFERENCE:
|
|
|
|
return value.asMemoryReference() == memoryReference;
|
|
|
|
case MEMORY:
|
|
|
|
return value.asMemory() == memory;
|
|
|
|
case BOOL:
|
|
|
|
return value.asBool() == boolValue;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-31 21:20:41 +00:00
|
|
|
} // namespace domain
|