-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.h
199 lines (168 loc) · 5.9 KB
/
calculator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* This file is part of libsysperf
*
* Copyright (C) 2001, 2004-2007 by Nokia Corporation.
*
* Contact: Eero Tamminen <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
/* ========================================================================= *
* File: calculator.h -- simple infix calculator with external symtab
*
* Author: Simo Piiroinen
*
* -------------------------------------------------------------------------
*
* History:
*
* 25-Sep-2006 Simo Piiroinen
* - added '?' operator (COL=="")?(COL="XYZ")
* - short-cut evaluation for '&&' and '||'
* - uses csvcell_t for holding values
* - code cleanup
*
* 28-Jun-2005 Simo Piiroinen
* - imported from track2
*
* 10-Jan-2005 Simo Piiroinen
* - supports string variables too
*
* 26-Aug-2004 Simo Piiroinen
* - first version
* ========================================================================= */
#ifndef CALCULATOR_H_
#define CALCULATOR_H_
#include "csv_table.h"
#ifdef __cplusplus
extern "C" {
#elif 0
} /* fool JED indentation ... */
#endif
/* ========================================================================= *
* typedefs & constants
* ========================================================================= */
typedef struct calcop_t calcop_t;
typedef struct calctok_t calctok_t;
typedef struct calcstk_t calcstk_t;
typedef struct calc_t calc_t;
/* ------------------------------------------------------------------------- *
* calcop_t
* ------------------------------------------------------------------------- */
struct calcop_t
{
const char *op_text; /* "+", "-", "<=", ... */
const char *op_name; /* "add", "sub", "le", ... */
int op_args; /* 0: value,
* 1: unary,
* 2: operator
*/
int op_ipri; /* priority to go INTO opstack */
int op_opri; /* prioruty to get OUT of opstack */
};
/* ------------------------------------------------------------------------- *
* calcop_t data
* ------------------------------------------------------------------------- */
enum
{
#define OP(str,name,args,ipri,opri) tc_##name,
#include "calculator.inc"
tc_count
};
/* ------------------------------------------------------------------------- *
* calctok_t
* ------------------------------------------------------------------------- */
struct calctok_t
{
int tok_code; /* tc_xxx */
calctok_t *tok_arg1; /* operator */
calctok_t *tok_arg2; /* operator or unary */
csvcell_t tok_val; /* token value */
csvcell_t tok_sym; /* token symbol */
int tok_col; /* column for symbol */
};
/* ------------------------------------------------------------------------- *
* calcstk_t
* ------------------------------------------------------------------------- */
struct calcstk_t
{
/* |---|------|-------|
* 0 head tail size
*
* next() push() new()
* peek() pop() push()
* new()
* top()
*/
int stk_head; /* next slot to read */
int stk_tail; /* last slot in use */
int stk_size; /* slots allocated */
int stk_fifo; /* fifo == 1 allows:
* calcstk_next()
* calcstk_peek()
* calcstk_new()
*
* and calcstk_clear() deletes tokens contained
* as implicitly does calcstk_dtor() too...
*
* fifo == 0 allows:
* calcstk_top()
* calcstk_pop()
* calcstk_push()
*/
calctok_t **stk_data;
};
/* ------------------------------------------------------------------------- *
* calc_t
* ------------------------------------------------------------------------- */
struct calc_t
{
/* expression to evaluate */
char *calc_expr; /* original text */
calcstk_t calc_fifo; /* tokenized text: all tokens are kept here in
* parse order - which allows reconstruction of
* error messages where offending token can
* be identified
*/
/* parse state: after calc_syntax_tree() vstk should contain
* only root token from which to evaluate value
* the expression
*/
calcstk_t calc_ostk; /* operator stack */
calcstk_t calc_vstk; /* value stack */
/* symbol lookup: symbol lookup is done only at evaluate state
* thus modifying symbol table will yield
* different result without expression
* recompilation */
void (*calc_getvar)(calc_t *self, void *user, calctok_t *tok);
void (*calc_setvar)(calc_t *self, void *user, calctok_t *tok);
void *calc_userdata; /* data to pass to above hooks */
};
/* ------------------------------------------------------------------------- *
* api functions
* ------------------------------------------------------------------------- */
/* calculator.c */
void calc_clear(calc_t *self);
void calc_delete(calc_t *self);
calc_t *calc_create(void);
int calc_compile(calc_t *self, const char *expr);
double calc_evaluate(calc_t *self);
double calc_compile_and_evaluate(calc_t *self, const char *expr);
const char *calctok_getsymbol(const calctok_t *self);
#ifdef __cplusplus
};
#endif
#endif /* CALCULATOR_H_ */