Commit aa7abb5c authored by Ruben Undheim's avatar Ruben Undheim

New upstream version 0.0.34+ds.1

parent ef962f60
# define build type
SET( CMAKE_BUILD_TYPE Release )
PROJECT(fparser CXX)
cmake_minimum_required(VERSION 2.8)
# default
set(LIB_VERSION_MAJOR 4)
set(LIB_VERSION_MINOR 5)
set(LIB_VERSION_PATCH 1)
set(LIB_VERSION_STRING ${LIB_VERSION_MAJOR}.${LIB_VERSION_MINOR}.${LIB_VERSION_PATCH})
set(VERSION "v${LIB_VERSION_STRING}")
#set(HEADERS fparser.hh fpconfig.hh fptypes.hh)
set(SOURCES fparser.cc fpoptimizer.cc)
remove(CMAKE_CXX_FLAGS -Dfparser_EXPORTS)
ADD_LIBRARY(fparser SHARED ${SOURCES})
set_target_properties(fparser PROPERTIES VERSION ${LIB_VERSION_STRING} SOVERSION ${LIB_VERSION_MAJOR})
INSTALL(TARGETS fparser DESTINATION lib${LIB_SUFFIX})
INSTALL(FILES fparser.hh DESTINATION include)
This diff is collapsed.
This diff is collapsed.
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
html
{
background-color: #E0E0E0;
}
body
{
background-color: white;
margin-left: 7%;
margin-top: 16px;
margin-right: 7%;
padding-top: 2em;
padding-left: 7%;
padding-right: 7%;
padding-bottom: 2%;
border-color: black;
border: solid;
border-width: 1px;
}
h1
{
text-align: center;
background-color: #FFEEBB;
padding-bottom: 0.2em;
padding-top: 0.1em;
}
h2
{
background-color: #FFFFCC;
padding-left: 0.5em;
}
h3
{
background-color: #FFFFEE;
}
blockquote
{
padding-left: 2em;
padding-right: 2em;
font-style: italic;
background-color: #FFFAF0;
}
li
{
padding-top: 0.3em;
}
pre
{
background-color: #E8E8E8;
padding-left: 1em;
padding-top: 0.5em;
padding-bottom: 0.5em;
}
code
{
font-family: monospace;
color: #900040;
}
.small
{
font-size: 80%;
}
.codecomment
{
color: green;
}
.highlight
{
background: #C0D0FF;
}
// Simple example file for the function parser
// ===========================================
/* When running the program, try for example with these values:
f(x) = x^2
min x: -5
max x: 5
step: 1
*/
#include "../fparser.hh"
#include <iostream>
#include <string>
int main()
{
std::string function;
double minx, maxx, step;
FunctionParser fparser;
fparser.AddConstant("pi", 3.1415926535897932);
while(true)
{
std::cout << "f(x) = ";
std::getline(std::cin, function);
if(std::cin.fail()) return 0;
int res = fparser.Parse(function, "x");
if(res < 0) break;
std::cout << std::string(res+7, ' ') << "^\n"
<< fparser.ErrorMsg() << "\n\n";
}
std::cout << "min x: ";
std::cin >> minx;
std::cout << "max x: ";
std::cin >> maxx;
std::cout << "step: ";
std::cin >> step;
if(std::cin.fail()) return 0;
double vals[] = { 0 };
for(vals[0] = minx; vals[0] <= maxx; vals[0] += step)
{
std::cout << "f(" << vals[0] << ") = " << fparser.Eval(vals)
<< std::endl;
}
return 0;
}
// Simple example file for the function parser
// ===========================================
/* Note that the library has to be compiled with
FP_SUPPORT_FLOAT_TYPE, FP_SUPPORT_LONG_DOUBLE_TYPE and
FP_SUPPORT_LONG_INT_TYPE
preprocessor macros defined for this example to work.
Try with these input values with the different floating point parser
types to see the difference in accuracy:
f(x) = x + 1.234567890123456789
min x: 0
max x: 2
step: 1
*/
#include "../fparser.hh"
#include <iostream>
#include <iomanip>
#include <string>
template<typename Parser>
void runExample(const char* valueTypeName)
{
typedef typename Parser::value_type Value_t;
std::cout << "Using " << valueTypeName << " parser." << std::endl;
Parser fparser;
std::string function;
Value_t minx, maxx, step;
fparser.AddConstant("pi", Value_t(3.1415926535897932));
std::cin.ignore();
while(true)
{
std::cout << "f(x) = ";
std::getline(std::cin, function);
if(std::cin.fail()) return;
int res = fparser.Parse(function, "x");
if(res < 0) break;
std::cout << std::string(res+7, ' ') << "^\n"
<< fparser.ErrorMsg() << "\n\n";
}
std::cout << "min x: ";
std::cin >> minx;
std::cout << "max x: ";
std::cin >> maxx;
std::cout << "step: ";
std::cin >> step;
if(std::cin.fail()) return;
Value_t vals[] = { 0 };
for(vals[0] = minx; vals[0] <= maxx; vals[0] += step)
{
std::cout << std::setprecision(20);
std::cout << "f(" << vals[0] << ") = " << fparser.Eval(vals)
<< std::endl;
}
}
int main()
{
int choice = 0;
do
{
std::cout << "1 = double, 2 = float, 3 = long double, 4 = long int: ";
std::cin >> choice;
} while(choice < 1 || choice > 4);
switch(choice)
{
case 1: runExample<FunctionParser>("double"); break;
case 2: runExample<FunctionParser_f>("float"); break;
case 3: runExample<FunctionParser_ld>("long double"); break;
case 4: runExample<FunctionParser_li>("long int"); break;
}
return 0;
}
/* NOTE:
Do not include this file in your project. The fparser.cc file #includes
this file internally and thus you don't need to do anything (other than keep
this file in the same directory as fparser.cc).
Part of this file is generated code (by using the make_function_name_parser
utility, found in the development version of this library). It's not intended
to be modified by hand.
*/
unsigned nameLength = 0;
const unsigned maximumNameLength = 0x80000000U-8;
/*
Due to the manner the identifier lengths are returned from
the readOpcode() function, the maximum supported length for
identifiers is 0x7FFFFFFF bytes. We minus 8 here to add some
buffer, because of the multibyteness of UTF-8.
Function names are limited to 0xFFFF bytes instead, but because
function names that long just are not defined, the point is moot.
*/
const unsigned char* const uptr = (const unsigned char*) input;
typedef signed char schar;
while(likely(nameLength < maximumNameLength))
{
unsigned char byte = uptr[nameLength+0];
/* Handle the common case of A-Za-z first */
if(byte >= 0x40)
{
if(byte < 0x80) // 0x40..0x7F - most common case
{
// Valid characters in 40..7F: A-Za-z_
// Valid bitmask for 40..5F: 01111111111111111111111111100001
// Valid bitmask for 60..7F: 01111111111111111111111111100000
if(sizeof(unsigned long) == 8)
{
const unsigned n = sizeof(unsigned long)*8-32;
// ^ avoids compiler warning when not 64-bit
unsigned long masklow6bits = 1UL << (byte & 0x3F);
if(masklow6bits & ~((1UL << 0) | (0x0FUL << (0x1B ))
| (1UL << n) | (0x1FUL << (0x1B+n))))
{ ++nameLength; continue; }
}
else
{
unsigned masklow5bits = 1 << (byte & 0x1F);
if((masklow5bits & ~(1 | (0x1F << 0x1B))) || byte == '_')
{ ++nameLength; continue; }
}
break;
}
if(byte < 0xF0)
{
if(byte < 0xE0)
{
if(byte < 0xC2) break; // 0x80..0xC1
if(byte == 0xC2 && uptr[nameLength+1]==0xA0) break; // skip nbsp
// C2-DF - next common case when >= 0x40
// Valid sequence: C2-DF 80-BF
if(schar(uptr[nameLength+1]) > schar(0xBF)) break;
nameLength += 2;
continue;
}
if(byte == 0xE0) // E0
{
// Valid sequence: E0 A0-BF 80-BF
if((unsigned char)(uptr[nameLength+1] - 0xA0) > (0xBF-0xA0)) break;
}
else
{
if(byte == 0xED) break; // ED is invalid
// Valid sequence: E1-EC 80-BF 80-BF
// And: EE-EF 80-BF 80-BF
if(byte == 0xE2)
{
// break on various space characters
if(uptr[nameLength+1] == 0x80
&& (schar(uptr[nameLength+2]) <= schar(0x8B)
|| (uptr[nameLength+2] == 0xAF))) break;
if(uptr[nameLength+1] == 0x81
&& uptr[nameLength+2] == 0x9F) break;
} else
if(byte == 0xE3 && uptr[nameLength+1] == 0x80
&& uptr[nameLength+2] == 0x80) break; // this too
if(schar(uptr[nameLength+1]) > schar(0xBF)) break;
}
if(schar(uptr[nameLength+2]) > schar(0xBF)) break;
nameLength += 3;
continue;
}
if(byte == 0xF0) // F0
{
// Valid sequence: F0 90-BF 80-BF 80-BF
if((unsigned char)(uptr[nameLength+1] - 0x90) > (0xBF-0x90)) break;
}
else
{
if(byte > 0xF4) break; // F5-FF are invalid
if(byte == 0xF4) // F4
{
// Valid sequence: F4 80-8F
if(schar(uptr[nameLength+1]) > schar(0x8F)) break;
}
else
{
// F1-F3
// Valid sequence: F1-F3 80-BF 80-BF 80-BF
if(schar(uptr[nameLength+1]) > schar(0xBF)) break;
}
}
if(schar(uptr[nameLength+2]) > schar(0xBF)) break;
if(schar(uptr[nameLength+3]) > schar(0xBF)) break;
nameLength += 4;
continue;
}
if(nameLength > 0)
{
if(sizeof(unsigned long) == 8)
{
// Valid bitmask for 00..1F: 00000000000000000000000000000000
// Valid bitmask for 20..3F: 00000000000000001111111111000000
const unsigned n = sizeof(unsigned long)*8-32;
// ^ avoids compiler warning when not 64-bit
unsigned long masklow6bits = 1UL << byte;
if(masklow6bits & (((1UL << 10)-1UL) << (16+n)))
{ ++nameLength; continue; }
}
else
{
if(byte >= '0' && byte <= '9')
{ ++nameLength; continue; }
}
}
break;
}
/* This function generated with make_function_name_parser.cc */
#define lO l3 lH
#define lN switch(
#define lM l4 lH
#define lL if('i' l5
#define lK 'n' l5
#define lJ 0x80000003U;
#define lI l1 3]={
#define lH case
#define lG 0x80000005U;
#define lF )==0)l0(
#define lE l8 3;}lH
#define lD std::memcmp(uptr+
#define lC l2 3 lF
#define lB lA 1]){lH
#define lA :lN uptr[
#define l9 'a' lB
#define l8 default:l0
#define l7 lG l0 5;}lH
#define l6 <<16)|
#define l5 ==uptr[
#define l4 lJ l0 3;
#define l3 0x80000004U;l0 4;
#define l2 lD 1,tmp,
#define l1 static const char tmp[
#define l0 return
lN
nameLength){lH
2:lL
0]&&'f' l5
1])l0(cIf
l6
0x80000002U;l0
2;lH
3
lA
0]){lH
l9'b':if('s' l5
2])l0(cAbs
l6
lM'r':if('g' l5
2])l0(cArg
l6
l4
lE'c' lB'o' lA
2]){lH's':l0(cCos
l6
lJ
lH't':l0(cCot
l6
lJ
lE's':if('c' l5
2])l0(cCsc
l6
l4
lE'e':if('x' l5
1]&&'p' l5
2])l0(cExp
l6
lM'i':if(lK
1]&&'t' l5
2])l0(cInt
l6
lM'l':if('o' l5
1]&&'g' l5
2])l0(cLog
l6
lM'm' lB'a':if('x' l5
2])l0(cMax
l6
lM'i':if(lK
2])l0(cMin
l6
l4
lE'p':if('o' l5
1]&&'w' l5
2])l0(cPow
l6
lM's' lB'e':if('c' l5
2])l0(cSec
l6
lM'i':if(lK
2])l0(cSin
l6
l4
lE't':if('a' l5
1]&&lK
2])l0(cTan
l6
l4
lE
4
lA
0]){lH
l9'c':if('o' l5
2]&&'s' l5
3])l0(cAcos
l6
lO's':lL
2]&&lK
3])l0(cAsin
l6
lO't':if('a' l5
2]&&lK
3])l0(cAtan
l6
l3
l8
4;}
lH'c' lB'b':if('r' l5
2]&&'t' l5
3])l0(cCbrt
l6
lO'e':lL
2]&&'l' l5
3])l0(cCeil
l6
lO'o' lA
2]){lH'n':if('j' l5
3])l0(cConj
l6
lO's':if('h' l5
3])l0(cCosh
l6
l3
l8
4;}
l8
4;}
lH'e':{lI'x','p','2'}
;if(lC
cExp2
l6
l3}
lH'i':{lI'm','a','g'}
;if(lC
cImag
l6
l3}
lH'l':{lI'o','g','2'}
;if(lC
cLog2
l6
l3}
lH'r':{lI'e','a','l'}
;if(lC
cReal
l6
l3}
lH's' lB'i':if(lK
2]&&'h' l5
3])l0(cSinh
l6
lO'q':if('r' l5
2]&&'t' l5
3])l0(cSqrt
l6
l3
l8
4;}
lH't':{lI'a','n','h'}
;if(lC
cTanh
l6
l3}
l8
4;}
lH
5
lA
0]){lH
l9'c':{lI'o','s','h'}
;if(lD
2,tmp,3
lF
cAcosh
l6
l7's':{lI'i','n','h'}
;if(lD
2,tmp,3