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