Subversion Repositories wimsdev

Rev

Rev 7798 | Rev 8160 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7798 Rev 7847
Line 436... Line 436...
436
{
436
{
437
    return p;
437
    return p;
438
}
438
}
439
 
439
 
440
char *(*substitute) (char *p)=_substit;
440
char *(*substitute) (char *p)=_substit;
-
 
441
 
-
 
442
double checked_eval( char* p)
-
 
443
{
-
 
444
    set_evalue_error(0);
-
 
445
    set_evalue_pointer(p);
-
 
446
    return _evalue(10);
-
 
447
}
441
 
448
 
442
/* evalue a string to double */
449
/* evalue a string to double */
443
double strevalue(char *p)
450
double strevalue(char *p)
444
{
451
{
445
    char buf[MAX_LINELEN+1];
452
    char buf[MAX_LINELEN+1];
446
 
453
 
447
    if(p==NULL) return 0;
454
    if(p==NULL) return 0;
448
    mystrncpy(buf,p,sizeof(buf));
455
    mystrncpy(buf,p,sizeof(buf));
449
    substitute(buf); nospace(buf);
456
    substitute(buf); nospace(buf);
450
    if(check_parentheses(buf,0)) return NAN;
457
    if(check_parentheses(buf,0)) {return NAN;}
451
    set_evalue_error(0);
-
 
452
    set_evalue_pointer(buf);
-
 
453
    return _evalue(10);
458
    return checked_eval(buf);
454
}
459
}
-
 
460
 
455
 
461
 
456
/* compile an expression for faster evaluation
462
/* compile an expression for faster evaluation
457
 * returns -1 if cannot be compiled.
463
 * returns -1 if cannot be compiled.
458
 * else returns the number of compilations.
464
 * else returns the number of compilations.
459
 */
465
 */
Line 477... Line 483...
477
 */
483
 */
478
    if(ev_varcnt!=NULL && ev_var!=NULL && *ev_varcnt>0) {
484
    if(ev_varcnt!=NULL && ev_var!=NULL && *ev_varcnt>0) {
479
        for(i=0;i<*ev_varcnt && strcmp(name,ev_var[i].name)!=0;i++);
485
        for(i=0;i<*ev_varcnt && strcmp(name,ev_var[i].name)!=0;i++);
480
        if(i<*ev_varcnt && i<2000) {
486
        if(i<*ev_varcnt && i<2000) {
481
        buf[0]=i/200+140; buf[1]=i%200+33; buf[2]=0;
487
        buf[0]=i/200+140; buf[1]=i%200+33; buf[2]=0;
482
        string_modify(p,p1,p2,"%s",buf);
488
        string_modify(p,p1,p2,"%s",buf);
483
        pe=p1+2; k++; continue;
489
        pe=p1+2; k++; continue;
484
        }
490
        }
485
    }
491
    }
486
    i=search_list(evalname,evalname_no,sizeof(evalname[0]),name);
492
    i=search_list(evalname,evalname_no,sizeof(evalname[0]),name);
487
    if(i>=0 && i<2000) {
493
    if(i>=0 && i<2000) {
Line 490... Line 496...
490
        pe=p1+2; k++; continue;
496
        pe=p1+2; k++; continue;
491
    }
497
    }
492
  }
498
  }
493
  return k;
499
  return k;
494
}
500
}
-
 
501
 
-
 
502
/* add evaluator (Dominique Bernardi june 2014)
-
 
503
In addition to the general evaluation functions, there is a simple mean
-
 
504
to evaluate standard functions with at most four variables
-
 
505
named "x", "y", "s" and "t". The simplest one is
-
 
506
 
-
 
507
double eval_simple (char *p, double x, double y, double s, double t);
-
 
508
 
-
 
509
which does exactly that. In case of multiple evaluation of the same function
-
 
510
for different values of the variable(s), it is possible to speed up a bit
-
 
511
the evaluation by "precompiling" the string to be evaluated.
-
 
512
This precompilation is done by
-
 
513
 
-
 
514
eval_struct* eval_create (char *p);
-
 
515
 
-
 
516
which returns a newly allocated pointer to something. One can use
-
 
517
this pointer in functions like
-
 
518
 
-
 
519
eval_x, eval_t, eval_x_y
-
 
520
 
-
 
521
When the struct is no longer useful, one can reclaim the memory it used with
-
 
522
 
-
 
523
void eval_destroy (eval_struct *q);
-
 
524
 
-
 
525
*/
-
 
526
 
-
 
527
void _aux (char *q, char *varn, char *subst, int *v)
-
 
528
{
-
 
529
  char *pp;
-
 
530
  for(pp=varchr(q,varn); pp; pp=varchr(pp,varn))
-
 
531
    {
-
 
532
      string_modify(q,pp,pp+strlen(varn),"%s",subst);
-
 
533
      pp+=strlen(subst);
-
 
534
    }
-
 
535
  *v = eval_getpos(subst);
-
 
536
}
-
 
537
 
-
 
538
eval_struct * eval_create (char *in_p)
-
 
539
{
-
 
540
  eval_struct *p = malloc (sizeof(eval_struct));
-
 
541
  char *q = malloc(MAX_LINELEN+1);
-
 
542
  strncpy (q, in_p, MAX_LINELEN);
-
 
543
  _aux (q, "x", EV_X, &p->x);
-
 
544
  _aux (q, "y", EV_Y, &p->y);
-
 
545
  _aux (q, "s", EV_S, &p->s);
-
 
546
  _aux (q, "t", EV_T, &p->t);
-
 
547
  evalue_compile(q);
-
 
548
  p->texte = q;
-
 
549
  return p;
-
 
550
}
-
 
551
 
-
 
552
double eval_multiple (eval_struct *p, double x, double y, double s, double t)
-
 
553
{
-
 
554
  eval_setval(p->x,x);
-
 
555
  eval_setval(p->y,y);
-
 
556
  eval_setval(p->s,s);
-
 
557
  eval_setval(p->t,t);
-
 
558
  return checked_eval(p->texte);
-
 
559
}
-
 
560
/* non yet useful
-
 
561
double eval_simple (char *p, double x, double y, double s, double t)
-
 
562
{
-
 
563
  eval_struct *q = eval_create (p);
-
 
564
  double r = eval (q, x, y, s, t);
-
 
565
  eval_destroy(q);
-
 
566
  return r;
-
 
567
}
-
 
568
 
-
 
569
*/
-
 
570
 
-
 
571
double eval_x (eval_struct *p, double x)
-
 
572
{
-
 
573
  eval_setval(p->x,x);
-
 
574
  return checked_eval(p->texte);
-
 
575
}
-
 
576
 
-
 
577
double eval_t (eval_struct *p, double t)
-
 
578
{
-
 
579
  eval_setval(p->t,t);
-
 
580
  return checked_eval(p->texte);
-
 
581
}
-
 
582
 
-
 
583
double eval_x_y (eval_struct *p, double x, double y)
-
 
584
{
-
 
585
  eval_setval(p->x,x);
-
 
586
  eval_setval(p->y,y);
-
 
587
  return checked_eval(p->texte);
-
 
588
}
-
 
589
 
-
 
590
void eval_destroy(eval_struct *q) {free (q->texte); free (q);}