You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
9.5 KiB

6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
  1. #include "ConfigYaml.h"
  2. #include <iostream>
  3. std::ostream& operator <<(std::ostream &os, const Label& label) {
  4. os << "\"" << label.label_ << "\"" << "=" << label.text_;
  5. return os;
  6. }
  7. std::ostream& operator << (std::ostream &os, const Formula& formula) {
  8. os << formula.formula_ << "=" << formula.content_;
  9. return os;
  10. }
  11. std::ostream& operator << (std::ostream& os, const Command& command) {
  12. os << command.action_;
  13. return os;
  14. }
  15. std::ostream& operator << (std::ostream& os, const Constant& constant) {
  16. os << "const " << constant.type_ << " " << constant.constant_ << " = " << constant.value_;
  17. return os;
  18. }
  19. std::ostream& operator << (std::ostream& os, const Module& module) {
  20. os << "Module: " << module.module_ << std::endl;
  21. for (auto& command : module.commands_) {
  22. os << command << std::endl;
  23. }
  24. return os;
  25. }
  26. std::string Label::createExpression() const {
  27. if (overwrite_) {
  28. return "label \"" + label_ + "\" = " + text_ + Configuration::overwrite_identifier_;
  29. }
  30. return "label \"" + label_ + "\" = " + text_ + Configuration::configuration_identifier_;
  31. }
  32. std::string Formula::createExpression() const {
  33. if (overwrite_) {
  34. return "formula " + formula_ + " = " + content_ + Configuration::overwrite_identifier_;
  35. }
  36. return "formula " + formula_ + " = " + content_ + Configuration::configuration_identifier_;
  37. }
  38. std::string Command::createExpression() const {
  39. if (overwrite_) {
  40. return action_ + "\t" + guard_ + " -> " + update_ + Configuration::overwrite_identifier_;
  41. }
  42. return "\t" + action_ + "\t" + guard_ + " -> " + update_+ Configuration::configuration_identifier_;
  43. }
  44. std::string Constant::createExpression() const {
  45. if (overwrite_) {
  46. return "const " + type_ + " " + constant_ + " = " + value_ + Configuration::overwrite_identifier_;
  47. }
  48. return "const " + type_ + " " + constant_ + " = " + value_ + Configuration::configuration_identifier_;
  49. }
  50. YAML::Node YAML::convert<Module>::encode(const Module& rhs) {
  51. YAML::Node node;
  52. node.push_back(rhs.module_);
  53. node.push_back(rhs.commands_);
  54. return node;
  55. }
  56. bool YAML::convert<Module>::decode(const YAML::Node& node, Module& rhs) {
  57. if (!node.Type() == NodeType::Map) {
  58. return false;
  59. }
  60. rhs.commands_ = node["commands"].as<std::vector<Command>>();
  61. rhs.module_ = node["module"].as<std::string>();
  62. if (node["module_text"]) {
  63. rhs.module_text_ = node["module_text"].as<std::string>();
  64. }
  65. if (node["overwrite"]) {
  66. rhs.overwrite_module = node["overwrite"].as<bool>();
  67. }
  68. return true;
  69. }
  70. YAML::Node YAML::convert<Command>::encode(const Command& rhs) {
  71. YAML::Node node;
  72. node.push_back(rhs.action_);
  73. node.push_back(rhs.guard_);
  74. node.push_back(rhs.overwrite_);
  75. node.push_back(rhs.update_);
  76. return node;
  77. }
  78. bool YAML::convert<Command>::decode(const YAML::Node& node, Command& rhs) {
  79. if (!node.Type() == NodeType::Map) {
  80. return false;
  81. }
  82. rhs.action_ = node["action"].as<std::string>();
  83. if (node["guard"]) {
  84. rhs.guard_ = node["guard"].as<std::string>();
  85. }
  86. if (node["update"]) {
  87. rhs.update_ = node["update"].as<std::string>();
  88. }
  89. if (node["overwrite"]) {
  90. rhs.overwrite_ = node["overwrite"].as<bool>();
  91. }
  92. if (node["index"]) {
  93. try {
  94. rhs.indexes_ = node["index"].as<std::vector<int>>();
  95. }
  96. catch(const std::exception& e) {
  97. rhs.indexes_ = {node["index"].as<int>()};
  98. }
  99. }
  100. return true;
  101. }
  102. YAML::Node YAML::convert<Label>::encode(const Label& rhs) {
  103. YAML::Node node;
  104. node.push_back(rhs.label_);
  105. node.push_back(rhs.text_);
  106. return node;
  107. }
  108. bool YAML::convert<Label>::decode(const YAML::Node& node, Label& rhs) {
  109. if (!node.Type() == NodeType::Map || !node["label"] || !node["text"]) {
  110. return false;
  111. }
  112. rhs.label_ = node["label"].as<std::string>();
  113. rhs.text_ = node["text"].as<std::string>();
  114. if (node["overwrite"]) {
  115. rhs.overwrite_ = node["overwrite"].as<bool>();
  116. }
  117. return true;
  118. }
  119. YAML::Node YAML::convert<Formula>::encode(const Formula& rhs) {
  120. YAML::Node node;
  121. node.push_back(rhs.content_);
  122. node.push_back(rhs.formula_);
  123. node.push_back(rhs.overwrite_);
  124. return node;
  125. }
  126. bool YAML::convert<Formula>::decode(const YAML::Node& node, Formula& rhs) {
  127. if (!node.IsDefined() || !node.Type() == NodeType::Map || !node["formula"] || !node["content"]) {
  128. return false;
  129. }
  130. rhs.formula_ = node["formula"].as<std::string>();
  131. rhs.content_ = node["content"].as<std::string>();
  132. if(node["overwrite"]) {
  133. rhs.overwrite_ = node["overwrite"].as<bool>();
  134. }
  135. return true;
  136. }
  137. YAML::Node YAML::convert<Constant>::encode(const Constant& rhs) {
  138. YAML::Node node;
  139. node.push_back(rhs.constant_);
  140. node.push_back(rhs.value_);
  141. node.push_back(rhs.type_);
  142. node.push_back(rhs.overwrite_);
  143. return node;
  144. }
  145. bool YAML::convert<Constant>::decode(const YAML::Node& node, Constant& rhs) {
  146. if (!node.IsDefined() || !node.Type() == NodeType::Map || !node["constant"] || !node["type"] || !node["value"]) {
  147. return false;
  148. }
  149. rhs.constant_ = node["constant"].as<std::string>();
  150. rhs.type_ = node["type"].as<std::string>();
  151. rhs.value_ = node["value"].as<std::string>();
  152. if(node["overwrite"]) {
  153. rhs.overwrite_ = node["overwrite"].as<bool>();
  154. }
  155. return true;
  156. }
  157. YAML::Node YAML::convert<Property>::encode(const Property& rhs) {
  158. YAML::Node node;
  159. node.push_back(rhs.property);
  160. node.push_back(rhs.value_);
  161. return node;
  162. }
  163. bool YAML::convert<Property>::decode(const YAML::Node& node, Property& rhs) {
  164. if (!node.IsDefined() || !node["property"] || !node["value"]) {
  165. return false;
  166. }
  167. rhs.property = node["property"].as<std::string>();
  168. try {
  169. rhs.value_ = node["value"].as<double>();
  170. }
  171. catch(const std::exception& e) {
  172. rhs.value_str_ = node["value"].as<std::string>();
  173. }
  174. return true;
  175. }
  176. const std::string Configuration::configuration_identifier_ { "; // created through configuration"};
  177. const std::string Configuration::overwrite_identifier_{"; // Overwritten through configuration"};
  178. YamlConfigParseResult YamlConfigParser::parseConfiguration() {
  179. std::vector<Configuration> configuration;
  180. std::vector<Property> properties;
  181. try {
  182. YAML::Node config = YAML::LoadFile(file_);
  183. std::vector<Label> labels;
  184. std::vector<Formula> formulas;
  185. std::vector<Module> modules;
  186. std::vector<Constant> constants;
  187. if (config["labels"]) {
  188. labels = config["labels"].as<std::vector<Label>>();
  189. }
  190. if (config["formulas"]) {
  191. formulas = config["formulas"].as<std::vector<Formula>>();
  192. }
  193. if (config["modules"]) {
  194. modules = config["modules"].as<std::vector<Module>>();
  195. }
  196. if (config["constants"]) {
  197. constants = config["constants"].as<std::vector<Constant>>();
  198. }
  199. if (config["properties"]) {
  200. properties = config["properties"].as<std::vector<Property>>();
  201. }
  202. for (auto& label : labels) {
  203. configuration.push_back({label.createExpression(), label.label_ , ConfigType::Label, label.overwrite_});
  204. }
  205. for (auto& formula : formulas) {
  206. configuration.push_back({formula.createExpression(), formula.formula_ ,ConfigType::Formula, formula.overwrite_});
  207. }
  208. for (auto& module : modules) {
  209. if (module.overwrite_module) {
  210. Configuration config = Configuration("module " + module.module_text_, "module " + module.module_, ConfigType::Module, true, module.module_, {0}, "endmodule");
  211. configuration.push_back(config);
  212. continue;
  213. }
  214. for (auto& command : module.commands_) {
  215. Configuration config;
  216. if (!command.guard_.empty() && !command.action_.empty() && command.update_.empty()) {
  217. config = Configuration(" " + command.guard_, command.action_, ConfigType::GuardOnly, true, module.module_, command.indexes_, "->");
  218. } else if (!command.update_.empty() && !command.action_.empty() && command.guard_.empty()) {
  219. config = Configuration( " " + command.update_, command.action_, ConfigType::UpdateOnly, true, module.module_, command.indexes_, ";");
  220. } else {
  221. config = Configuration(command.createExpression(), command.action_, ConfigType::Module, command.overwrite_, module.module_, command.indexes_);
  222. }
  223. configuration.push_back(config);
  224. }
  225. }
  226. for (auto& constant : constants) {
  227. // std::cout << constant.constant_ << std::endl;
  228. configuration.push_back({constant.createExpression(), "const " + constant.type_ + " " + constant.constant_, ConfigType::Constant, constant.overwrite_});
  229. }
  230. }
  231. catch(const std::exception& e) {
  232. std::cout << "Exception '" << typeid(e).name() << "' caught:" << std::endl;
  233. std::cout << "\t" << e.what() << std::endl;
  234. std::cout << "while parsing configuration " << file_ << std::endl;
  235. }
  236. return YamlConfigParseResult(configuration, properties);
  237. }