schema_editor/comdel/parser/ast_nodes.cpp

172 lines
4.5 KiB
C++
Raw Permalink Normal View History

2022-05-27 06:18:17 +00:00
#include "ast_nodes.h"
2022-03-29 19:31:45 +00:00
2022-04-24 20:21:45 +00:00
/*************************** NUMBER NODE ********************************/
2022-05-27 06:18:17 +00:00
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);
2022-03-29 19:31:45 +00:00
} else {
2022-05-27 06:18:17 +00:00
this->value = std::stoll(expression, nullptr, 10);
2022-03-29 19:31:45 +00:00
}
} else {
2022-05-27 06:18:17 +00:00
this->value = std::stoll(expression, nullptr, 10);
2022-03-29 19:31:45 +00:00
}
}
2022-04-24 20:21:45 +00:00
/*************************** 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);
}
}
2022-04-24 20:21:45 +00:00
/*************************** 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() {
2022-05-27 06:18:17 +00:00
if (is(INT)) {
2022-04-24 20:21:45 +00:00
return intValue.value();
}
return 0;
}
std::string ValueNode::asString() {
2022-05-27 06:18:17 +00:00
if (is(STRING)) {
2022-04-24 20:21:45 +00:00
return stringValue.value();
}
return "";
}
std::string ValueNode::asIdentifier() {
2022-05-27 06:18:17 +00:00
if (is(IDENTIFIER) || is(WIRE)) {
2022-04-24 20:21:45 +00:00
return identifierValue.value();
}
return "";
}
Color ValueNode::asColor() {
if (is(COLOR)) {
return colorValue.value();
}
return {};
}
2022-04-24 20:21:45 +00:00
bool ValueNode::asBool() {
2022-05-27 06:18:17 +00:00
if (is(BOOL)) {
2022-04-24 20:21:45 +00:00
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<bool>(_value);
return value;
}
ValueNode ValueNode::ofInt(long long int _value) {
ValueNode value;
value.type = EnumNode(INT);
value.intValue = std::optional<long long>(_value);
return value;
}
ValueNode ValueNode::ofString(std::string _value) {
ValueNode value;
value.type = EnumNode(STRING);
value.stringValue = std::optional<std::string>(_value);
return value;
}
ValueNode ValueNode::ofIdentifier(std::string _value) {
ValueNode value;
value.type = EnumNode(IDENTIFIER);
value.identifierValue = std::optional<std::string>(_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;
}
2022-05-15 21:55:03 +00:00
ValueNode ValueNode::ofWire(std::optional<std::string> _value) {
2022-04-24 20:21:45 +00:00
ValueNode value;
value.type = EnumNode(WIRE);
2022-05-15 21:55:03 +00:00
value.identifierValue = _value;
return value;
}
ValueNode ValueNode::ofMemory(std::optional<std::string> _value) {
ValueNode value;
value.type = EnumNode(MEMORY);
value.identifierValue = _value;
2022-04-24 20:21:45 +00:00
return value;
}
/*************************** DisplayItem NODE ********************************/
std::optional<long long int> 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<Color> 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<std::string> 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<PropertyNode> DisplayItemNode::getProperty(const std::string &property) {
for(auto &prop: values) {
if(prop.key.value == property) {
return prop;
}
}
return std::nullopt;
}