56 typedef double (*
mp_f2_f)(double, double);
58 template <
class Tin,
class Tout, mp_f1_f F>
62 ASSERT(args.size() == 1 + 1);
66 Tout *out =
dynamic_cast<Tout *
>(args[0]);
69 Tin *arg1 =
dynamic_cast<Tin *
>(args[1]);
77 template <
class Tin,
class Tout, mp_f2_f F>
81 ASSERT(args.size() == 1 + 1);
86 Tout *out =
dynamic_cast<Tout *
>(args[0]);
89 Tin *arg1 =
dynamic_cast<Tin *
>(args[1]);
92 Tin *arg2 =
dynamic_cast<Tin *
>(args[2]);
105 ASSERT(args.size() == 1 + 1);
111 const Real a1 = (*arg1)();
112 if (a1 > 1. || a1 < -1.) {
122 ASSERT(args.size() == 1 + 1);
128 const Real a1 = (*arg1)();
129 if (a1 > 1. || a1 < -1.) {
139 ASSERT(args.size() == 1 + 1);
148 if (
fabs(
fabs(a) - M_PI_2) < std::numeric_limits<double>::epsilon()) {
158 ASSERT(args.size() == 1 + 1);
164 if ((*arg1)() <= 1.) {
174 ASSERT(args.size() == 1 + 1);
181 if (a1 >= 1. || a1 <= -1.) {
191 ASSERT(args.size() == 1 + 1);
197 if ((*arg1)() <= 0.) {
207 ASSERT(args.size() == 1 + 1);
213 if ((*arg1)() < 0.) {
223 ASSERT(args.size() == 1 + 0);
237 ASSERT(args.size() == 1 + 0);
243 *out = -1. + 2.*(
Real(rand())/
Real(RAND_MAX));
251 ASSERT(args.size() == 1 + 1);
258 srand((
unsigned int)(*arg1)());
266 ASSERT(args.size() == 1 + 2);
276 const char *fmt = ((std::string&)(*arg1)()).c_str();
279 switch ((*arg2)().GetType()) {
281 rc = snprintf(buf,
sizeof(buf), fmt, (*arg2)().GetBool());
285 rc = snprintf(buf,
sizeof(buf), fmt, (*arg2)().GetInt());
289 rc = snprintf(buf,
sizeof(buf), fmt, (*arg2)().GetReal());
293 rc = snprintf(buf,
sizeof(buf), fmt, (*arg2)().GetString().c_str());
301 if (rc < 0 || (
unsigned long)rc >=
sizeof(buf)) {
308 *out = std::string(buf);
346 ASSERT(args.size() == 1 + 1);
348 switch (args[1]->Type()) {
350 silent_cout((*dynamic_cast<MathParser::MathArgBool_t*>(args[1])) << std::endl);
354 silent_cout((*dynamic_cast<MathParser::MathArgInt_t*>(args[1])) << std::endl);
358 silent_cout((*dynamic_cast<MathParser::MathArgReal_t*>(args[1])) << std::endl);
362 silent_cout((*dynamic_cast<MathParser::MathArgString_t*>(args[1])) << std::endl);
375 ASSERT(args.size() == 1 + 2);
388 silent_cout(
"mp_stop(SUCCESS)" << std::endl);
392 silent_cout(
"mp_stop(FAILURE)" << std::endl);
403 ASSERT(args.size() == 1 + 1);
413 *out = 1./
tan((*arg1)());
421 ASSERT(args.size() == 1 + 1);
430 if (
fabs(a) < std::numeric_limits<double>::epsilon()) {
440 ASSERT(args.size() == 1 + 1);
450 *out =
atan2(1., (*arg1)());
458 ASSERT(args.size() == 1 + 2);
475 *out =
atan2(a2, a1);
483 ASSERT(args.size() == 1 + 1);
493 *out = 1./
tanh((*arg1)());
501 ASSERT(args.size() == 1 + 1);
507 if (
fabs((*arg1)()) < std::numeric_limits<double>::epsilon()) {
517 ASSERT(args.size() == 1 + 1);
540 ASSERT(args.size() == 1 + 2);
556 *out = std::max(a1, a2);
564 ASSERT(args.size() == 1 + 2);
580 *out = std::min(a1, a2);
589 ASSERT(args.size() == 1 + 1);
599 *out = atanh(1./(*arg1)());
608 ASSERT(args.size() == 1 + 1);
622 }
else if (a1 < 0.) {
635 ASSERT(args.size() == 1 + 1);
659 ASSERT(args.size() == 1 + 2);
678 }
else if (a1 > a2) {
691 ASSERT(args.size() == 1 + 1);
723 ASSERT(args.size() == 1 + 3);
789 ASSERT(args.size() == 1 + 1);
795 switch (args[0]->Type()) {
799 (*out)() = (*arg1)().GetBool();
805 (*out)() = (*arg1)().GetInt();
811 (*out)() = (*arg1)().GetReal();
904 : type(
TypedValue::VAR_STRING), bConst(isConst), s(s)
910 : type(t), bConst(isConst)
933 : type(var.type), bConst(
false)
974 snprintf(buf,
sizeof(buf),
"%ld", (
long)var.
GetInt());
975 Set(std::string(buf));
979 snprintf(buf,
sizeof(buf),
"%e", (
double)var.
GetReal());
980 Set(std::string(buf));
1045 snprintf(buf,
sizeof(buf),
"%ld", (
long)var.
GetInt());
1046 Set(std::string(buf));
1051 snprintf(buf,
sizeof(buf),
"%e", (
double)var.
GetReal());
1052 Set(std::string(buf));
1074 if (r != 0. && r != 1.) {
1075 silent_cerr(
" Error: implicit cast "
1079 <<
" alters its value"
1085 silent_cerr(
"Warning: implicit cast "
1088 <<
" may alter its value"
1108 silent_cerr(
" Error: implicit cast "
1112 <<
" alters its value"
1118 silent_cerr(
"Warning: implicit cast "
1121 <<
" may alter its value"
1168 return TypeNames[t].
name;
1277 if (
Const() && !isConst && !bForce) {
1452 snprintf(buf,
sizeof(buf),
"%ld", (
long)v.
GetInt());
1456 snprintf(buf,
sizeof(buf),
"%e", (
double)v.
GetReal());
1546 snprintf(buf,
sizeof(buf),
"%ld", (
long)v.
GetInt());
1551 snprintf(buf,
sizeof(buf),
"%e", (
double)v.
GetReal());
1700 return out << v.
GetInt();
1705 std::ostringstream os;
1707 if (os.str().find(
'.') == std::string::npos) {
1710 return out << os.str();
1714 return out <<
'"' << v.
GetString() <<
'"';
1869 for (i = 0; isspace(s[i]); ++i) {
1875 memmove(s, &s[i], l + 1);
1878 for (i = l - 1; isspace(s[i]); --i) {
1900 return pgin->GetType();
1918 return pgin->GetVal();
1956 }
else if (t ==
NAME) {
2022 f->
fname = std::string(
"asin");
2024 f->
args.resize(1 + 1);
2027 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, asin>;
2029 f->
errmsg = std::string(
"invalid arg to asin()");
2031 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2032 silent_cerr(
"static namespace: "
2033 "unable to insert handler "
2034 "for function " << f->
fname << std::endl);
2040 f->
fname = std::string(
"acos");
2042 f->
args.resize(1 + 1);
2045 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, acos>;
2047 f->
errmsg = std::string(
"invalid arg to acos()");
2049 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2050 silent_cerr(
"static namespace: "
2051 "unable to insert handler "
2052 "for function " << f->
fname << std::endl);
2058 f->
fname = std::string(
"atan");
2060 f->
args.resize(1 + 1);
2063 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, atan>;
2066 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2067 silent_cerr(
"static namespace: "
2068 "unable to insert handler "
2069 "for function " << f->
fname << std::endl);
2075 f->
fname = std::string(
"actan");
2077 f->
args.resize(1 + 1);
2083 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2084 silent_cerr(
"static namespace: "
2085 "unable to insert handler "
2086 "for function " << f->
fname << std::endl);
2092 f->
fname = std::string(
"atan2");
2094 f->
args.resize(1 + 2);
2098 f->
f = mp_func_2<MathParser::MathArgReal_t, MathParser::MathArgReal_t, atan2>;
2101 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2102 silent_cerr(
"static namespace: "
2103 "unable to insert handler "
2104 "for function " << f->
fname << std::endl);
2110 f->
fname = std::string(
"actan2");
2112 f->
args.resize(1 + 2);
2119 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2120 silent_cerr(
"static namespace: "
2121 "unable to insert handler "
2122 "for function " << f->
fname << std::endl);
2128 f->
fname = std::string(
"cos");
2130 f->
args.resize(1 + 1);
2133 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, cos>;
2136 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2137 silent_cerr(
"static namespace: "
2138 "unable to insert handler "
2139 "for function " << f->
fname << std::endl);
2145 f->
fname = std::string(
"sin");
2147 f->
args.resize(1 + 1);
2150 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, sin>;
2153 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2154 silent_cerr(
"static namespace: "
2155 "unable to insert handler "
2156 "for function " << f->
fname << std::endl);
2162 f->
fname = std::string(
"tan");
2164 f->
args.resize(1 + 1);
2167 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, tan>;
2170 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2171 silent_cerr(
"static namespace: "
2172 "unable to insert handler "
2173 "for function " << f->
fname << std::endl);
2179 f->
fname = std::string(
"ctan");
2181 f->
args.resize(1 + 1);
2187 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2188 silent_cerr(
"static namespace: "
2189 "unable to insert handler "
2190 "for function " << f->
fname << std::endl);
2196 f->
fname = std::string(
"cosh");
2198 f->
args.resize(1 + 1);
2201 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, cosh>;
2204 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2205 silent_cerr(
"static namespace: "
2206 "unable to insert handler "
2207 "for function " << f->
fname << std::endl);
2213 f->
fname = std::string(
"sinh");
2215 f->
args.resize(1 + 1);
2218 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, sinh>;
2221 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2222 silent_cerr(
"static namespace: "
2223 "unable to insert handler "
2224 "for function " << f->
fname << std::endl);
2230 f->
fname = std::string(
"tanh");
2232 f->
args.resize(1 + 1);
2235 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, tanh>;
2238 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2239 silent_cerr(
"static namespace: "
2240 "unable to insert handler "
2241 "for function " << f->
fname << std::endl);
2247 f->
fname = std::string(
"ctanh");
2249 f->
args.resize(1 + 1);
2255 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2256 silent_cerr(
"static namespace: "
2257 "unable to insert handler "
2258 "for function " << f->
fname << std::endl);
2265 f->
fname = std::string(
"acosh");
2267 f->
args.resize(1 + 1);
2270 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, acosh>;
2273 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2274 silent_cerr(
"static namespace: "
2275 "unable to insert handler "
2276 "for function " << f->
fname << std::endl);
2282 f->
fname = std::string(
"asinh");
2284 f->
args.resize(1 + 1);
2287 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, asinh>;
2290 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2291 silent_cerr(
"static namespace: "
2292 "unable to insert handler "
2293 "for function " << f->
fname << std::endl);
2299 f->
fname = std::string(
"atanh");
2301 f->
args.resize(1 + 1);
2304 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, atanh>;
2307 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2308 silent_cerr(
"static namespace: "
2309 "unable to insert handler "
2310 "for function " << f->
fname << std::endl);
2316 f->
fname = std::string(
"actanh");
2318 f->
args.resize(1 + 1);
2324 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2325 silent_cerr(
"static namespace: "
2326 "unable to insert handler "
2327 "for function " << f->
fname << std::endl);
2334 f->
fname = std::string(
"exp");
2336 f->
args.resize(1 + 1);
2339 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, exp>;
2342 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2343 silent_cerr(
"static namespace: "
2344 "unable to insert handler "
2345 "for function " << f->
fname << std::endl);
2351 f->
fname = std::string(
"log");
2353 f->
args.resize(1 + 1);
2356 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, log>;
2359 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2360 silent_cerr(
"static namespace: "
2361 "unable to insert handler "
2362 "for function " << f->
fname << std::endl);
2368 f->
fname = std::string(
"log10");
2370 f->
args.resize(1 + 1);
2373 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, log10>;
2376 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2377 silent_cerr(
"static namespace: "
2378 "unable to insert handler "
2379 "for function " << f->
fname << std::endl);
2385 f->
fname = std::string(
"sqrt");
2387 f->
args.resize(1 + 1);
2390 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, sqrt>;
2393 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2394 silent_cerr(
"static namespace: "
2395 "unable to insert handler "
2396 "for function " << f->
fname << std::endl);
2402 f->
fname = std::string(
"abs");
2404 f->
args.resize(1 + 1);
2407 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, fabs>;
2410 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2411 silent_cerr(
"static namespace: "
2412 "unable to insert handler "
2413 "for function " << f->
fname << std::endl);
2419 f->
fname = std::string(
"sign");
2421 f->
args.resize(1 + 1);
2427 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2428 silent_cerr(
"static namespace: "
2429 "unable to insert handler "
2430 "for function " << f->
fname << std::endl);
2436 f->
fname = std::string(
"copysign");
2438 f->
args.resize(1 + 2);
2442 f->
f = mp_func_2<MathParser::MathArgReal_t, MathParser::MathArgReal_t, copysign>;
2445 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2446 silent_cerr(
"static namespace: "
2447 "unable to insert handler "
2448 "for function " << f->
fname << std::endl);
2454 f->
fname = std::string(
"max");
2456 f->
args.resize(1 + 2);
2463 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2464 silent_cerr(
"static namespace: "
2465 "unable to insert handler "
2466 "for function " << f->
fname << std::endl);
2472 f->
fname = std::string(
"min");
2474 f->
args.resize(1 + 2);
2481 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2482 silent_cerr(
"static namespace: "
2483 "unable to insert handler "
2484 "for function " << f->
fname << std::endl);
2490 f->
fname = std::string(
"floor");
2492 f->
args.resize(1 + 1);
2495 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, floor>;
2498 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2499 silent_cerr(
"static namespace: "
2500 "unable to insert handler "
2501 "for function " << f->
fname << std::endl);
2507 f->
fname = std::string(
"ceil");
2509 f->
args.resize(1 + 1);
2512 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, ceil>;
2515 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2516 silent_cerr(
"static namespace: "
2517 "unable to insert handler "
2518 "for function " << f->
fname << std::endl);
2525 f->
fname = std::string(
"round");
2527 f->
args.resize(1 + 1);
2530 f->
f = mp_func_1<MathParser::MathArgReal_t, MathParser::MathArgReal_t, rint>;
2533 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2534 silent_cerr(
"static namespace: "
2535 "unable to insert handler "
2536 "for function " << f->
fname << std::endl);
2543 f->
fname = std::string(
"rand");
2545 f->
args.resize(1 + 0);
2550 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2551 silent_cerr(
"static namespace: "
2552 "unable to insert handler "
2553 "for function " << f->
fname << std::endl);
2559 f->
fname = std::string(
"random");
2561 f->
args.resize(1 + 0);
2566 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2567 silent_cerr(
"static namespace: "
2568 "unable to insert handler "
2569 "for function " << f->
fname << std::endl);
2575 f->
fname = std::string(
"seed");
2577 f->
args.resize(1 + 1);
2583 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2584 silent_cerr(
"static namespace: "
2585 "unable to insert handler "
2586 "for function " << f->
fname << std::endl);
2592 f->
fname = std::string(
"step");
2594 f->
args.resize(1 + 1);
2600 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2601 silent_cerr(
"static namespace: "
2602 "unable to insert handler "
2603 "for function " << f->
fname << std::endl);
2609 f->
fname = std::string(
"ramp");
2611 f->
args.resize(1 + 1);
2617 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2618 silent_cerr(
"static namespace: "
2619 "unable to insert handler "
2620 "for function " << f->
fname << std::endl);
2626 f->
fname = std::string(
"sramp");
2628 f->
args.resize(1 + 2);
2635 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2636 silent_cerr(
"static namespace: "
2637 "unable to insert handler "
2638 "for function " << f->
fname << std::endl);
2644 f->
fname = std::string(
"par");
2646 f->
args.resize(1 + 1);
2652 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2653 silent_cerr(
"static namespace: "
2654 "unable to insert handler "
2655 "for function " << f->
fname << std::endl);
2661 f->
fname = std::string(
"in_ll");
2663 f->
args.resize(1 + 3);
2668 f->
f = mp_in<IN_LL>;
2671 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2672 silent_cerr(
"static namespace: "
2673 "unable to insert handler "
2674 "for function " << f->
fname << std::endl);
2679 f->
fname = std::string(
"in_le");
2681 f->
args.resize(1 + 3);
2686 f->
f = mp_in<IN_LE>;
2689 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2690 silent_cerr(
"static namespace: "
2691 "unable to insert handler "
2692 "for function " << f->
fname << std::endl);
2697 f->
fname = std::string(
"in_el");
2699 f->
args.resize(1 + 3);
2704 f->
f = mp_in<IN_EL>;
2707 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2708 silent_cerr(
"static namespace: "
2709 "unable to insert handler "
2710 "for function " << f->
fname << std::endl);
2715 f->
fname = std::string(
"in_ee");
2717 f->
args.resize(1 + 3);
2722 f->
f = mp_in<IN_EE>;
2725 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2726 silent_cerr(
"static namespace: "
2727 "unable to insert handler "
2728 "for function " << f->
fname << std::endl);
2734 f->
fname = std::string(
"print");
2736 f->
args.resize(1 + 1);
2742 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2743 silent_cerr(
"static namespace: "
2744 "unable to insert handler "
2745 "for function " << f->
fname << std::endl);
2751 f->
fname = std::string(
"sprintf");
2753 f->
args.resize(1 + 2);
2760 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2761 silent_cerr(
"static namespace: "
2762 "unable to insert handler "
2763 "for function " << f->
fname << std::endl);
2769 f->
fname = std::string(
"stop");
2771 f->
args.resize(1 + 2);
2778 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2779 silent_cerr(
"static namespace: "
2780 "unable to insert handler "
2781 "for function " << f->
fname << std::endl);
2807 f->
args.resize(1 + 1);
2808 f->
args[0] = data[i].arg;
2813 if (!
func.insert(funcType::value_type(f->
fname, f)).second) {
2814 silent_cerr(
"static namespace: "
2815 "unable to insert handler "
2816 "for function " << f->
fname << std::endl);
2825 for (funcType::iterator f =
func.begin(); f !=
func.end(); ++f) {
2834 for (funcType::const_iterator f =
func.begin(); f !=
func.end(); ++f) {
2835 silent_cerr(
"*** " << f->second->fname << std::endl);
2839 if (
func.find(fname) !=
func.end()) {
2849 funcType::const_iterator i =
func.find(fname);
2851 if (i !=
func.end()) {
2863 switch (f->
args[0]->Type()) {
2868 return TypedValue((*dynamic_cast<MathArgBool_t*>(f->
args[0]))());
2874 return TypedValue((*dynamic_cast<MathArgReal_t*>(f->
args[0]))());
2877 return TypedValue((*dynamic_cast<MathArgString_t*>(f->
args[0]))());
2893 for (
Int i = 0; TypeNames[i].
name != NULL; i++) {
2894 if (strcmp(s, TypeNames[i].name) == 0) {
2895 return TypeNames[i].
type;
2905 for (
Int i = 0; TypeModifierNames[i].
name != NULL; i++) {
2906 if (strcmp(s, TypeModifierNames[i].name) == 0) {
2907 return TypeModifierNames[i].
type;
2917 for (
Int i = 0; DeclarationModifierNames[i].
name != NULL; i++) {
2918 if (strcmp(s, DeclarationModifierNames[i].name) == 0) {
2919 return DeclarationModifierNames[i].
type;
2973 while ((c =
in->
get()), isspace(c)) {
2977 if (c == EOF ||
in->
eof()) {
2982 for (c =
in->
get(); c !=
'\n'; c =
in->
get()) {
2997 if (c ==
'.' || isdigit(c)) {
3010 while ((c =
in->
get()) ==
'.' || isdigit(c)) {
3018 if (i >=
sizeof(s)) {
3023 if (std::strchr(
"efdgEFDG", c) != 0) {
3027 if (i >=
sizeof(s)) {
3031 if ((c =
in->
get()) ==
'-' || c ==
'+') {
3033 if (i >=
sizeof(s)) {
3041 if (i >=
sizeof(s)) {
3049 while (isdigit((c =
in->
get()))) {
3051 if (i >=
sizeof(s)) {
3071 long l = strtol(s, &endptr, 10);
3072 int save_errno = errno;
3073 if (endptr == s || endptr[0] !=
'\0') {
3074 silent_cerr(
" MathParser - unable to parse \"" << s <<
"\" as integer"
3079 if (save_errno == ERANGE) {
3081 if (l == LONG_MIN) {
3084 std::string(
"integer value ") + std::string(s, endptr - s) +
" underflow");
3087 if (l == LONG_MAX) {
3090 std::string(
"integer value ") + std::string(s, endptr - s) +
" overflow");
3104 double d = strtod(s, &endptr);
3105 int save_errno = errno;
3106 if (endptr == s || endptr[0] !=
'\0') {
3107 silent_cerr(
" MathParser - unable to parse \"" << s <<
"\" as real"
3112 if (save_errno == ERANGE) {
3114 if (std::abs(d) == HUGE_VAL) {
3117 std::string(
"real value ") + std::string(s, endptr - s) +
" overflow");
3123 std::string(
"real value ") + std::string(s, endptr - s) +
" underflow");
3138 if (isalpha(c) || c ==
'_') {
3141 while ((c =
in->
get())) {
3163 if ((c =
in->
get()) ==
'*') {
3171 }
else if (c ==
'/') {
3174 silent_cerr(
"warning: '/*' "
3175 "inside a comment at line "
3178 goto end_of_comment;
3198 if ((c =
in->
get()), c ==
'=') {
3205 if ((c =
in->
get()), c ==
'=') {
3212 if ((c =
in->
get()), c ==
'=') {
3219 if ((c =
in->
get()), c ==
'=') {
3226 if ((c =
in->
get()), c !=
'&') {
3232 if ((c =
in->
get()), c !=
'|') {
3238 if ((c =
in->
get()), c !=
'|') {
3262 if ((c =
in->
get()), c !=
':') {
3269 while ((c =
in->
get()) !=
'"') {
3275 if (c == EOF ||
in->
eof()) {
3294 std::string::const_iterator i = s.begin();
3295 if (*i !=
'_' && !isalpha(*i)) {
3299 while (++i != s.end()) {
3300 if (*i !=
'_' && !isalnum(*i)) {
3339 d = ((!(d && e)) && (d || e));
3511 if (d < 0. && e <= 0.) {
3512 DEBUGCERR(
"can't compute (" << d <<
")^("
3513 << e <<
") in power()" << std::endl);
3533 for (
Int k = i-1; k-- > 0; ) {
3578 for (
unsigned i = 1; i < f->
args.size(); i++) {
3579 switch (f->
args[i]->Type()) {
3653 if (i < f->args.size() - 1) {
3659 "mandatory argument expected");
3669 "argument separator expected");
3676 if (f->
t(f->
args)) {
3679 <<
" " "(msg: " << f->
errmsg <<
")"
3680 <<
" in evalfunc()" << std::endl);
3727 std::string(
"unable to find namespace \"") +
namebuf +
"\"");
3729 currNameSpace = i->second;
3730 currTable = currNameSpace->
GetTable();
3745 std::string(
"function \"") +
namebuf +
"\" not found");
3752 std::string(
"closing parenthesis expected after function \"")
3753 + f->
fname +
"\" in expr()");
3784 bool isIfndef =
false;
3785 bool isConst =
false;
3789 switch (declarationmodifier) {
3796 std::string(
"unhandled definition modifier \"") +
namebuf +
"\"");
3801 "after definition modifier in declaration");
3807 switch (typemodifier) {
3814 std::string(
"unhandled type modifier \"") +
namebuf +
"\"");
3819 "after type modifier in declaration");
3838 "after type in declaration");
3843 +
namebuf +
"\" is a keyword");
3855 std::string(
"unable to find namespace \"") +
namebuf +
"\"");
3857 currNameSpace = i->second;
3858 currTable = currNameSpace->
GetTable();
3870 if (currTable == 0) {
3872 std::string(
"namespace \"") + currNameSpace->
sGetName()
3873 +
"\" does not support variables");
3895 v = currTable->
Put(varname.c_str(), newvar);
3898 silent_cerr(
"warning, ifndef variable " << v->
GetTypeName() <<
" \"" << name
3899 <<
"\" not yet defined; set to \"" << newvar <<
"\" at line " <<
mbdyn_get_line_data() << std::endl);
3907 std::string(
"cannot redefine var \"") + name +
"\"");
3910 if (v->
Const() && !isIfndef) {
3913 std::string(
"cannot redefine const named value \"") + name +
"\"");
3918 std::string(
"cannot redefine non-var named value \"") + name +
"\"");
3922 dynamic_cast<Var *
>(v)->SetVal(d);
3926 silent_cerr(
"warning, skipping redefinition of \"" << name.c_str() <<
"\""
3928 <<
" (orig=" << v->
GetVal() <<
", unchanged; new=" << d <<
")"
3932 silent_cerr(
"warning, skipping redefinition of " << v->
GetTypeName() <<
" \"" << name.c_str() <<
"\""
3933 <<
" (orig=" << v->
GetVal() <<
", unchanged; new=" << d <<
")"
3941 if (currTable == 0) {
3943 std::string(
"namespace \"") + currNameSpace->
sGetName()
3944 +
"\" does not support variables");
3953 std::string(
"cannot create const named value \"")
3954 +
namebuf +
"\" with no value");
3970 "definition modifier without type");
3975 "type modifier without type");
3987 std::string(
"unable to find namespace \"") +
namebuf +
"\"");
3989 currNameSpace = i->second;
3990 currTable = currNameSpace->
GetTable();
4005 std::string(
"function \"") +
namebuf +
"\" not found");
4012 std::string(
"closing parenthesis expected after function \"")
4013 + f->
fname +
"\" in expr()");
4021 if (currTable == 0) {
4023 std::string(
"namespace \"") + currNameSpace->
sGetName()
4024 +
"\" does not support variables");
4036 std::string(
"cannot assign const named value \"") + name +
"\"");
4041 std::string(
"cannot assign non-var named value \"") + name +
"\"");
4043 dynamic_cast<Var *
>(v)->Cast(d);
4071 std::vector<char *> argv(1);
4074 unsigned int i = 0, in_quotes = 0;
4109 if (in_quotes == 0) {
4114 while (isspace((c =
in->
get()))) {
4117 if (c !=
',' && c !=
']') {
4118 silent_cerr(
"need a separator "
4119 "after closing quotes" << std::endl);
4132 argv.resize(argc + 2);
4151 if (i >=
sizeof(buf)) {
4152 silent_cerr(
"MathParser::readplugin(): buffer overflow" << std::endl);
4159 silent_cerr(
"eof encountered while parsing plugin"
4173 char *pginname = argv[0];
4174 char *varname = argv[1];
4181 if (pginname == NULL || *pginname ==
'\0') {
4182 silent_cerr(
"illegal or missing plugin name" << std::endl);
4186 if (varname == NULL || *varname ==
'\0') {
4187 silent_cerr(
"illegal or missing plugin variable name"
4197 silent_cerr(
"variable \"" << varname <<
"\" already defined"
4206 if (strcasecmp(p->name, pginname) != 0) {
4210 for (
int i = 0; argv[i] != NULL; i++) {
4211 silent_cout(
"argv[" << i <<
"]=" << argv[i]
4220 pgin->
Read(argc - 2, &argv[2]);
4238 for (
int i = 0; argv[i] != NULL; i++) {
4248 silent_cerr(
" plugin '" << pginname <<
"' not supported" << std::endl);
4269 bRedefineVars(bRedefineVars),
4293 bRedefineVars(bRedefineVars),
4295 defaultNameSpace(0),
4297 currtoken(UNKNOWNTOKEN)
4331 std::string(
"cannot redefine non-var named value \"") + var->
GetName() +
"\"");
4367 std::string(
"cannot redefine non-var named value \"") + var->
GetName() +
"\"");
4379 std::string(
"error while adding integer var \"") + s +
"\"");
4413 std::cout <<
"GetLastStmt: \"", e->
Output(std::cout) <<
"\" = " << d << std::endl;
4449 MathParser::GetExpr(
void)
4459 std::cout <<
"GetExpr: \"", e->
Output(std::cout) <<
"\"" << std::endl;
4519 std::cout <<
"Get: \"", e->
Output(std::cout) <<
"\" = " << vv << std::endl;
4543 std::cout <<
"Get: \"", e->
Output(std::cout) <<
"\" = " << vv << std::endl;
4569 out <<
Get(val) << sep;
4588 pedantic_cout(
"registering plugin \"" << name <<
"\"" << std::endl);
4606 pedantic_cout(
"MathParser::RegisterNameSpace: "
4607 "registering namespace \"" << ns->
sGetName() <<
"\""
4622 NameSpaceMap::const_iterator i =
nameSpaceMap.find(name);
4696 d = EECreate<EE_Greater>(d ,
binary());
4702 d = EECreate<EE_Greater_Equal>(d ,
binary());
4708 d = EECreate<EE_Equal_Equal>(d ,
binary());
4714 d = EECreate<EE_Lesser_Equal>(d ,
binary());
4720 d = EECreate<EE_Lesser>(d ,
binary());
4726 d = EECreate<EE_Not_Equal>(d ,
binary());
4755 d = EECreate<EE_Plus>(d ,
mult());
4761 d = EECreate<EE_Minus>(d ,
mult());
4790 d = EECreate<EE_Multiply>(d ,
power());
4796 d = EECreate<EE_Divide>(d ,
power());
4802 d = EECreate<EE_Modulus>(d ,
power());
4839 d = EECreate<EE_Power>(d ,
power());
4853 return EECreate<EE_Unary_minus>(
expr());
4862 return EECreate<EE_NOT>(
expr());
4870 MathParser::parsefunc(MathFunc_t* f)
4872 for (
unsigned i = 1; i < f->args.size(); i++) {
4873 switch (f->args[i]->Type()) {
4881 switch (f->args[i]->Type()) {
4902 f->args[i]->SetExpr(ee);
4903 if (dynamic_cast<EE_Value *>(ee)) {
4918 if (i < f->args.size() - 1) {
4924 "mandatory argument expected");
4934 "argument separator expected");
4982 std::string(
"unable to find namespace \"") +
namebuf +
"\"");
4984 currNameSpace = i->second;
4985 currTable = currNameSpace->
GetTable();
5001 std::string(
"function \"") + name +
"\" not found");
5007 std::string(
"closing parenthesis expected after function \"") + f->fname +
"\" in expr()");
5020 return new EE_Var(v, currNameSpace);
5044 bool bIsIfndef =
false;
5045 bool bIsConst =
false;
5049 switch (declarationmodifier) {
5056 std::string(
"unhandled definition modifier \"") +
namebuf +
"\"");
5061 "after definition modifier in declaration");
5067 switch (typemodifier) {
5074 std::string(
"unhandled type modifier \"") +
namebuf +
"\"");
5079 "after type modifier in declaration");
5098 "after type in declaration");
5103 std::string(
"name \"") +
namebuf +
"\" is a keyword");
5115 std::string(
"unable to find namespace \"") +
namebuf +
"\"");
5117 currNameSpace = i->second;
5118 currTable = currNameSpace->
GetTable();
5169 if (currTable == 0) {
5171 std::string(
"namespace \"") + currNameSpace->
sGetName() +
"\" does not support variables");
5198 v = currTable->
Put(varname.c_str(), newvar);
5201 silent_cerr(
"warning, ifndef variable " << v->
GetTypeName() <<
" \"" << name
5202 <<
"\" not yet defined; set to \"" << newvar <<
"\" at line " <<
mbdyn_get_line_data() << std::endl);
5210 std::string(
"cannot redefine var \"") + name +
"\"");
5213 if (v->
Const() && !bIsIfndef) {
5216 std::string(
"cannot redefine const named value \"") + name +
"\"");
5221 std::string(
"cannot redefine non-var named value \"") + name +
"\"");
5225 dynamic_cast<Var *
>(v)->SetVal(e->Eval());
5229 silent_cerr(
"warning, skipping redefinition of \"" << name.c_str() <<
"\""
5231 <<
" (orig=" << v->
GetVal() <<
", unchanged; new=" << e->Eval() <<
")"
5235 silent_cerr(
"warning, skipping redefinition of " << v->
GetTypeName() <<
" \"" << name.c_str() <<
"\""
5236 <<
" (orig=" << v->
GetVal() <<
", unchanged; new=" << e->Eval() <<
")"
5246 return new EE_Value(dynamic_cast<Var *>(v)->GetVal());
5249 return new EE_Var(v, currNameSpace);
5253 if (currTable == 0) {
5255 std::string(
"namespace \"") + currNameSpace->
sGetName() +
"\" does not support variables");
5264 std::string(
"cannot create const named value \"") +
namebuf +
"\" with no value");
5275 return new EE_Var(v, currNameSpace);
5281 "definition modifier without type");
5286 "type modifier without type");
5298 std::string(
"unable to find namespace \"") +
namebuf +
"\"");
5300 currNameSpace = i->second;
5301 currTable = currNameSpace->
GetTable();
5316 std::string(
"function \"") +
namebuf +
"\" not found");
5322 std::string(
"closing parenthesis expected after function \"")
5323 + f->fname +
"\" in expr()");
5331 if (currTable == 0) {
5333 std::string(
"namespace \"") + currNameSpace->
sGetName()
5334 +
"\" does not support variables");
5343 std::string(
"cannot assign const named value \"") + name +
"\"");
5348 std::string(
"cannot assign non-var named value \"") + name +
"\"");
5352 dynamic_cast<Var *
>(v)->Cast(e->
Eval());
5354 return new EE_Var(v, currNameSpace);
5366 e =
new EE_Var(v, currNameSpace);
5375 std::string(
"unable to find variable \"")
5404 std::vector<char *> argv(1);
5407 unsigned int i = 0, in_quotes = 0;
5442 if (in_quotes == 0) {
5447 while (isspace((c =
in->
get()))) {
5450 if (c !=
',' && c !=
']') {
5451 silent_cerr(
"need a separator "
5452 "after closing quotes" << std::endl);
5465 argv.resize(argc + 2);
5484 if (i >=
sizeof(buf)) {
5485 silent_cerr(
"MathParser::readplugin(): buffer overflow" << std::endl);
5492 silent_cerr(
"eof encountered while parsing plugin"
5506 char *pginname = argv[0];
5507 char *varname = argv[1];
5514 if (pginname == NULL || *pginname ==
'\0') {
5515 silent_cerr(
"illegal or missing plugin name" << std::endl);
5519 if (varname == NULL || *varname ==
'\0') {
5520 silent_cerr(
"illegal or missing plugin variable name"
5530 silent_cerr(
"variable \"" << varname <<
"\" already defined"
5538 struct PlugInRegister *p = 0;
5540 if (strcasecmp(p->name, pginname) == 0) {
5546 silent_cerr(
" plugin '" << pginname <<
"' not supported" << std::endl);
5551 for (
int i = 0; argv[i] != NULL; i++) {
5552 silent_cout(
"argv[" << i <<
"]=" << argv[i]
5561 pgin->
Read(argc - 2, &argv[2]);
5573 PlugInVar(varname, pgin));
5579 for (
int i = 0; argv[i] != NULL; i++) {
GradientExpression< UnaryExpr< FuncTanh, Expr > > tanh(const GradientExpression< Expr > &u)
void trim_arg(char *const s)
TypedValue::Type GetType(void) const
MathArgPriv_t< Real, AT_REAL > MathArgReal_t
TypedValue stmtlist(void)
virtual Table * GetTable(void)
static int mp_in(const MathParser::MathArgs &args)
TypedValue relational_int(TypedValue d)
TypedValue operator+(const TypedValue &v) const
static int mp_cast(const MathParser::MathArgs &args)
NameSpace(const std::string &name)
MathParser(const MathParser &)
std::map< std::string, NameSpace * > NameSpaceMap
static const char ONE_LINE_REMARK
static int mp_step(const MathParser::MathArgs &args)
GradientExpression< BinaryExpr< FuncPow, LhsExpr, RhsExpr > > pow(const GradientExpression< LhsExpr > &u, const GradientExpression< RhsExpr > &v)
bool IsKeyWord(NameSpace *ns, const char *const s) const
#define MBDYN_EXCEPT_ARGS
static int mp_ramp(const MathParser::MathArgs &args)
const TypedValue & operator-=(const TypedValue &v)
#define DEBUGCOUTFNAME(fname)
TypedValue::Type GetType(void) const
NamedValue * InsertSym(const char *const s, const Real &v, int redefine=0)
virtual TypedValue::Type GetType(void) const =0
Var * Put(const std::string &name, const TypedValue &v)
static int mp_greater_than_or_equal_to_0_t(const MathParser::MathArgs &args)
#define MBDYN_EXCEPT_ARGS_PASSTHRU
const TypedValue & Set(const bool &b)
bool IsDeclarationModifier(const char *const s) const
static int mp_acosh_t(const MathParser::MathArgs &args)
bool MayChange(void) const
int RegisterNameSpace(NameSpace *ns)
virtual TypedValue GetVal(void) const =0
static int mp_actg2(const MathParser::MathArgs &args)
std::vector< MathArg_t * > MathArgs
static int mp_sign(const MathParser::MathArgs &args)
TypedValue & operator=(const TypedValue &var)
TypedValue binary_int(TypedValue d)
const char * what(void) const
static int mp_srnd(const MathParser::MathArgs &args)
static int mp_greater_than_0_t(const MathParser::MathArgs &args)
#define SAFEDELETEARR(pnt)
bool operator>(const TypedValue &v) const
static const declarationmodifiernames DeclarationModifierNames[]
bool operator!(const TypedValue &v)
std::ostream & operator<<(std::ostream &out, const MathParser::MathArgVoid_t &)
bool IsTypeModifier(const char *const s) const
void GetForever(std::ostream &out, const char *const sep="\n")
#define MBDYN_EXCEPT_ARGS_NOOPT_PASSTHRU
static int mp_rand(const MathParser::MathArgs &args)
virtual TypedValue Eval(void) const =0
MathParser::PlugIn *(* constructor)(MathParser &, void *)
MathArgPriv_t< bool, AT_BOOL > MathArgBool_t
TypedValue & Cast(const TypedValue &var, bool bErr=false)
const TypedValue & operator*=(const TypedValue &v)
bool operator<(const TypedValue &v) const
const char *const GetTypeName(void) const
GradientExpression< UnaryExpr< FuncFabs, Expr > > fabs(const GradientExpression< Expr > &u)
static int mp_ctgh(const MathParser::MathArgs &args)
StaticNameSpace(Table *pTable=0)
static int mp_rndm(const MathParser::MathArgs &args)
TypedValue operator/(const TypedValue &v) const
static int mp_actg(const MathParser::MathArgs &args)
MathParser::MathFunc_t * GetFunc(const std::string &fname) const
TypedValue::Type GetType(void) const
void Cast(const TypedValue &v, bool bErr=false)
TypedValue power_int(TypedValue d)
static const typemodifiernames TypeModifierNames[]
static int mp_atanh_t(const MathParser::MathArgs &args)
enum Token GetToken(void)
bool IsType(const char *const s) const
void AllocName(const char *const s)
void func(const T &u, const T &v, const T &w, doublereal e, T &f)
TypedValue GetVal(void) const
MathArgPriv_t< TypedValue, AT_ANY > MathArgAny_t
static int mp_tan_t(const MathParser::MathArgs &args)
ErrWrongType(const char *file, int line, const char *func, const std::string r=std::string())
double(* mp_f2_f)(double, double)
static const TypeName_t TypeNames[]
static int mp_sprintf(const MathParser::MathArgs &args)
virtual MathParser::MathFunc_t * GetFunc(const std::string &fname) const =0
static int mp_ctgh_t(const MathParser::MathArgs &args)
struct PlugInRegister * next
static bool IsFlag(EEFlags f)
bool operator<=(const TypedValue &v) const
virtual ~NamedValue(void)
virtual TypedValue EvalFunc(MathParser::MathFunc_t *f) const
#define SAFENEW(pnt, item)
#define MBDYN_EXCEPT_ARGS_DECL_NODEF
virtual std::ostream & Output(std::ostream &out) const =0
virtual bool MayChange(void) const =0
StaticNameSpace * defaultNameSpace
TypedValue operator-(const TypedValue &v)
TypedValue::TypeModifier GetTypeModifier(const char *const s) const
static int mp_min(const MathParser::MathArgs &args)
static int mp_stop(const MathParser::MathArgs &args)
MathParser::DeclarationModifier type
NamedValue(const char *const s)
doublereal copysign(doublereal x, doublereal y)
virtual const std::string & sGetName(void) const
PlugInVar(const char *const s, MathParser::PlugIn *p)
static int mp_acos_t(const MathParser::MathArgs &args)
const NameSpaceMap & GetNameSpaceMap(void) const
Var(const char *const s, const TypedValue &v)
TypedValue evalfunc(MathParser::NameSpace *ns, MathParser::MathFunc_t *f)
bool bNameValidate(const std::string &s) const
double(* mp_f1_f)(double)
static int mp_sramp(const MathParser::MathArgs &args)
TypedValue readplugin(void)
const std::string & GetString(void) const
static int mp_func_1(const MathParser::MathArgs &args)
#define ASSERT(expression)
TypedValue operator%(const TypedValue &v) const
virtual TypedValue EvalFunc(MathParser::MathFunc_t *f) const =0
const TypedValue & operator+=(const TypedValue &v)
bool operator>=(const TypedValue &v) const
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
bool operator||(const TypedValue &v) const
MathArgPriv_t< std::string, AT_STRING > MathArgString_t
static int mp_ctg_t(const MathParser::MathArgs &args)
int GetLineNumber(void) const
TypedValue operator+(const TypedValue &v)
bool operator!=(const TypedValue &v) const
TypedValue operator*(const TypedValue &v) const
static std::stack< cleanup * > c
TypedValue::TypeModifier type
static int mp_asin_t(const MathParser::MathArgs &args)
NameSpace * GetNameSpace(const std::string &name) const
void TokenPush(enum Token t)
virtual bool Const(void) const =0
virtual bool IsFunc(const std::string &fname) const =0
NamedValue * Get(const std::string &name) const
bool operator&&(const TypedValue &v) const
Real GetLastStmt(Real d=0., Token t=ARGSEP)
#define SAFESTRDUP(pnt, src)
bool IsFunc(const std::string &fname) const
TypedValue logical_int(TypedValue d)
HighParser::ErrOut mbdyn_get_line_data(void)
TypedValue operator-(const TypedValue &v) const
static const doublereal a
void PutSymbolTable(Table &T)
virtual Table * GetTable(void)=0
virtual const char *const GetTypeName(void) const
void SetConst(bool isConst=true, bool bForce=false)
bool operator==(const TypedValue &v) const
#define MBDYN_EXCEPT_ARGS_DECL_NOOPT
TypedValue relational(void)
static doublereal buf[BUFSIZE]
TypedValue mult_int(TypedValue d)
MathArgPriv_t< Int, AT_INT > MathArgInt_t
const char * GetName(void) const
std::stack< TokenVal > TokenStack
virtual int Read(int argc, char *argv[])=0
static int mp_ctg(const MathParser::MathArgs &args)
GradientExpression< BinaryExpr< FuncAtan2, LhsExpr, RhsExpr > > atan2(const GradientExpression< LhsExpr > &u, const GradientExpression< RhsExpr > &v)
MathParser::DeclarationModifier GetDeclarationModifier(const char *const s) const
static int mp_max(const MathParser::MathArgs &args)
TypedValue::Type GetType(const char *const s) const
int RegisterPlugIn(const char *name, MathParser::PlugIn *(*)(MathParser &, void *), void *arg)
NameSpaceMap nameSpaceMap
void SetVal(const bool &b)
static int mp_func_2(const MathParser::MathArgs &args)
Table & GetSymbolTable(void) const
GradientExpression< UnaryExpr< FuncTan, Expr > > tan(const GradientExpression< Expr > &u)
const TypedValue & operator%=(const TypedValue &v)
static int mp_par(const MathParser::MathArgs &args)
virtual bool IsVar(void) const
static int mp_print(const MathParser::MathArgs &args)
ErrGeneric(const char *file, int line, const char *func, const std::string r=std::string())
bool MayChange(void) const
void SetType(TypedValue::Type t, bool isConst=false)
TypedValue GetVal(void) const
const TypedValue & operator/=(const TypedValue &v)
struct MathParser::PlugInRegister * PlugIns