forked from raduprv/Eternal-Lands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.c
342 lines (301 loc) · 6.89 KB
/
calc.c
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#ifndef _MSC_VER
#include <strings.h>
#endif
#include "calc.h"
#include "hud.h"
#include "platform.h"
#define CALCSTACKMAX 256
#define CALCTOK_OPAR 1
#define CALCTOK_CPAR 2
#define CALCTOK_PLUS 3
#define CALCTOK_MINUS 4
#define CALCTOK_MUL 5
#define CALCTOK_DIV 6
#define CALCTOK_NUM 7
#define CALCTOK_END 8
#define CALCTOK_XOP 9
#define CALCTOK_LOP 10
#define CALCTOK_MOD 11
/*Implementation of #calc command
#calc expr
expr can be any combination of positive (no unary minus) real numbers, [+/-*()] operators and L
Ln (n is a positive real number) returns the xp for lvl n (fractional too: L1.5 is halfway between L1 and L2)
*/
typedef struct caltoken {
int type;
double value;
} CalcTok;
typedef struct calstack {
int pos;
CalcTok **stack;
} CalcStack;
int reduce_stack(CalcStack* cs);
CalcTok* next_calctoken(char* str, int *spos);
CalcStack* init_calcstack();
void done_calcstack(CalcStack* cs);
CalcTok* calcpop(CalcStack* cs);
CalcTok* calcinspect(CalcStack* cs,int pos);
void calcpush(CalcStack* cs, CalcTok* ct);
int calc_error=CALCERR_OK;
double last_res=0;
//XP table
//using exp_lev from hud.c
#define XPT_MAX 179
#define XPLDIFF(a,b) (exp_lev[b]-exp_lev[a])
#define XPL(a) (exp_lev[a])
//Parsing functions
int calc_geterror(){
int t_err=calc_error;
calc_error=CALCERR_OK;
return t_err;
}
double calc_exp(char* str){
CalcStack* cs;
CalcTok* ct;
int pos=0;
double res=0;
calc_error=CALCERR_OK;
cs= init_calcstack();
ct=next_calctoken(str,&pos);
while(ct->type!=CALCTOK_END && calc_error==CALCERR_OK){
calcpush(cs,ct);
while(reduce_stack(cs));
ct=next_calctoken(str,&pos);
}
calcpush(cs,ct);
if (calc_error==CALCERR_OK) {
while(reduce_stack(cs));
ct = calcinspect(cs,0);
if (cs->pos!=1||ct==NULL) calc_error=CALCERR_SYNTAX;
else
if (ct->type==CALCTOK_NUM) last_res = res = ct->value;
else calc_error=CALCERR_SYNTAX;
}
done_calcstack(cs);
return res;
}
int reduce_stack(CalcStack* cs){
CalcTok *cs1,*cs2,*cs3,*cs4,*nt;
int t1,t2,t3,t4;
cs1=calcinspect(cs,0);
cs2=calcinspect(cs,-1);
cs3=calcinspect(cs,-2);
cs4=calcinspect(cs,-3);
t1=(cs1) ? (cs1->type):(0);
t2=(cs2) ? (cs2->type):(0);
t3=(cs3) ? (cs3->type):(0);
t4=(cs4) ? (cs4->type):(0);
//L operator
if(t1==CALCTOK_NUM&&t2==CALCTOK_LOP){
int lvl;
calcpop(cs);calcpop(cs);
if(cs1->value>=0&&cs1->value<=XPT_MAX){
nt=(CalcTok*)malloc(sizeof(CalcTok));
nt->type=CALCTOK_NUM;
lvl=(int)trunc(cs1->value);
nt->value=XPL(lvl)+(cs1->value-lvl)*XPLDIFF(lvl,lvl+1);
calcpush(cs,nt);
} else calc_error=CALCERR_LOPSYNTAX;
free(cs1);free(cs2);
return 1;
}
//X operator
if(t1==CALCTOK_NUM&&t2==CALCTOK_XOP){
int i;
calcpop(cs);calcpop(cs);
if(cs1->value>=0&&cs1->value<=XPL(XPT_MAX)){
nt=(CalcTok*)malloc(sizeof(CalcTok));
nt->type=CALCTOK_NUM;
for(i=0;i<=XPT_MAX;i++) {
if(XPL(i)>=cs1->value) break;
}
i--;
nt->value=i+(cs1->value-XPL(i))/(XPLDIFF(i,i+1));
calcpush(cs,nt);
} else calc_error=CALCERR_XOPSYNTAX;
free(cs1);free(cs2);
return 1;
}
//mul
if(t1==CALCTOK_NUM&&t2==CALCTOK_MUL&&t3==CALCTOK_NUM){
calcpop(cs);calcpop(cs);calcpop(cs);
nt=(CalcTok*)malloc(sizeof(CalcTok));
nt->type=CALCTOK_NUM;
nt->value=cs1->value*cs3->value;
calcpush(cs,nt);
free(cs1);free(cs2);free(cs3);
return 1;
}
//div
if(t1==CALCTOK_NUM&&t2==CALCTOK_DIV&&t3==CALCTOK_NUM){
calcpop(cs);calcpop(cs);calcpop(cs);
nt=(CalcTok*)malloc(sizeof(CalcTok));
if(cs1->value!=0){
nt->type=CALCTOK_NUM;
nt->value=cs3->value/cs1->value;
} else calc_error=CALCERR_DIVIDE;
calcpush(cs,nt);
free(cs1);free(cs2);free(cs3);
return 1;
}
//plus&minus
if(
(t1==CALCTOK_PLUS||t1==CALCTOK_MINUS||t1==CALCTOK_END||t1==CALCTOK_CPAR)&&
t2==CALCTOK_NUM&&
(t3==CALCTOK_PLUS||t3==CALCTOK_MINUS)&&
t4==CALCTOK_NUM
){
calcpop(cs);calcpop(cs);calcpop(cs);calcpop(cs);
nt=(CalcTok*)malloc(sizeof(CalcTok));
nt->type=CALCTOK_NUM;
if(t3==CALCTOK_MINUS) nt->value=cs4->value-cs2->value;
else nt->value=cs4->value+cs2->value;
calcpush(cs,nt);
calcpush(cs,cs1);
free(cs2);free(cs3);free(cs4);
return 1;
}
//modulo
if(t1==CALCTOK_NUM&&t2==CALCTOK_MOD&&t3==CALCTOK_NUM){
calcpop(cs);calcpop(cs);calcpop(cs);
nt=(CalcTok*)malloc(sizeof(CalcTok));
if(cs1->value!=0){
nt->type=CALCTOK_NUM;
nt->value=fmod(cs3->value, cs1->value);
} else calc_error=CALCERR_DIVIDE;
calcpush(cs,nt);
free(cs1);free(cs2);free(cs3);
return 1;
}
//pars
if(t1==CALCTOK_CPAR&&t2==CALCTOK_NUM&&t3==CALCTOK_OPAR){
calcpop(cs);calcpop(cs);calcpop(cs);
calcpush(cs,cs2);
free(cs1);free(cs3);
return 1;
}
//done
if(t1==CALCTOK_END&&t2==CALCTOK_NUM&&t3==0){
calcpop(cs);calcpop(cs);
calcpush(cs,cs2);
free(cs1);
return 1;
}
return 0;
}
CalcTok* next_calctoken(char *str, int *spos){
CalcTok* ct = (CalcTok*) malloc(sizeof(CalcTok));
int startpos = 0;
int pos = *spos;
char *pp;
ct->type=CALCTOK_END;
ct->value=0;
while(str[pos]==' ') pos++;
startpos=pos;
switch (str[pos]){
case '(':
ct->type=CALCTOK_OPAR;pos++;
break;
case ')':
ct->type=CALCTOK_CPAR;pos++;
break;
case '+':
ct->type=CALCTOK_PLUS;pos++;
break;
case '-':
ct->type=CALCTOK_MINUS;pos++;
break;
case '*':
ct->type=CALCTOK_MUL;pos++;
break;
case '/':
ct->type=CALCTOK_DIV;pos++;
break;
case '%':
ct->type=CALCTOK_MOD;pos++;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
ct->type=CALCTOK_NUM;
ct->value=strtod(str+startpos,&pp);
pos=pp-str;
if (str[pos]=='K'||str[pos]=='k'){
pos++;
ct->value*=1000;
}
break;
case 'M':
case 'm':
ct->type=CALCTOK_NUM;ct->value=last_res;pos++;
break;
case 'X':
case 'x':
ct->type=CALCTOK_XOP;pos++;
break;
case 'L':
case 'l':
ct->type=CALCTOK_LOP;pos++;
break;
case '\0':
ct->type=CALCTOK_END;
break;
default:
ct->type=CALCTOK_END;calc_error=CALCERR_SYNTAX;
break;
}
*spos=pos;
return ct;
}
//Token stack
CalcStack* init_calcstack(){
CalcStack* cs = (CalcStack*) malloc(sizeof(CalcStack));
cs->pos=0;
cs->stack = (CalcTok**)malloc(CALCSTACKMAX*sizeof(CalcTok*));
return cs;
}
void done_calcstack(CalcStack* cs){
int i;
for (i=0;i<cs->pos;i++) free(cs->stack[i]);
free(cs->stack);
free(cs);
}
void calcpush(CalcStack* cs, CalcTok* ct){
if (cs->pos+1>CALCSTACKMAX) {
calc_error=CALCERR_MEM;
return;
}
cs->stack[cs->pos]=ct;
cs->pos++;
}
CalcTok* calcpop(CalcStack *cs){
if (cs->pos<=0) {
calc_error=CALCERR_MEM;
return NULL;
}
cs->pos--;
return cs->stack[cs->pos+1];
}
CalcTok* calcinspect(CalcStack *cs, int p){
if (cs->pos+p-1<0) return NULL;
return cs->stack[cs->pos+p-1];
}
/*
int main(int args, char**argv){
char* test="L0";
double res=calc_exp(test);
if (trunc(res)==res) printf("%.0f-----------%i\n",res,calc_error);
else printf("%.2f-----------%i\n",res,calc_error);
}
*/