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