#include "ast_nodes.h" /*************************** NUMBER NODE ********************************/ NumberNode::NumberNode(const std::string &expression) { if (expression.size() > 2) { if (expression.substr(0, 2) == "0x") { this->value = std::stoll(expression, nullptr, 16); } else if (expression.substr(0, 2) == "0b") { this->value = std::stoll(expression, nullptr, 2); } else { this->value = std::stoll(expression, nullptr, 10); } } else { this->value = std::stoll(expression, nullptr, 10); } } /*************************** COLOR NODE *********************************/ ColorNode::ColorNode(const std::string &expression) { auto value = expression.substr(1); color.r = std::stoul(value.substr(0, 2), nullptr, 16); color.g = std::stoul(value.substr(2, 4), nullptr, 16); color.b = std::stoul(value.substr(4, 6), nullptr, 16); color.a = 255; if(value.length() == 8) { color.a = std::stoul(value.substr(6,8), nullptr, 16); } } /*************************** STRING NODE ********************************/ std::string StringNode::asString() { return value.substr(1, value.length() - 2); } /*************************** VALUE NODE ********************************/ ValueNode::ValueType ValueNode::getType() const { return type.value; } long long int ValueNode::asInt() { if (is(INT)) { return intValue.value(); } return 0; } std::string ValueNode::asString() { if (is(STRING)) { return stringValue.value(); } return ""; } std::string ValueNode::asIdentifier() { if (is(IDENTIFIER) || is(WIRE)) { return identifierValue.value(); } return ""; } Color ValueNode::asColor() { if (is(COLOR)) { return colorValue.value(); } return {}; } bool ValueNode::asBool() { if (is(BOOL)) { return boolValue.value(); } return false; } bool ValueNode::is(ValueNode::ValueType valueType) { return type.value == valueType; } ValueNode ValueNode::ofBool(bool _value) { ValueNode value; value.type = EnumNode(BOOL); value.boolValue = std::optional(_value); return value; } ValueNode ValueNode::ofInt(long long int _value) { ValueNode value; value.type = EnumNode(INT); value.intValue = std::optional(_value); return value; } ValueNode ValueNode::ofString(std::string _value) { ValueNode value; value.type = EnumNode(STRING); value.stringValue = std::optional(_value); return value; } ValueNode ValueNode::ofIdentifier(std::string _value) { ValueNode value; value.type = EnumNode(IDENTIFIER); value.identifierValue = std::optional(_value); return value; } ValueNode ValueNode::ofNull() { ValueNode value; value.type = EnumNode(NIL); return value; } ValueNode ValueNode::ofColor(Color color) { ValueNode value; value.type = EnumNode(COLOR); value.colorValue = color; return value; } ValueNode ValueNode::ofWire(std::optional _value) { ValueNode value; value.type = EnumNode(WIRE); value.identifierValue = _value; return value; } ValueNode ValueNode::ofMemory(std::optional _value) { ValueNode value; value.type = EnumNode(MEMORY); value.identifierValue = _value; return value; } /*************************** DisplayItem NODE ********************************/ std::optional DisplayItemNode::asInt(const std::string &property, long long int _default) { auto prop = getProperty(property); if(prop.has_value()) { return prop->value.is(ValueNode::INT) ? std::optional(prop->value.asInt()) : std::nullopt; } return _default; } std::optional DisplayItemNode::asColor(const std::string &property, Color _default) { auto prop = getProperty(property); if(prop.has_value()) { return prop->value.is(ValueNode::COLOR) ? std::optional(prop->value.asColor()) : std::nullopt; } return _default; } std::optional DisplayItemNode::asString(const std::string &property, std::string _default) { auto prop = getProperty(property); if(prop.has_value()) { return prop->value.is(ValueNode::STRING) ? std::optional(prop->value.asString()) : std::nullopt; } return _default; } std::optional DisplayItemNode::getProperty(const std::string &property) { for(auto &prop: values) { if(prop.key.value == property) { return prop; } } return std::nullopt; }