schema_editor/comdel/parser/ast_nodes.cpp

137 lines
3.3 KiB
C++
Raw 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
/*************************** AST NODE ********************************/
2022-03-29 19:31:45 +00:00
AstNode::~AstNode() = default;
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 ********************************/
long long 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;
}