add physical constants in eval_expr.y
This commit is contained in:
parent
fc3a3ed4e0
commit
b7c6118288
|
|
@ -16,6 +16,7 @@ struct symrec
|
||||||
{
|
{
|
||||||
char *name; /* name of symbol */
|
char *name; /* name of symbol */
|
||||||
double (*fnctptr)(double); /* value of a FNCT */
|
double (*fnctptr)(double); /* value of a FNCT */
|
||||||
|
double value;
|
||||||
struct symrec *next; /* link field */
|
struct symrec *next; /* link field */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -31,29 +32,41 @@ static void kkerror(char *s);
|
||||||
static double toint(double x);
|
static double toint(double x);
|
||||||
static void get_expr(double x);
|
static void get_expr(double x);
|
||||||
static void get_char(int c);
|
static void get_char(int c);
|
||||||
|
static double rnd6(double x) {return round_to_n_digits(x, 6);}
|
||||||
|
|
||||||
struct fn
|
struct fn
|
||||||
{
|
{
|
||||||
char *fname;
|
char *fname;
|
||||||
double (*fnct)();
|
double (*fnct)();
|
||||||
|
double value;
|
||||||
};
|
};
|
||||||
static int lex_state = 0;
|
static int lex_state = 0;
|
||||||
struct fn fn_array[]
|
struct fn fn_array[]
|
||||||
= {
|
= {
|
||||||
{"int" , toint},
|
{"int" , toint, 0},
|
||||||
{"sin" , sin},
|
{"sin" , sin, 0},
|
||||||
{"cos" , cos},
|
{"cos" , cos, 0},
|
||||||
{"asin", asin},
|
{"exp" , exp, 0},
|
||||||
{"acos", acos},
|
{"asin" , asin, 0},
|
||||||
{"atan", atan},
|
{"acos" , acos, 0},
|
||||||
{"log" , log10},
|
{"atan" , atan, 0},
|
||||||
{"ln" , log},
|
{"log" , log10, 0},
|
||||||
{"exp" , exp},
|
{"ln" , log, 0},
|
||||||
{"sqrt", sqrt},
|
{"exp" , exp, 0},
|
||||||
{0 , 0}
|
{"sqrt" , sqrt, 0},
|
||||||
|
{"round" , rnd6, 0},
|
||||||
|
{"pi" , NULL, 3.141592653589793},
|
||||||
|
{"e" , NULL, 2.718281828459045},
|
||||||
|
{"k" , NULL, 1.380649e-23},
|
||||||
|
{"h" , NULL, 6.62607e-34},
|
||||||
|
{"echarge" , NULL, 1.60217646e-19},
|
||||||
|
{"abszero" , NULL, 273.15},
|
||||||
|
{"c" , NULL, 2.99792458e8},
|
||||||
|
{0 , 0, 0}
|
||||||
};
|
};
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
|
||||||
/* %define api.prefix {kk} */
|
/* %define api.prefix {kk} */
|
||||||
%union {
|
%union {
|
||||||
int c;
|
int c;
|
||||||
|
|
@ -87,7 +100,8 @@ line:
|
||||||
;
|
;
|
||||||
|
|
||||||
exp: NUM {$$ = $1;}
|
exp: NUM {$$ = $1;}
|
||||||
| FNCT '(' exp ')' {$$ = $1 ? (*($1->fnctptr))($3) : 0.0;}
|
| FNCT '(' exp ')' {$$ = $1 ? ($1->fnctptr ? (*($1->fnctptr))($3) : 0.0) : 0.0;}
|
||||||
|
| FNCT {$$ = $1 ? $1->value : 0.0;}
|
||||||
| exp '+' exp {$$ = $1 + $3; }
|
| exp '+' exp {$$ = $1 + $3; }
|
||||||
| exp '-' exp {$$ = $1 - $3;}
|
| exp '-' exp {$$ = $1 - $3;}
|
||||||
| exp '*' exp {$$ = $1 * $3;}
|
| exp '*' exp {$$ = $1 * $3;}
|
||||||
|
|
@ -187,6 +201,7 @@ void eval_expr_init_table(void) /* puts arithmetic functions in table. */
|
||||||
{
|
{
|
||||||
ptr = putsym (fn_array[i].fname);
|
ptr = putsym (fn_array[i].fname);
|
||||||
ptr->fnctptr = fn_array[i].fnct;
|
ptr->fnctptr = fn_array[i].fnct;
|
||||||
|
ptr->value = fn_array[i].value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -238,7 +253,7 @@ static int kklex()
|
||||||
str--;
|
str--;
|
||||||
|
|
||||||
sscanf(str, "%99[.0-9a-zA-Z_]%n", s, &rd);
|
sscanf(str, "%99[.0-9a-zA-Z_]%n", s, &rd);
|
||||||
kklval.val = atof_spice(s);
|
kklval.val = atof_eng(s);
|
||||||
str += rd;
|
str += rd;
|
||||||
dbg(dbglev, "lex(): NUM: %s\n", s);
|
dbg(dbglev, "lex(): NUM: %s\n", s);
|
||||||
return NUM;
|
return NUM;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue