Mercurial > libavcodec.hg
comparison eval.c @ 4085:16b88d6b3546 libavcodec
factorize AVEvalExpr alloc and init
author | michael |
---|---|
date | Fri, 27 Oct 2006 20:38:10 +0000 |
parents | cedb63307f3d |
children | 9076b11ea35f |
comparison
equal
deleted
inserted
replaced
4084:90e848a24c70 | 4085:16b88d6b3546 |
---|---|
281 } | 281 } |
282 | 282 |
283 return d; | 283 return d; |
284 } | 284 } |
285 | 285 |
286 static AVEvalExpr * new_eval_expr(int type, int value, AVEvalExpr *p0, AVEvalExpr *p1){ | |
287 AVEvalExpr * e = av_mallocz(sizeof(AVEvalExpr)); | |
288 e->type =type ; | |
289 e->value =value ; | |
290 e->param[0] =p0 ; | |
291 e->param[1] =p1 ; | |
292 return e; | |
293 } | |
294 | |
286 static AVEvalExpr * parse_pow(Parser *p, int *sign){ | 295 static AVEvalExpr * parse_pow(Parser *p, int *sign){ |
287 *sign= (*p->s == '+') - (*p->s == '-'); | 296 *sign= (*p->s == '+') - (*p->s == '-'); |
288 p->s += *sign&1; | 297 p->s += *sign&1; |
289 return parse_primary(p); | 298 return parse_primary(p); |
290 } | 299 } |
291 | 300 |
292 static AVEvalExpr * parse_factor(Parser *p){ | 301 static AVEvalExpr * parse_factor(Parser *p){ |
293 int sign, sign2; | 302 int sign, sign2; |
294 AVEvalExpr * e = parse_pow(p, &sign); | 303 AVEvalExpr * e = parse_pow(p, &sign); |
295 while(p->s[0]=='^'){ | 304 while(p->s[0]=='^'){ |
296 AVEvalExpr * tmp = av_mallocz(sizeof(AVEvalExpr)); | 305 AVEvalExpr * tmp; |
297 | 306 |
298 p->s++; | 307 p->s++; |
299 | 308 tmp= new_eval_expr(e_pow, 1, e, parse_pow(p, &sign2)); |
300 tmp->type = e_pow; | |
301 tmp->value = 1.; | |
302 tmp->param[0] = e; | |
303 tmp->param[1] = parse_pow(p, &sign2); | |
304 if (tmp->param[1]) tmp->param[1]->value *= (sign2|1); | 309 if (tmp->param[1]) tmp->param[1]->value *= (sign2|1); |
305 e = tmp; | 310 e = tmp; |
306 } | 311 } |
307 if (e) e->value *= (sign|1); | 312 if (e) e->value *= (sign|1); |
308 return e; | 313 return e; |
309 } | 314 } |
310 | 315 |
311 static AVEvalExpr * parse_term(Parser *p){ | 316 static AVEvalExpr * parse_term(Parser *p){ |
312 AVEvalExpr * e = parse_factor(p); | 317 AVEvalExpr * e = parse_factor(p); |
313 while(p->s[0]=='*' || p->s[0]=='/'){ | 318 while(p->s[0]=='*' || p->s[0]=='/'){ |
314 AVEvalExpr * tmp = av_mallocz(sizeof(AVEvalExpr)); | 319 int c= *p->s++; |
315 if(*p->s++ == '*') tmp->type = e_mul; | 320 e= new_eval_expr(c == '*' ? e_mul : e_div, 1, e, parse_factor(p)); |
316 else tmp->type = e_div; | |
317 tmp->value = 1.; | |
318 tmp->param[0] = e; | |
319 tmp->param[1] = parse_factor(p); | |
320 e = tmp; | |
321 } | 321 } |
322 return e; | 322 return e; |
323 } | 323 } |
324 | 324 |
325 static AVEvalExpr * parse_expr(Parser *p) { | 325 static AVEvalExpr * parse_expr(Parser *p) { |
330 p->stack_index--; | 330 p->stack_index--; |
331 | 331 |
332 e = parse_term(p); | 332 e = parse_term(p); |
333 | 333 |
334 while(*p->s == '+' || *p->s == '-') { | 334 while(*p->s == '+' || *p->s == '-') { |
335 AVEvalExpr * tmp = av_mallocz(sizeof(AVEvalExpr)); | 335 e= new_eval_expr(e_add, 1, e, parse_term(p)); |
336 tmp->type = e_add; | |
337 tmp->value = 1.; | |
338 tmp->param[0] = e; | |
339 tmp->param[1] = parse_term(p); | |
340 e = tmp; | |
341 }; | 336 }; |
342 | 337 |
343 p->stack_index++; | 338 p->stack_index++; |
344 | 339 |
345 return e; | 340 return e; |