e6e11e909852e4176cb2fa6dfb41a6c734f927ff
[apps/home/calculator.git] / include / calculator_parser.h
1 /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   * 
4   * Licensed under the Flora License, Version 1.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   * 
8   *     http://www.tizenopensource.org/license
9   * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17
18
19 #ifndef __CALCULATOR_PARSER_H
20 #define __CALCULATOR_PARSER_H
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif                          /* __cplusplus */
25
26 #include <glib.h>
27
28         typedef enum {
29                 CHAR_IS_NULL = 0,       //
30                 CHAR_IS_DIGIT,  //0,1,2...
31                 CHAR_IS_LEFT_PARENTHESE,        //(
32                 CHAR_IS_RIGHT_PARENTHESE,
33                 CHAR_IS_PLUS_MINUS,     //+/-
34                 CHAR_IS_PI,     //Pi
35                 CHAR_IS_E,      //e
36                 CHAR_IS_MULTIPLY_DIVIDE,        // */
37                 CHAR_IS_CHARACTER,      //a,P,C,^
38                 CHAR_IS_POINT,  //point
39         } last_char_t;
40
41 /**
42
43 *@enum calculator_state_t, define calculator current state
44
45 * This enum is used to judge or record which state is current.
46
47 */
48         typedef enum {
49                 CALCULATOR_WAITING_INPUT = 0,           /**<waiting input state*/
50                 CALCULATOR_OPERAND_INPUT,                       /**<operand input state*/
51                 CALCULATOR_OPERAND_FRACTION_INPUT,      /**<fraction operand input state*/
52                 CALCULATOR_OPERATOR_INPUT,                      /**<[basic] operator input state*/
53                 CALCULATOR_OPERATOR_OPERAND_INPUT,      /**<operand after [basic] operator input state*/
54                 CALCULATOR_SPECIAL_SYMBOL_INPUT,        /**<special symbol inputting state*/
55                 CALCULATOR_SPECIAL_SYMBOL_INPUT_OVER,
56                                                 /**<special symbol inputed state*/
57                 CALCULATOR_SPECIAL_FUNCTION_INPUT,      /**<function[excluding basic operator] input state*/
58                 CALCULATOR_NAVIGATION,                          /**<browser in entry state, depreated*/
59                 CALCULATOR_CALCULATED,                          /**<calculation completed state*/
60                 CALCULATOR_ERROR_OCCURED,                       /**<error occured state*/
61         } calculator_state_t;
62
63 /**
64
65 *@enum calculator_calculate_priority_t, define calculation priorities
66
67 * This enum is used to mark operator &functions's priority.
68
69 */
70         typedef enum {
71                 CALCULATOR_CALCULATE_PRIORITY_INVALID = 0,
72                                                         /**<invalid priority*/
73                 CALCULATOR_CALCULATE_PRIORITY_LOW,              /**<lowest priority*/
74                 CALCULATOR_CALCULATE_PRIORITY_MIDDLE,   /**<middle priority*/
75                 CALCULATOR_CALCULATE_PRIORITY_HIGH,             /**<high priority*/
76                 CALCULATOR_CALCULATE_PRIORITY_HIGHER,   /**<higher priority*/
77                 CALCULATOR_CALCULATE_PRIORITY_HIGHEST,  /**<highest priority*/
78         } calculator_calculate_priority_t;
79
80 /**
81
82 *@enum operator_type_t, define calculator operator type
83
84 */
85         typedef enum {
86                 OPERATOR_TYPE_INVALID = 0,
87                                         /**<operator type invalid*/
88                 OPERATOR_TYPE_UNARY,            /**<operator unary*/
89                 OPERATOR_TYPE_BINARY,           /**<operator binary*/
90                 OPERATOR_TYPE_CNT,              /**<max count*/
91         } operator_type_t;
92
93 /**
94
95 *@enum function_category_t, define calculator function category
96
97 */
98         typedef enum {
99                 FUNCTION_INVALID = 0,
100                                 /**<category invalid*/
101                 FUNCTION_PREFIX,        /**<prefix category, operand follows functions*/
102                 FUNCTION_POSTFIX,       /**<postfix category, function follows operand*/
103                 FUNCTION_CONSTANT,
104                                 /**<constant category, only for PI*/
105                 FUNCTION_CNT,           /**<max count*/
106         } function_category_t;
107
108         typedef enum {
109                 OP_INVALID = 0,
110
111                 /* basic pannel */
112                 OP_PARENTHESIS, // 1
113                 OP_DELETE,
114                 OP_CLEAR,
115                 OP_DIVIDE,
116
117                 OP_NUM_7,       // 5
118                 OP_NUM_8,
119                 OP_NUM_9,
120                 OP_MULTIPLY,
121
122                 OP_NUM_4,       // 9
123                 OP_NUM_5,
124                 OP_NUM_6,
125                 OP_MINUS,
126
127                 OP_NUM_1,       // 13
128                 OP_NUM_2,
129                 OP_NUM_3,
130                 OP_PLUS,
131
132                 OP_DOT,         // 17
133                 OP_NUM_0,
134                 OP_PLUS_MINUS,
135                 OP_EQUAL,
136
137                 /* function pannel */
138                 OP_PERCENT,     //21
139                 OP_ROOT,
140                 OP_FACT,
141
142                 OP_SIN,         // 24
143                 OP_COS,
144                 OP_TAN,
145
146                 OP_LN,          // 27
147                 OP_LOG,
148                 OP_1X,
149
150                 OP_EX,          // 30
151                 OP_X2,
152                 OP_XY,
153
154                 OP_ABS,         // 33
155                 OP_PI,
156                 OP_E,
157         } op_id_t;
158
159         typedef struct {
160                 op_id_t op_id;
161                 char *op_sym;
162                 char *op_name;
163         } op_item_t;
164
165 /**
166
167 * @struct calculator_node_data_t, calculator node data structure
168
169 * This structure is used as node data of n-ary tree
170
171 */
172         typedef struct {
173                 double tmp_result;                                                      /**<store result by temoprary*/
174                 calculator_calculate_priority_t node_calcu_priority;
175                                                                 /**<node calculation priority*/
176                 operator_type_t operator_type;                                  /**<node operator type*/
177                 int children_num;                                                       /**<node children number*/
178                 char cur_operator;                                                      /**<node operator char*/
179                 int negative_flag;                                                      /**<node negative flag*/
180         } calculator_node_data_t;
181
182 /**
183
184 * @struct calculator_parentheses_data_t, calculator parentheses data structure
185
186 */
187         typedef struct {
188 //      GNode* tree;            /**<parentheses tree node*/
189                 char *start_pos;/**<parentheses start position in original string*/
190                 char *end_pos;  /**<parentheses end position in original string*/
191                 bool matched;
192                         /**<parentheses if is matched*/
193         } calculator_parentheses_data_t;
194
195 /**
196
197 * @struct function_t, calculator function data structure
198
199 */
200         typedef struct {
201                 char *func_name;                                                /**<function name, use only prompt*/
202                 char *func_string;                                              /**<function string, use in text view*/
203                 gint str_len;                                                   /**<function string length*/
204                 char function_char;                                     /**<function char*/
205                 function_category_t category;                   /**<fuction category, input after operand*/
206                 operator_type_t op_type;                                /**<operator type*/
207                 calculator_calculate_priority_t priority;
208                                                         /**<calculation priority*/
209                 bool has_parentheses;                           /**<if function has parentheses*/
210         } function_t;
211
212 /**
213 * @describe
214 *
215 *
216 * @param    tmp_result
217 * @return    bool
218 * @exception
219 */
220 //bool calculator_calculate_truncate_result(double* tmp_result);
221
222 /**
223 * @describe
224 *
225 *
226 * @param    szInputString
227 * @return    bool
228 * @exception
229 */
230         int calculator_get_open_braket(const char *szInputString);
231
232 /**
233 * @describe
234 *
235 *
236 * @param    szInput
237 * @param    pDigitCnt
238 * @param    pPointCnt
239 * @return    bool
240 * @exception
241 */
242         bool calculator_get_digits_number(char *szInput, int *pDigitCnt,
243                                           int *pPointCnt);
244
245 /**
246 * @describe
247 *
248 *
249 * @param    szInput
250 * @param    pDigitCnt
251 * @param    pPointCnt
252 * @return    bool
253 * @exception
254 */
255         bool calculator_calculate(char *string, double *result,
256                                   char *error_msg);
257
258 #ifdef __cplusplus
259 }
260 #endif                          /* __cplusplus */
261 #endif