2015-07-11 18:56:02 +08:00
|
|
|
|
/*
|
2019-01-20 23:15:10 +08:00
|
|
|
|
Copyright (c) 2008-2019 Jan W. Krieger (<jan@jkrieger.de>, <j.krieger@dkfz.de>)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
|
|
This software is free software: you can redistribute it and/or modify
|
2019-02-08 00:24:46 +08:00
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 2.1 of the License, or
|
2015-07-11 18:56:02 +08:00
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-02-08 00:24:46 +08:00
|
|
|
|
GNU Lesser General Public License for more details.
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-02-08 00:24:46 +08:00
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2015-07-11 18:56:02 +08:00
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define COMPILING_THIS_JKMATHPARSER
|
2019-05-30 04:40:02 +08:00
|
|
|
|
#include "jkqtcommon/jkqtpmathparser.h" // class's header file
|
2015-07-11 18:56:02 +08:00
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <float.h>
|
2019-05-30 04:47:26 +08:00
|
|
|
|
#include <ctime>
|
2019-05-30 04:40:02 +08:00
|
|
|
|
#include "jkqtcommon/jkqtpstringtools.h"
|
2019-01-20 17:49:29 +08:00
|
|
|
|
|
|
|
|
|
/* This just distinguishes between the different path formats on Windows and Unix:
|
|
|
|
|
* - on Windows you use a backslash '\' do separate directories
|
|
|
|
|
* - in Unix you use a slash '/' to separate directories
|
|
|
|
|
*/
|
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
|
/** \brief a separator between two directories in a path between \c " quotes */
|
|
|
|
|
#define JKQTPPATHSEPARATOR_STRING "\\"
|
|
|
|
|
/** \brief a separator between two directories in a path between \c ' quotes */
|
|
|
|
|
#define JKQTPPATHSEPARATOR_CHAR '\\'
|
|
|
|
|
#else
|
|
|
|
|
/** \brief a separator between two directories in a path between \c " quotes */
|
|
|
|
|
#define JKQTPPATHSEPARATOR_STRING "/"
|
|
|
|
|
/** \brief a separator between two directories in a path between \c ' quotes */
|
|
|
|
|
#define JKQTPPATHSEPARATOR_CHAR '/'
|
|
|
|
|
#endif
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************************
|
2018-12-28 22:17:40 +08:00
|
|
|
|
* default-function implementations for math parser
|
2015-07-11 18:56:02 +08:00
|
|
|
|
******************************************************************************************/
|
2018-12-28 22:17:40 +08:00
|
|
|
|
namespace { // anonymous namespace to limit availability to this module (CPP-file)
|
|
|
|
|
JKQTPMathParser::jkmpResult fFloatToStr(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
std::ostringstream ost;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpString;
|
|
|
|
|
if (n!=1) p->jkmpError("floattostr accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("floattostr needs double argument");
|
|
|
|
|
ost<<params[0].num;
|
|
|
|
|
r.str=ost.str();
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fIntToStr(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
std::ostringstream ost;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpString;
|
|
|
|
|
if (n!=1) p->jkmpError("inttostr accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("inttostr needs double argument");
|
|
|
|
|
ost<<int64_t(params[0].num);
|
|
|
|
|
r.str=ost.str();
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fBoolToStr(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
std::ostringstream ost;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpString;
|
|
|
|
|
if (n!=1) p->jkmpError("booltostr accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpBool) p->jkmpError("floattostr needs bool argument");
|
|
|
|
|
r.str=(r.boolean)?"true":"false";
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fToSystemPathSeparator(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpString;
|
|
|
|
|
if (n!=1) p->jkmpError("tosystempathseparator accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpString) p->jkmpError("tosystempathseparator needs string argument");
|
|
|
|
|
r.str="";
|
|
|
|
|
for (size_t i=0; i<params[0].str.size(); i++) {
|
|
|
|
|
char ch=params[0].str[i];
|
|
|
|
|
if (ch=='/' || ch=='\\') ch=JKQTPPATHSEPARATOR_CHAR;
|
|
|
|
|
r.str+=ch;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fSetDefault(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpString;
|
|
|
|
|
if (n!=2) p->jkmpError("setdefault accepts 2 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpString) p->jkmpError("setdefault needs a string as first argument");
|
|
|
|
|
r=params[1];
|
|
|
|
|
if (p->variableExists(params[0].str)) {
|
|
|
|
|
r=p->getVariable(params[0].str);
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fStrDate(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpString;
|
|
|
|
|
std::string f="%Y-%m-%d";
|
|
|
|
|
if (n>1) p->jkmpError("strdate accepts 0 or 1 argumentment");
|
|
|
|
|
if (n>0 && params[0].type!=JKQTPMathParser::jkmpString) p->jkmpError("strdate needs a string as first argument");
|
|
|
|
|
if (n>0) f=params[0].str;
|
|
|
|
|
char re[1024];
|
|
|
|
|
time_t rawtime;
|
|
|
|
|
struct tm* timeinfo;
|
|
|
|
|
time(&rawtime);
|
|
|
|
|
timeinfo=localtime(&rawtime);
|
|
|
|
|
strftime(re, 1024, f.c_str(), timeinfo);
|
|
|
|
|
r.str=re;
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fCMDParam(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpString;
|
|
|
|
|
std::string def="";
|
|
|
|
|
if (n<1 || n>2) p->jkmpError("cmdparam(name, default) accepts 1 or 2 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpString) p->jkmpError("cmdparam needs a string as first argument");
|
|
|
|
|
if (n>1 && params[1].type!=JKQTPMathParser::jkmpString) p->jkmpError("cmdparam needs a string as second argument");
|
|
|
|
|
if (n>1) def=params[1].str;
|
|
|
|
|
|
|
|
|
|
r.str=p->getArgCVParam(params[0].str, def);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fSinc(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("sinc accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("sinc needs double argument");
|
|
|
|
|
//r.num=sin(params[0].num)/params[0].num;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
double x=params[0].num;
|
|
|
|
|
static double const taylor_0_bound = 3*DBL_MIN ;
|
|
|
|
|
static double const taylor_2_bound = sqrt(taylor_0_bound);
|
|
|
|
|
static double const taylor_n_bound = sqrt(taylor_2_bound);
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
double result = 1;
|
|
|
|
|
if (fabs(x) >= taylor_n_bound)
|
|
|
|
|
{
|
|
|
|
|
result= sin(x)/x;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// approximation by taylor series in x at 0 up to order 0
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
if (fabs(x) >= taylor_0_bound)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
{
|
2018-12-28 22:17:40 +08:00
|
|
|
|
double x2 = x*x;
|
|
|
|
|
|
|
|
|
|
// approximation by taylor series in x at 0 up to order 2
|
|
|
|
|
result -= x2/static_cast<double>(6);
|
|
|
|
|
|
|
|
|
|
if (fabs(x) >= taylor_2_bound)
|
|
|
|
|
{
|
|
|
|
|
// approximation by taylor series in x at 0 up to order 4
|
|
|
|
|
result += (x2*x2)/static_cast<double>(120);
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
r.num=result;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fTanc(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("tanc accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("tanc needs double argument");
|
|
|
|
|
r.num=(params[0].num==0)?1.0:tan(params[0].num)/params[0].num;
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fSin(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("sin accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("sin needs double argument");
|
|
|
|
|
r.num=sin(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fCos(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("cos accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("cos needs double argument");
|
|
|
|
|
r.num=cos(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fTan(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("tan accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("tan needs double argument");
|
|
|
|
|
r.num=tan(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fExp(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("exp accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("exp needs double argument");
|
|
|
|
|
r.num=exp(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fLog(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("log accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("log needs double argument");
|
|
|
|
|
r.num=log(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fLog10(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("log10 accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("log10 needs double argument");
|
|
|
|
|
r.num=log10(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fLog2(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("log2 accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("log2 needs double argument");
|
|
|
|
|
r.num=log2(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fSqrt(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("sqrt accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("sqrt needs double argument");
|
|
|
|
|
r.num=sqrt(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
JKQTPMathParser::jkmpResult fCbrt(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("cbrt accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("cbrt needs double argument");
|
|
|
|
|
r.num=cbrt(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fSqr(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("sqr accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("sqr needs double argument");
|
|
|
|
|
r.num=params[0].num*params[0].num;
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fAbs(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("abs accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("abs needs double argument");
|
|
|
|
|
r.num=fabs(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fIf(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
if (n!=3) p->jkmpError("If accepts 3 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpBool) p->jkmpError("If needs bool as first argument");
|
|
|
|
|
if (params[0].boolean) return params[1]; else return params[2];
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fASin(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("asin accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("asin needs double argument");
|
|
|
|
|
r.num=asin(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fACos(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("acos accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("acos needs double argument");
|
|
|
|
|
r.num=acos(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fATan(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("atan accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("atan needs double argument");
|
|
|
|
|
r.num=atan(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fATan2(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=2) p->jkmpError("atan2 accepts 2 argument");
|
|
|
|
|
if ((params[0].type!=JKQTPMathParser::jkmpDouble)||(params[1].type!=JKQTPMathParser::jkmpDouble)) p->jkmpError("atan2 needs double argument");
|
|
|
|
|
r.num=atan2(params[0].num, params[1].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fSinh(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("sinh accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("sinh needs double argument");
|
|
|
|
|
r.num=sinh(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fCosh(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("cosh accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("cosh needs double argument");
|
|
|
|
|
r.num=cosh(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fTanh(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("tanh accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("tanh needs double argument");
|
|
|
|
|
r.num=tanh(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fErf(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("erf accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("erf needs double argument");
|
|
|
|
|
r.num=erf(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fErfc(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("erfc accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("erfc needs double argument");
|
|
|
|
|
r.num=erfc(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult flGamma(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("lgamma accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("lgamma needs double argument");
|
|
|
|
|
r.num=lgamma(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult ftGamma(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("tgamma accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("tgamma needs double argument");
|
|
|
|
|
r.num=tgamma(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fJ0(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("j0 accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("j0 needs double argument");
|
|
|
|
|
r.num=j0(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fJ1(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("j1 accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("j1 needs double argument");
|
|
|
|
|
r.num=j1(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fY0(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("y0 accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("y0 needs double argument");
|
|
|
|
|
r.num=y0(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fY1(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("y1 accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("y1 needs double argument");
|
|
|
|
|
r.num=y1(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fYn(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=2) p->jkmpError("yn accepts 2 argument");
|
|
|
|
|
if ((params[0].type!=JKQTPMathParser::jkmpDouble)||(params[1].type!=JKQTPMathParser::jkmpDouble)) p->jkmpError("yn needs double argument");
|
2019-01-26 20:00:40 +08:00
|
|
|
|
r.num=yn(static_cast<int>(params[0].num), params[1].num);
|
2018-12-28 22:17:40 +08:00
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fJn(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=2) p->jkmpError("jn accepts 2 argument");
|
|
|
|
|
if ((params[0].type!=JKQTPMathParser::jkmpDouble)||(params[1].type!=JKQTPMathParser::jkmpDouble)) p->jkmpError("jn needs double argument");
|
2019-01-26 20:00:40 +08:00
|
|
|
|
r.num=jn(static_cast<int>(params[0].num), params[1].num);
|
2018-12-28 22:17:40 +08:00
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fSRand(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("srand accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("srand needs double argument");
|
|
|
|
|
r.num=0;
|
2019-01-26 20:00:40 +08:00
|
|
|
|
srand(static_cast<unsigned int>(params[0].num));
|
2018-12-28 22:17:40 +08:00
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fRand(JKQTPMathParser::jkmpResult* /*params*/, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=0) p->jkmpError("rand accepts 0 argument");
|
|
|
|
|
r.num=double(rand())/double(RAND_MAX);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fCeil(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("ceil accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("ceil needs double argument");
|
|
|
|
|
r.num=ceil(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fFloor(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("floor accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("floor needs double argument");
|
|
|
|
|
r.num=floor(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fTrunc(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("trunc accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("trunc needs double argument");
|
|
|
|
|
r.num=trunc(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fRound(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("round accepts 1 argument");
|
|
|
|
|
if (params[0].type!=JKQTPMathParser::jkmpDouble) p->jkmpError("round needs double argument");
|
|
|
|
|
r.num=round(params[0].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fFMod(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=2) p->jkmpError("fmod accepts 2 argument");
|
|
|
|
|
if ((params[0].type!=JKQTPMathParser::jkmpDouble)||(params[1].type!=JKQTPMathParser::jkmpDouble)) p->jkmpError("fmod needs double argument");
|
2019-01-26 20:00:40 +08:00
|
|
|
|
r.num=fmod(static_cast<int>(params[0].num), params[1].num);
|
2018-12-28 22:17:40 +08:00
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fMin(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=2) p->jkmpError("min accepts 2 argument");
|
|
|
|
|
if ((params[0].type!=JKQTPMathParser::jkmpDouble)||(params[1].type!=JKQTPMathParser::jkmpDouble)) p->jkmpError("min needs double argument");
|
2019-01-26 20:00:40 +08:00
|
|
|
|
r.num=fmin(static_cast<int>(params[0].num), params[1].num);
|
2018-12-28 22:17:40 +08:00
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fMax(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=2) p->jkmpError("max accepts 2 argument");
|
|
|
|
|
if ((params[0].type!=JKQTPMathParser::jkmpDouble)||(params[1].type!=JKQTPMathParser::jkmpDouble)) p->jkmpError("max needs double argument");
|
2019-01-26 20:00:40 +08:00
|
|
|
|
r.num=fmax(static_cast<int>(params[0].num), params[1].num);
|
2018-12-28 22:17:40 +08:00
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult QFSPIMLightsheetEvaluationItem_fGauss(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=2) p->jkmpError("gauss accepts 2 argument");
|
|
|
|
|
if ((params[0].type!=JKQTPMathParser::jkmpDouble)||(params[1].type!=JKQTPMathParser::jkmpDouble)) p->jkmpError("gauss needs double argument");
|
|
|
|
|
r.num=exp(-2*params[0].num*params[0].num/params[1].num/params[1].num);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fSlit(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=2) p->jkmpError("slit accepts 2 argument");
|
|
|
|
|
if ((params[0].type!=JKQTPMathParser::jkmpDouble)||(params[1].type!=JKQTPMathParser::jkmpDouble)) p->jkmpError("slit needs double argument");
|
|
|
|
|
r.num=((params[0].num>=-1.0*params[1].num/2)&&(params[0].num<=params[1].num/2))?1.0:0.0;
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fTheta(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("theta accepts 1 argument");
|
|
|
|
|
if ((params[0].type!=JKQTPMathParser::jkmpDouble)) p->jkmpError("theta needs double argument");
|
|
|
|
|
r.num=(params[0].num>=0)?1.0:0.0;
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fSigmoid(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("sigmoid accepts 1 argument");
|
|
|
|
|
if ((params[0].type!=JKQTPMathParser::jkmpDouble)) p->jkmpError("sigmoid needs double argument");
|
|
|
|
|
r.num=1.0/(1+exp(-1.0*params[0].num));
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult fSign(JKQTPMathParser::jkmpResult* params, unsigned char n, JKQTPMathParser* p){
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
if (n!=1) p->jkmpError("sign accepts 1 argument");
|
|
|
|
|
if ((params[0].type!=JKQTPMathParser::jkmpDouble)) p->jkmpError("sign needs double argument");
|
|
|
|
|
r.num=0.0;
|
|
|
|
|
if (params[0].num<0) { r.num=-1; }
|
|
|
|
|
else if (params[0].num>0) { r.num=+1; }
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
inline std::string strip(const std::string& s) {
|
2018-12-28 22:17:40 +08:00
|
|
|
|
std::string r;
|
2019-01-26 20:00:40 +08:00
|
|
|
|
r.reserve(s.size());
|
2018-12-28 22:17:40 +08:00
|
|
|
|
for (size_t i=0; i<s.size(); i++){
|
|
|
|
|
if ((s[i]!=' ')&&(s[i]!='\t')&&(s[i]!='\r')&&(s[i]!='\n')) {
|
|
|
|
|
r+=s[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return r;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-05-30 04:40:02 +08:00
|
|
|
|
void JKQTPMathParser::jkmpError(const std::string &st) {
|
|
|
|
|
if (jkmathparser_exception_function!=nullptr) {
|
|
|
|
|
jkmathparser_exception_function(st);
|
|
|
|
|
} else {
|
|
|
|
|
throw jkmpException(st);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void JKQTPMathParser::setException_function(JKQTPMathParser::jkmpexceptionf exception_function) {
|
|
|
|
|
jkmathparser_exception_function=exception_function;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void JKQTPMathParser::resetException_function() {
|
|
|
|
|
jkmathparser_exception_function=nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-11 18:56:02 +08:00
|
|
|
|
std::string JKQTPMathParser::tokentostring(JKQTPMathParser::jkmpTokenType token) {
|
|
|
|
|
switch(token) {
|
|
|
|
|
case END: return "END";
|
|
|
|
|
case PRINT: return "PRINT (;)";
|
|
|
|
|
case PARAMETER_DIV: return "PARAMETER_DIV (,)";
|
2019-01-26 20:00:40 +08:00
|
|
|
|
case STRING_DELIM: return "STRING_DELIM (' or \")";
|
|
|
|
|
case NAME: return "NAME";
|
|
|
|
|
case NUMBER: return "NUMBER";
|
|
|
|
|
case PLUS: return "PLUS (+)";
|
|
|
|
|
case MINUS: return "MINUS (-)";
|
|
|
|
|
case MUL: return "MUL (*)";
|
|
|
|
|
case DIV: return "DIV (/)";
|
|
|
|
|
case MODULO: return "MODULO (%)";
|
|
|
|
|
case ASSIGN: return "ASSIGN (=)";
|
|
|
|
|
case LBRACKET: return "LBRACKET '('";
|
|
|
|
|
case RBRACKET: return "RBRACKET ')'";
|
|
|
|
|
case POWER: return "POWER (^)";
|
|
|
|
|
case FACTORIAL_LOGIC_NOT: return "FACTORIAL_LOGIC_NOT (!)";
|
|
|
|
|
case LOGIC_NOT: return "LOGIC_NOT (!/not)";
|
|
|
|
|
case LOGIC_AND: return "LOGIC_AND (&&/and)";
|
|
|
|
|
case LOGIC_OR: return "LOGIC_OR (||/or)";
|
|
|
|
|
case LOGIC_XOR: return "LOGIC_XOR (xor)";
|
|
|
|
|
case LOGIC_NOR: return "LOGIC_NOR (nor)";
|
|
|
|
|
case LOGIC_NAND: return "LOGIC_NAND (nand)";
|
|
|
|
|
case LOGIC_TRUE: return "LOGIC_TRUE (true)";
|
|
|
|
|
case LOGIC_FALSE: return "LOGIC_FALSE (false)";
|
|
|
|
|
case COMP_EQUALT: return "COMP_EQUALT (==)";
|
|
|
|
|
case COMP_UNEQUAL: return "COMP_UNEQUAL (!=)";
|
|
|
|
|
case COMP_GREATER: return "COMP_GREATER (>)";
|
|
|
|
|
case COMP_SMALLER: return "COMP_SMALLER (<)";
|
|
|
|
|
case COMP_GEQUAL: return "COMP_GEQUAL (>=)";
|
|
|
|
|
case COMP_SEQUAL: return "COMP_SEQUAL (<=)";
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
return "unknown";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string JKQTPMathParser::currenttokentostring() {
|
|
|
|
|
switch(CurrentToken) {
|
|
|
|
|
case END: return "END";
|
|
|
|
|
case PRINT: return "PRINT (;)";
|
|
|
|
|
case PARAMETER_DIV: return "PARAMETER_DIV (,)";
|
2019-01-26 20:00:40 +08:00
|
|
|
|
case STRING_DELIM: return "STRING_DELIM (' or \")";
|
|
|
|
|
case NAME: return jkqtp_format("NAME (%s)", StringValue.c_str());
|
|
|
|
|
case NUMBER: return jkqtp_format("NUMBER (%lf)", NumberValue);
|
|
|
|
|
case PLUS: return "PLUS (+)";
|
|
|
|
|
case MINUS: return "MINUS (-)";
|
|
|
|
|
case MUL: return "MUL (*)";
|
|
|
|
|
case DIV: return "DIV (/)";
|
|
|
|
|
case MODULO: return "MODULO (%)";
|
|
|
|
|
case ASSIGN: return "ASSIGN (=)";
|
|
|
|
|
case LBRACKET: return "LBRACKET '('";
|
|
|
|
|
case RBRACKET: return "RBRACKET ')'";
|
|
|
|
|
case POWER: return "POWER (^)";
|
|
|
|
|
case FACTORIAL_LOGIC_NOT: return "FACTORIAL_LOGIC_NOT (!)";
|
|
|
|
|
case LOGIC_NOT: return "LOGIC_NOT (!/not)";
|
|
|
|
|
case LOGIC_AND: return "LOGIC_AND (&/and)";
|
|
|
|
|
case LOGIC_OR: return "LOGIC_OR (|/or)";
|
|
|
|
|
case LOGIC_XOR: return "LOGIC_XOR (xor)";
|
|
|
|
|
case LOGIC_NOR: return "LOGIC_NOR (nor)";
|
|
|
|
|
case LOGIC_NAND: return "LOGIC_NAND (nand)";
|
|
|
|
|
case LOGIC_TRUE: return "LOGIC_TRUE (true)";
|
|
|
|
|
case LOGIC_FALSE: return "LOGIC_FALSE (false)";
|
|
|
|
|
case COMP_EQUALT: return "COMP_EQUALT (==)";
|
|
|
|
|
case COMP_UNEQUAL: return "COMP_UNEQUAL (!=)";
|
|
|
|
|
case COMP_GREATER: return "COMP_GREATER (>)";
|
|
|
|
|
case COMP_SMALLER: return "COMP_SMALLER (<)";
|
|
|
|
|
case COMP_GEQUAL: return "COMP_GEQUAL (>=)";
|
|
|
|
|
case COMP_SEQUAL: return "COMP_SEQUAL (<=)";
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
return "unknown";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************************
|
2019-05-30 04:40:02 +08:00
|
|
|
|
* JKQTPMathParser
|
2015-07-11 18:56:02 +08:00
|
|
|
|
******************************************************************************************/
|
|
|
|
|
|
|
|
|
|
// class constructor
|
|
|
|
|
JKQTPMathParser::JKQTPMathParser() {
|
2018-11-18 18:59:30 +08:00
|
|
|
|
jkmathparser_exception_function=nullptr;
|
|
|
|
|
data=nullptr;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
argc=0;
|
2018-11-18 18:59:30 +08:00
|
|
|
|
argv=nullptr;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
addStandardFunctions();
|
|
|
|
|
addStandardVariables();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void JKQTPMathParser::addStandardVariables(){
|
2019-01-26 20:00:40 +08:00
|
|
|
|
addVariableDouble("pi", M_PI);
|
|
|
|
|
addVariableDouble("e", M_E);
|
|
|
|
|
addVariableDouble("sqrt2", sqrt(2));
|
|
|
|
|
addVariableString("version", "0.2");
|
|
|
|
|
addVariableDouble("log2e", M_LOG2E);
|
|
|
|
|
addVariableDouble("log10e", M_LOG10E);
|
|
|
|
|
addVariableDouble("ln2", M_LN2);
|
|
|
|
|
addVariableDouble("ln10", M_LN10);
|
|
|
|
|
addVariableDouble("h", 6.6260689633E-34);
|
|
|
|
|
addVariableDouble("hbar", 1.05457162853E-34);
|
|
|
|
|
addVariableDouble("epsilon0", 8.854187817E-12);
|
|
|
|
|
addVariableDouble("mu0", 12.566370614E-7);
|
|
|
|
|
addVariableDouble("c", 299792458);
|
|
|
|
|
addVariableDouble("ce", 1.60217648740E-19);
|
|
|
|
|
addVariableDouble("muB", 927.40091523E-26);
|
|
|
|
|
addVariableDouble("muB_eV", 5.788381755579E-5);
|
|
|
|
|
addVariableDouble("muN", 5.0507832413E-27);
|
|
|
|
|
addVariableDouble("muN_eV", 3.152451232645E-8);
|
|
|
|
|
addVariableDouble("me", 9.1093821545E-31);
|
|
|
|
|
addVariableDouble("mp", 1.67262163783E-27);
|
|
|
|
|
addVariableDouble("mn", 1.67492721184E-27);
|
|
|
|
|
addVariableDouble("NA", 6.0221417930E23);
|
|
|
|
|
addVariableDouble("kB", 1.380650424E-23);
|
|
|
|
|
addVariableDouble("kB_eV", 8.61734315E-5);
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void JKQTPMathParser::addStandardFunctions(){
|
2019-01-26 20:00:40 +08:00
|
|
|
|
addFunction("sinc", fSinc);
|
|
|
|
|
addFunction("asin", fASin);
|
|
|
|
|
addFunction("acos", fACos);
|
|
|
|
|
addFunction("atan", fATan);
|
|
|
|
|
addFunction("atan2", fATan2);
|
|
|
|
|
addFunction("sin", fSin);
|
|
|
|
|
addFunction("cos", fCos);
|
|
|
|
|
addFunction("tan", fTan);
|
|
|
|
|
addFunction("sinh", fSinh);
|
|
|
|
|
addFunction("cosh", fCosh);
|
|
|
|
|
addFunction("tanh", fTanh);
|
|
|
|
|
addFunction("log", fLog);
|
|
|
|
|
addFunction("log2", fLog2);
|
|
|
|
|
addFunction("log10", fLog10);
|
|
|
|
|
addFunction("exp", fExp);
|
|
|
|
|
addFunction("sqrt", fSqrt);
|
2015-07-11 18:56:02 +08:00
|
|
|
|
addFunction("cbrt", fCbrt);
|
|
|
|
|
addFunction("sqr", fSqr);
|
2019-01-26 20:00:40 +08:00
|
|
|
|
addFunction("abs", fAbs);
|
|
|
|
|
addFunction("if", fIf);
|
|
|
|
|
addFunction("erf", fErf);
|
|
|
|
|
addFunction("erfc", fErfc);
|
|
|
|
|
addFunction("lgamma", flGamma);
|
|
|
|
|
addFunction("tgamma", ftGamma);
|
|
|
|
|
addFunction("j0", fJ0);
|
|
|
|
|
addFunction("j1", fJ1);
|
|
|
|
|
addFunction("jn", fJn);
|
|
|
|
|
addFunction("y0", fY0);
|
|
|
|
|
addFunction("y1", fY1);
|
|
|
|
|
addFunction("yn", fYn);
|
|
|
|
|
addFunction("rand", fRand);
|
|
|
|
|
addFunction("srand", fSRand);
|
|
|
|
|
addFunction("ceil", fCeil);
|
|
|
|
|
addFunction("floor", fFloor);
|
|
|
|
|
addFunction("trunc", fTrunc);
|
|
|
|
|
addFunction("round", fRound);
|
|
|
|
|
addFunction("fmod", fFMod);
|
|
|
|
|
addFunction("min", fMin);
|
|
|
|
|
addFunction("max", fMax);
|
|
|
|
|
addFunction("inttostr", fIntToStr);
|
|
|
|
|
addFunction("floattostr", fFloatToStr);
|
|
|
|
|
addFunction("num2str", fFloatToStr);
|
|
|
|
|
addFunction("booltostr", fBoolToStr);
|
|
|
|
|
addFunction("bool2str", fBoolToStr);
|
|
|
|
|
addFunction("gauss", QFSPIMLightsheetEvaluationItem_fGauss);
|
|
|
|
|
addFunction("slit", fSlit);
|
|
|
|
|
addFunction("theta", fTheta);
|
|
|
|
|
addFunction("tanc", fTanc);
|
|
|
|
|
addFunction("sigmoid", fSigmoid);
|
|
|
|
|
addFunction("sign", fSign);
|
|
|
|
|
addFunction("tosystempathseparator", fToSystemPathSeparator);
|
|
|
|
|
addFunction("setdefault", fSetDefault);
|
2015-07-11 18:56:02 +08:00
|
|
|
|
addFunction("strdate", fStrDate);
|
2019-01-26 20:00:40 +08:00
|
|
|
|
addFunction("cmdparam", fCMDParam);
|
|
|
|
|
addFunction("argv", fCMDParam);
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// class destructor
|
|
|
|
|
JKQTPMathParser::~JKQTPMathParser()
|
|
|
|
|
{
|
2019-01-26 20:00:40 +08:00
|
|
|
|
clearFunctions();
|
|
|
|
|
clearVariables();
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-30 04:40:02 +08:00
|
|
|
|
void JKQTPMathParser::setData(void *__value)
|
|
|
|
|
{
|
|
|
|
|
this->data = __value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *JKQTPMathParser::getData() const
|
|
|
|
|
{
|
|
|
|
|
return this->data;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
void JKQTPMathParser::addVariableDouble(const std::string& ni, double* v)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
{
|
2019-01-26 20:00:40 +08:00
|
|
|
|
std::string name=strip(ni);
|
2015-07-11 18:56:02 +08:00
|
|
|
|
jkmpVariable nv;
|
|
|
|
|
nv.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
nv.num=v;
|
|
|
|
|
if (variableExists(name)) nv.internal=variables[name].internal; else nv.internal=false;
|
|
|
|
|
variables[name]=nv;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
void JKQTPMathParser::addVariableString(const std::string& ni, std::string* v)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
{
|
2019-01-26 20:00:40 +08:00
|
|
|
|
std::string name=strip(ni);
|
2015-07-11 18:56:02 +08:00
|
|
|
|
jkmpVariable nv;
|
|
|
|
|
nv.type=JKQTPMathParser::jkmpString;
|
|
|
|
|
nv.str=v;
|
|
|
|
|
if (variableExists(name)) nv.internal=variables[name].internal; else nv.internal=false;
|
|
|
|
|
variables[name]=nv;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
void JKQTPMathParser::addVariableBoolean(const std::string& ni, bool* v)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
{
|
2019-01-26 20:00:40 +08:00
|
|
|
|
std::string name=strip(ni);
|
2015-07-11 18:56:02 +08:00
|
|
|
|
jkmpVariable nv;
|
|
|
|
|
nv.type=JKQTPMathParser::jkmpBool;
|
|
|
|
|
nv.boolean=v;
|
|
|
|
|
if (variableExists(name)) nv.internal=variables[name].internal; else nv.internal=false;
|
|
|
|
|
variables[name]=nv;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
void JKQTPMathParser::addVariableDouble(const std::string& name, double v)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
{
|
|
|
|
|
jkmpVariable nv;
|
|
|
|
|
nv.type=JKQTPMathParser::jkmpDouble;
|
2019-01-26 20:00:40 +08:00
|
|
|
|
nv.num=new double;//static_cast<double*>(malloc(sizeof(double)));
|
2015-07-11 18:56:02 +08:00
|
|
|
|
nv.internal=true;
|
|
|
|
|
*(nv.num)=v;
|
|
|
|
|
// std::cout<<*(nv.num)<<std::endl;
|
2019-01-26 20:00:40 +08:00
|
|
|
|
variables[strip(name)]=nv;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
void JKQTPMathParser::addVariableString(const std::string& name, const std::string& v)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
{
|
|
|
|
|
jkmpVariable nv;
|
|
|
|
|
nv.type=JKQTPMathParser::jkmpString;
|
|
|
|
|
nv.str=new std::string;
|
|
|
|
|
nv.internal=true;
|
|
|
|
|
*(nv.str)=v;
|
|
|
|
|
variables[strip(name)]=nv;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
void JKQTPMathParser::addVariableBoolean(const std::string& name, bool v)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
{
|
|
|
|
|
jkmpVariable nv;
|
|
|
|
|
nv.type=JKQTPMathParser::jkmpBool;
|
|
|
|
|
nv.boolean=new bool;
|
|
|
|
|
nv.internal=true;
|
|
|
|
|
*(nv.boolean)=v;
|
2019-01-26 20:00:40 +08:00
|
|
|
|
variables[strip(name)]=nv;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
void JKQTPMathParser::addVariable(const std::string& name, JKQTPMathParser::jkmpResult result)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
{
|
|
|
|
|
switch(result.type) {
|
|
|
|
|
case jkmpDouble:
|
|
|
|
|
addVariableDouble(name, result.num);
|
|
|
|
|
break;
|
|
|
|
|
case jkmpString:
|
|
|
|
|
addVariableString(name, result.str);
|
|
|
|
|
break;
|
|
|
|
|
case jkmpBool:
|
|
|
|
|
addVariableBoolean(name, result.boolean);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void JKQTPMathParser::printVariables() {
|
|
|
|
|
if (variables.size()>0) {
|
|
|
|
|
for(std::map<std::string, jkmpVariable>::const_iterator i=variables.begin(); i!=variables.end(); ++i) {
|
|
|
|
|
jkmpVariable v=variables[i->first];
|
|
|
|
|
std::cout<<"'"<<i->first.c_str()<<"'"<<"\t\t";
|
|
|
|
|
if (v.internal) std::cout<<"intern"; else std::cout<<"extern";
|
|
|
|
|
std::cout<<"\t";
|
|
|
|
|
if (v.type==JKQTPMathParser::jkmpBool) std::cout<<"bool\t";
|
|
|
|
|
if (v.type==JKQTPMathParser::jkmpDouble) std::cout<<"double\t";
|
|
|
|
|
if (v.type==JKQTPMathParser::jkmpString) std::cout<<"string\t";
|
|
|
|
|
|
|
|
|
|
std::cout<<std::endl;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::pair<std::string, JKQTPMathParser::jkmpVariable> > JKQTPMathParser::getVariables()
|
|
|
|
|
{
|
|
|
|
|
std::vector<std::pair<std::string, jkmpVariable> > result;
|
|
|
|
|
if (variables.size()>0) {
|
|
|
|
|
for(std::map<std::string, jkmpVariable>::const_iterator i=variables.begin(); i!=variables.end(); ++i) {
|
|
|
|
|
jkmpVariable v=variables[i->first];
|
|
|
|
|
result.push_back(make_pair(i->first, v));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
void JKQTPMathParser::deleteVariable(const std::string& name) {
|
2015-07-11 18:56:02 +08:00
|
|
|
|
if (variableExists(name)) {
|
|
|
|
|
jkmpVariable v=variables[name];
|
|
|
|
|
if (v.internal) {
|
|
|
|
|
if (v.type==JKQTPMathParser::jkmpDouble) { delete v.num; }
|
|
|
|
|
else if (v.type==JKQTPMathParser::jkmpString) { delete v.str; }
|
|
|
|
|
else if (v.type==JKQTPMathParser::jkmpBool) { delete v.boolean; }
|
|
|
|
|
}
|
|
|
|
|
variables.erase(variables.find(name));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-30 04:40:02 +08:00
|
|
|
|
void JKQTPMathParser::clearFunctions() {functions.clear();}
|
|
|
|
|
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
|
|
void JKQTPMathParser::clearVariables(){
|
|
|
|
|
if (variables.size()>0) {
|
|
|
|
|
for(std::map<std::string, jkmpVariable>::const_iterator i=variables.begin(); i!=variables.end(); ++i) {
|
|
|
|
|
jkmpVariable v=variables[i->first];
|
|
|
|
|
if (v.internal) {
|
|
|
|
|
if (v.type==JKQTPMathParser::jkmpDouble) { delete v.num; }
|
|
|
|
|
else if (v.type==JKQTPMathParser::jkmpString) { delete v.str; }
|
|
|
|
|
else if (v.type==JKQTPMathParser::jkmpBool) { delete v.boolean; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
variables.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-28 05:52:00 +08:00
|
|
|
|
// gibt den aktuellen Wert einer Variablen zur<75>ck
|
2019-01-26 20:00:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult JKQTPMathParser::getVariable(const std::string& name)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
{
|
|
|
|
|
if (variableExists(name)) {
|
|
|
|
|
jkmpVariable v=variables[name];
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=v.type;
|
|
|
|
|
if (v.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
r.num=*(v.num);
|
|
|
|
|
} else if (v.type==JKQTPMathParser::jkmpString) {
|
|
|
|
|
r.str=*(v.str);
|
|
|
|
|
} else if (v.type==JKQTPMathParser::jkmpBool) {
|
|
|
|
|
r.boolean=*(v.boolean);
|
|
|
|
|
}
|
|
|
|
|
return r;
|
|
|
|
|
} else {
|
|
|
|
|
// error
|
|
|
|
|
//std::cout <<"error for: '"<<name<<"'"<<std::endl;
|
|
|
|
|
jkmpError(jkqtp_format("variable '%s' does not exist (getVariable)", name.c_str()));
|
|
|
|
|
}
|
|
|
|
|
JKQTPMathParser::jkmpResult res;
|
|
|
|
|
res.isValid=false;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult JKQTPMathParser::getVariableOrInvalid(const std::string& name)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
{
|
|
|
|
|
if (variableExists(name)) {
|
|
|
|
|
jkmpVariable v=variables[name];
|
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=v.type;
|
|
|
|
|
if (v.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
r.num=*(v.num);
|
|
|
|
|
} else if (v.type==JKQTPMathParser::jkmpString) {
|
|
|
|
|
r.str=*(v.str);
|
|
|
|
|
} else if (v.type==JKQTPMathParser::jkmpBool) {
|
|
|
|
|
r.boolean=*(v.boolean);
|
|
|
|
|
}
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
JKQTPMathParser::jkmpResult res;
|
|
|
|
|
res.isValid=false;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpVariable JKQTPMathParser::getVariableDef(const std::string& name)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
{
|
|
|
|
|
if (variableExists(name)) {
|
2019-01-26 20:00:40 +08:00
|
|
|
|
return variables[name];
|
2015-07-11 18:56:02 +08:00
|
|
|
|
} else {
|
|
|
|
|
// error
|
|
|
|
|
//std::cout <<"error for: '"<<name<<"'"<<std::endl;
|
|
|
|
|
jkmpError(jkqtp_format("variable '%s' does not exist (getVariableDef)", name.c_str()));
|
|
|
|
|
}
|
|
|
|
|
return jkmpVariable();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpEvaluateFunc JKQTPMathParser::getFunctionDef(const std::string& name){
|
2015-07-11 18:56:02 +08:00
|
|
|
|
if (functionExists(name)) {
|
2019-01-26 20:00:40 +08:00
|
|
|
|
return functions[name].function;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
} else {
|
|
|
|
|
// error
|
|
|
|
|
//std::cout <<name<<std::endl;
|
|
|
|
|
jkmpError(jkqtp_format("function '%s' does not exist (getFunctionDef)", name.c_str()));
|
|
|
|
|
}
|
2018-11-18 18:59:30 +08:00
|
|
|
|
return nullptr;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-30 04:40:02 +08:00
|
|
|
|
bool JKQTPMathParser::tempvariableExists(const std::string &name){
|
|
|
|
|
if (tempvariables.size()<=0) return false;
|
2019-06-30 23:34:41 +08:00
|
|
|
|
for (int i=static_cast<int>(tempvariables.size())-1; i>=0; i--) {
|
|
|
|
|
if (tempvariables[static_cast<size_t>(i)].name==name) return true;
|
2019-05-30 04:40:02 +08:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool JKQTPMathParser::variableExists(const std::string &name){ return tempvariableExists(name)||(variables.find(name)!=variables.end()); }
|
|
|
|
|
|
|
|
|
|
bool JKQTPMathParser::functionExists(const std::string &name){ return !(functions.find(name)==functions.end()); }
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
void JKQTPMathParser::addTempVariable(const std::string& name, JKQTPMathParser::jkmpResult value) {
|
2019-05-30 04:40:02 +08:00
|
|
|
|
jkmpTempVariable v;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
v.name=name;
|
|
|
|
|
v.type=value.type;
|
|
|
|
|
v.internal=true;
|
|
|
|
|
if (v.type==JKQTPMathParser::jkmpDouble) { v.num=new double; }
|
|
|
|
|
else if (v.type==JKQTPMathParser::jkmpString) { v.str=new std::string; }
|
|
|
|
|
else if (v.type==JKQTPMathParser::jkmpBool) { v.boolean=new bool; }
|
|
|
|
|
tempvariables.push_back(v);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
void JKQTPMathParser::setVariableDouble(const std::string& name, double value) {
|
2015-07-11 18:56:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult r;
|
|
|
|
|
r.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
r.num=value;
|
|
|
|
|
setVariable(name, r);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
void JKQTPMathParser::setVariable(const std::string& name, JKQTPMathParser::jkmpResult value)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
{
|
2019-01-26 20:00:40 +08:00
|
|
|
|
bool nexist=!variableExists(name);
|
|
|
|
|
jkmpVariable v=variables[name];
|
|
|
|
|
v.type=value.type;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
if (value.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
if (nexist) {v.num=new double; v.internal=true;}
|
|
|
|
|
*(v.num)=value.num;
|
|
|
|
|
} else if (value.type==JKQTPMathParser::jkmpString) {
|
|
|
|
|
if (nexist) {v.str=new std::string; v.internal=true;}
|
|
|
|
|
*(v.str)=value.str;
|
|
|
|
|
} else if (value.type==JKQTPMathParser::jkmpBool) {
|
|
|
|
|
if (nexist) {v.boolean=new bool; v.internal=true;}
|
|
|
|
|
*(v.boolean)=value.boolean;
|
|
|
|
|
}
|
|
|
|
|
variables[name]=v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// wertet eine Funktion aus
|
2019-01-26 20:00:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult JKQTPMathParser::evaluateFunction(const std::string& name, JKQTPMathParser::jkmpResult* params, unsigned char n)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
{
|
|
|
|
|
if (functionExists(name)) {
|
|
|
|
|
//std::cout <<"found" ;
|
2019-01-26 20:00:40 +08:00
|
|
|
|
return functions[name].function(params, n, this);
|
2015-07-11 18:56:02 +08:00
|
|
|
|
} else {
|
|
|
|
|
// error
|
|
|
|
|
//jkmpError("function does not exist")
|
|
|
|
|
jkmpError(jkqtp_format("function '%s' does not exist (evaluateFunction)", name.c_str()));
|
|
|
|
|
}
|
|
|
|
|
JKQTPMathParser::jkmpResult res;
|
|
|
|
|
res.isValid=false;
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
void JKQTPMathParser::addFunction(const std::string& name, jkmpEvaluateFunc function) {
|
2015-07-11 18:56:02 +08:00
|
|
|
|
jkmpFunctionDescriptor f;
|
|
|
|
|
f.function=function;
|
|
|
|
|
f.name=name;
|
|
|
|
|
functions[name]=f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpTokenType JKQTPMathParser::getToken(){
|
2019-01-26 20:00:40 +08:00
|
|
|
|
char ch=0;
|
|
|
|
|
while(program->get(ch) && isspace(ch)) {
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (ch) {
|
|
|
|
|
case 0:
|
|
|
|
|
case -1:
|
|
|
|
|
return CurrentToken=END;
|
|
|
|
|
case ';':
|
|
|
|
|
return CurrentToken=PRINT;
|
|
|
|
|
case '*':
|
|
|
|
|
return CurrentToken=MUL;
|
|
|
|
|
case '/':
|
|
|
|
|
return CurrentToken=DIV;
|
|
|
|
|
case '%':
|
|
|
|
|
return CurrentToken=MODULO;
|
|
|
|
|
case '+':
|
|
|
|
|
return CurrentToken=PLUS;
|
|
|
|
|
case '-':
|
|
|
|
|
return CurrentToken=MINUS;
|
|
|
|
|
case '(':
|
|
|
|
|
return CurrentToken=LBRACKET;
|
|
|
|
|
case ')':
|
|
|
|
|
return CurrentToken=RBRACKET;
|
|
|
|
|
case ',':
|
|
|
|
|
return CurrentToken=PARAMETER_DIV;
|
|
|
|
|
case '"':
|
|
|
|
|
return CurrentToken=STRING_DELIM;
|
|
|
|
|
case '^':
|
|
|
|
|
return CurrentToken=POWER;
|
|
|
|
|
case '!':{
|
|
|
|
|
char ch1=0;
|
|
|
|
|
if (*program) program->get(ch1);
|
|
|
|
|
if (ch1=='=') return CurrentToken=COMP_UNEQUAL;
|
|
|
|
|
// else
|
|
|
|
|
program->putback(ch1);
|
|
|
|
|
return CurrentToken=FACTORIAL_LOGIC_NOT;
|
|
|
|
|
}
|
|
|
|
|
case '&':{
|
|
|
|
|
char ch1=0;
|
|
|
|
|
if (*program) program->get(ch1);
|
|
|
|
|
if (ch1=='&') return CurrentToken=LOGIC_AND;
|
|
|
|
|
// else
|
|
|
|
|
program->putback(ch1);
|
|
|
|
|
jkmpError("undefined operator '&'; Did you mean LOGICAL_AND ('&&' / 'and')?");
|
2018-12-24 03:27:24 +08:00
|
|
|
|
break;
|
2019-01-26 20:00:40 +08:00
|
|
|
|
}
|
|
|
|
|
case '|':{
|
|
|
|
|
char ch1=0;
|
|
|
|
|
if (*program) program->get(ch1);
|
|
|
|
|
if (ch1=='|') return CurrentToken=LOGIC_OR;
|
|
|
|
|
// else
|
|
|
|
|
program->putback(ch1);
|
|
|
|
|
jkmpError("undefined operator '|'; Did you mean LOGICAL_OR ('||' / 'or')?");
|
2018-12-24 03:27:24 +08:00
|
|
|
|
break;
|
2019-01-26 20:00:40 +08:00
|
|
|
|
}
|
|
|
|
|
case '=':{
|
|
|
|
|
char ch1=0;
|
|
|
|
|
if (*program) program->get(ch1);
|
|
|
|
|
if (ch1=='=') return CurrentToken=COMP_EQUALT;
|
|
|
|
|
// else
|
|
|
|
|
program->putback(ch1);
|
|
|
|
|
return CurrentToken=ASSIGN;
|
|
|
|
|
}
|
|
|
|
|
case '>':{
|
|
|
|
|
char ch1=0;
|
|
|
|
|
if (*program) program->get(ch1);
|
|
|
|
|
if (ch1=='=') return CurrentToken=COMP_GEQUAL;
|
|
|
|
|
// else
|
|
|
|
|
program->putback(ch1);
|
|
|
|
|
return CurrentToken=COMP_GREATER;
|
|
|
|
|
}
|
|
|
|
|
case '<':{
|
|
|
|
|
char ch1=0;
|
|
|
|
|
if (*program) program->get(ch1);
|
|
|
|
|
if (ch1=='=') return CurrentToken=COMP_SEQUAL;
|
|
|
|
|
// else
|
|
|
|
|
program->putback(ch1);
|
|
|
|
|
return CurrentToken=COMP_SMALLER;
|
|
|
|
|
}
|
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
|
case '5': case '6': case '7': case '8': case '9':{
|
|
|
|
|
program->putback(ch);
|
|
|
|
|
(*program) >> NumberValue;
|
|
|
|
|
return CurrentToken=NUMBER;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
if (isalpha(ch) || (ch=='_')) { // try to recognize NAME, LOGIC_TRUE, LOGIC_FALSE, DIFF_LBRACKET
|
|
|
|
|
StringValue=ch;
|
|
|
|
|
while (program->get(ch) && (isalnum(ch) || (ch=='_') || (ch=='.'))) {
|
2015-07-11 18:56:02 +08:00
|
|
|
|
if (isalnum(ch) || (ch=='_') || (ch=='.')) {
|
|
|
|
|
StringValue.push_back(ch);
|
|
|
|
|
}
|
2019-01-26 20:00:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
program->putback(ch); // now put the last thing read back int the stream, as it
|
|
|
|
|
// could belong to the next token
|
|
|
|
|
|
|
|
|
|
if (StringValue=="true") return CurrentToken=LOGIC_TRUE;
|
|
|
|
|
if (StringValue=="false") return CurrentToken=LOGIC_FALSE;
|
|
|
|
|
if (StringValue=="and") return CurrentToken=LOGIC_AND;
|
|
|
|
|
if (StringValue=="or") return CurrentToken=LOGIC_OR;
|
|
|
|
|
if (StringValue=="xor") return CurrentToken=LOGIC_XOR;
|
|
|
|
|
if (StringValue=="not") return CurrentToken=LOGIC_NOT;
|
|
|
|
|
if (StringValue=="nor") return CurrentToken=LOGIC_NOR;
|
|
|
|
|
if (StringValue=="nand") return CurrentToken=LOGIC_NAND;
|
|
|
|
|
|
|
|
|
|
return CurrentToken=NAME;
|
|
|
|
|
}
|
|
|
|
|
// the parser has found an unknown token. an exception will be thrown
|
|
|
|
|
//std::cout<<StringValue<<", "<<ch<<std::endl;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
jkmpError(jkqtp_format("unknown token currentCharacter='%s', currentString='%s'", jkqtp_chartostr(ch).c_str(), StringValue.c_str()));
|
2019-01-26 20:00:40 +08:00
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
return END;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpNode* JKQTPMathParser::parse(const std::string& prog){
|
|
|
|
|
program=new std::istringstream(prog);
|
2018-11-18 18:59:30 +08:00
|
|
|
|
JKQTPMathParser::jkmpNode* res=nullptr;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpNodeList* resList=new JKQTPMathParser::jkmpNodeList(this);
|
2019-01-26 20:00:40 +08:00
|
|
|
|
while(true) {
|
|
|
|
|
getToken();
|
2015-07-11 18:56:02 +08:00
|
|
|
|
if (CurrentToken == END) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
res= logicalExpression(false);
|
|
|
|
|
resList->add(res);
|
2019-01-26 20:00:40 +08:00
|
|
|
|
}
|
|
|
|
|
delete program;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
if (resList->getCount()==1) {
|
|
|
|
|
delete resList;
|
|
|
|
|
return res;
|
|
|
|
|
} else {
|
|
|
|
|
return resList;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult JKQTPMathParser::evaluate(const std::string& prog) {
|
2015-07-11 18:56:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpNode* res=parse(prog);
|
|
|
|
|
JKQTPMathParser::jkmpResult r=res->evaluate();
|
|
|
|
|
delete res;
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpNode* JKQTPMathParser::logicalExpression(bool get){
|
|
|
|
|
JKQTPMathParser::jkmpNode* left=logicalTerm(get);
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
for(;;) // forever, do until you find anything else than an expressions
|
|
|
|
|
switch(CurrentToken) {
|
|
|
|
|
case LOGIC_OR:
|
|
|
|
|
left= new jkmpBinaryBoolNode(jkmpLOPor, left, logicalTerm(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
case LOGIC_XOR:
|
|
|
|
|
left= new jkmpBinaryBoolNode(jkmpLOPxor, left, logicalTerm(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
case LOGIC_NOR:
|
|
|
|
|
left= new jkmpBinaryBoolNode(jkmpLOPnor, left, logicalTerm(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return left;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpNode* JKQTPMathParser::logicalTerm(bool get){
|
|
|
|
|
JKQTPMathParser::jkmpNode* left=compExpression(get);
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
for(;;) // forever, do until you find anything else than an expressions
|
|
|
|
|
switch(CurrentToken) {
|
|
|
|
|
case LOGIC_AND:
|
|
|
|
|
left= new jkmpBinaryBoolNode(jkmpLOPand, left, compExpression(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
case LOGIC_NAND:
|
|
|
|
|
left= new jkmpBinaryBoolNode(jkmpLOPnand, left, compExpression(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return left;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpNode* JKQTPMathParser::compExpression(bool get){
|
|
|
|
|
JKQTPMathParser::jkmpNode* left=mathExpression(get);
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
for(;;) // forever, do until you find anything else than an expressions
|
|
|
|
|
switch(CurrentToken) {
|
|
|
|
|
case COMP_EQUALT:
|
|
|
|
|
left= new jkmpCompareNode(jkmpCOMPequal, left, mathExpression(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
case COMP_UNEQUAL:
|
|
|
|
|
left= new jkmpCompareNode(jkmpCOMPnequal, left, mathExpression(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
case COMP_GREATER:
|
|
|
|
|
left= new jkmpCompareNode(jkmpCOMPgreater, left, mathExpression(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
case COMP_SMALLER:
|
|
|
|
|
left= new jkmpCompareNode(jkmpCOMPlesser, left, mathExpression(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
case COMP_GEQUAL:
|
|
|
|
|
left= new jkmpCompareNode(jkmpCOMPgreaterequal, left, mathExpression(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
case COMP_SEQUAL:
|
|
|
|
|
left= new jkmpCompareNode(jkmpCOMPlesserequal, left, mathExpression(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return left;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpNode* JKQTPMathParser::mathExpression(bool get){
|
|
|
|
|
JKQTPMathParser::jkmpNode* left=mathTerm(get);
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
for(;;) // forever, do until you find anything else than an expressions
|
|
|
|
|
switch(CurrentToken) {
|
|
|
|
|
case PLUS:
|
|
|
|
|
left= new jkmpBinaryArithmeticNode('+', left, mathTerm(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
case MINUS:
|
|
|
|
|
left= new jkmpBinaryArithmeticNode('-', left, mathTerm(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return left;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpNode* JKQTPMathParser::mathTerm(bool get){
|
|
|
|
|
JKQTPMathParser::jkmpNode* left=primary(get);
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
for(;;) // forever, do until you find anything else than a term
|
|
|
|
|
switch(CurrentToken) {
|
|
|
|
|
case MUL:
|
|
|
|
|
left= new jkmpBinaryArithmeticNode('*', left, primary(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
case DIV:
|
|
|
|
|
left= new jkmpBinaryArithmeticNode('/', left, primary(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
case MODULO:
|
|
|
|
|
left= new jkmpBinaryArithmeticNode('%', left, primary(true), this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return left;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpNode* JKQTPMathParser::primary(bool get){
|
2018-11-18 18:59:30 +08:00
|
|
|
|
JKQTPMathParser::jkmpNode* res=nullptr;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
if (get) getToken();
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
switch(CurrentToken) {
|
|
|
|
|
case NUMBER: {
|
2015-07-11 18:56:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult val;
|
|
|
|
|
val.type=JKQTPMathParser::jkmpDouble;
|
2019-01-26 20:00:40 +08:00
|
|
|
|
val.num=NumberValue;
|
|
|
|
|
getToken();
|
|
|
|
|
res= new jkmpConstantNode(val, this, nullptr);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case NAME: {
|
2019-05-30 04:40:02 +08:00
|
|
|
|
//JKQTPMathParser::jkmpNode* def=nullptr;
|
2019-01-26 20:00:40 +08:00
|
|
|
|
std::string varname=StringValue;
|
|
|
|
|
getToken();
|
|
|
|
|
if (CurrentToken == ASSIGN) { // assign a variable name
|
|
|
|
|
res=new jkmpVariableAssignNode(varname, logicalExpression(true), this, nullptr);
|
|
|
|
|
} else if (CurrentToken == LBRACKET) { // function found
|
|
|
|
|
JKQTPMathParser::jkmpNode** params=static_cast<JKQTPMathParser::jkmpNode**>(malloc(255*sizeof(JKQTPMathParser::jkmpNode*)));
|
|
|
|
|
unsigned char num=0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getToken();
|
|
|
|
|
while ((CurrentToken != RBRACKET)&&(CurrentToken!=END)) {
|
2015-07-11 18:56:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpNode* parameter=logicalExpression(num>0);
|
2019-01-26 20:00:40 +08:00
|
|
|
|
params[num]=parameter;
|
|
|
|
|
num++;
|
|
|
|
|
if ((CurrentToken!=RBRACKET)&&(CurrentToken!=PARAMETER_DIV)&&(CurrentToken!=END))
|
2015-07-11 18:56:02 +08:00
|
|
|
|
jkmpError(jkqtp_format("')' or ',' expected, but '%s' found", currenttokentostring().c_str()));
|
2019-01-26 20:00:40 +08:00
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
if ( CurrentToken != RBRACKET ) jkmpError(jkqtp_format("')' expected, but '%s' found", currenttokentostring().c_str()));;
|
2018-11-18 18:59:30 +08:00
|
|
|
|
JKQTPMathParser::jkmpNode** p=nullptr;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
if (num>0) {
|
2019-01-26 20:00:40 +08:00
|
|
|
|
p=static_cast<JKQTPMathParser::jkmpNode**>(malloc(sizeof(JKQTPMathParser::jkmpNode*) * num));
|
2015-07-11 18:56:02 +08:00
|
|
|
|
for (int i=0; i<num; i++) {
|
|
|
|
|
p[i]=params[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-26 20:00:40 +08:00
|
|
|
|
res=new jkmpFunctionNode(varname, p, num, this, nullptr);
|
|
|
|
|
free(params);
|
|
|
|
|
getToken();
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
} else {
|
|
|
|
|
res=new jkmpVariableNode(varname, this, nullptr);
|
|
|
|
|
}
|
|
|
|
|
//res= def;
|
|
|
|
|
break;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
case STRING_DELIM: {// found primary: "data" == string constant
|
2015-07-11 18:56:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult val;
|
|
|
|
|
val.type=JKQTPMathParser::jkmpString;
|
2019-01-26 20:00:40 +08:00
|
|
|
|
val.str=readDelim('"');
|
|
|
|
|
res= new jkmpConstantNode(val, this, nullptr);
|
|
|
|
|
getToken();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
case MINUS: // found primary: - primary
|
|
|
|
|
res= new jkmpUnaryNode('-', primary(true), this, nullptr);
|
|
|
|
|
break;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
case LOGIC_NOT:
|
|
|
|
|
res= new jkmpUnaryNode('!', primary(true), this, nullptr);
|
|
|
|
|
break;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
case LBRACKET: { // found primary ( expression )
|
2015-07-11 18:56:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpNode* expr=logicalExpression(true);
|
2019-01-26 20:00:40 +08:00
|
|
|
|
if (CurrentToken != RBRACKET) jkmpError(jkqtp_format("')' expected, but '%s' found", currenttokentostring().c_str()));
|
|
|
|
|
getToken(); // swallow ")"
|
|
|
|
|
res= expr;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case FACTORIAL_LOGIC_NOT:
|
2015-07-11 18:56:02 +08:00
|
|
|
|
case LOGIC_TRUE: {// found 'true'
|
|
|
|
|
JKQTPMathParser::jkmpResult val;
|
|
|
|
|
val.type=JKQTPMathParser::jkmpBool;
|
2019-01-26 20:00:40 +08:00
|
|
|
|
val.boolean=true;
|
|
|
|
|
res= new jkmpConstantNode(val, this, nullptr);
|
|
|
|
|
getToken();
|
|
|
|
|
break;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
2019-01-26 20:00:40 +08:00
|
|
|
|
case LOGIC_FALSE: {// found 'false'
|
2015-07-11 18:56:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult val;
|
|
|
|
|
val.type=JKQTPMathParser::jkmpBool;
|
2019-01-26 20:00:40 +08:00
|
|
|
|
val.boolean=false;
|
|
|
|
|
res= new jkmpConstantNode(val, this, nullptr);
|
|
|
|
|
getToken();
|
|
|
|
|
break;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
default:
|
|
|
|
|
jkmpError("primary expected");
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
if (CurrentToken==POWER) {
|
|
|
|
|
res=new jkmpBinaryArithmeticNode('^', res, primary(true), this, nullptr);
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
return res;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string JKQTPMathParser::readDelim(char delimiter){
|
2019-01-26 20:00:40 +08:00
|
|
|
|
std::string res="";
|
|
|
|
|
char ch=0;
|
|
|
|
|
|
|
|
|
|
while(program->get(ch)) {
|
|
|
|
|
if (ch==delimiter ) {
|
2019-05-30 04:40:02 +08:00
|
|
|
|
const char ch1=static_cast<char>(program->peek());
|
2019-01-26 20:00:40 +08:00
|
|
|
|
if (ch1==delimiter) {
|
|
|
|
|
program->get(ch);
|
|
|
|
|
res=res+delimiter;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-01-26 20:00:40 +08:00
|
|
|
|
} else if ((*program) && (ch!=delimiter)) res=res+ch;
|
|
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
return res;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************************
|
2018-12-28 05:52:00 +08:00
|
|
|
|
* Klassenhierarchie, um Ausdr<EFBFBD>cke darzustellen
|
2015-07-11 18:56:02 +08:00
|
|
|
|
******************************************************************************************/
|
|
|
|
|
JKQTPMathParser::jkmpUnaryNode::jkmpUnaryNode(char op, JKQTPMathParser::jkmpNode* c, JKQTPMathParser* p, JKQTPMathParser::jkmpNode* par){
|
|
|
|
|
child=c;
|
|
|
|
|
child->setParent(this);
|
|
|
|
|
setParser(p);
|
|
|
|
|
setParent(par);
|
|
|
|
|
operation=op;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-28 05:52:00 +08:00
|
|
|
|
JKQTPMathParser::jkmpUnaryNode::~jkmpUnaryNode() {delete child;}
|
|
|
|
|
|
2015-07-11 18:56:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult JKQTPMathParser::jkmpUnaryNode::evaluate(){
|
|
|
|
|
JKQTPMathParser::jkmpResult c=child->evaluate();
|
|
|
|
|
JKQTPMathParser::jkmpResult res;
|
|
|
|
|
res=c;
|
|
|
|
|
|
|
|
|
|
switch(operation) {
|
|
|
|
|
case '!':
|
|
|
|
|
if (c.type==JKQTPMathParser::jkmpBool) {
|
|
|
|
|
res.boolean=!c.boolean;
|
|
|
|
|
return res;
|
|
|
|
|
} else parser->jkmpError("'!' only defined for bool");
|
|
|
|
|
break;
|
|
|
|
|
case '-':
|
|
|
|
|
if (c.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
res.num=-c.num;
|
|
|
|
|
return res;
|
|
|
|
|
} else parser->jkmpError("'-' only defined for double");
|
|
|
|
|
break;
|
|
|
|
|
default: parser->jkmpError("unknown unary operation");
|
|
|
|
|
}
|
|
|
|
|
res.isValid=false;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpBinaryArithmeticNode::jkmpBinaryArithmeticNode(char op, JKQTPMathParser::jkmpNode* l, JKQTPMathParser::jkmpNode* r, JKQTPMathParser* p, JKQTPMathParser::jkmpNode* par){
|
|
|
|
|
left=l;
|
|
|
|
|
right=r;
|
|
|
|
|
left->setParent(this);
|
|
|
|
|
right->setParent(this);
|
|
|
|
|
setParser(p);
|
|
|
|
|
setParent(par);
|
|
|
|
|
operation=op;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-28 05:52:00 +08:00
|
|
|
|
JKQTPMathParser::jkmpBinaryArithmeticNode::~jkmpBinaryArithmeticNode() { delete left; delete right;}
|
|
|
|
|
|
2015-07-11 18:56:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult JKQTPMathParser::jkmpBinaryArithmeticNode::evaluate(){
|
2018-12-28 05:52:00 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult l=left->evaluate();
|
2015-07-11 18:56:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult r=right->evaluate();
|
|
|
|
|
JKQTPMathParser::jkmpResult res;
|
|
|
|
|
|
|
|
|
|
switch(operation) {
|
|
|
|
|
case '+':
|
|
|
|
|
if (l.type==r.type) {
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
res.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
res.num=l.num+r.num;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpString) {
|
|
|
|
|
res.type=JKQTPMathParser::jkmpString;
|
|
|
|
|
res.str=l.str+r.str;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
parser->jkmpError("bool may not be added");
|
|
|
|
|
} else parser->jkmpError("different Types while adding");
|
|
|
|
|
break;
|
|
|
|
|
case '-':
|
|
|
|
|
if (l.type==r.type) {
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
res.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
res.num=l.num-r.num;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
parser->jkmpError("bool or string may not be subtractet");
|
|
|
|
|
} else parser->jkmpError("different Types while subtracting");
|
|
|
|
|
break;
|
|
|
|
|
case '*':
|
|
|
|
|
if (l.type==r.type) {
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
res.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
res.num=l.num*r.num;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
parser->jkmpError("bool or string may not be multiplied");
|
|
|
|
|
} else parser->jkmpError("different Types while multiplying");
|
|
|
|
|
break;
|
|
|
|
|
case '/':
|
|
|
|
|
if (l.type==r.type) {
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
res.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
res.num=l.num/r.num;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
parser->jkmpError("bool or string may not be divided");
|
|
|
|
|
} else parser->jkmpError("different Types while dividing");
|
|
|
|
|
break;
|
|
|
|
|
case '%':
|
|
|
|
|
if (l.type==r.type) {
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
if ((l.num==floor(l.num)) && (r.num==floor(r.num))) {
|
|
|
|
|
res.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
res.num=l.num/r.num;
|
|
|
|
|
return res;
|
|
|
|
|
} else {
|
|
|
|
|
parser->jkmpError("modulo is only defined for integer arguments");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
parser->jkmpError("modulo is not defined for bool or string arguments");
|
|
|
|
|
} else parser->jkmpError("different Types while calculating modulo");
|
|
|
|
|
break;
|
|
|
|
|
case '^':
|
|
|
|
|
if (l.type==r.type) {
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
res.type=JKQTPMathParser::jkmpDouble;
|
|
|
|
|
res.num=pow(l.num, r.num);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
parser->jkmpError("bool or string may not be taken to powers");
|
|
|
|
|
} else parser->jkmpError("different types while taking to power");
|
|
|
|
|
break;
|
|
|
|
|
default: parser->jkmpError("unknown arithmetic operation");
|
|
|
|
|
}
|
|
|
|
|
res.isValid=false;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpCompareNode::jkmpCompareNode(char op, JKQTPMathParser::jkmpNode* l, JKQTPMathParser::jkmpNode* r, JKQTPMathParser* p, JKQTPMathParser::jkmpNode* par){
|
|
|
|
|
left=l;
|
|
|
|
|
right=r;
|
|
|
|
|
left->setParent(this);
|
|
|
|
|
right->setParent(this);
|
|
|
|
|
setParser(p);
|
|
|
|
|
setParent(par);
|
|
|
|
|
operation=op;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-28 05:52:00 +08:00
|
|
|
|
JKQTPMathParser::jkmpCompareNode::~jkmpCompareNode() { delete left; delete right;}
|
|
|
|
|
|
2015-07-11 18:56:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult JKQTPMathParser::jkmpCompareNode::evaluate(){
|
2018-12-28 05:52:00 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult l=left->evaluate();
|
2015-07-11 18:56:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpResult r=right->evaluate();
|
|
|
|
|
JKQTPMathParser::jkmpResult res;
|
|
|
|
|
res.type=JKQTPMathParser::jkmpBool;
|
|
|
|
|
|
|
|
|
|
if (l.type!=r.type) parser->jkmpError("you can't compare different datatypes");
|
|
|
|
|
|
|
|
|
|
switch(operation) {
|
|
|
|
|
case jkmpCOMPequal:
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
res.boolean=(l.num==r.num);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpBool) {
|
|
|
|
|
res.boolean=(l.boolean==r.boolean);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpString) {
|
|
|
|
|
res.boolean=(l.str==r.str);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case jkmpCOMPnequal:
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
res.boolean=(l.num!=r.num);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpBool) {
|
|
|
|
|
res.boolean=(l.boolean!=r.boolean);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpString) {
|
|
|
|
|
res.boolean=(l.str!=r.str);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case jkmpCOMPgreater:
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
res.boolean=(l.num>r.num);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpBool) {
|
|
|
|
|
res.boolean=(l.boolean>r.boolean);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpString) {
|
|
|
|
|
res.boolean=(l.str>r.str);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case jkmpCOMPlesser:
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
res.boolean=(l.num<r.num);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpBool) {
|
|
|
|
|
res.boolean=(l.boolean<r.boolean);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpString) {
|
|
|
|
|
res.boolean=(l.str<r.str);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case jkmpCOMPgreaterequal:
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
res.boolean=(l.num>=r.num);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpBool) {
|
|
|
|
|
res.boolean=(l.boolean>=r.boolean);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpString) {
|
|
|
|
|
res.boolean=(l.str>=r.str);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case jkmpCOMPlesserequal:
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpDouble) {
|
|
|
|
|
res.boolean=(l.num<=r.num);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpBool) {
|
|
|
|
|
res.boolean=(l.boolean<=r.boolean);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (l.type==JKQTPMathParser::jkmpString) {
|
|
|
|
|
res.boolean=(l.str<=r.str);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default: parser->jkmpError("unknown compare operation");
|
|
|
|
|
}
|
|
|
|
|
res.isValid=false;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpBinaryBoolNode::jkmpBinaryBoolNode(char op, JKQTPMathParser::jkmpNode* l, JKQTPMathParser::jkmpNode* r, JKQTPMathParser* p, JKQTPMathParser::jkmpNode* par){
|
|
|
|
|
left=l;
|
|
|
|
|
right=r;
|
|
|
|
|
left->setParent(this);
|
|
|
|
|
right->setParent(this);
|
|
|
|
|
setParser(p);
|
|
|
|
|
setParent(par);
|
|
|
|
|
operation=op;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpResult JKQTPMathParser::jkmpBinaryBoolNode::evaluate(){
|
|
|
|
|
JKQTPMathParser::jkmpResult l=left->evaluate();
|
|
|
|
|
JKQTPMathParser::jkmpResult r=right->evaluate();
|
|
|
|
|
JKQTPMathParser::jkmpResult res;
|
|
|
|
|
|
|
|
|
|
if ((l.type!=JKQTPMathParser::jkmpBool)||(r.type!=JKQTPMathParser::jkmpBool)) parser->jkmpError("logical operations only for bool");
|
|
|
|
|
|
|
|
|
|
switch(operation) {
|
|
|
|
|
case jkmpLOPand:
|
|
|
|
|
res.type=JKQTPMathParser::jkmpBool;
|
|
|
|
|
res.boolean=l.boolean&&r.boolean;
|
|
|
|
|
return res;
|
|
|
|
|
case jkmpLOPor:
|
|
|
|
|
res.type=JKQTPMathParser::jkmpBool;
|
|
|
|
|
res.boolean=l.boolean||r.boolean;
|
|
|
|
|
return res;
|
|
|
|
|
case jkmpLOPnor:
|
|
|
|
|
res.type=JKQTPMathParser::jkmpBool;
|
|
|
|
|
res.boolean=!(l.boolean||r.boolean);
|
|
|
|
|
return res;
|
|
|
|
|
case jkmpLOPxor:
|
|
|
|
|
res.type=JKQTPMathParser::jkmpBool;
|
|
|
|
|
res.boolean=(l.boolean&& (!r.boolean))||(r.boolean&& (!l.boolean));
|
|
|
|
|
return res;
|
|
|
|
|
case jkmpLOPnand:
|
|
|
|
|
res.type=JKQTPMathParser::jkmpBool;
|
|
|
|
|
res.boolean=!(l.boolean&&r.boolean);
|
|
|
|
|
return res;
|
|
|
|
|
default: parser->jkmpError("unknown error");
|
|
|
|
|
}
|
|
|
|
|
res.isValid=false;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-12-28 05:52:00 +08:00
|
|
|
|
JKQTPMathParser::jkmpBinaryBoolNode::~jkmpBinaryBoolNode() { delete left; delete right;}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpVariableNode::jkmpVariableNode(const std::string& name, JKQTPMathParser* p, JKQTPMathParser::jkmpNode* par) {
|
2015-07-11 18:56:02 +08:00
|
|
|
|
var=name;
|
|
|
|
|
setParser(p);
|
|
|
|
|
setParent(par);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpResult JKQTPMathParser::jkmpVariableNode::evaluate() {
|
|
|
|
|
return getParser()->getVariable(var);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void JKQTPMathParser::jkmpNodeList::add(JKQTPMathParser::jkmpNode* n){
|
|
|
|
|
list.push_back(n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpResult JKQTPMathParser::jkmpNodeList::evaluate(){
|
|
|
|
|
int n=getCount();
|
|
|
|
|
JKQTPMathParser::jkmpResult res;
|
|
|
|
|
//std::cout<<"Nodelist.count()="<<n<<std::endl;
|
|
|
|
|
if (n>0) {
|
2019-01-26 20:00:40 +08:00
|
|
|
|
for (size_t i=0; i<static_cast<size_t>(n); i++) {
|
2015-07-11 18:56:02 +08:00
|
|
|
|
res=list[i]->evaluate();
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
} else parser->jkmpError("NodeList empty");
|
|
|
|
|
res.isValid=false;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 23:34:41 +08:00
|
|
|
|
int JKQTPMathParser::jkmpNodeList::getCount() {
|
|
|
|
|
return static_cast<int>(list.size());
|
|
|
|
|
}
|
2019-05-30 04:40:02 +08:00
|
|
|
|
|
2019-06-30 23:34:41 +08:00
|
|
|
|
JKQTPMathParser::jkmpNodeList::jkmpNodeList(JKQTPMathParser *p) {
|
|
|
|
|
setParser(p); setParent(nullptr);
|
|
|
|
|
}
|
2018-12-28 05:52:00 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpNodeList::~jkmpNodeList() = default;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
|
|
|
2019-06-30 23:34:41 +08:00
|
|
|
|
JKQTPMathParser::jkmpVariableAssignNode::~jkmpVariableAssignNode() {
|
|
|
|
|
delete child;
|
|
|
|
|
}
|
2018-12-28 05:52:00 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpVariableAssignNode::jkmpVariableAssignNode(const std::string& var, JKQTPMathParser::jkmpNode* c, JKQTPMathParser* p, JKQTPMathParser::jkmpNode* par){
|
2015-07-11 18:56:02 +08:00
|
|
|
|
child=c;
|
|
|
|
|
child->setParent(this);
|
|
|
|
|
setParser(p);
|
|
|
|
|
setParent(par);
|
|
|
|
|
variable=var;
|
|
|
|
|
// std::cout<<"assign: "<<var<<std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpResult JKQTPMathParser::jkmpVariableAssignNode::evaluate(){
|
|
|
|
|
JKQTPMathParser::jkmpResult res=child->evaluate();
|
|
|
|
|
// std::cout<<"assign: "<<variable<<" "<<res.num<<std::endl;
|
|
|
|
|
getParser()->setVariable(variable, res);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpFunctionNode::jkmpFunctionNode(const std::string& name, JKQTPMathParser::jkmpNode** c, unsigned char num, JKQTPMathParser* p, JKQTPMathParser::jkmpNode* par) {
|
2015-07-11 18:56:02 +08:00
|
|
|
|
child=c;
|
|
|
|
|
n=num;
|
|
|
|
|
fun=name;
|
|
|
|
|
setParser(p);
|
|
|
|
|
setParent(par);
|
|
|
|
|
if (n>0) {
|
|
|
|
|
for (int i=0; i<n; i++) {
|
|
|
|
|
child[i]->setParent(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
function=p->getFunctionDef(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpResult JKQTPMathParser::jkmpFunctionNode::evaluate() {
|
|
|
|
|
JKQTPMathParser::jkmpResult data[255];
|
|
|
|
|
if (n>0) {
|
|
|
|
|
for (int i=0; i<n; i++) {
|
|
|
|
|
data[i]=child[i]->evaluate();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-30 04:40:02 +08:00
|
|
|
|
// JKQTPMathParser::jkmpResult r= getParser()->evaluateFunction(fun, data,n);
|
2015-07-11 18:56:02 +08:00
|
|
|
|
return function(data,n, parser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpFunctionNode::~jkmpFunctionNode() {
|
|
|
|
|
/*if (n>0) {
|
|
|
|
|
for (int i=0; i<n; i++) {
|
|
|
|
|
delete child[i];
|
|
|
|
|
}
|
|
|
|
|
}*/
|
2018-11-18 18:59:30 +08:00
|
|
|
|
if ((child!=nullptr) && (n>0)) {
|
2015-07-11 18:56:02 +08:00
|
|
|
|
for (int i=0; i<n; i++) {
|
2019-01-26 20:00:40 +08:00
|
|
|
|
delete child[i];
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
free(child);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpResult::jkmpResult()
|
|
|
|
|
{
|
|
|
|
|
isValid=true;
|
|
|
|
|
type=jkmpDouble; /*!< \brief type of the result */
|
|
|
|
|
str=""; /*!< \brief contains result if \c type==jkmpString */
|
|
|
|
|
num=0; /*!< \brief contains result if \c type==jkmpDouble */
|
|
|
|
|
boolean=false; /*!< \brief contains result if \c type==jkmpBool */
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-30 04:40:02 +08:00
|
|
|
|
std::string JKQTPMathParser::jkmpResult::toString() {
|
|
|
|
|
switch(type) {
|
|
|
|
|
case jkmpDouble: return jkqtp_floattostr(num);
|
|
|
|
|
case jkmpString: return str;
|
|
|
|
|
case jkmpBool: return jkqtp_booltostr(boolean);
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string JKQTPMathParser::jkmpResult::toTypeString() {
|
|
|
|
|
switch(type) {
|
|
|
|
|
case jkmpDouble: return jkqtp_floattostr(num)+" [number]";
|
|
|
|
|
case jkmpString: return str+" [string]";
|
|
|
|
|
case jkmpBool: return jkqtp_booltostr(boolean)+" [bool]";
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-11 18:56:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpVariable::jkmpVariable()
|
|
|
|
|
{
|
|
|
|
|
type=jkmpDouble; /*!< \brief type of the variable */
|
|
|
|
|
internal=false; /*!< \brief this is an internal variable */
|
2018-11-18 18:59:30 +08:00
|
|
|
|
str=nullptr; /*!< \brief this points to the variable data if \c type==jkmpString */
|
|
|
|
|
num=nullptr; /*!< \brief this points to the variable data if \c type==jkmpDouble */
|
|
|
|
|
boolean=nullptr;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void JKQTPMathParser::setArgCV(int argc, char **argv) {
|
2019-01-26 20:00:40 +08:00
|
|
|
|
this->argc=argc;
|
|
|
|
|
this->argv=argv;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
std::string JKQTPMathParser::getArgCVParam(const std::string& name, const std::string& defaultResult) {
|
2015-07-11 18:56:02 +08:00
|
|
|
|
if (!argv ) return defaultResult;
|
|
|
|
|
if (argc<=1) return defaultResult;
|
2019-01-26 20:00:40 +08:00
|
|
|
|
for (int i=1; i<argc; i++) {
|
|
|
|
|
if (name==std::string(argv[i]) && i+1<argc) {
|
|
|
|
|
return std::string(argv[i+1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return defaultResult;
|
2015-07-11 18:56:02 +08:00
|
|
|
|
}
|
2018-12-28 05:52:00 +08:00
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpNode::~jkmpNode() = default;
|
2018-12-28 05:52:00 +08:00
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpConstantNode::jkmpConstantNode(JKQTPMathParser::jkmpResult d, JKQTPMathParser *p, JKQTPMathParser::jkmpNode *par) { data=d; setParser(p); setParent(par); }
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpResult JKQTPMathParser::jkmpConstantNode::evaluate() { return data; }
|
|
|
|
|
|
2019-01-26 20:00:40 +08:00
|
|
|
|
JKQTPMathParser::jkmpException::~jkmpException() = default;
|
2018-12-28 05:52:00 +08:00
|
|
|
|
|
2019-05-30 04:40:02 +08:00
|
|
|
|
JKQTPMathParser::jkmpException::jkmpException() {
|
|
|
|
|
errormessage="unknown error";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpException::jkmpException(const std::string &msg) {
|
|
|
|
|
errormessage=msg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string JKQTPMathParser::jkmpException::getMessage() const {
|
|
|
|
|
return errormessage;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-28 05:52:00 +08:00
|
|
|
|
const char *JKQTPMathParser::jkmpException::what() const noexcept {
|
|
|
|
|
return getMessage().c_str();
|
|
|
|
|
}
|
2019-05-30 04:40:02 +08:00
|
|
|
|
|
|
|
|
|
JKQTPMathParser *JKQTPMathParser::jkmpNode::getParser(){ return parser; }
|
|
|
|
|
|
|
|
|
|
void JKQTPMathParser::jkmpNode::setParser(JKQTPMathParser *mp){ parser=mp; }
|
|
|
|
|
|
|
|
|
|
JKQTPMathParser::jkmpNode *JKQTPMathParser::jkmpNode::getParent(){ return parent; }
|
|
|
|
|
|
|
|
|
|
void JKQTPMathParser::jkmpNode::setParent(JKQTPMathParser::jkmpNode *par) { parent=par; }
|