[88] | 1 | #ifdef STATISTICS |
---|
| 2 | #include "Behavioural/include/Stat_binary_tree.h" |
---|
| 3 | |
---|
| 4 | namespace morpheo { |
---|
| 5 | namespace behavioural { |
---|
| 6 | |
---|
| 7 | #define string2operator(x) (x=="+")?add:((x=="-")?sub:((x=="*")?mul:((x=="/" )?div:((x=="++")?inc:dec)))) |
---|
| 8 | |
---|
| 9 | void Stat_binary_tree::import (std::string expr, |
---|
| 10 | std::map<std::string, counter_t*> * operand) |
---|
| 11 | { |
---|
| 12 | const std::string delims (" "); // délimiteur : " " |
---|
| 13 | const std::string numbers ("0123456789"); // délimiteur : " " |
---|
| 14 | std::string::size_type index_begin, index_end; |
---|
| 15 | |
---|
| 16 | Stat_binary_tree * tree = this; |
---|
| 17 | |
---|
| 18 | index_begin = expr.find_first_not_of(delims); |
---|
| 19 | |
---|
| 20 | while (index_begin != std::string::npos) |
---|
| 21 | { |
---|
| 22 | index_end = expr.find_first_of(delims, index_begin); |
---|
| 23 | |
---|
| 24 | if (index_end == std::string::npos) |
---|
| 25 | { |
---|
| 26 | index_end = expr.length(); |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | std::string str = expr.substr(index_begin, index_end-index_begin); |
---|
| 30 | |
---|
| 31 | // 3 possibilités : |
---|
| 32 | // * operator |
---|
| 33 | // * constante |
---|
| 34 | // * variable |
---|
| 35 | { |
---|
| 36 | // Test constantes |
---|
| 37 | std::string::size_type index = str.find_first_not_of(numbers); |
---|
| 38 | if (index == std::string::npos) |
---|
| 39 | { |
---|
| 40 | // std::cout << " * c'est une constante." << std::endl; |
---|
| 41 | counter_t cst = atoi(str.c_str()); |
---|
| 42 | |
---|
| 43 | if (tree->_data_type == NONE) |
---|
| 44 | change_type(cst); |
---|
| 45 | else |
---|
| 46 | if (tree==NULL) |
---|
| 47 | tree = new Stat_binary_tree (cst); |
---|
| 48 | else |
---|
| 49 | tree = tree->insert_tree (cst); |
---|
| 50 | } |
---|
| 51 | else |
---|
| 52 | { |
---|
| 53 | // Test variables |
---|
| 54 | std::map<std::string, counter_t*>::iterator it = operand->find(str); |
---|
| 55 | if (it != operand->end()) |
---|
| 56 | { |
---|
| 57 | // std::cout << " * c'est une variable." << std::endl; |
---|
| 58 | counter_t * var = it->second; |
---|
| 59 | |
---|
| 60 | if (tree->_data_type == NONE) |
---|
| 61 | change_type(var); |
---|
| 62 | else |
---|
| 63 | if (tree==NULL) |
---|
| 64 | tree = new Stat_binary_tree (var); |
---|
| 65 | else |
---|
| 66 | tree = tree->insert_tree (var); |
---|
| 67 | } |
---|
| 68 | else |
---|
| 69 | { |
---|
| 70 | if ((str == "+") or |
---|
| 71 | (str == "-") or |
---|
| 72 | (str == "*") or |
---|
| 73 | (str == "/")) |
---|
| 74 | { |
---|
| 75 | // std::cout << " * c'est un operator à 2 opérandes." << std::endl; |
---|
| 76 | |
---|
| 77 | operator_t op = string2operator(str); |
---|
| 78 | |
---|
| 79 | if (tree->_data_type == NONE) |
---|
| 80 | change_type(op); |
---|
| 81 | else |
---|
| 82 | if (tree==NULL) |
---|
| 83 | tree = new Stat_binary_tree (op); |
---|
| 84 | else |
---|
| 85 | tree = tree->insert_tree (op); |
---|
| 86 | } |
---|
| 87 | else |
---|
| 88 | { |
---|
| 89 | if ((str == "++") or |
---|
| 90 | (str == "--")) |
---|
| 91 | { |
---|
| 92 | // std::cout << " * c'est un operator à 1 opérande." << std::endl; |
---|
| 93 | |
---|
| 94 | operator_t op = string2operator(str); |
---|
| 95 | |
---|
| 96 | if (tree->_data_type == NONE) |
---|
| 97 | change_type(op); |
---|
| 98 | else |
---|
| 99 | if (tree==NULL) |
---|
| 100 | tree = new Stat_binary_tree (op); |
---|
| 101 | else |
---|
| 102 | tree = tree->insert_tree (op); |
---|
| 103 | } |
---|
| 104 | else |
---|
| 105 | { |
---|
| 106 | // std::cout << " * c'est autre chose." << std::endl; |
---|
| 107 | throw(ERRORMORPHEO("Stat_binary_tree::string2tree",toString(_("expression '%s' doesn't a constant, a declarated variable or an operator.\n"),str.c_str()))); |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | index_begin = expr.find_first_not_of(delims, index_end); |
---|
| 115 | |
---|
| 116 | if (index_begin != std::string::npos) |
---|
| 117 | tree = tree->goto_next_root(); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | if (tree == NULL) |
---|
| 121 | throw (ERRORMORPHEO("Stat_binary_tree::string2tree",_("The tree generated is empty.\n"))); |
---|
| 122 | |
---|
| 123 | // std::cout << "<Stat::string2tree> goto_top_level" << std::endl; |
---|
| 124 | |
---|
| 125 | tree = tree->goto_top_level(); |
---|
| 126 | |
---|
| 127 | // std::cout << "<Stat::string2tree> valid" << std::endl; |
---|
| 128 | |
---|
| 129 | // print(); |
---|
| 130 | |
---|
| 131 | if ((not tree->valid()) or (not valid())) |
---|
| 132 | throw (ERRORMORPHEO("Stat_binary_tree::string2tree",_("the tree generated is invalid.\n"))); |
---|
| 133 | |
---|
| 134 | // std::cout << "<Stat::string2tree> End" << std::endl; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | }; |
---|
| 138 | }; |
---|
| 139 | #endif |
---|