├── .clusterfuzzlite ├── project.yaml ├── README.md ├── Dockerfile ├── build.sh └── parser_fuzzer.cpp ├── doc ├── muParserX.jpg ├── muParserX.png ├── muParserX_2.png └── custom │ ├── example.txt │ ├── Main.txt │ ├── listvar.cpp │ ├── list_expr_var.cpp │ ├── footer.html │ ├── calc.cpp │ └── features.txt ├── .vscode ├── settings.json └── tasks.json ├── sample ├── timer.h └── timer.cpp ├── .gitignore ├── muparserx.pc ├── muparserx.in.pc ├── muparserxConfigVersion.cmake ├── cmake ├── muparserxConfigVersion.in.cmake └── muparserxConfig.cmake ├── .github └── workflows │ ├── cflite_pr.yml │ └── basic.yml ├── LICENSE ├── CMakeSettings.json ├── parser ├── mpOprtPostfixCommon.h ├── mpParserMessageProvider.h ├── mpMatrixError.h ├── mpFwdDecl.h ├── mpIPackage.cpp ├── utGeneric.h ├── mpIPackage.h ├── suSortPred.h ├── mpPackageCmplx.h ├── mpPackageStr.h ├── mpPackageMatrix.h ├── mpParser.h ├── mpPackageNonCmplx.h ├── mpIPrecedence.h ├── mpPackageCommon.h ├── mpScriptTokens.h ├── mpOprtIndex.h ├── mpRPN.h ├── mpValueCache.h ├── mpScriptTokens.cpp ├── mpIOprtBinShortcut.h ├── mpIfThenElse.h ├── mpIValReader.cpp ├── mpPackageStr.cpp ├── mpPackageMatrix.cpp ├── mpOprtPostfixCommon.cpp ├── mpOprtMatrix.h ├── mpPackageUnit.h ├── mpICallback.h ├── mpIOprtBinShortcut.cpp ├── mpOprtBinShortcut.h ├── mpParser.cpp ├── mpStringConversionHelper.h ├── mpFuncStr.h ├── mpTest.h ├── mpValueCache.cpp ├── mpIfThenElse.cpp ├── mpIValReader.h ├── mpDefines.h ├── mpPackageCmplx.cpp ├── mpFuncMatrix.h ├── mpOprtBinShortcut.cpp ├── mpVariable.h ├── mpPackageNonCmplx.cpp ├── mpIOprt.h ├── mpFuncNonCmplx.h ├── mpFuncCommon.h ├── mpOprtBinAssign.h ├── mpOprtIndex.cpp ├── mpOprtCmplx.h ├── mpICallback.cpp └── mpValReader.h ├── Readme.md └── CMakeLists.txt /.clusterfuzzlite/project.yaml: -------------------------------------------------------------------------------- 1 | language: c++ 2 | -------------------------------------------------------------------------------- /doc/muParserX.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beltoforion/muparserx/HEAD/doc/muParserX.jpg -------------------------------------------------------------------------------- /doc/muParserX.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beltoforion/muparserx/HEAD/doc/muParserX.png -------------------------------------------------------------------------------- /doc/muParserX_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beltoforion/muparserx/HEAD/doc/muParserX_2.png -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "type_traits": "cpp", 4 | "limits": "cpp" 5 | } 6 | } -------------------------------------------------------------------------------- /.clusterfuzzlite/README.md: -------------------------------------------------------------------------------- 1 | # ClusterFuzzLite set up 2 | 3 | This folder contains a fuzzing set for [ClusterFuzzLite](https://google.github.io/clusterfuzzlite). 4 | -------------------------------------------------------------------------------- /sample/timer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef timer_H 3 | #define timer_H 4 | 5 | void StartTimer(void); 6 | double StopTimer(void); 7 | double PrintTimer(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /.clusterfuzzlite/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM gcr.io/oss-fuzz-base/base-builder 2 | RUN apt-get update && apt-get install -y make autoconf automake libtool 3 | 4 | COPY . $SRC/muparserx 5 | COPY .clusterfuzzlite/build.sh $SRC/build.sh 6 | WORKDIR $SRC/muparserx 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.obj 2 | *.ilk 3 | *.pdb 4 | *.lastbuildstate 5 | *.opensdf 6 | *.suo 7 | *.sdf 8 | *.log 9 | *.tlog 10 | *.ilk 11 | *.db 12 | *.db-shm 13 | *.ipch 14 | .vs/* 15 | out/* 16 | CMakeFiles/* 17 | CMakeCache.txt 18 | *.a 19 | example 20 | -------------------------------------------------------------------------------- /.clusterfuzzlite/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -eu 2 | 3 | mkdir build 4 | cd build 5 | cmake ../ 6 | make 7 | 8 | # Copy all fuzzer executables to $OUT/ 9 | $CXX $CFLAGS $LIB_FUZZING_ENGINE \ 10 | $SRC/muparserx/.clusterfuzzlite/parser_fuzzer.cpp \ 11 | -stdlib=libc++ -std=c++17 \ 12 | -o $OUT/parser_fuzzer \ 13 | -I$SRC/muparserx/parser \ 14 | $SRC/muparserx/build/libmuparserx.a 15 | -------------------------------------------------------------------------------- /.clusterfuzzlite/parser_fuzzer.cpp: -------------------------------------------------------------------------------- 1 | #include "mpParser.h" 2 | #include 3 | 4 | using namespace mup; 5 | 6 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { 7 | std::string fuzz_input(reinterpret_cast(data), size); 8 | ParserX p; 9 | 10 | try { 11 | p.SetExpr(fuzz_input); 12 | p.Eval(); 13 | } catch (...) { 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /muparserx.pc: -------------------------------------------------------------------------------- 1 | prefix=/usr/local 2 | exec_prefix=${prefix} 3 | libdir=${exec_prefix}/lib 4 | includedir=${prefix}/include/muparserx 5 | 6 | Name: muparserx 7 | Description: A mathematical expression parser library. 8 | URL: https://beltoforion.de/en/muparserx 9 | Version: 4.0.12 10 | Requires: 11 | Requires.private: 12 | Conflicts: 13 | Cflags: -I${includedir} 14 | Libs: -L${libdir} -lmuparserx 15 | Libs.private: 16 | -------------------------------------------------------------------------------- /doc/custom/example.txt: -------------------------------------------------------------------------------- 1 | /** \example listvar.cpp 2 | This example shows how to list parser variables. 3 | */ 4 | 5 | /** \example list_expr_var.cpp 6 | This example shows how to list variables used in an expression. 7 | */ 8 | 9 | /** \example calc.cpp 10 | This example shows a typical use case of muParser. It sets up the parser 11 | engine and is defining several variables. 12 | */ 13 | } 14 | */ 15 | 16 | -------------------------------------------------------------------------------- /muparserx.in.pc: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | exec_prefix=${prefix} 3 | libdir=${exec_prefix}/lib@LIB_SUFFIX@ 4 | includedir=${prefix}/include/muparserx 5 | 6 | Name: muparserx 7 | Description: A mathematical expression parser library. 8 | URL: https://beltoforion.de/en/muparserx 9 | Version: @MUPARSERX_VERSION@ 10 | Requires: 11 | Requires.private: 12 | Conflicts: 13 | Cflags: -I${includedir} 14 | Libs: -L${libdir} -lmuparserx 15 | Libs.private: 16 | -------------------------------------------------------------------------------- /muparserxConfigVersion.cmake: -------------------------------------------------------------------------------- 1 | set(PACKAGE_FIND_NAME "muparserx") 2 | set(PACKAGE_VERSION "4.0.12") 3 | 4 | # Check whether the requested PACKAGE_FIND_VERSION is compatible 5 | if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}") 6 | set(PACKAGE_VERSION_COMPATIBLE FALSE) 7 | else() 8 | set(PACKAGE_VERSION_COMPATIBLE TRUE) 9 | if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}") 10 | set(PACKAGE_VERSION_EXACT TRUE) 11 | endif() 12 | endif() 13 | -------------------------------------------------------------------------------- /cmake/muparserxConfigVersion.in.cmake: -------------------------------------------------------------------------------- 1 | set(PACKAGE_FIND_NAME "muparserx") 2 | set(PACKAGE_VERSION "@MUPARSERX_VERSION@") 3 | 4 | # Check whether the requested PACKAGE_FIND_VERSION is compatible 5 | if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}") 6 | set(PACKAGE_VERSION_COMPATIBLE FALSE) 7 | else() 8 | set(PACKAGE_VERSION_COMPATIBLE TRUE) 9 | if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}") 10 | set(PACKAGE_VERSION_EXACT TRUE) 11 | endif() 12 | endif() 13 | -------------------------------------------------------------------------------- /doc/custom/Main.txt: -------------------------------------------------------------------------------- 1 | /** \mainpage 2 | 3 |
4 | 5 |
6 | 7 | \section intro_sec Introduction 8 | 9 | muParserX is a mathematical expression parser with support for complex numbers and matrix calculations. This is the API documentation of muParserX. 10 | For additional documentation consult the project home page at google code. 11 | 12 | \section toc Table of content 13 | \ref page_features
14 | */ 15 | 16 | 17 | -------------------------------------------------------------------------------- /doc/custom/listvar.cpp: -------------------------------------------------------------------------------- 1 | void ListVar(const mu::ParserBase &parser) 2 | { 3 | mu::varmap_type variables = parser.GetVar(); 4 | if (!variables.size()) 5 | return; 6 | 7 | cout << "\nParser variables:\n"; 8 | cout << "-----------------\n"; 9 | cout << "Number: " << (int)variables.size() << "\n"; 10 | varmap_type::const_iterator item = variables.begin(); 11 | for (; item!=variables.end(); ++item) 12 | mu::console() << _T("Name: ") << item->first << _T(" Address: [0x") << item->second << _T("]\n"); 13 | } 14 | -------------------------------------------------------------------------------- /doc/custom/list_expr_var.cpp: -------------------------------------------------------------------------------- 1 | void ListExprVar(const mu::ParserBase &parser) 2 | { 3 | varmap_type variables = parser.GetUsedVar(); 4 | if (!variables.size()) 5 | mu::console() << "Expression does not contain variables\n"; 6 | else 7 | { 8 | mu::console() << "Number: " << (int)variables.size() << "\n"; 9 | mu::varmap_type::const_iterator item = variables.begin(); 10 | 11 | for (; item!=variables.end(); ++item) 12 | mu::console() << "Name: " << item->first << " Address: [0x" << item->second << "]\n"; 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /doc/custom/footer.html: -------------------------------------------------------------------------------- 1 |
2 |

3 |


4 | muParserX documentation - (C) 2023 Ingo Berg 5 |

6 |
7 | 8 | 12 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: g++ Aktive Datei kompilieren", 6 | "command": "/usr/bin/g++", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${file}", 11 | "-o", 12 | "${fileDirname}/${fileBasenameNoExtension}" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Vom Debugger generierte Aufgabe." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /.github/workflows/cflite_pr.yml: -------------------------------------------------------------------------------- 1 | name: ClusterFuzzLite PR fuzzing 2 | on: 3 | workflow_dispatch: 4 | pull_request: 5 | branches: [ main ] 6 | permissions: read-all 7 | jobs: 8 | PR: 9 | runs-on: ubuntu-latest 10 | strategy: 11 | fail-fast: false 12 | matrix: 13 | sanitizer: [address] 14 | steps: 15 | - name: Build Fuzzers (${{ matrix.sanitizer }}) 16 | id: build 17 | uses: google/clusterfuzzlite/actions/build_fuzzers@v1 18 | with: 19 | sanitizer: ${{ matrix.sanitizer }} 20 | language: c++ 21 | bad-build-check: false 22 | - name: Run Fuzzers (${{ matrix.sanitizer }}) 23 | id: run 24 | uses: google/clusterfuzzlite/actions/run_fuzzers@v1 25 | with: 26 | github-token: ${{ secrets.GITHUB_TOKEN }} 27 | fuzz-seconds: 180 28 | mode: 'code-change' 29 | report-unreproducible-crashes: false 30 | sanitizer: ${{ matrix.sanitizer }} 31 | -------------------------------------------------------------------------------- /sample/timer.cpp: -------------------------------------------------------------------------------- 1 | #include "timer.h" 2 | 3 | #include 4 | #include 5 | 6 | #if defined(__WIN32__) || defined(_WIN32) 7 | #include 8 | DWORD tvs, tve; 9 | #else 10 | #include 11 | struct timeval tvs, tve; 12 | #endif 13 | 14 | 15 | void StartTimer(void) 16 | { 17 | #if defined(__WIN32__) || defined(_WIN32) 18 | tvs = GetTickCount(); 19 | #else 20 | if (gettimeofday(&tvs,0)) fprintf(stderr,"cant get time!\n"); 21 | #endif 22 | } 23 | 24 | double StopTimer(void) 25 | { 26 | #if defined(__WIN32__) || defined(_WIN32) 27 | tve = GetTickCount(); 28 | return tve - tvs; 29 | #else 30 | if (gettimeofday(&tve,0)) fprintf(stderr,"cant get time!\n"); 31 | return 1000 * (tve.tv_sec - tvs.tv_sec + (double)(tve.tv_usec - tvs.tv_usec) / 1000000.0); 32 | #endif 33 | } 34 | 35 | double PrintTimer(void) 36 | { 37 | double t; 38 | 39 | #if defined(__WIN32__) || defined(_WIN32) 40 | tve = GetTickCount(); 41 | t = (double)(tve - tvs) / 1000.0; 42 | printf("%.3f ",t); 43 | return t; 44 | #else 45 | if (gettimeofday(&tve,0)) fprintf(stderr,"cant get time!\n"); 46 | t = 1000 * (tve.tv_sec - tvs.tv_sec + (double)(tve.tv_usec - tvs.tv_usec)/1000000.0); 47 | printf("%.3f ",t); 48 | return t; 49 | #endif 50 | } 51 | 52 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2023, Ingo Berg 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /CMakeSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "x64-Debug", 5 | "generator": "Ninja", 6 | "configurationType": "Debug", 7 | "inheritEnvironments": [ "msvc_x64_x64" ], 8 | "buildRoot": "${projectDir}\\out\\build\\${name}", 9 | "installRoot": "${projectDir}\\out\\install\\${name}", 10 | "cmakeCommandArgs": "", 11 | "buildCommandArgs": "", 12 | "ctestCommandArgs": "", 13 | "variables": [ 14 | { 15 | "name": "USE_WIDE_STRING", 16 | "value": "True", 17 | "type": "BOOL" 18 | } 19 | ] 20 | }, 21 | { 22 | "name": "x64-Clang-Debug", 23 | "generator": "Ninja", 24 | "configurationType": "Debug", 25 | "buildRoot": "${projectDir}\\out\\build\\${name}", 26 | "installRoot": "${projectDir}\\out\\install\\${name}", 27 | "cmakeCommandArgs": "", 28 | "buildCommandArgs": "", 29 | "ctestCommandArgs": "", 30 | "inheritEnvironments": [ "clang_cl_x64_x64" ], 31 | "variables": [ 32 | { 33 | "name": "CMAKE_CXX_COMPILER", 34 | "value": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\Llvm\\x64\\bin\\clang.exe", 35 | "type": "FILEPATH" 36 | }, 37 | { 38 | "name": "USE_WIDE_STRING", 39 | "value": "True", 40 | "type": "BOOL" 41 | } 42 | ] 43 | } 44 | ] 45 | } -------------------------------------------------------------------------------- /parser/mpOprtPostfixCommon.h: -------------------------------------------------------------------------------- 1 | #ifndef MP_OPRT_POSTFIX_COMMON_H 2 | #define MP_OPRT_POSTFIX_COMMON_H 3 | 4 | /** \file 5 | \brief Definitions of classes used as callbacks for standard postfix operators. 6 | */ 7 | 8 | /** \defgroup postop Postfix operator callbacks 9 | 10 | This group lists the objects representing the postfix operators of muParserX. 11 | */ 12 | 13 | #include "mpIOprt.h" 14 | #include "mpValue.h" 15 | #include "mpError.h" 16 | 17 | 18 | MUP_NAMESPACE_START 19 | 20 | //------------------------------------------------------------------------------ 21 | /** \brief Calculate factorial of a non-negative integer. 22 | \ingroup postop 23 | */ 24 | class OprtFact : public IOprtPostfix 25 | { 26 | public: 27 | 28 | OprtFact(); 29 | 30 | virtual void Eval(ptr_val_type& ret, const ptr_val_type *arg, int) override; 31 | virtual const char_type* GetDesc() const override; 32 | virtual IToken* Clone() const override; 33 | }; 34 | 35 | //------------------------------------------------------------------------------ 36 | /** \brief Returns percentage of given number. 37 | \ingroup postop 38 | */ 39 | class OprtPercentage : public IOprtPostfix 40 | { 41 | public: 42 | 43 | OprtPercentage(); 44 | 45 | virtual void Eval(ptr_val_type& ret, const ptr_val_type *arg, int) override; 46 | virtual const char_type* GetDesc() const override; 47 | virtual IToken* Clone() const override; 48 | }; 49 | } // namespace mu 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /parser/mpParserMessageProvider.h: -------------------------------------------------------------------------------- 1 | #ifndef MP_PARSER_MESSAGE_PROVIDER_H 2 | #define MP_PARSER_MESSAGE_PROVIDER_H 3 | 4 | #include 5 | #include 6 | 7 | #include "mpDefines.h" 8 | #include "mpTypes.h" 9 | 10 | 11 | MUP_NAMESPACE_START 12 | 13 | //----------------------------------------------------------------------------------------------- 14 | /** \brief Base class for Parser Message providing classes. */ 15 | class ParserMessageProviderBase 16 | { 17 | friend class std::unique_ptr; 18 | 19 | public: 20 | ParserMessageProviderBase(); 21 | virtual ~ParserMessageProviderBase(); 22 | 23 | void Init(); 24 | string_type GetErrorMsg(EErrorCodes errc) const; 25 | 26 | private: 27 | // Disable CC and assignment operator for this class and derivatives 28 | ParserMessageProviderBase(const ParserMessageProviderBase &ref); 29 | ParserMessageProviderBase& operator=(const ParserMessageProviderBase &ref); 30 | 31 | protected: 32 | std::vector m_vErrMsg; 33 | 34 | virtual void InitErrorMessages() = 0; 35 | }; 36 | 37 | //----------------------------------------------------------------------------------------------- 38 | /** \brief English versions of parser messages. */ 39 | class ParserMessageProviderEnglish : public ParserMessageProviderBase 40 | { 41 | public: 42 | ParserMessageProviderEnglish(); 43 | 44 | protected: 45 | virtual void InitErrorMessages(); 46 | }; 47 | 48 | //----------------------------------------------------------------------------------------------- 49 | /** \brief German versions of parser messages. */ 50 | class ParserMessageProviderGerman : public ParserMessageProviderBase 51 | { 52 | public: 53 | ParserMessageProviderGerman(); 54 | 55 | protected: 56 | virtual void InitErrorMessages(); 57 | }; 58 | 59 | MUP_NAMESPACE_END 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /doc/custom/calc.cpp: -------------------------------------------------------------------------------- 1 | #include "mpParser.h" 2 | 3 | 4 | void Calc() 5 | { 6 | char line[100]; 7 | try 8 | { 9 | // Create value objects that will be bound to parser variables 10 | Value fVal = 1.11; 11 | Value sVal = "hello world"; 12 | Value arr1(3, 0); 13 | arr1[0] = 1.11, 14 | arr1[1] = 2.22; 15 | arr1[2] = 3.33; 16 | 17 | Parser parser; 18 | 19 | // Define parser variables and bind them to their value objects 20 | parser.DefineVar("va", Variable(&arr1)); 21 | parser.DefineVar("a", Variable(&fVal)); 22 | parser.DefineVar("sa", Variable(&sVal)); 23 | 24 | parser.SetExpr("sin(a)+b"); 25 | 26 | // The returned result is of type Value, value is a Variant like 27 | // type that can be either a boolean an integer or a floating point value 28 | Value result = parser.Eval(); 29 | 30 | // Value supports C++ streaming like this: 31 | cout << "Result:\n" << result << "\n"; 32 | 33 | // Or if you need the specific type use this: 34 | switch (result.GetType()) 35 | { 36 | case 's': cout << result.GetString() << " (string)" << "\n"; break; 37 | case 'i': cout << result.GetInt() << " (int)" << "\n"; break; 38 | case 'f': cout << result.GetFloat() << " (float)" << "\n"; break; 39 | case 'c': cout << result.GetFloat() << "+" << result.GetImag() << "i (complex)" << "\n"; break; 40 | case 'b': break; 41 | } 42 | } 43 | catch(ParserError &e) 44 | { 45 | cout << e.GetMsg() << "\n\n"; 46 | 47 | if (e.GetContext().Ident.length()) 48 | cout << "Ident.: " << e.GetContext().Ident << "\n"; 49 | 50 | if (e.GetExpr().length()) 51 | cout << "Expr.: " << e.GetExpr() << "\n"; 52 | 53 | if (e.GetToken().length()) 54 | cout << "Token: " << e.GetToken() << "\n"; 55 | 56 | cout << "Pos: " << e.GetPos() << "\n"; 57 | cout << "Errc: " << e.GetCode() << "\n"; 58 | } 59 | } // Calc() -------------------------------------------------------------------------------- /cmake/muparserxConfig.cmake: -------------------------------------------------------------------------------- 1 | if(DEFINED INCLUDED_MUPARSERX_CONFIG_CMAKE) 2 | return() 3 | endif() 4 | set(INCLUDED_MUPARSERX_CONFIG_CMAKE TRUE) 5 | 6 | ######################################################################## 7 | # muparserxConfig - cmake project configuration 8 | # 9 | # The following will be set after find_package(muparserx CONFIG): 10 | # muparserx_LIBRARIES - development libraries 11 | # muparserx_INCLUDE_DIRS - development includes 12 | ######################################################################## 13 | 14 | ######################################################################## 15 | ## installation root 16 | ######################################################################## 17 | if (UNIX) 18 | get_filename_component(MUPARSERX_ROOT "${CMAKE_CURRENT_LIST_DIR}/../../.." ABSOLUTE) 19 | elseif (WIN32) 20 | get_filename_component(MUPARSERX_ROOT "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE) 21 | endif () 22 | 23 | ######################################################################## 24 | ## locate the library 25 | ######################################################################## 26 | find_library( 27 | MUPARSERX_LIBRARY muparserx 28 | PATHS ${MUPARSERX_ROOT}/lib${LIB_SUFFIX} 29 | PATH_SUFFIXES ${CMAKE_LIBRARY_ARCHITECTURE} 30 | NO_DEFAULT_PATH 31 | ) 32 | if(NOT MUPARSERX_LIBRARY) 33 | message(FATAL_ERROR "cannot find muparserx library in ${MUPARSERX_ROOT}/lib${LIB_SUFFIX}") 34 | endif() 35 | set(muparserx_LIBRARIES ${MUPARSERX_LIBRARY}) 36 | 37 | ######################################################################## 38 | ## locate the includes 39 | ######################################################################## 40 | find_path( 41 | MUPARSERX_INCLUDE_DIR mpDefines.h 42 | PATHS ${MUPARSERX_ROOT}/include/muparserx 43 | NO_DEFAULT_PATH 44 | ) 45 | if(NOT MUPARSERX_INCLUDE_DIR) 46 | message(FATAL_ERROR "cannot find muparserx includes in ${MUPARSERX_ROOT}/include/muparserx") 47 | endif() 48 | set(muparserx_INCLUDE_DIRS ${MUPARSERX_INCLUDE_DIR}) 49 | 50 | -------------------------------------------------------------------------------- /.github/workflows/basic.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - '**/*.md' 7 | pull_request: 8 | paths-ignore: 9 | - '**/*.md' 10 | 11 | env: 12 | CMAKE_BUILD_TYPE: Debug 13 | CMAKE_BUILD_DIR: ${{ github.workspace }}/build 14 | 15 | jobs: 16 | build: 17 | strategy: 18 | fail-fast: false 19 | matrix: 20 | os: [windows-2022, ubuntu-22.04, macOS-12] 21 | 22 | runs-on: ${{ matrix.os }} 23 | 24 | steps: 25 | - uses: actions/checkout@v3 26 | 27 | - name: Build (ascii) 28 | shell: bash 29 | run: | 30 | mkdir -p $CMAKE_BUILD_DIR 31 | cd $CMAKE_BUILD_DIR 32 | cmake -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE .. 33 | cmake --build . 34 | 35 | - name: Test Examples 36 | shell: bash 37 | working-directory: ${{ env.CMAKE_BUILD_DIR }} 38 | run: | 39 | if [[ ${{ matrix.os }} == windows* ]]; then 40 | EXAMPLE="$CMAKE_BUILD_TYPE/example.exe" 41 | else 42 | EXAMPLE="./example" 43 | fi 44 | $EXAMPLE &> example.log << EOF 45 | quit 46 | EOF 47 | cat example.log 48 | grep -e "Test passed.*expressions" example.log || (grep -e "Test failed.*expressions" example.log; exit 1) 49 | 50 | - name: Build (wide string) 51 | shell: bash 52 | run: | 53 | mkdir -p $CMAKE_BUILD_DIR 54 | cd $CMAKE_BUILD_DIR 55 | cmake -DUSE_WIDE_STRING=ON -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE .. 56 | cmake --build . 57 | 58 | - name: Test Examples (wide string) 59 | shell: bash 60 | working-directory: ${{ env.CMAKE_BUILD_DIR }} 61 | run: | 62 | if [[ ${{ matrix.os }} == windows* ]]; then 63 | EXAMPLE="$CMAKE_BUILD_TYPE/example.exe" 64 | else 65 | EXAMPLE="./example" 66 | fi 67 | $EXAMPLE &> example.log << EOF 68 | quit 69 | EOF 70 | cat example.log 71 | grep -e "Test passed.*expressions" example.log || (grep -e "Test failed.*expressions" example.log; exit 1) 72 | -------------------------------------------------------------------------------- /parser/mpMatrixError.h: -------------------------------------------------------------------------------- 1 | #ifndef MU_MATRIX_ERROR_H 2 | #define MU_MATRIX_ERROR_H 3 | 4 | /* 5 | __________ ____ ___ 6 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 7 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 8 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 9 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 10 | \/ \/ \/ \/ \_/ 11 | Copyright (C) 2023, Ingo Berg 12 | All rights reserved. 13 | 14 | Redistribution and use in source and binary forms, with or without 15 | modification, are permitted provided that the following conditions are met: 16 | 17 | * Redistributions of source code must retain the above copyright notice, 18 | this list of conditions and the following disclaimer. 19 | * Redistributions in binary form must reproduce the above copyright notice, 20 | this list of conditions and the following disclaimer in the documentation 21 | and/or other materials provided with the distribution. 22 | 23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | POSSIBILITY OF SUCH DAMAGE. 33 | */ 34 | 35 | #include 36 | #include 37 | 38 | MUP_NAMESPACE_START 39 | 40 | class MatrixError : public std::runtime_error 41 | { 42 | public: 43 | explicit MatrixError(const std::string &sMsg) 44 | :std::runtime_error(sMsg) 45 | {} 46 | }; 47 | 48 | MUP_NAMESPACE_END 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /parser/mpFwdDecl.h: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #ifndef MUP_FWD_DECL_H 32 | #define MUP_FWD_DECL_H 33 | 34 | #include "mpDefines.h" 35 | 36 | 37 | MUP_NAMESPACE_START 38 | 39 | class ParserXBase; 40 | class ParserMessageProviderBase; 41 | 42 | class ICallback; 43 | class IToken; 44 | class IValue; 45 | class IValueReader; 46 | class IPrecedence; 47 | class IOprtIndex; 48 | class Value; 49 | class ValueCache; 50 | template 51 | class TokenPtr; 52 | 53 | MUP_NAMESPACE_END 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /parser/mpIPackage.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023 Ingo Berg 9 | All rights reserved. 10 | 11 | muParserX - A C++ math parser library with array and string support 12 | Copyright (C) 2023, Ingo Berg 13 | All rights reserved. 14 | 15 | Redistribution and use in source and binary forms, with or without 16 | modification, are permitted provided that the following conditions are met: 17 | 18 | * Redistributions of source code must retain the above copyright notice, 19 | this list of conditions and the following disclaimer. 20 | * Redistributions in binary form must reproduce the above copyright notice, 21 | this list of conditions and the following disclaimer in the documentation 22 | and/or other materials provided with the distribution. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 25 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 28 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | #include "mpIPackage.h" 36 | 37 | #include "mpDefines.h" 38 | #include "mpParserBase.h" 39 | 40 | 41 | MUP_NAMESPACE_START 42 | 43 | //------------------------------------------------------------------------------ 44 | IPackage::IPackage() 45 | {} 46 | 47 | //------------------------------------------------------------------------------ 48 | IPackage::~IPackage() 49 | {} 50 | 51 | MUP_NAMESPACE_END 52 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | ![_title](https://user-images.githubusercontent.com/2202567/196066131-a421b3c0-20e0-46e7-88ee-15ae29b215cd.jpg) 2 | 3 | [![GitHub issues](https://img.shields.io/github/issues/beltoforion/muparserx.svg?maxAge=360)](https://github.com/beltoforion/muparserx/issues) 4 | [![Version](https://img.shields.io/github/release/beltoforion/muparserx.svg?maxAge=360)](https://github.com/beltoforion/muparserx/blob/master/CHANGELOG) 5 | [![Packaging status](https://repology.org/badge/tiny-repos/muparserx.svg)](https://repology.org/project/muparserx/versions) 6 | 7 | 8 | muparserx 9 | =========================== 10 | 11 | 12 | V4.0.13 (20230307) 13 | ------------------ 14 | Bugfixes: 15 | - untracked Issue: Wide string build on windows was broken due to a typo introduced in 4.0.12. 16 | 17 | V4.0.12 (20230304) 18 | ------------------ 19 | Bugfixes: 20 | - Issue 116: Changed implementation of parsing double values to fix #116 on Mac. 21 | - Issue 115: String constants starting woth "or" confused with operator during parsing step 22 | - Issue 117: Equals operator behavior inconsistent when checking boolean values. (no type check) 23 | 24 | Changes: 25 | - C++17 is now the minimum required C++ version to compile the code 26 | - added a wide string option to cmake (USE_WIDE_STRING) 27 | - removed compatibility fixes for older compilers (mostly MSVC) 28 | - fixed compiler warnings 29 | 30 | V4.0.11 (20211123) 31 | ------------------ 32 | Bugfixes: 33 | - Issue 112: Compilation issue due to an invalid header file name 34 | 35 | V4.0.10 (20211122) 36 | ------------------ 37 | Bugfixes: 38 | - Issue 107: Complex multiplication-assignment did not work correctly 39 | - Issue 110: Short evaluation for binary operators added (thanks to user egyptyu) 40 | 41 | V4.0.9 (20200619) 42 | ----------------- 43 | Changes: 44 | - Copied unit tests from muparser (no new failures) 45 | - Introduced a maximum expression length of 10000 46 | - Expressions will be checked for non printable characters 47 | 48 | Bugfixes: 49 | - Issue 68: Integer test causes floating point exceptions; fixed as suggested 50 | 51 | V4.0.8 (20181218) 52 | ----------------- 53 | Changes: 54 | - Build system changed to CMake 55 | 56 | V4.0.7 (20160331) 57 | ----------------- 58 | Bugfixes: 59 | - Issue 68: Assertion fails (i.e "abs(-3)>abs(2)") 60 | - untracked issue: cbrt function did not work properly 61 | - new functions: atan2, reminder, fmod 62 | 63 | 64 | -------------------------------------------------------------------------------- /parser/utGeneric.h: -------------------------------------------------------------------------------- 1 | #ifndef _UT_GENERIC_H 2 | #define _UT_GENERIC_H 3 | 4 | /* 5 | __________ ____ ___ 6 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 7 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 8 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 9 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 10 | \/ \/ \/ \/ \_/ 11 | Copyright (C) 2023, Ingo Berg 12 | All rights reserved. 13 | 14 | Redistribution and use in source and binary forms, with or without 15 | modification, are permitted provided that the following conditions are met: 16 | 17 | * Redistributions of source code must retain the above copyright notice, 18 | this list of conditions and the following disclaimer. 19 | * Redistributions in binary form must reproduce the above copyright notice, 20 | this list of conditions and the following disclaimer in the documentation 21 | and/or other materials provided with the distribution. 22 | 23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | POSSIBILITY OF SUCH DAMAGE. 33 | */ 34 | 35 | namespace utils 36 | { 37 | template 38 | class scoped_setter 39 | { 40 | public: 41 | 42 | scoped_setter(T &ref, T new_val) 43 | :m_ref(ref) 44 | ,m_buf(ref) 45 | { 46 | ref = new_val; 47 | } 48 | 49 | ~scoped_setter() 50 | { 51 | m_ref = m_buf; 52 | } 53 | 54 | private: 55 | T &m_ref; 56 | T m_buf; 57 | 58 | // Disable CC and assignment operator 59 | scoped_setter(const scoped_setter &ref); 60 | scoped_setter& operator=(const scoped_setter &ref); 61 | }; 62 | } 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /parser/mpIPackage.h: -------------------------------------------------------------------------------- 1 | #ifndef MU_IPACKAGE_H 2 | #define MU_IPACKAGE_H 3 | 4 | /* 5 | __________ ____ ___ 6 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 7 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 8 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 9 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 10 | \/ \/ \/ \/ \_/ 11 | Copyright (C) 2023 Ingo Berg 12 | All rights reserved. 13 | 14 | muParserX - A C++ math parser library with array and string support 15 | Copyright (C) 2023, Ingo Berg 16 | All rights reserved. 17 | 18 | Redistribution and use in source and binary forms, with or without 19 | modification, are permitted provided that the following conditions are met: 20 | 21 | * Redistributions of source code must retain the above copyright notice, 22 | this list of conditions and the following disclaimer. 23 | * Redistributions in binary form must reproduce the above copyright notice, 24 | this list of conditions and the following disclaimer in the documentation 25 | and/or other materials provided with the distribution. 26 | 27 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 28 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 29 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 30 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 31 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 32 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 33 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 34 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 | POSSIBILITY OF SUCH DAMAGE. 37 | */ 38 | 39 | #include 40 | #include "mpFwdDecl.h" 41 | #include "mpTypes.h" 42 | 43 | 44 | MUP_NAMESPACE_START 45 | 46 | class IPackage 47 | { 48 | public: 49 | 50 | virtual void AddToParser(ParserXBase *pParser) = 0; 51 | virtual string_type GetDesc() const = 0; 52 | virtual string_type GetPrefix() const = 0; 53 | 54 | protected: 55 | 56 | IPackage(); 57 | virtual ~IPackage(); 58 | }; 59 | 60 | MUP_NAMESPACE_END 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /parser/suSortPred.h: -------------------------------------------------------------------------------- 1 | #ifndef SU_PRED_H 2 | #define SU_PRED_H 3 | 4 | /* 5 | __________ ____ ___ 6 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 7 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 8 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 9 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 10 | \/ \/ \/ \/ \_/ 11 | Copyright (C) 2023, Ingo Berg 12 | All rights reserved. 13 | 14 | Redistribution and use in source and binary forms, with or without 15 | modification, are permitted provided that the following conditions are met: 16 | 17 | * Redistributions of source code must retain the above copyright notice, 18 | this list of conditions and the following disclaimer. 19 | * Redistributions in binary form must reproduce the above copyright notice, 20 | this list of conditions and the following disclaimer in the documentation 21 | and/or other materials provided with the distribution. 22 | 23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | POSSIBILITY OF SUCH DAMAGE. 33 | */ 34 | 35 | 36 | 37 | /** \brief Namespace containing utility functions and classes for string processing. */ 38 | namespace su 39 | { 40 | namespace pred 41 | { 42 | /** \brief Sort two strings based on their length. 43 | */ 44 | template 45 | struct SortByLength 46 | { 47 | bool operator()(const TString& a_sLeft, const TString& a_sRight) const 48 | { 49 | if (a_sLeft.length() == a_sRight.length()) 50 | { 51 | return a_sLeft < a_sRight; 52 | } 53 | else 54 | { 55 | return a_sLeft.length() < a_sRight.length(); 56 | } 57 | } 58 | }; 59 | } // namespace pred 60 | } // end of namespace 61 | 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /parser/mpPackageCmplx.h: -------------------------------------------------------------------------------- 1 | #ifndef MU_PACKAGE_CMPLX_H 2 | #define MU_PACKAGE_CMPLX_H 3 | 4 | /* 5 | __________ ____ ___ 6 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 7 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 8 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 9 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 10 | \/ \/ \/ \/ \_/ 11 | Copyright (C) 2023, Ingo Berg 12 | All rights reserved. 13 | 14 | Redistribution and use in source and binary forms, with or without 15 | modification, are permitted provided that the following conditions are met: 16 | 17 | * Redistributions of source code must retain the above copyright notice, 18 | this list of conditions and the following disclaimer. 19 | * Redistributions in binary form must reproduce the above copyright notice, 20 | this list of conditions and the following disclaimer in the documentation 21 | and/or other materials provided with the distribution. 22 | 23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | POSSIBILITY OF SUCH DAMAGE. 33 | */ 34 | 35 | #include 36 | #include "mpIPackage.h" 37 | #include "mpIOprt.h" 38 | 39 | 40 | MUP_NAMESPACE_START 41 | 42 | //------------------------------------------------------------------------------ 43 | /** \brief Package for installing complex functions and operators. */ 44 | class PackageCmplx: public IPackage 45 | { 46 | friend class std::unique_ptr; 47 | 48 | public: 49 | 50 | static IPackage* Instance(); 51 | virtual void AddToParser(ParserXBase *pParser); 52 | virtual string_type GetDesc() const; 53 | virtual string_type GetPrefix() const; 54 | 55 | private: 56 | 57 | static std::unique_ptr s_pInstance; 58 | }; 59 | 60 | MUP_NAMESPACE_END 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /parser/mpPackageStr.h: -------------------------------------------------------------------------------- 1 | #ifndef MU_PACKAGE_STR_H 2 | #define MU_PACKAGE_STR_H 3 | 4 | /* 5 | __________ ____ ___ 6 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 7 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 8 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 9 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 10 | \/ \/ \/ \/ \_/ 11 | Copyright (C) 2023, Ingo Berg 12 | All rights reserved. 13 | 14 | Redistribution and use in source and binary forms, with or without 15 | modification, are permitted provided that the following conditions are met: 16 | 17 | * Redistributions of source code must retain the above copyright notice, 18 | this list of conditions and the following disclaimer. 19 | * Redistributions in binary form must reproduce the above copyright notice, 20 | this list of conditions and the following disclaimer in the documentation 21 | and/or other materials provided with the distribution. 22 | 23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | POSSIBILITY OF SUCH DAMAGE. 33 | */ 34 | 35 | #include 36 | 37 | #include "mpIPackage.h" 38 | #include "mpIOprt.h" 39 | 40 | 41 | MUP_NAMESPACE_START 42 | 43 | //------------------------------------------------------------------------------ 44 | /** \brief Package for installing unit postfix operators into muParserX. */ 45 | class PackageStr : public IPackage 46 | { 47 | friend class std::unique_ptr; 48 | 49 | public: 50 | 51 | static IPackage* Instance(); 52 | virtual void AddToParser(ParserXBase *pParser); 53 | virtual string_type GetDesc() const; 54 | virtual string_type GetPrefix() const; 55 | 56 | private: 57 | 58 | static std::unique_ptr s_pInstance; 59 | }; 60 | 61 | MUP_NAMESPACE_END 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /parser/mpPackageMatrix.h: -------------------------------------------------------------------------------- 1 | #ifndef MU_PACKAGE_MATRIX_H 2 | #define MU_PACKAGE_MATRIX_H 3 | 4 | /* 5 | __________ ____ ___ 6 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 7 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 8 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 9 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 10 | \/ \/ \/ \/ \_/ 11 | Copyright (C) 2023, Ingo Berg 12 | All rights reserved. 13 | 14 | Redistribution and use in source and binary forms, with or without 15 | modification, are permitted provided that the following conditions are met: 16 | 17 | * Redistributions of source code must retain the above copyright notice, 18 | this list of conditions and the following disclaimer. 19 | * Redistributions in binary form must reproduce the above copyright notice, 20 | this list of conditions and the following disclaimer in the documentation 21 | and/or other materials provided with the distribution. 22 | 23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | POSSIBILITY OF SUCH DAMAGE. 33 | */ 34 | 35 | #include 36 | #include "mpIPackage.h" 37 | #include "mpIOprt.h" 38 | 39 | 40 | MUP_NAMESPACE_START 41 | 42 | //------------------------------------------------------------------------------ 43 | /** \brief Package for installing complex functions and operators. */ 44 | class PackageMatrix : public IPackage 45 | { 46 | friend class std::unique_ptr; 47 | 48 | public: 49 | 50 | static IPackage* Instance(); 51 | virtual void AddToParser(ParserXBase *pParser); 52 | virtual string_type GetDesc() const; 53 | virtual string_type GetPrefix() const; 54 | 55 | private: 56 | 57 | static std::unique_ptr s_pInstance; 58 | }; 59 | 60 | MUP_NAMESPACE_END 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /parser/mpParser.h: -------------------------------------------------------------------------------- 1 | #ifndef MUP_PARSERX_H 2 | #define MUP_PARSERX_H 3 | 4 | /** \file 5 | \brief Definition of the parser class 6 | 7 |
 8 |                __________                                 ____  ___
 9 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
10 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
11 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
12 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
13 |         \/                     \/           \/     \/           \_/
14 |                                        Copyright (C) 2023, Ingo Berg
15 |                                        All rights reserved.
16 | 
17 |   Redistribution and use in source and binary forms, with or without 
18 |   modification, are permitted provided that the following conditions are met:
19 | 
20 |    * Redistributions of source code must retain the above copyright notice, 
21 |      this list of conditions and the following disclaimer.
22 |    * Redistributions in binary form must reproduce the above copyright notice, 
23 |      this list of conditions and the following disclaimer in the documentation 
24 |      and/or other materials provided with the distribution.
25 | 
26 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
27 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
28 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
29 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
30 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
31 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
32 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
33 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
34 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
35 |   POSSIBILITY OF SUCH DAMAGE.
36 | 
37 | */ 38 | 39 | //--- Parser framework ----------------------------------------------------- 40 | #include "mpDefines.h" 41 | #include "mpParserBase.h" 42 | 43 | 44 | MUP_NAMESPACE_START 45 | 46 | /** \brief The parser implementation. 47 | \sa ParserXBase 48 | 49 | This is the class that implements the parser. It installs all functions 50 | and operatore and defines the constants. 51 | */ 52 | class ParserX : public ParserXBase 53 | { 54 | public: 55 | ParserX(unsigned ePackages = pckALL_COMPLEX); 56 | 57 | static void ResetErrorMessageProvider(ParserMessageProviderBase *pProvider); 58 | }; 59 | } // namespace mu 60 | 61 | #endif 62 | 63 | -------------------------------------------------------------------------------- /parser/mpPackageNonCmplx.h: -------------------------------------------------------------------------------- 1 | #ifndef MU_PACKAGE_NON_CMPLX_H 2 | #define MU_PACKAGE_NON_CMPLX_H 3 | 4 | /* 5 | __________ ____ ___ 6 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 7 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 8 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 9 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 10 | \/ \/ \/ \/ \_/ 11 | Copyright (C) 2023, Ingo Berg 12 | All rights reserved. 13 | 14 | Redistribution and use in source and binary forms, with or without 15 | modification, are permitted provided that the following conditions are met: 16 | 17 | * Redistributions of source code must retain the above copyright notice, 18 | this list of conditions and the following disclaimer. 19 | * Redistributions in binary form must reproduce the above copyright notice, 20 | this list of conditions and the following disclaimer in the documentation 21 | and/or other materials provided with the distribution. 22 | 23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | POSSIBILITY OF SUCH DAMAGE. 33 | */ 34 | 35 | #include 36 | #include "mpIPackage.h" 37 | #include "mpIOprt.h" 38 | 39 | 40 | MUP_NAMESPACE_START 41 | 42 | //------------------------------------------------------------------------------ 43 | /** \brief Package for installing complex functions and operators. */ 44 | class PackageNonCmplx: public IPackage 45 | { 46 | friend class std::unique_ptr; 47 | 48 | public: 49 | 50 | static IPackage* Instance(); 51 | 52 | virtual void AddToParser(ParserXBase *pParser); 53 | virtual string_type GetDesc() const; 54 | virtual string_type GetPrefix() const; 55 | 56 | private: 57 | 58 | static std::unique_ptr s_pInstance; 59 | }; 60 | 61 | MUP_NAMESPACE_END 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /parser/mpIPrecedence.h: -------------------------------------------------------------------------------- 1 | /** \file mpIPrecedence.h 2 | \brief Definition of base classes needed for parser operator definitions. 3 | 4 |
 5 |                __________                                 ____  ___
 6 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
 7 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
 8 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
 9 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
10 |         \/                     \/           \/     \/           \_/
11 |                                        Copyright (C) 2023 Ingo Berg
12 |                                        All rights reserved.
13 | 
14 |   muParserX - A C++ math parser library with array and string support
15 |   Copyright (C) 2023, Ingo Berg
16 |   All rights reserved.
17 | 
18 |   Redistribution and use in source and binary forms, with or without 
19 |   modification, are permitted provided that the following conditions are met:
20 | 
21 |    * Redistributions of source code must retain the above copyright notice, 
22 |      this list of conditions and the following disclaimer.
23 |    * Redistributions in binary form must reproduce the above copyright notice, 
24 |      this list of conditions and the following disclaimer in the documentation 
25 |      and/or other materials provided with the distribution.
26 | 
27 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
28 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
29 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
30 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
31 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
32 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
33 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
34 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
35 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
36 |   POSSIBILITY OF SUCH DAMAGE.
37 | 
38 | */ 39 | #ifndef MUP_IPRECEDENCE_H 40 | #define MUP_IPRECEDENCE_H 41 | 42 | #include "mpTypes.h" 43 | 44 | 45 | MUP_NAMESPACE_START 46 | 47 | //------------------------------------------------------------------------------ 48 | /** \brief Interface for binary and ternary operators 49 | \ingroup binop 50 | */ 51 | class IPrecedence 52 | { 53 | public: 54 | virtual ~IPrecedence(){} 55 | virtual int GetPri() const = 0; 56 | virtual EOprtAsct GetAssociativity() const = 0; 57 | }; 58 | } // namespace mu 59 | 60 | #endif 61 | 62 | -------------------------------------------------------------------------------- /doc/custom/features.txt: -------------------------------------------------------------------------------- 1 | /*! \page page_features Features 2 | \section fea_overview Feature overview 3 | 4 |
    5 |
  • Supported data types: double, integer, complex, boolean, string, array
  • 6 |
  • Extensible with custom operators (binary, infix or postfix)
  • 7 |
  • Extensible with custom functions with an arbitrary number of function arguments
  • 8 |
  • Support for an unlimited number of variables and constants
  • 9 |
  • No limit on expression complexity
  • 10 |
  • Reads binary, hexadecimal, complex, integer and string values from expressions and 11 | can be extended to read user defined values as well.
  • 12 |
  • Supports a large variety of predefined \ref fea_predefined_op, \ref fea_predefined_fun 13 | and \ref fea_predefined_const.
  • 14 |
15 | 16 | \section fea_predefined_const Predefined Constants 17 | By default the parser supports the following mathematical constants: 18 |
    19 |
  • The eulerian number with:
    20 | e = 2.718281828459045235360287
     
  • 21 |
  • The mathematical constant equal to a circle's circumference divided by its diameter.
    22 | pi = 3.141592653589793238462643
     
  • 23 |
  • The imaginary unit with:
    24 | i = sqrt(-1)
     
  • 25 |
26 | 27 | \section fea_predefined_op Predefined Operators 28 |

Binary operators:

29 |
    30 |
  • Standard operators:
    31 | "+", "-", "*", "/", "^"
     
  • 32 |
  • Assignment operators:
    33 | "=", "+=", "-=", "*=", "/="
     
  • 34 |
  • Logical operators:
    35 | "and", "or", "xor","==", "!=", ">", "<", "<=", ">="
     
  • 36 |
  • Bit manipulation:
    37 | "&", "|", "<<", ">>"
     
  • 38 |
  • String concatenation:
    39 | "//"
     
  • 40 |
41 | 42 |

Postfix operators:

43 |
    44 |
  • Unit postfixes (nano, micro, milli, kilo, giga, mega):
    45 | "{n}", "{mu}", "{m}", "{k}", "{G}", "{M}"
     
  • 46 |
47 | 48 |

49 |

Infix operators:

50 |
    51 |
  • Sign operator and type conversions:
    52 | "-", "(float)", "(int)"
     
  • 53 |
54 | 55 | \section fea_predefined_fun Predefined Functions 56 |
    57 |
  • Standard functions:
    58 | abs, sin, cos, tan, sinh, cosh, tanh, ln, log, log10, exp, sqrt
     
  • 59 |
  • Unlimited number of arguments:
    60 | min, max, sum
     
  • 61 |
  • String functions:
    62 | str2dbl, strlen, toupper
     
  • 63 |
  • Complex functions:
    64 | real, imag, conj, arg, norm
     
  • 65 |
  • Array functions:
    66 | sizeof
     
  • 67 |
68 | 69 | */ -------------------------------------------------------------------------------- /parser/mpPackageCommon.h: -------------------------------------------------------------------------------- 1 | #ifndef MU_PACKAGE_COMMON_H 2 | #define MU_PACKAGE_COMMON_H 3 | 4 | /* 5 | __________ ____ ___ 6 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 7 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 8 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 9 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 10 | \/ \/ \/ \/ \_/ 11 | Copyright (C) 2023, Ingo Berg 12 | All rights reserved. 13 | 14 | Redistribution and use in source and binary forms, with or without 15 | modification, are permitted provided that the following conditions are met: 16 | 17 | * Redistributions of source code must retain the above copyright notice, 18 | this list of conditions and the following disclaimer. 19 | * Redistributions in binary form must reproduce the above copyright notice, 20 | this list of conditions and the following disclaimer in the documentation 21 | and/or other materials provided with the distribution. 22 | 23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | POSSIBILITY OF SUCH DAMAGE. 33 | */ 34 | 35 | #include 36 | #include "mpIPackage.h" 37 | #include "mpIOprt.h" 38 | 39 | 40 | MUP_NAMESPACE_START 41 | 42 | //------------------------------------------------------------------------------ 43 | /** \brief Package for installing operators and functions which 44 | are always present. 45 | */ 46 | class PackageCommon: public IPackage 47 | { 48 | friend class std::unique_ptr; 49 | 50 | public: 51 | 52 | static IPackage* Instance(); 53 | virtual void AddToParser(ParserXBase *pParser) override; 54 | virtual string_type GetDesc() const override; 55 | virtual string_type GetPrefix() const override; 56 | 57 | private: 58 | 59 | static std::unique_ptr s_pInstance; 60 | }; 61 | 62 | MUP_NAMESPACE_END 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /parser/mpScriptTokens.h: -------------------------------------------------------------------------------- 1 | #ifndef MUP_SCRIPT_TOKENS_H 2 | #define MUP_SCRIPT_TOKENS_H 3 | 4 | /** \file 5 | \brief Definition of basic types used by muParserX 6 | 7 |
 8 |                __________                                 ____  ___
 9 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
10 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
11 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
12 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
13 |         \/                     \/           \/     \/           \_/
14 |                                        Copyright (C) 2023, Ingo Berg
15 |                                        All rights reserved.
16 | 
17 |   Redistribution and use in source and binary forms, with or without 
18 |   modification, are permitted provided that the following conditions are met:
19 | 
20 |    * Redistributions of source code must retain the above copyright notice, 
21 |      this list of conditions and the following disclaimer.
22 |    * Redistributions in binary form must reproduce the above copyright notice, 
23 |      this list of conditions and the following disclaimer in the documentation 
24 |      and/or other materials provided with the distribution.
25 | 
26 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
27 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
28 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
29 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
30 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
31 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
32 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
33 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
34 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
35 |   POSSIBILITY OF SUCH DAMAGE.
36 | 
37 | */ 38 | 39 | #include "mpIToken.h" 40 | 41 | 42 | MUP_NAMESPACE_START 43 | 44 | //--------------------------------------------------------------------------- 45 | /** \brief A class for encapsulation a newline token. */ 46 | class TokenNewline : public IToken 47 | { 48 | public: 49 | 50 | TokenNewline(); 51 | 52 | //--------------------------------------------- 53 | // IToken interface 54 | //--------------------------------------------- 55 | 56 | virtual IToken* Clone() const; 57 | virtual string_type AsciiDump() const; 58 | 59 | int GetStackOffset() const; 60 | void SetStackOffset(int nOffset); 61 | 62 | private: 63 | int m_nOffset; 64 | }; 65 | 66 | MUP_NAMESPACE_END 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /parser/mpOprtIndex.h: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #ifndef MP_OPRT_INDEX_H 32 | #define MP_OPRT_INDEX_H 33 | 34 | /** \file 35 | \brief Definitions of index operator classes. 36 | */ 37 | 38 | /** \defgroup binop Binary operator callbacks 39 | 40 | This group lists the objects representing the binary operators of muParserX. 41 | */ 42 | 43 | #include 44 | #include "mpIOprt.h" 45 | #include "mpValue.h" 46 | #include "mpError.h" 47 | 48 | 49 | MUP_NAMESPACE_START 50 | 51 | //----------------------------------------------------------------------------------------------- 52 | /** \brief Default implementation of a multidimensional index operator. 53 | */ 54 | class OprtIndex : public ICallback 55 | { 56 | public: 57 | OprtIndex(); 58 | virtual void Eval(ptr_val_type& ret, const ptr_val_type *arg, int argc) override; 59 | virtual const char_type* GetDesc() const override; 60 | virtual IToken* Clone() const override; 61 | }; 62 | 63 | MUP_NAMESPACE_END 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /parser/mpRPN.h: -------------------------------------------------------------------------------- 1 | #ifndef MUP_RPN_H 2 | #define MUP_RPN_H 3 | 4 | /* 5 | __________ ____ ___ 6 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 7 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 8 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 9 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 10 | \/ \/ \/ \/ \_/ 11 | Copyright (C) 2023, Ingo Berg 12 | All rights reserved. 13 | 14 | Redistribution and use in source and binary forms, with or without 15 | modification, are permitted provided that the following conditions are met: 16 | 17 | * Redistributions of source code must retain the above copyright notice, 18 | this list of conditions and the following disclaimer. 19 | * Redistributions in binary form must reproduce the above copyright notice, 20 | this list of conditions and the following disclaimer in the documentation 21 | and/or other materials provided with the distribution. 22 | 23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | POSSIBILITY OF SUCH DAMAGE. 33 | */ 34 | 35 | #include "mpFwdDecl.h" 36 | #include "mpTypes.h" 37 | 38 | 39 | MUP_NAMESPACE_START 40 | 41 | //--------------------------------------------------------------------------- 42 | /** \brief A class representing the reverse polnish notation of the expression. 43 | 44 | */ 45 | class RPN 46 | { 47 | public: 48 | 49 | RPN(); 50 | ~RPN(); 51 | 52 | void Add(ptr_tok_type tok); 53 | void AddNewline(ptr_tok_type tok, int n); 54 | void Pop(int num); 55 | void Reset(); 56 | void Finalize(); 57 | void AsciiDump() const; 58 | 59 | const token_vec_type& GetData() const; 60 | std::size_t GetSize() const; 61 | 62 | int GetRequiredStackSize() const; 63 | void EnableOptimizer(bool bStat); 64 | 65 | private: 66 | 67 | token_vec_type m_vRPN; 68 | int m_nStackPos; 69 | int m_nLine; 70 | int m_nMaxStackPos; 71 | bool m_bEnableOptimizer; 72 | }; 73 | 74 | MUP_NAMESPACE_END 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /parser/mpValueCache.h: -------------------------------------------------------------------------------- 1 | #ifndef MUP_VALUE_CACHE_H 2 | #define MUP_VALUE_CACHE_H 3 | 4 | /** \file 5 | \brief Implementation of a cache for recycling unused value items. 6 | 7 |
 8 |                __________                                 ____  ___
 9 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
10 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
11 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
12 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
13 |         \/                     \/           \/     \/           \_/
14 |                                        Copyright (C) 2023, Ingo Berg
15 |                                        All rights reserved.
16 | 
17 |   Redistribution and use in source and binary forms, with or without 
18 |   modification, are permitted provided that the following conditions are met:
19 | 
20 |    * Redistributions of source code must retain the above copyright notice, 
21 |      this list of conditions and the following disclaimer.
22 |    * Redistributions in binary form must reproduce the above copyright notice, 
23 |      this list of conditions and the following disclaimer in the documentation 
24 |      and/or other materials provided with the distribution.
25 | 
26 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
27 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
28 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
29 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
30 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
31 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
32 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
33 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
34 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
35 |   POSSIBILITY OF SUCH DAMAGE.
36 | 
37 | */ 38 | #include 39 | 40 | #include "mpFwdDecl.h" 41 | 42 | 43 | MUP_NAMESPACE_START 44 | 45 | /** \brief The ValueCache class provides a simple mechanism to recycle 46 | unused value items. 47 | 48 | This class serves as a factory for value items. It allows skipping 49 | unnecessary and slow new/delete calls by storing unused value 50 | objects in an internal buffer for later reuse. By eliminating new/delete 51 | calls the parser is sped up approximately by factor 3-4. 52 | */ 53 | class ValueCache 54 | { 55 | public: 56 | ValueCache(int size=10); 57 | ~ValueCache(); 58 | 59 | void ReleaseAll(); 60 | void ReleaseToCache(Value *pValue); 61 | Value* CreateFromCache(); 62 | 63 | private: 64 | ValueCache(const ValueCache &ref); 65 | ValueCache& operator=(const ValueCache &ref); 66 | 67 | int m_nIdx; 68 | std::vector m_vCache; 69 | }; 70 | 71 | MUP_NAMESPACE_END 72 | 73 | #endif // include guard 74 | -------------------------------------------------------------------------------- /parser/mpScriptTokens.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #include "mpScriptTokens.h" 32 | #include "mpTypes.h" 33 | 34 | 35 | MUP_NAMESPACE_START 36 | 37 | //--------------------------------------------------------------------------- 38 | TokenNewline::TokenNewline() 39 | :IToken(cmSCRIPT_NEWLINE) 40 | ,m_nOffset(0) 41 | {} 42 | 43 | //--------------------------------------------------------------------------- 44 | IToken* TokenNewline::Clone() const 45 | { 46 | return new TokenNewline(*this); 47 | } 48 | 49 | //--------------------------------------------------------------------------- 50 | void TokenNewline::SetStackOffset(int nOffset) 51 | { 52 | m_nOffset = nOffset; 53 | } 54 | 55 | //--------------------------------------------------------------------------- 56 | int TokenNewline::GetStackOffset() const 57 | { 58 | return m_nOffset; 59 | } 60 | 61 | //--------------------------------------------------------------------------- 62 | string_type TokenNewline::AsciiDump() const 63 | { 64 | stringstream_type ss; 65 | 66 | ss << g_sCmdCode[ GetCode() ]; 67 | ss << _T(" [addr=0x") << std::hex << this << std::dec; 68 | ss << _T("; pos=") << GetExprPos(); 69 | ss << _T("; offset=") << m_nOffset; 70 | ss << _T("]"); 71 | return ss.str(); 72 | } 73 | 74 | MUP_NAMESPACE_END 75 | -------------------------------------------------------------------------------- /parser/mpIOprtBinShortcut.h: -------------------------------------------------------------------------------- 1 | #ifndef MUP_I_OPRT_BIN_SHORTCUT_H 2 | #define MUP_I_OPRT_BIN_SHORTCUT_H 3 | 4 | /** \file 5 | \brief Definition of basic types used by muParserX 6 | 7 |
 8 |                __________                                 ____  ___
 9 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
10 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     /
11 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \
12 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
13 |         \/                     \/           \/     \/           \_/
14 |   Copyright (C) 2023 Ingo Berg, et al.
15 |   All rights reserved.
16 | 
17 |   Redistribution and use in source and binary forms, with or without
18 |   modification, are permitted provided that the following conditions are met:
19 | 
20 |    * Redistributions of source code must retain the above copyright notice,
21 |      this list of conditions and the following disclaimer.
22 |    * Redistributions in binary form must reproduce the above copyright notice,
23 |      this list of conditions and the following disclaimer in the documentation
24 |      and/or other materials provided with the distribution.
25 | 
26 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
27 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 |   POSSIBILITY OF SUCH DAMAGE.
36 | 
37 | */ 38 | 39 | #include "mpIToken.h" 40 | #include "mpIPrecedence.h" 41 | 42 | 43 | MUP_NAMESPACE_START 44 | 45 | //--------------------------------------------------------------------------- 46 | /** \brief A class for encapsulation if-then-else tokens. */ 47 | class IOprtBinShortcut : public IToken, public IPrecedence 48 | { 49 | public: 50 | IOprtBinShortcut(ECmdCode eCmd, const char_type *a_szIdent, int nPrec, EOprtAsct m_eAsc); 51 | 52 | void SetOffset(int nOffset); 53 | int GetOffset() const; 54 | 55 | //--------------------------------------------- 56 | // IToken interface 57 | //--------------------------------------------- 58 | 59 | virtual IToken* Clone() const override; 60 | virtual string_type AsciiDump() const override; 61 | virtual IPrecedence* AsIPrecedence() override; 62 | 63 | //--------------------------------------------- 64 | // IPrecedence interface 65 | //--------------------------------------------- 66 | 67 | virtual int GetPri() const override; 68 | virtual EOprtAsct GetAssociativity() const override; 69 | 70 | private: 71 | int m_nPrec; 72 | EOprtAsct m_eAsc; 73 | int m_nOffset; 74 | }; 75 | 76 | MUP_NAMESPACE_END 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /parser/mpIfThenElse.h: -------------------------------------------------------------------------------- 1 | /** \file 2 | \brief Definition of basic types used by muParserX 3 | 4 |
 5 |                __________                                 ____  ___
 6 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
 7 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
 8 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
 9 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
10 |         \/                     \/           \/     \/           \_/
11 |                                        Copyright (C) 2023 Ingo Berg
12 |                                        All rights reserved.
13 | 
14 |   muParserX - A C++ math parser library with array and string support
15 |   Copyright (C) 2023, Ingo Berg
16 |   All rights reserved.
17 | 
18 |   Redistribution and use in source and binary forms, with or without 
19 |   modification, are permitted provided that the following conditions are met:
20 | 
21 |    * Redistributions of source code must retain the above copyright notice, 
22 |      this list of conditions and the following disclaimer.
23 |    * Redistributions in binary form must reproduce the above copyright notice, 
24 |      this list of conditions and the following disclaimer in the documentation 
25 |      and/or other materials provided with the distribution.
26 | 
27 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
28 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
29 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
30 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
31 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
32 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
33 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
34 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
35 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
36 |   POSSIBILITY OF SUCH DAMAGE.
37 | 
38 | */ 39 | #ifndef MUP_IF_THEN_ELSE_H 40 | #define MUP_IF_THEN_ELSE_H 41 | 42 | #include "mpIToken.h" 43 | #include "mpIPrecedence.h" 44 | 45 | 46 | MUP_NAMESPACE_START 47 | 48 | //--------------------------------------------------------------------------- 49 | /** \brief A class for encapsulation if-then-else tokens. */ 50 | class TokenIfThenElse : public IToken, 51 | public IPrecedence 52 | { 53 | public: 54 | 55 | TokenIfThenElse(ECmdCode eCmd); 56 | void SetOffset(int nOffset); 57 | int GetOffset() const; 58 | 59 | //--------------------------------------------- 60 | // IToken interface 61 | //--------------------------------------------- 62 | 63 | virtual IToken* Clone() const override; 64 | virtual string_type AsciiDump() const override; 65 | virtual IPrecedence* AsIPrecedence() override; 66 | 67 | //--------------------------------------------- 68 | // IPrecedence interface 69 | //--------------------------------------------- 70 | 71 | virtual int GetPri() const override; 72 | virtual EOprtAsct GetAssociativity() const override; 73 | 74 | private: 75 | int m_nOffset; 76 | }; 77 | 78 | MUP_NAMESPACE_END 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /parser/mpIValReader.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023 Ingo Berg 9 | All rights reserved. 10 | 11 | muParserX - A C++ math parser library with array and string support 12 | Copyright (c) 2023, Ingo Berg 13 | All rights reserved. 14 | 15 | Redistribution and use in source and binary forms, with or without 16 | modification, are permitted provided that the following conditions are met: 17 | 18 | * Redistributions of source code must retain the above copyright notice, 19 | this list of conditions and the following disclaimer. 20 | * Redistributions in binary form must reproduce the above copyright notice, 21 | this list of conditions and the following disclaimer in the documentation 22 | and/or other materials provided with the distribution. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 25 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 28 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | #include "mpIValReader.h" 36 | #include "mpTokenReader.h" 37 | 38 | #include 39 | 40 | 41 | MUP_NAMESPACE_START 42 | 43 | //-------------------------------------------------------------------------------------------- 44 | IValueReader::IValueReader() 45 | :m_pTokenReader(nullptr) 46 | {} 47 | 48 | //-------------------------------------------------------------------------------------------- 49 | IValueReader::~IValueReader() 50 | {} 51 | 52 | //-------------------------------------------------------------------------------------------- 53 | IValueReader::IValueReader(const IValueReader &ref) 54 | { 55 | m_pTokenReader = ref.m_pTokenReader; 56 | } 57 | 58 | //-------------------------------------------------------------------------------------------- 59 | void IValueReader::SetParent(TokenReader *pTokenReader) 60 | { 61 | assert(pTokenReader); 62 | m_pTokenReader = pTokenReader; 63 | } 64 | 65 | //-------------------------------------------------------------------------------------------- 66 | const IToken* IValueReader::TokenHistory(std::size_t pos) const 67 | { 68 | const TokenReader::token_buf_type &buf = m_pTokenReader->GetTokens(); 69 | std::size_t size = buf.size(); 70 | return (pos>=size) ? nullptr : buf[size-1-pos].Get(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /parser/mpPackageStr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #include "mpPackageStr.h" 32 | 33 | #include "mpParserBase.h" 34 | #include "mpFuncStr.h" 35 | #include "mpOprtBinCommon.h" 36 | #include "mpValReader.h" 37 | 38 | MUP_NAMESPACE_START 39 | 40 | //------------------------------------------------------------------------------ 41 | std::unique_ptr PackageStr::s_pInstance; 42 | 43 | //------------------------------------------------------------------------------ 44 | IPackage* PackageStr::Instance() 45 | { 46 | if (s_pInstance.get()==nullptr) 47 | { 48 | s_pInstance.reset(new PackageStr); 49 | } 50 | 51 | return s_pInstance.get(); 52 | } 53 | 54 | //------------------------------------------------------------------------------ 55 | void PackageStr::AddToParser(ParserXBase *pParser) 56 | { 57 | pParser->AddValueReader(new StrValReader()); 58 | 59 | // Functions 60 | pParser->DefineFun(new FunStrLen()); 61 | pParser->DefineFun(new FunStrToDbl()); 62 | pParser->DefineFun(new FunStrToUpper()); 63 | pParser->DefineFun(new FunStrToLower()); 64 | 65 | // Operators 66 | pParser->DefineOprt(new OprtStrAdd); 67 | } 68 | 69 | //------------------------------------------------------------------------------ 70 | string_type PackageStr::GetDesc() const 71 | { 72 | return _T("A package for string operations."); 73 | } 74 | 75 | //------------------------------------------------------------------------------ 76 | string_type PackageStr::GetPrefix() const 77 | { 78 | return _T(""); 79 | } 80 | 81 | MUP_NAMESPACE_END 82 | -------------------------------------------------------------------------------- /parser/mpPackageMatrix.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #include "mpPackageMatrix.h" 32 | 33 | #include "mpParserBase.h" 34 | #include "mpFuncMatrix.h" 35 | #include "mpOprtMatrix.h" 36 | 37 | MUP_NAMESPACE_START 38 | 39 | //------------------------------------------------------------------------------ 40 | std::unique_ptr PackageMatrix::s_pInstance; 41 | 42 | //------------------------------------------------------------------------------ 43 | IPackage* PackageMatrix::Instance() 44 | { 45 | if (s_pInstance.get()==nullptr) 46 | { 47 | s_pInstance.reset(new PackageMatrix); 48 | } 49 | 50 | return s_pInstance.get(); 51 | } 52 | 53 | //------------------------------------------------------------------------------ 54 | void PackageMatrix::AddToParser(ParserXBase *pParser) 55 | { 56 | // Matrix functions 57 | pParser->DefineFun(new FunMatrixOnes()); 58 | pParser->DefineFun(new FunMatrixZeros()); 59 | pParser->DefineFun(new FunMatrixEye()); 60 | pParser->DefineFun(new FunMatrixSize()); 61 | 62 | // Matrix Operators 63 | pParser->DefinePostfixOprt(new OprtTranspose()); 64 | 65 | // Colon operator 66 | //pParser->DefineOprt(new OprtColon()); 67 | //pParser->DefineAggregator(new AggColon()); 68 | } 69 | 70 | //------------------------------------------------------------------------------ 71 | string_type PackageMatrix::GetDesc() const 72 | { 73 | return _T("Operators and functions for matrix operations"); 74 | } 75 | 76 | //------------------------------------------------------------------------------ 77 | string_type PackageMatrix::GetPrefix() const 78 | { 79 | return _T(""); 80 | } 81 | 82 | MUP_NAMESPACE_END 83 | -------------------------------------------------------------------------------- /parser/mpOprtPostfixCommon.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mpOprtPostfixCommon.h" 3 | 4 | MUP_NAMESPACE_START 5 | 6 | //----------------------------------------------------------- 7 | // 8 | // class OprtFact 9 | // 10 | //----------------------------------------------------------- 11 | 12 | OprtFact::OprtFact() 13 | :IOprtPostfix(_T("!")) 14 | {} 15 | 16 | //----------------------------------------------------------- 17 | void OprtFact::Eval(ptr_val_type& ret, const ptr_val_type *arg, int) 18 | { 19 | if (!arg[0]->IsInteger()) 20 | throw ParserError(ErrorContext(ecTYPE_CONFLICT_FUN, GetExprPos(), GetIdent(), arg[0]->GetType(), 'i', 1)); 21 | 22 | int_type input = arg[0]->GetInteger(); 23 | float_type input_long = float_type(input); 24 | 25 | if (input < 0) { 26 | throw ParserError(ErrorContext(ecDOMAIN_ERROR, GetExprPos(), 27 | GetIdent())); 28 | } 29 | 30 | float_type result = 1; 31 | for (float_type i = 1.0; i <= input_long; i += 1.0) 32 | { 33 | result *= i; 34 | 35 | // Only throw exceptions if IEEE 754 is not supported. The 36 | // Prefered way of dealing with overflows is relying on: 37 | // 38 | // http://en.wikipedia.org/wiki/IEEE_754-1985 39 | // 40 | // If the compiler does not support IEEE 754, chances are 41 | // you are running on a pretty fucked up system. 42 | // 43 | if ( !std::numeric_limits::is_iec559 && 44 | (result>std::numeric_limits::max() || result < 1.0) ) 45 | { 46 | throw ParserError(ErrorContext(ecOVERFLOW, GetExprPos(), GetIdent())); 47 | } 48 | // 49 | } 50 | 51 | *ret = result; 52 | } 53 | 54 | //----------------------------------------------------------- 55 | const char_type* OprtFact::GetDesc() const 56 | { 57 | return _T("x! - Returns factorial of a non-negative integer."); 58 | } 59 | 60 | //----------------------------------------------------------- 61 | IToken* OprtFact::Clone() const 62 | { 63 | return new OprtFact(*this); 64 | } 65 | 66 | //----------------------------------------------------------- 67 | // 68 | // class OprtPercentage 69 | // 70 | //----------------------------------------------------------- 71 | 72 | OprtPercentage::OprtPercentage() 73 | :IOprtPostfix(_T("%")) 74 | {} 75 | 76 | //----------------------------------------------------------- 77 | void OprtPercentage::Eval(ptr_val_type& ret, const ptr_val_type *arg, int) 78 | { 79 | 80 | switch (arg[0]->GetType()) { 81 | case 'i': 82 | case 'f': { 83 | float_type input = arg[0]->GetFloat(); 84 | *ret = input / 100.0; 85 | break; 86 | } 87 | default: 88 | throw ParserError(ErrorContext(ecTYPE_CONFLICT_FUN, GetExprPos(), GetIdent(), arg[0]->GetType(), 'f', 1)); 89 | break; 90 | } 91 | } 92 | 93 | //----------------------------------------------------------- 94 | const char_type* OprtPercentage::GetDesc() const 95 | { 96 | return _T("x% - Returns percentage of integer/float."); 97 | } 98 | 99 | //----------------------------------------------------------- 100 | IToken* OprtPercentage::Clone() const 101 | { 102 | return new OprtPercentage(*this); 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /parser/mpOprtMatrix.h: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #ifndef MP_OPRT_MATRIX_H 32 | #define MP_OPRT_MATRIX_H 33 | 34 | /** \file 35 | \brief Definitions of classes used as callbacks for matrix operators. 36 | */ 37 | 38 | 39 | #include 40 | #include "mpIOprt.h" 41 | #include "mpValue.h" 42 | #include "mpError.h" 43 | 44 | 45 | MUP_NAMESPACE_START 46 | 47 | //----------------------------------------------------------------------------------------------- 48 | class OprtTranspose : public IOprtPostfix 49 | { 50 | public: 51 | OprtTranspose(); 52 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; 53 | virtual const char_type* GetDesc() const override; 54 | virtual IToken* Clone() const override; 55 | }; 56 | 57 | //----------------------------------------------------------------------------------------------- 58 | /** \brief On the fly array creation using the curly bracket operator. 59 | */ 60 | class OprtCreateArray : public ICallback 61 | { 62 | public: 63 | OprtCreateArray(); 64 | virtual void Eval(ptr_val_type& ret, const ptr_val_type *arg, int argc) override; 65 | virtual const char_type* GetDesc() const override; 66 | virtual IToken* Clone() const override; 67 | }; 68 | 69 | //----------------------------------------------------------------------------------------------- 70 | class OprtColon : public IOprtBin 71 | { 72 | public: 73 | OprtColon(); 74 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; 75 | virtual const char_type* GetDesc() const override; 76 | virtual IToken* Clone() const override; 77 | }; 78 | MUP_NAMESPACE_END 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /parser/mpPackageUnit.h: -------------------------------------------------------------------------------- 1 | #ifndef MU_PACKAGE_UNIT_H 2 | #define MU_PACKAGE_UNIT_H 3 | 4 | /* 5 | __________ ____ ___ 6 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 7 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 8 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 9 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 10 | \/ \/ \/ \/ \_/ 11 | Copyright (C) 2023, Ingo Berg 12 | All rights reserved. 13 | 14 | Redistribution and use in source and binary forms, with or without 15 | modification, are permitted provided that the following conditions are met: 16 | 17 | * Redistributions of source code must retain the above copyright notice, 18 | this list of conditions and the following disclaimer. 19 | * Redistributions in binary form must reproduce the above copyright notice, 20 | this list of conditions and the following disclaimer in the documentation 21 | and/or other materials provided with the distribution. 22 | 23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | POSSIBILITY OF SUCH DAMAGE. 33 | */ 34 | 35 | #include 36 | #include "mpIPackage.h" 37 | #include "mpIOprt.h" 38 | 39 | MUP_NAMESPACE_START 40 | 41 | #define MUP_POSTFIX_DEF(CLASS) \ 42 | class CLASS : public IOprtPostfix \ 43 | { \ 44 | public: \ 45 | CLASS(IPackage* pPackage=nullptr); \ 46 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc); \ 47 | virtual const char_type* GetDesc() const; \ 48 | virtual IToken* Clone() const; \ 49 | }; 50 | 51 | MUP_POSTFIX_DEF(OprtNano) 52 | MUP_POSTFIX_DEF(OprtMicro) 53 | MUP_POSTFIX_DEF(OprtMilli) 54 | MUP_POSTFIX_DEF(OprtKilo) 55 | MUP_POSTFIX_DEF(OprtMega) 56 | MUP_POSTFIX_DEF(OprtGiga) 57 | 58 | #undef MUP_POSTFIX_DEF 59 | 60 | //------------------------------------------------------------------------------ 61 | /** \brief Package for installing unit postfix operators into muParserX. */ 62 | class PackageUnit : public IPackage 63 | { 64 | friend class std::unique_ptr; 65 | 66 | public: 67 | 68 | static IPackage* Instance(); 69 | virtual void AddToParser(ParserXBase *pParser); 70 | virtual string_type GetDesc() const; 71 | virtual string_type GetPrefix() const; 72 | 73 | private: 74 | 75 | static std::unique_ptr s_pInstance; 76 | }; 77 | 78 | MUP_NAMESPACE_END 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /parser/mpICallback.h: -------------------------------------------------------------------------------- 1 | /** \file 2 | \brief Definition of the interface for parser callback objects. 3 | 4 |
 5 |                __________                                 ____  ___
 6 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
 7 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
 8 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
 9 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
10 |         \/                     \/           \/     \/           \_/
11 |                                        Copyright (C) 2023 Ingo Berg
12 |                                        All rights reserved.
13 | 
14 |   muParserX - A C++ math parser library with array and string support
15 |   Copyright (C) 2023, Ingo Berg
16 |   All rights reserved.
17 | 
18 |   Redistribution and use in source and binary forms, with or without 
19 |   modification, are permitted provided that the following conditions are met:
20 | 
21 |    * Redistributions of source code must retain the above copyright notice, 
22 |      this list of conditions and the following disclaimer.
23 |    * Redistributions in binary form must reproduce the above copyright notice, 
24 |      this list of conditions and the following disclaimer in the documentation 
25 |      and/or other materials provided with the distribution.
26 | 
27 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
28 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
29 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
30 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
31 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
32 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
33 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
34 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
35 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
36 |   POSSIBILITY OF SUCH DAMAGE.
37 | 
38 | */ 39 | #ifndef MU_IPARSER_CALLBACK_H 40 | #define MU_IPARSER_CALLBACK_H 41 | 42 | //--- muParserX framework -------------------------------------------- 43 | #include "mpIToken.h" 44 | #include "mpIPackage.h" 45 | 46 | 47 | MUP_NAMESPACE_START 48 | 49 | /** \brief Interface for callback objects. 50 | 51 | All Parser functions and operators must implement this interface. 52 | */ 53 | class ICallback : public IToken 54 | { 55 | public: 56 | typedef ParserXBase parent_type; 57 | 58 | ICallback(ECmdCode a_iCode, 59 | const char_type *a_szName, 60 | int a_nArgNum = 1); 61 | virtual ~ICallback() override; 62 | 63 | virtual ICallback* AsICallback() override; 64 | virtual IValue* AsIValue() override; 65 | 66 | virtual void Eval(ptr_val_type& ret, const ptr_val_type *arg, int argc) = 0; 67 | virtual const char_type* GetDesc() const = 0; 68 | virtual string_type AsciiDump() const override; 69 | 70 | int GetArgc() const; 71 | int GetArgsPresent() const; 72 | void SetParent(parent_type *a_pParent); 73 | void SetNumArgsPresent(int argc); 74 | 75 | protected: 76 | parent_type* GetParent(); 77 | void SetArgc(int argc); 78 | 79 | private: 80 | parent_type *m_pParent; ///< Pointer to the parser object using this callback 81 | int m_nArgc; ///< Number of this function can take Arguments. 82 | int m_nArgsPresent; ///< Number of arguments actually submitted 83 | }; // class ICallback 84 | 85 | MUP_NAMESPACE_END 86 | 87 | #endif 88 | 89 | -------------------------------------------------------------------------------- /parser/mpIOprtBinShortcut.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023 Ingo Berg, et al. 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #include "mpIOprtBinShortcut.h" 32 | 33 | MUP_NAMESPACE_START 34 | 35 | //--------------------------------------------------------------------------- 36 | // logic operator 37 | // && 、 and 、 ||、 or 38 | // 39 | //--------------------------------------------------------------------------- 40 | 41 | IOprtBinShortcut::IOprtBinShortcut(ECmdCode eCmd, const char_type* a_szIdent, int nPrec, EOprtAsct eAsc) 42 | :IToken(eCmd, a_szIdent) 43 | , IPrecedence() 44 | , m_nPrec(nPrec) 45 | , m_eAsc(eAsc) 46 | , m_nOffset() 47 | {} 48 | 49 | //--------------------------------------------------------------------------- 50 | IToken* IOprtBinShortcut::Clone() const 51 | { 52 | return new IOprtBinShortcut(*this); 53 | } 54 | 55 | //--------------------------------------------------------------------------- 56 | void IOprtBinShortcut::SetOffset(int nOffset) 57 | { 58 | m_nOffset = nOffset; 59 | } 60 | 61 | //--------------------------------------------------------------------------- 62 | int IOprtBinShortcut::GetOffset() const 63 | { 64 | return m_nOffset; 65 | } 66 | 67 | //--------------------------------------------------------------------------- 68 | string_type IOprtBinShortcut::AsciiDump() const 69 | { 70 | stringstream_type ss; 71 | 72 | ss << GetIdent(); 73 | ss << _T(" [addr=0x") << std::hex << this << std::dec; 74 | ss << _T("; pos=") << GetExprPos(); 75 | ss << _T("; offset=") << m_nOffset; 76 | ss << _T("]"); 77 | return ss.str(); 78 | } 79 | 80 | //--------------------------------------------------------------------------- 81 | int IOprtBinShortcut::GetPri() const 82 | { 83 | return m_nPrec; 84 | } 85 | 86 | //--------------------------------------------------------------------------- 87 | EOprtAsct IOprtBinShortcut::GetAssociativity() const 88 | { 89 | return m_eAsc; 90 | } 91 | 92 | //--------------------------------------------------------------------------- 93 | IPrecedence* IOprtBinShortcut::AsIPrecedence() 94 | { 95 | return this; 96 | } 97 | 98 | MUP_NAMESPACE_END 99 | -------------------------------------------------------------------------------- /parser/mpOprtBinShortcut.h: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg, et al. 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #ifndef MP_SC_OPRT_BIN_H 32 | #define MP_SC_OPRT_BIN_H 33 | 34 | /** \file 35 | \brief Definitions of classes used as callbacks for standard binary operators. 36 | */ 37 | 38 | /** \defgroup binop Binary operator callbacks 39 | 40 | This group lists the objects representing the binary operators of muParserX. 41 | */ 42 | 43 | #include 44 | #include "mpIOprtBinShortcut.h" 45 | #include "mpValue.h" 46 | #include "mpError.h" 47 | 48 | 49 | MUP_NAMESPACE_START 50 | 51 | //------------------------------------------------------------------------------ 52 | /** \brief Callback class for a logical or operator. begin 53 | \ingroup binop 54 | */ 55 | class OprtShortcutLogicOrBegin : public IOprtBinShortcut 56 | { 57 | public: 58 | OprtShortcutLogicOrBegin(const char_type* szIdent = _T("||")); 59 | virtual IToken* Clone() const override; 60 | }; 61 | 62 | 63 | //------------------------------------------------------------------------------ 64 | /** \brief Callback class for a logical or operator. end 65 | \ingroup binop 66 | */ 67 | class OprtShortcutLogicOrEnd : public IOprtBinShortcut 68 | { 69 | public: 70 | OprtShortcutLogicOrEnd(const char_type* szIdent = _T("||")); 71 | virtual IToken* Clone() const override; 72 | }; 73 | 74 | 75 | //------------------------------------------------------------------------------ 76 | /** \brief Callback class for a logical and operator. begin 77 | \ingroup binop 78 | */ 79 | class OprtShortcutLogicAndBegin : public IOprtBinShortcut 80 | { 81 | public: 82 | OprtShortcutLogicAndBegin(const char_type* szIdent = _T("&&")); 83 | virtual IToken* Clone() const override; 84 | }; 85 | 86 | //------------------------------------------------------------------------------ 87 | /** \brief Callback class for a logical and operator. begin 88 | \ingroup binop 89 | */ 90 | class OprtShortcutLogicAndEnd : public IOprtBinShortcut 91 | { 92 | public: 93 | OprtShortcutLogicAndEnd(const char_type* szIdent = _T("&&")); 94 | virtual IToken* Clone() const override; 95 | }; 96 | 97 | } // namespace mu 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /parser/mpParser.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "mpParser.h" 33 | 34 | //--- Standard includes ---------------------------------------------------- 35 | #include 36 | #include 37 | #include 38 | 39 | //--- Parser framework ----------------------------------------------------- 40 | #include "mpPackageUnit.h" 41 | #include "mpPackageStr.h" 42 | #include "mpPackageCmplx.h" 43 | #include "mpPackageNonCmplx.h" 44 | #include "mpPackageCommon.h" 45 | #include "mpPackageMatrix.h" 46 | 47 | using namespace std; 48 | 49 | 50 | /** \brief Namespace for mathematical applications. */ 51 | MUP_NAMESPACE_START 52 | 53 | //--------------------------------------------------------------------------- 54 | /** \brief Default constructor. 55 | 56 | Call ParserXBase class constructor and initiate function, operator 57 | and constant initialization. 58 | */ 59 | ParserX::ParserX(unsigned ePackages) 60 | :ParserXBase() 61 | { 62 | DefineNameChars(_T("0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")); 63 | DefineOprtChars(_T("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-*^/?<>=#!$%&|~'_µ{}")); 64 | DefineInfixOprtChars(_T("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ()/+-*^?<>=#!$%&|~'_")); 65 | 66 | if (ePackages & pckUNIT) 67 | AddPackage(PackageUnit::Instance()); 68 | 69 | if (ePackages & pckSTRING) 70 | AddPackage(PackageStr::Instance()); 71 | 72 | if (ePackages & pckCOMPLEX) 73 | AddPackage(PackageCmplx::Instance()); 74 | 75 | if (ePackages & pckNON_COMPLEX) 76 | AddPackage(PackageNonCmplx::Instance()); 77 | 78 | if (ePackages & pckCOMMON) 79 | AddPackage(PackageCommon::Instance()); 80 | 81 | if (ePackages & pckMATRIX) 82 | AddPackage(PackageMatrix::Instance()); 83 | } 84 | 85 | //------------------------------------------------------------------------------ 86 | void ParserX::ResetErrorMessageProvider(ParserMessageProviderBase *pProvider) 87 | { 88 | ParserErrorMsg::Reset(pProvider); 89 | } 90 | 91 | } // namespace mu 92 | -------------------------------------------------------------------------------- /parser/mpStringConversionHelper.h: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #ifndef MP_STRING_CONVERSION_HELPER_H 32 | #define MP_STRING_CONVERSION_HELPER_H 33 | 34 | #include // for strtod 35 | #include // for wcstof 36 | #include // for strlen 37 | #include // for enable_if, is_floating_point 38 | 39 | MUP_NAMESPACE_START 40 | 41 | template 42 | class StringConversionHelper 43 | { 44 | public: 45 | static size_t StrLen(const TChar* str) 46 | { 47 | static_assert(std::is_same::value || std::is_same::value, "TChar must be either char or wchar_t"); 48 | return StrLenImpl(str, std::integral_constant::value>()); 49 | } 50 | 51 | static double ParseDouble(const TChar* str, int &parsedLen, bool& success) 52 | { 53 | static_assert(std::is_same::value || std::is_same::value, "TChar must be either char or wchar_t"); 54 | return ParseDoubleImpl(str, parsedLen, success, std::integral_constant::value>()); 55 | } 56 | 57 | private: 58 | static size_t StrLenImpl(const char* str, std::true_type) 59 | { 60 | return std::strlen(str); 61 | } 62 | 63 | static size_t StrLenImpl(const wchar_t* str, std::false_type) 64 | { 65 | return std::wcslen(str); 66 | } 67 | 68 | static double ParseDoubleImpl(const char* str, int &parsedLen, bool& success, std::true_type) 69 | { 70 | char* endptr; 71 | double value = std::strtod(str, &endptr); 72 | success = (endptr != str); 73 | parsedLen = endptr - str; 74 | return value; 75 | } 76 | 77 | static double ParseDoubleImpl(const wchar_t* str, int &parsedLen, bool& success, std::false_type) 78 | { 79 | wchar_t* endptr; 80 | double value = std::wcstod(str, &endptr); 81 | success = (endptr != str); 82 | parsedLen = endptr - str; 83 | return value; 84 | } 85 | }; 86 | 87 | 88 | MUP_NAMESPACE_END 89 | 90 | #endif -------------------------------------------------------------------------------- /parser/mpFuncStr.h: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #ifndef MUP_FUNC_STR_H 32 | #define MUP_FUNC_STR_H 33 | 34 | #include "mpICallback.h" 35 | 36 | 37 | MUP_NAMESPACE_START 38 | 39 | //------------------------------------------------------------------------------ 40 | /** \brief Callback object for determining the length of a string. 41 | \ingroup functions 42 | */ 43 | class FunStrLen : public ICallback 44 | { 45 | public: 46 | FunStrLen(); 47 | virtual void Eval(ptr_val_type& ret, const ptr_val_type *a_pArg, int a_iArgc) override; 48 | virtual const char_type* GetDesc() const override; 49 | virtual IToken* Clone() const override; 50 | }; 51 | 52 | //------------------------------------------------------------------------------ 53 | /** \brief Convert a string to upper case letters. 54 | \ingroup functions 55 | */ 56 | class FunStrToUpper : public ICallback 57 | { 58 | public: 59 | FunStrToUpper(); 60 | virtual void Eval(ptr_val_type& ret, const ptr_val_type *a_pArg, int a_iArgc) override; 61 | virtual const char_type* GetDesc() const override; 62 | virtual IToken* Clone() const override; 63 | }; 64 | 65 | //------------------------------------------------------------------------------ 66 | /** \brief Convert a string to lower case letters. 67 | \ingroup functions 68 | */ 69 | class FunStrToLower : public ICallback 70 | { 71 | public: 72 | FunStrToLower(); 73 | virtual void Eval(ptr_val_type& ret, const ptr_val_type *a_pArg, int a_iArgc) override; 74 | virtual const char_type* GetDesc() const override; 75 | virtual IToken* Clone() const override; 76 | }; 77 | 78 | //------------------------------------------------------------------------------ 79 | /** \brief Parse string to a floating point value. 80 | \ingroup functions 81 | */ 82 | class FunStrToDbl : public ICallback 83 | { 84 | public: 85 | FunStrToDbl (); 86 | virtual void Eval(ptr_val_type& ret, const ptr_val_type *a_pArg, int a_iArgc) override; 87 | virtual const char_type* GetDesc() const override; 88 | virtual IToken* Clone() const override; 89 | }; // class FunStrToDbl 90 | } // namespace mu 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /parser/mpTest.h: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #ifndef MU_PARSER_TEST_H 32 | #define MU_PARSER_TEST_H 33 | 34 | #include 35 | #include // for accumulate 36 | #include "mpParser.h" 37 | #include "mpOprtBinCommon.h" 38 | 39 | 40 | MUP_NAMESPACE_START 41 | 42 | /** \brief Test cases for unit testing the parser framework. 43 | */ 44 | class ParserTester // final 45 | { 46 | private: 47 | static int c_iCount; 48 | 49 | int TestParserValue(); 50 | int TestErrorCodes(); 51 | int TestStringFun(); 52 | int TestVector(); 53 | int TestBinOp(); 54 | int TestPostfix(); 55 | int TestInfix(); 56 | int TestEqn(); 57 | int TestMultiArg(); 58 | int TestUndefVar(); 59 | int TestIfElse(); 60 | int TestMatrix(); 61 | int TestComplex(); 62 | int TestScript(); 63 | int TestValReader(); 64 | int TestIssueReports(); 65 | 66 | void Assessment(int a_iNumErr) const; 67 | void Abort() const; 68 | 69 | public: 70 | typedef int (ParserTester::*testfun_type)(); 71 | 72 | ParserTester(); 73 | 74 | /** \brief Destructor (trivial). */ 75 | ~ParserTester() {}; 76 | 77 | /** \brief Copy constructor is deactivated. */ 78 | ParserTester(const ParserTester &a_Obj) 79 | :m_vTestFun() 80 | ,m_stream(a_Obj.m_stream) 81 | {}; 82 | 83 | void Run(); 84 | 85 | private: 86 | std::vector m_vTestFun; 87 | 88 | #if defined(MUP_USE_WIDE_STRING) 89 | std::wostream *m_stream; 90 | #else 91 | std::ostream *m_stream; 92 | #endif 93 | 94 | void AddTest(testfun_type a_pFun); 95 | 96 | // Test Double Parser 97 | int EqnTest(const string_type &a_str, Value a_val, bool a_fPass, int nExprVar = -1, bool evaluateOnce = false); 98 | int ThrowTest(const string_type &a_str, int a_nErrc, int a_nPos = -1, string_type a_sIdent = string_type()); 99 | }; // ParserTester 100 | } // namespace mu 101 | 102 | #endif 103 | 104 | 105 | -------------------------------------------------------------------------------- /parser/mpValueCache.cpp: -------------------------------------------------------------------------------- 1 | /** \file 2 | \brief Definition of a class for caching unused value items and recycling them. 3 | 4 |
  5 |                __________                                 ____  ___
  6 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
  7 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
  8 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
  9 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
 10 |         \/                     \/           \/     \/           \_/
 11 |                                        Copyright (C) 2023, Ingo Berg
 12 |                                        All rights reserved.
 13 | 
 14 |   Redistribution and use in source and binary forms, with or without 
 15 |   modification, are permitted provided that the following conditions are met:
 16 | 
 17 |    * Redistributions of source code must retain the above copyright notice, 
 18 |      this list of conditions and the following disclaimer.
 19 |    * Redistributions in binary form must reproduce the above copyright notice, 
 20 |      this list of conditions and the following disclaimer in the documentation 
 21 |      and/or other materials provided with the distribution.
 22 | 
 23 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 24 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 25 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 26 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 27 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
 28 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 29 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 30 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 31 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 32 |   POSSIBILITY OF SUCH DAMAGE.
 33 | 
34 | */ 35 | #include "mpValueCache.h" 36 | 37 | #include "mpValue.h" 38 | 39 | 40 | MUP_NAMESPACE_START 41 | 42 | //------------------------------------------------------------------------------ 43 | ValueCache::ValueCache(int size) 44 | :m_nIdx(-1) 45 | ,m_vCache(size, (mup::Value*)0) // hint to myself: don't use nullptr gcc will go postal... 46 | {} 47 | 48 | //------------------------------------------------------------------------------ 49 | ValueCache::~ValueCache() 50 | { 51 | ReleaseAll(); 52 | } 53 | 54 | //------------------------------------------------------------------------------ 55 | void ValueCache::ReleaseAll() 56 | { 57 | for (std::size_t i=0; iGetRef()==0); 74 | 75 | // Add the value to the cache if the cache has room for it 76 | // otherwise release the value item instantly 77 | if ( m_nIdx < ((int)m_vCache.size()-1) ) 78 | { 79 | m_nIdx++; 80 | m_vCache[m_nIdx] = pValue; 81 | } 82 | else 83 | delete pValue; 84 | } 85 | 86 | //------------------------------------------------------------------------------ 87 | Value* ValueCache::CreateFromCache() 88 | { 89 | Value *pValue = nullptr; 90 | if (m_nIdx>=0) 91 | { 92 | pValue = m_vCache[m_nIdx]; 93 | m_vCache[m_nIdx] = nullptr; 94 | m_nIdx--; 95 | } 96 | else 97 | { 98 | pValue = new Value(); 99 | pValue->BindToCache(this); 100 | } 101 | 102 | return pValue; 103 | } 104 | 105 | MUP_NAMESPACE_END 106 | -------------------------------------------------------------------------------- /parser/mpIfThenElse.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023 Ingo Berg 9 | All rights reserved. 10 | 11 | muParserX - A C++ math parser library with array and string support 12 | Copyright (C) 2023, Ingo Berg 13 | All rights reserved. 14 | 15 | Redistribution and use in source and binary forms, with or without 16 | modification, are permitted provided that the following conditions are met: 17 | 18 | * Redistributions of source code must retain the above copyright notice, 19 | this list of conditions and the following disclaimer. 20 | * Redistributions in binary form must reproduce the above copyright notice, 21 | this list of conditions and the following disclaimer in the documentation 22 | and/or other materials provided with the distribution. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 25 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 28 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | #include "mpIfThenElse.h" 36 | 37 | MUP_NAMESPACE_START 38 | 39 | //--------------------------------------------------------------------------- 40 | // 41 | // If part of if-then-else clauses 42 | // 43 | //--------------------------------------------------------------------------- 44 | 45 | TokenIfThenElse::TokenIfThenElse(ECmdCode eCode) 46 | :IToken(eCode, g_sCmdCode[ eCode ]) 47 | ,IPrecedence() 48 | ,m_nOffset() 49 | {} 50 | 51 | //--------------------------------------------------------------------------- 52 | IToken* TokenIfThenElse::Clone() const 53 | { 54 | return new TokenIfThenElse(*this); 55 | } 56 | 57 | //--------------------------------------------------------------------------- 58 | void TokenIfThenElse::SetOffset(int nOffset) 59 | { 60 | m_nOffset = nOffset; 61 | } 62 | 63 | //--------------------------------------------------------------------------- 64 | int TokenIfThenElse::GetOffset() const 65 | { 66 | return m_nOffset; 67 | } 68 | 69 | //--------------------------------------------------------------------------- 70 | string_type TokenIfThenElse::AsciiDump() const 71 | { 72 | stringstream_type ss; 73 | 74 | ss << GetIdent(); 75 | ss << _T(" [addr=0x") << std::hex << this << std::dec; 76 | ss << _T("; pos=") << GetExprPos(); 77 | ss << _T("; offset=") << m_nOffset; 78 | ss << _T("]"); 79 | return ss.str(); 80 | } 81 | 82 | //--------------------------------------------------------------------------- 83 | int TokenIfThenElse::GetPri() const 84 | { 85 | return (int)prIF_THEN_ELSE; 86 | } 87 | 88 | //--------------------------------------------------------------------------- 89 | EOprtAsct TokenIfThenElse::GetAssociativity() const 90 | { 91 | return oaNONE; 92 | } 93 | 94 | //--------------------------------------------------------------------------- 95 | IPrecedence* TokenIfThenElse::AsIPrecedence() 96 | { 97 | return this; 98 | } 99 | 100 | MUP_NAMESPACE_END 101 | -------------------------------------------------------------------------------- /parser/mpIValReader.h: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023 Ingo Berg 9 | All rights reserved. 10 | 11 | muParserX - A C++ math parser library with array and string support 12 | Copyright (C) 2023, Ingo Berg 13 | All rights reserved. 14 | 15 | Redistribution and use in source and binary forms, with or without 16 | modification, are permitted provided that the following conditions are met: 17 | 18 | * Redistributions of source code must retain the above copyright notice, 19 | this list of conditions and the following disclaimer. 20 | * Redistributions in binary form must reproduce the above copyright notice, 21 | this list of conditions and the following disclaimer in the documentation 22 | and/or other materials provided with the distribution. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 25 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 28 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | #ifndef MU_IPARSER_VALUE_READER_H 36 | #define MU_IPARSER_VALUE_READER_H 37 | 38 | #include "mpValue.h" 39 | #include "mpIToken.h" 40 | #include "mpTokenReader.h" 41 | 42 | /** \defgroup valreader Value reader classes 43 | 44 | This group lists all classes that detect and parse values in an expression string. 45 | */ 46 | 47 | 48 | MUP_NAMESPACE_START 49 | 50 | class TokenReader; 51 | 52 | /** \brief Interface for custom value reader objects. 53 | \ingroup valreader 54 | 55 | Value readers are objects used for identifying values 56 | in an expression. 57 | */ 58 | class IValueReader 59 | { 60 | public: 61 | 62 | IValueReader(); 63 | IValueReader(const IValueReader &ref); 64 | 65 | virtual ~IValueReader(); 66 | 67 | /** \brief Check a certain position in an expression for the presence of a value. 68 | \param a_iPos [in/out] Reference to an integer value representing the current 69 | position of the parser in the expression. 70 | \param a_Val If a value is found it is stored in a_Val 71 | \return true if a value was found 72 | */ 73 | virtual bool IsValue(const char_type *a_szExpr, 74 | int &a_iPos, 75 | Value &a_Val ) = 0; 76 | 77 | /** \brief Clone this ValueReader object. 78 | \return Pointer to the cloned value reader object. 79 | */ 80 | virtual IValueReader* Clone(TokenReader *pParent) const = 0; 81 | 82 | /** \brief Assign this value reader object to a token 83 | reader object. 84 | 85 | The token reader does the tokenization of the expression. 86 | It uses this value reader to detect values. 87 | */ 88 | virtual void SetParent(TokenReader *pTokenReader); 89 | 90 | protected: 91 | 92 | const IToken* TokenHistory(std::size_t pos) const; 93 | 94 | private: 95 | 96 | TokenReader *m_pTokenReader; ///< Pointer to the TokenReader class used for token recognition 97 | }; // class IValueReader 98 | 99 | MUP_NAMESPACE_END 100 | 101 | #endif 102 | -------------------------------------------------------------------------------- /parser/mpDefines.h: -------------------------------------------------------------------------------- 1 | #ifndef MUP_DEFINES_H 2 | #define MUP_DEFINES_H 3 | 4 | /** \file 5 | \brief A file containing macros used by muParserX 6 | 7 |
  8 |                __________                                 ____  ___
  9 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
 10 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
 11 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
 12 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
 13 |         \/                     \/           \/     \/           \_/
 14 |   Copyright (C) 2023 Ingo Berg, et. al.
 15 |   All rights reserved.
 16 | 
 17 |   Redistribution and use in source and binary forms, with or without 
 18 |   modification, are permitted provided that the following conditions are met:
 19 | 
 20 |    * Redistributions of source code must retain the above copyright notice, 
 21 |      this list of conditions and the following disclaimer.
 22 |    * Redistributions in binary form must reproduce the above copyright notice, 
 23 |      this list of conditions and the following disclaimer in the documentation 
 24 |      and/or other materials provided with the distribution.
 25 | 
 26 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 27 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 28 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 29 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 30 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
 31 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 32 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 33 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 34 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 35 |   POSSIBILITY OF SUCH DAMAGE.
 36 | 
37 | */ 38 | #include 39 | 40 | #if defined(MUP_USE_WIDE_STRING) 41 | #if !defined(_T) 42 | #define _T(x) L##x 43 | #endif // not defined _T 44 | #define MUP_STRING_TYPE std::wstring 45 | #else 46 | #ifndef _T 47 | /** \brief Macro needed for the "unicodification" of strings. */ 48 | #define _T(x) x 49 | #endif 50 | 51 | /** \brief The string type used by muParserX. 52 | 53 | This macro is needed for UNICODE support. 54 | */ 55 | #define MUP_STRING_TYPE std::string 56 | #endif 57 | 58 | /** \brief A macro containing the version of muParserX. */ 59 | #define MUP_PARSER_VERSION _T("4.0.12 (2023-03-04)") 60 | 61 | /** \brief A macro for setting the parser namespace. */ 62 | #define MUP_NAMESPACE_START namespace mup { 63 | 64 | /** \brief Closing bracket for the parser namespace macro. */ 65 | #define MUP_NAMESPACE_END } 66 | 67 | /** \brief Floating point type used by the parser. */ 68 | #define MUP_FLOAT_TYPE double 69 | 70 | #define MUP_INT_TYPE int64_t 71 | 72 | /** \brief Verifies whether a given condition is met. 73 | 74 | If the condition is not met an exception is thrown otherwise nothing happens. 75 | This macro is used for implementing asserts. Unlike MUP_ASSERT, MUP_VERIFY 76 | will not be removed in release builds. 77 | */ 78 | #define MUP_VERIFY(COND) \ 79 | if (!(COND)) \ 80 | { \ 81 | stringstream_type ss; \ 82 | ss << _T("Assertion \"") _T(#COND) _T("\" failed: ") \ 83 | << __FILE__ << _T(" line ") \ 84 | << __LINE__ << _T("."); \ 85 | throw ParserError( ss.str() ); \ 86 | } 87 | 88 | #if defined(_DEBUG) 89 | #define MUP_TOK_CAST(TYPE, POINTER) dynamic_cast(POINTER); 90 | 91 | /** \brief Debug macro to force an abortion of the programm with a certain message. 92 | */ 93 | #define MUP_FAIL(MSG) \ 94 | bool MSG=false; \ 95 | assert(MSG); 96 | 97 | #define MUP_LEAKAGE_REPORT 98 | #else 99 | #define MUP_FAIL(MSG) 100 | #define MUP_TOK_CAST(TYPE, POINTER) static_cast(POINTER); 101 | #endif 102 | 103 | #endif 104 | 105 | 106 | -------------------------------------------------------------------------------- /parser/mpPackageCmplx.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #include "mpPackageCmplx.h" 32 | 33 | #include "mpParserBase.h" 34 | #include "mpFuncCmplx.h" 35 | #include "mpOprtCmplx.h" 36 | #include "mpOprtBinCommon.h" 37 | 38 | MUP_NAMESPACE_START 39 | 40 | //------------------------------------------------------------------------------ 41 | std::unique_ptr PackageCmplx::s_pInstance; 42 | 43 | //------------------------------------------------------------------------------ 44 | IPackage* PackageCmplx::Instance() 45 | { 46 | if (s_pInstance.get()==nullptr) 47 | { 48 | s_pInstance.reset(new PackageCmplx); 49 | } 50 | 51 | return s_pInstance.get(); 52 | } 53 | 54 | //------------------------------------------------------------------------------ 55 | void PackageCmplx::AddToParser(ParserXBase *pParser) 56 | { 57 | // Constants 58 | pParser->DefineConst( _T("i"), cmplx_type(0.0, 1.0) ); 59 | 60 | // Complex valued functions 61 | pParser->DefineFun(new FunCmplxReal()); 62 | pParser->DefineFun(new FunCmplxImag()); 63 | pParser->DefineFun(new FunCmplxConj()); 64 | pParser->DefineFun(new FunCmplxArg()); 65 | pParser->DefineFun(new FunCmplxNorm()); 66 | pParser->DefineFun(new FunCmplxSin()); 67 | pParser->DefineFun(new FunCmplxCos()); 68 | pParser->DefineFun(new FunCmplxTan()); 69 | pParser->DefineFun(new FunCmplxSinH()); 70 | pParser->DefineFun(new FunCmplxCosH()); 71 | pParser->DefineFun(new FunCmplxTanH()); 72 | pParser->DefineFun(new FunCmplxSqrt()); 73 | pParser->DefineFun(new FunCmplxExp()); 74 | pParser->DefineFun(new FunCmplxLn()); 75 | pParser->DefineFun(new FunCmplxLog()); 76 | pParser->DefineFun(new FunCmplxLog2()); 77 | pParser->DefineFun(new FunCmplxLog10()); 78 | pParser->DefineFun(new FunCmplxAbs()); 79 | pParser->DefineFun(new FunCmplxPow()); 80 | 81 | // Complex valued operators 82 | pParser->DefineOprt(new OprtAddCmplx()); 83 | pParser->DefineOprt(new OprtSubCmplx()); 84 | pParser->DefineOprt(new OprtMulCmplx()); 85 | pParser->DefineOprt(new OprtDivCmplx()); 86 | pParser->DefineOprt(new OprtPowCmplx()); 87 | pParser->DefineInfixOprt(new OprtSignCmplx()); 88 | } 89 | 90 | //------------------------------------------------------------------------------ 91 | string_type PackageCmplx::GetDesc() const 92 | { 93 | return _T(""); 94 | } 95 | 96 | //------------------------------------------------------------------------------ 97 | string_type PackageCmplx::GetPrefix() const 98 | { 99 | return _T(""); 100 | } 101 | 102 | MUP_NAMESPACE_END 103 | -------------------------------------------------------------------------------- /parser/mpFuncMatrix.h: -------------------------------------------------------------------------------- 1 | /** \file 2 | \brief Definition of functions for complex valued operations. 3 | 4 |
  5 |                __________                                 ____  ___
  6 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
  7 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
  8 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
  9 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
 10 |         \/                     \/           \/     \/           \_/
 11 |                                        Copyright (C) 2023, Ingo Berg
 12 |                                        All rights reserved.
 13 | 
 14 |   Redistribution and use in source and binary forms, with or without 
 15 |   modification, are permitted provided that the following conditions are met:
 16 | 
 17 |    * Redistributions of source code must retain the above copyright notice, 
 18 |      this list of conditions and the following disclaimer.
 19 |    * Redistributions in binary form must reproduce the above copyright notice, 
 20 |      this list of conditions and the following disclaimer in the documentation 
 21 |      and/or other materials provided with the distribution.
 22 | 
 23 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 24 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 25 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 26 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 27 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
 28 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 29 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 30 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 31 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 32 |   POSSIBILITY OF SUCH DAMAGE.
 33 | 
34 | */ 35 | #ifndef MUP_FUNC_MATRIX_H 36 | #define MUP_FUNC_MATRIX_H 37 | 38 | #include "mpICallback.h" 39 | 40 | 41 | MUP_NAMESPACE_START 42 | 43 | //----------------------------------------------------------------------- 44 | /** \brief Parser callback object for creating matrices consisting 45 | entirely of ones. 46 | \ingroup functions 47 | */ 48 | class FunMatrixOnes : public ICallback 49 | { 50 | public: 51 | FunMatrixOnes(); 52 | virtual ~FunMatrixOnes(); 53 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; 54 | virtual const char_type* GetDesc() const override; 55 | virtual IToken* Clone() const override; 56 | }; 57 | 58 | //----------------------------------------------------------------------- 59 | /** \brief Parser callback object for creating matrices consisting 60 | entirely of zeros. 61 | \ingroup functions 62 | */ 63 | class FunMatrixZeros : public ICallback 64 | { 65 | public: 66 | FunMatrixZeros(); 67 | virtual ~FunMatrixZeros(); 68 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; 69 | virtual const char_type* GetDesc() const override; 70 | virtual IToken* Clone() const override; 71 | }; 72 | 73 | //----------------------------------------------------------------------- 74 | /** \brief Parser callback object for creating unity matrices. 75 | \ingroup functions 76 | */ 77 | class FunMatrixEye : public ICallback 78 | { 79 | public: 80 | FunMatrixEye(); 81 | virtual ~FunMatrixEye(); 82 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; 83 | virtual const char_type* GetDesc() const override; 84 | virtual IToken* Clone() const override; 85 | }; 86 | 87 | 88 | //----------------------------------------------------------------------- 89 | /** \brief Determines the dimensions of a matrix. 90 | \ingroup functions 91 | */ 92 | class FunMatrixSize : public ICallback 93 | { 94 | public: 95 | FunMatrixSize(); 96 | virtual ~FunMatrixSize(); 97 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; 98 | virtual const char_type* GetDesc() const override; 99 | virtual IToken* Clone() const override; 100 | }; 101 | } // namespace mu 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /parser/mpOprtBinShortcut.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023 Ingo Berg, et al. 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #include "mpOprtBinShortcut.h" 32 | 33 | MUP_NAMESPACE_START 34 | //----------------------------------------------------------------------------------------------- 35 | // 36 | // class OprtShortcutLogicOrBegin 37 | // 38 | //----------------------------------------------------------------------------------------------- 39 | 40 | OprtShortcutLogicOrBegin::OprtShortcutLogicOrBegin(const char_type* szIdent) 41 | :IOprtBinShortcut(cmSHORTCUT_BEGIN, szIdent, (int)prLOGIC_OR, oaLEFT) 42 | {} 43 | 44 | 45 | //----------------------------------------------------------------------------------------------- 46 | IToken* OprtShortcutLogicOrBegin::Clone() const 47 | { 48 | return new OprtShortcutLogicOrBegin(*this); 49 | } 50 | 51 | 52 | //----------------------------------------------------------------------------------------------- 53 | // 54 | // class OprtShortcutLogicOrEnd 55 | // 56 | //----------------------------------------------------------------------------------------------- 57 | 58 | OprtShortcutLogicOrEnd::OprtShortcutLogicOrEnd(const char_type* szIdent) 59 | :IOprtBinShortcut(cmSHORTCUT_END, szIdent, (int)prLOGIC_OR, oaLEFT) 60 | {} 61 | 62 | 63 | //----------------------------------------------------------------------------------------------- 64 | IToken* OprtShortcutLogicOrEnd::Clone() const 65 | { 66 | return new OprtShortcutLogicOrEnd(*this); 67 | } 68 | 69 | //----------------------------------------------------------------------------------------------- 70 | // 71 | // class OprtShortcutLogicAndBegin 72 | // 73 | //----------------------------------------------------------------------------------------------- 74 | 75 | OprtShortcutLogicAndBegin::OprtShortcutLogicAndBegin(const char_type* szIdent) 76 | :IOprtBinShortcut(cmSHORTCUT_BEGIN, szIdent, (int)prLOGIC_AND, oaLEFT) 77 | {} 78 | 79 | //----------------------------------------------------------------------------------------------- 80 | IToken* OprtShortcutLogicAndBegin::Clone() const 81 | { 82 | return new OprtShortcutLogicAndBegin(*this); 83 | } 84 | 85 | 86 | //----------------------------------------------------------------------------------------------- 87 | // 88 | // class OprtShortcutLogicAndEnd 89 | // 90 | //----------------------------------------------------------------------------------------------- 91 | 92 | OprtShortcutLogicAndEnd::OprtShortcutLogicAndEnd(const char_type* szIdent) 93 | :IOprtBinShortcut(cmSHORTCUT_END, szIdent, (int)prLOGIC_AND, oaLEFT) 94 | {} 95 | 96 | //----------------------------------------------------------------------------------------------- 97 | IToken* OprtShortcutLogicAndEnd::Clone() const 98 | { 99 | return new OprtShortcutLogicAndEnd(*this); 100 | } 101 | 102 | } -------------------------------------------------------------------------------- /parser/mpVariable.h: -------------------------------------------------------------------------------- 1 | /** \file 2 | \brief Definition of the muParserX variable class. 3 | 4 |
  5 |                __________                                 ____  ___
  6 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
  7 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
  8 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
  9 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
 10 |         \/                     \/           \/     \/           \_/
 11 |                                        Copyright (C) 2023, Ingo Berg
 12 |                                        All rights reserved.
 13 | 
 14 |   Redistribution and use in source and binary forms, with or without 
 15 |   modification, are permitted provided that the following conditions are met:
 16 | 
 17 |    * Redistributions of source code must retain the above copyright notice, 
 18 |      this list of conditions and the following disclaimer.
 19 |    * Redistributions in binary form must reproduce the above copyright notice, 
 20 |      this list of conditions and the following disclaimer in the documentation 
 21 |      and/or other materials provided with the distribution.
 22 | 
 23 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 24 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 25 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 26 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 27 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
 28 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 29 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 30 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 31 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 32 |   POSSIBILITY OF SUCH DAMAGE.
 33 | 
34 | */ 35 | 36 | #ifndef MP_VARIABLE_H 37 | #define MP_VARIABLE_H 38 | 39 | #include "mpIValue.h" 40 | #include "mpTypes.h" 41 | 42 | 43 | MUP_NAMESPACE_START 44 | 45 | //------------------------------------------------------------------------------ 46 | /** \brief The variable class represents a parser variable. 47 | 48 | This class stores a pointer to a value object and refers all 49 | operations to this value object. 50 | */ 51 | class Variable : public IValue 52 | { 53 | public: 54 | 55 | Variable(IValue *pVal); 56 | 57 | Variable(const Variable &a_Var); 58 | Variable& operator=(const Variable &a_Var); 59 | 60 | virtual IValue& At(int nRow, int nCol); 61 | virtual IValue& At(const IValue &nRows, const IValue &nCols); 62 | 63 | virtual IValue& operator=(const Value &val); 64 | virtual IValue& operator=(const matrix_type &val); 65 | virtual IValue& operator=(const cmplx_type &val); 66 | virtual IValue& operator=(int_type val); 67 | virtual IValue& operator=(float_type val); 68 | virtual IValue& operator=(string_type val); 69 | virtual IValue& operator=(bool_type val); 70 | virtual IValue& operator+=(const IValue &ref); 71 | virtual IValue& operator-=(const IValue &ref); 72 | virtual IValue& operator*=(const IValue &val); 73 | 74 | virtual ~Variable(); 75 | 76 | virtual char_type GetType() const; 77 | 78 | virtual int_type GetInteger() const; 79 | virtual float_type GetFloat() const; 80 | virtual float_type GetImag() const; 81 | virtual bool GetBool() const; 82 | virtual const cmplx_type& GetComplex() const; 83 | virtual const string_type& GetString() const; 84 | virtual const matrix_type& GetArray() const; 85 | virtual int GetRows() const; 86 | virtual int GetCols() const; 87 | 88 | virtual bool IsVariable() const; 89 | virtual IToken* Clone() const; 90 | virtual Value* AsValue(); 91 | 92 | void SetFloat(float_type a_fVal); 93 | void SetString(const string_type &a_sVal); 94 | void SetBool(bool a_bVal); 95 | 96 | void Bind(IValue *pValue); 97 | 98 | IValue* GetPtr() const; 99 | string_type AsciiDump() const; 100 | 101 | private: 102 | 103 | IValue *m_pVal; ///< Pointer to the value object bound to this variable 104 | 105 | void Assign(const Variable &a_Var); 106 | void CheckType(char_type a_cType) const; 107 | }; // class Variable 108 | 109 | MUP_NAMESPACE_END 110 | 111 | #endif 112 | 113 | 114 | -------------------------------------------------------------------------------- /parser/mpPackageNonCmplx.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #include "mpPackageNonCmplx.h" 32 | 33 | #include "mpParserBase.h" 34 | #include "mpFuncNonCmplx.h" 35 | #include "mpOprtNonCmplx.h" 36 | #include "mpOprtBinCommon.h" 37 | 38 | 39 | MUP_NAMESPACE_START 40 | 41 | //------------------------------------------------------------------------------ 42 | std::unique_ptr PackageNonCmplx::s_pInstance; 43 | 44 | //------------------------------------------------------------------------------ 45 | IPackage* PackageNonCmplx::Instance() 46 | { 47 | if (s_pInstance.get()==nullptr) 48 | { 49 | s_pInstance.reset(new PackageNonCmplx); 50 | } 51 | 52 | return s_pInstance.get(); 53 | } 54 | 55 | //------------------------------------------------------------------------------ 56 | void PackageNonCmplx::AddToParser(ParserXBase *pParser) 57 | { 58 | pParser->DefineFun(new FunSin()); 59 | pParser->DefineFun(new FunCos()); 60 | pParser->DefineFun(new FunTan()); 61 | pParser->DefineFun(new FunSinH()); 62 | pParser->DefineFun(new FunCosH()); 63 | pParser->DefineFun(new FunTanH()); 64 | pParser->DefineFun(new FunASin()); 65 | pParser->DefineFun(new FunACos()); 66 | pParser->DefineFun(new FunATan()); 67 | pParser->DefineFun(new FunASinH()); 68 | pParser->DefineFun(new FunACosH()); 69 | pParser->DefineFun(new FunATanH()); 70 | pParser->DefineFun(new FunLog()); 71 | pParser->DefineFun(new FunLog10()); 72 | pParser->DefineFun(new FunLog2()); 73 | pParser->DefineFun(new FunLn()); 74 | pParser->DefineFun(new FunExp()); 75 | pParser->DefineFun(new FunSqrt()); 76 | pParser->DefineFun(new FunCbrt()); 77 | pParser->DefineFun(new FunAbs()); 78 | 79 | // binary functions 80 | pParser->DefineFun(new FunPow()); 81 | pParser->DefineFun(new FunHypot()); 82 | pParser->DefineFun(new FunAtan2()); 83 | pParser->DefineFun(new FunFmod()); 84 | pParser->DefineFun(new FunRemainder()); 85 | 86 | // Operator callbacks 87 | pParser->DefineInfixOprt(new OprtSign()); 88 | pParser->DefineInfixOprt(new OprtSignPos()); 89 | pParser->DefineOprt(new OprtAdd()); 90 | pParser->DefineOprt(new OprtSub()); 91 | pParser->DefineOprt(new OprtMul()); 92 | pParser->DefineOprt(new OprtDiv()); 93 | pParser->DefineOprt(new OprtPow); 94 | } 95 | 96 | //------------------------------------------------------------------------------ 97 | string_type PackageNonCmplx::GetDesc() const 98 | { 99 | return _T(""); 100 | } 101 | 102 | //------------------------------------------------------------------------------ 103 | string_type PackageNonCmplx::GetPrefix() const 104 | { 105 | return _T(""); 106 | } 107 | 108 | MUP_NAMESPACE_END 109 | -------------------------------------------------------------------------------- /parser/mpIOprt.h: -------------------------------------------------------------------------------- 1 | /** \file mpIOprt.h 2 | \brief Definition of base classes needed for parser operator definitions. 3 | 4 |
  5 |                __________                                 ____  ___
  6 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
  7 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
  8 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
  9 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
 10 |         \/                     \/           \/     \/           \_/
 11 |                                        Copyright (C) 2023 Ingo Berg
 12 |                                        All rights reserved.
 13 | 
 14 |   muParserX - A C++ math parser library with array and string support
 15 |   Copyright (C) 2023, Ingo Berg
 16 |   All rights reserved.
 17 | 
 18 |   Redistribution and use in source and binary forms, with or without 
 19 |   modification, are permitted provided that the following conditions are met:
 20 | 
 21 |    * Redistributions of source code must retain the above copyright notice, 
 22 |      this list of conditions and the following disclaimer.
 23 |    * Redistributions in binary form must reproduce the above copyright notice, 
 24 |      this list of conditions and the following disclaimer in the documentation 
 25 |      and/or other materials provided with the distribution.
 26 | 
 27 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 28 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 29 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 30 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 31 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
 32 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 33 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 34 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 35 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 36 |   POSSIBILITY OF SUCH DAMAGE.
 37 | 
38 | */ 39 | #ifndef MUP_IPARSER_OPERATOR_H 40 | #define MUP_IPARSER_OPERATOR_H 41 | 42 | #include "mpICallback.h" 43 | #include "mpIPrecedence.h" 44 | 45 | 46 | MUP_NAMESPACE_START 47 | 48 | //------------------------------------------------------------------------------ 49 | /** \brief Interface for binary operators. 50 | \ingroup binop 51 | 52 | All classes representing binary operator callbacks must be derived from 53 | this base class. 54 | */ 55 | class IOprtBin : public ICallback, 56 | public IPrecedence 57 | { 58 | public: 59 | 60 | IOprtBin(const char_type *a_szIdent, int nPrec, EOprtAsct eAsc); 61 | virtual ~IOprtBin(); 62 | virtual string_type AsciiDump() const; 63 | 64 | //------------------------------------------ 65 | // IPrecedence implementation 66 | //------------------------------------------ 67 | 68 | virtual IPrecedence* AsIPrecedence(); 69 | virtual EOprtAsct GetAssociativity() const; 70 | virtual int GetPri() const; 71 | 72 | private: 73 | int m_nPrec; 74 | EOprtAsct m_eAsc; 75 | }; // class IOperator 76 | 77 | 78 | //------------------------------------------------------------------------------ 79 | /** \brief Interface for unary postfix operators. 80 | \ingroup postfix 81 | */ 82 | class IOprtPostfix : public ICallback 83 | { 84 | public: 85 | IOprtPostfix(const char_type *a_szIdent); 86 | virtual ~IOprtPostfix(); 87 | virtual string_type AsciiDump() const; 88 | }; // class IOperator 89 | 90 | 91 | //------------------------------------------------------------------------------ 92 | /** \brief Interface for unary infix operators. 93 | \ingroup infix 94 | */ 95 | class IOprtInfix : public ICallback, 96 | public IPrecedence 97 | { 98 | public: 99 | IOprtInfix(const char_type *a_szIdent, int nPrec); 100 | virtual ~IOprtInfix(); 101 | virtual string_type AsciiDump() const; 102 | 103 | //------------------------------------------ 104 | // IPrecedence implementation 105 | //------------------------------------------ 106 | 107 | virtual IPrecedence* AsIPrecedence(); 108 | virtual int GetPri() const; 109 | virtual EOprtAsct GetAssociativity() const; 110 | 111 | private: 112 | int m_nPrec; 113 | }; // class IOperator 114 | } // namespace mu 115 | 116 | #endif 117 | 118 | -------------------------------------------------------------------------------- /parser/mpFuncNonCmplx.h: -------------------------------------------------------------------------------- 1 | /** \file 2 | \brief Implementation of basic functions used by muParserX. 3 | 4 |
  5 |                __________                                 ____  ___
  6 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
  7 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
  8 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
  9 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
 10 |         \/                     \/           \/     \/           \_/
 11 |                                        Copyright (C) 2023, Ingo Berg
 12 |                                        All rights reserved.
 13 | 
 14 |   Redistribution and use in source and binary forms, with or without 
 15 |   modification, are permitted provided that the following conditions are met:
 16 | 
 17 |    * Redistributions of source code must retain the above copyright notice, 
 18 |      this list of conditions and the following disclaimer.
 19 |    * Redistributions in binary form must reproduce the above copyright notice, 
 20 |      this list of conditions and the following disclaimer in the documentation 
 21 |      and/or other materials provided with the distribution.
 22 | 
 23 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 24 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 25 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 26 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 27 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
 28 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 29 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 30 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 31 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 32 |   POSSIBILITY OF SUCH DAMAGE.
 33 |   
34 | */ 35 | #ifndef MUP_FUNC_NON_CMPLX_H 36 | #define MUP_FUNC_NON_CMPLX_H 37 | 38 | #include "mpICallback.h" 39 | 40 | /** \defgroup functions Function callback objects. 41 | 42 | This group lists the objects representing parser functions. 43 | */ 44 | 45 | 46 | MUP_NAMESPACE_START 47 | 48 | #define MUP_UNARY_FUNC_DEF(CLASS) \ 49 | class CLASS : public ICallback \ 50 | { \ 51 | public: \ 52 | CLASS(); \ 53 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; \ 54 | virtual const char_type* GetDesc() const override; \ 55 | virtual IToken* Clone() const override; \ 56 | }; 57 | 58 | MUP_UNARY_FUNC_DEF(FunSin) 59 | MUP_UNARY_FUNC_DEF(FunCos) 60 | MUP_UNARY_FUNC_DEF(FunTan) 61 | // arcus functions 62 | MUP_UNARY_FUNC_DEF(FunASin) 63 | MUP_UNARY_FUNC_DEF(FunACos) 64 | MUP_UNARY_FUNC_DEF(FunATan) 65 | // hyperbolic functions 66 | MUP_UNARY_FUNC_DEF(FunSinH) 67 | MUP_UNARY_FUNC_DEF(FunCosH) 68 | MUP_UNARY_FUNC_DEF(FunTanH) 69 | // hyperbolic arcus functions 70 | MUP_UNARY_FUNC_DEF(FunASinH) 71 | MUP_UNARY_FUNC_DEF(FunACosH) 72 | MUP_UNARY_FUNC_DEF(FunATanH) 73 | // logarithm functions 74 | MUP_UNARY_FUNC_DEF(FunLog) 75 | MUP_UNARY_FUNC_DEF(FunLog10) 76 | MUP_UNARY_FUNC_DEF(FunLog2) 77 | MUP_UNARY_FUNC_DEF(FunLn) 78 | // square root 79 | MUP_UNARY_FUNC_DEF(FunSqrt) 80 | MUP_UNARY_FUNC_DEF(FunCbrt) 81 | MUP_UNARY_FUNC_DEF(FunExp) 82 | MUP_UNARY_FUNC_DEF(FunAbs) 83 | #undef MUP_UNARY_FUNC_DEF 84 | 85 | #define MUP_BINARY_FUNC_DEF(CLASS) \ 86 | class CLASS : public ICallback \ 87 | { \ 88 | public: \ 89 | CLASS(); \ 90 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; \ 91 | virtual const char_type* GetDesc() const override; \ 92 | virtual IToken* Clone() const override; \ 93 | }; 94 | 95 | MUP_BINARY_FUNC_DEF(FunPow) 96 | MUP_BINARY_FUNC_DEF(FunHypot) 97 | MUP_BINARY_FUNC_DEF(FunAtan2) 98 | MUP_BINARY_FUNC_DEF(FunFmod) 99 | MUP_BINARY_FUNC_DEF(FunRemainder) 100 | #undef MUP_BINARY_FUNC_DEF 101 | 102 | } // namespace mu 103 | 104 | #endif 105 | -------------------------------------------------------------------------------- /parser/mpFuncCommon.h: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023 Ingo Berg 9 | All rights reserved. 10 | 11 | muParserX - A C++ math parser library with array and string support 12 | Copyright (C) 2023, Ingo Berg 13 | All rights reserved. 14 | 15 | Redistribution and use in source and binary forms, with or without 16 | modification, are permitted provided that the following conditions are met: 17 | 18 | * Redistributions of source code must retain the above copyright notice, 19 | this list of conditions and the following disclaimer. 20 | * Redistributions in binary form must reproduce the above copyright notice, 21 | this list of conditions and the following disclaimer in the documentation 22 | and/or other materials provided with the distribution. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 25 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 28 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | #ifndef MUP_FUNC_COMMON_H 36 | #define MUP_FUNC_COMMON_H 37 | 38 | #include "mpICallback.h" 39 | 40 | 41 | MUP_NAMESPACE_START 42 | 43 | //------------------------------------------------------------------------------ 44 | /** \brief Parser function callback for determining the size of an array. 45 | \ingroup functions 46 | */ 47 | class FunParserID : public ICallback 48 | { 49 | public: 50 | FunParserID (); 51 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; 52 | virtual const char_type* GetDesc() const override; 53 | virtual IToken* Clone() const override; 54 | }; // class FunParserID 55 | 56 | //------------------------------------------------------------------------------ 57 | /** \brief Determine maximal value from the parameter list. 58 | \ingroup functions 59 | */ 60 | class FunMax : public ICallback 61 | { 62 | public: 63 | FunMax(); 64 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; 65 | virtual const char_type* GetDesc() const override; 66 | virtual IToken* Clone() const override; 67 | }; // class FunMax 68 | 69 | //------------------------------------------------------------------------------ 70 | /** \brief Determine minimal value from the parameter list. 71 | \ingroup functions 72 | */ 73 | class FunMin : public ICallback 74 | { 75 | public: 76 | FunMin(); 77 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; 78 | virtual const char_type* GetDesc() const override; 79 | virtual IToken* Clone() const override; 80 | }; // class FunMin 81 | 82 | //------------------------------------------------------------------------------ 83 | /** \brief Parser callback for summing up all function arguments. 84 | \ingroup functions 85 | */ 86 | class FunSum : public ICallback 87 | { 88 | public: 89 | FunSum(); 90 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; 91 | virtual const char_type* GetDesc() const override; 92 | virtual IToken* Clone() const override; 93 | }; // class FunSum 94 | 95 | //------------------------------------------------------------------------------ 96 | /** \brief Parser function callback for determining the size of an array. 97 | \ingroup functions 98 | */ 99 | class FunSizeOf : public ICallback 100 | { 101 | public: 102 | FunSizeOf(); 103 | virtual ~FunSizeOf(); 104 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; 105 | virtual const char_type* GetDesc() const override; 106 | virtual IToken* Clone() const override; 107 | }; // class FunSizeOf 108 | 109 | MUP_NAMESPACE_END 110 | 111 | #endif 112 | -------------------------------------------------------------------------------- /parser/mpOprtBinAssign.h: -------------------------------------------------------------------------------- 1 | /** \file 2 | \brief This file contains the definition of binary assignment 3 | operators used in muParser. 4 | 5 |
  6 |                __________                                 ____  ___
  7 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
  8 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
  9 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
 10 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
 11 |         \/                     \/           \/     \/           \_/
 12 |                                        Copyright (C) 2023, Ingo Berg
 13 |                                        All rights reserved.
 14 | 
 15 |   Redistribution and use in source and binary forms, with or without 
 16 |   modification, are permitted provided that the following conditions are met:
 17 | 
 18 |    * Redistributions of source code must retain the above copyright notice, 
 19 |      this list of conditions and the following disclaimer.
 20 |    * Redistributions in binary form must reproduce the above copyright notice, 
 21 |      this list of conditions and the following disclaimer in the documentation 
 22 |      and/or other materials provided with the distribution.
 23 | 
 24 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 25 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 26 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 27 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 28 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
 29 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 30 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 31 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 32 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 33 |   POSSIBILITY OF SUCH DAMAGE.
 34 | 
35 | */ 36 | #ifndef MUP_OPRT_BIN_ASSIGN_H 37 | #define MUP_OPRT_BIN_ASSIGN_H 38 | 39 | //--- Standard includes ---------------------------------------------------------- 40 | #include 41 | 42 | //--- muParserX framework -------------------------------------------------------- 43 | #include "mpIOprt.h" 44 | #include "mpValue.h" 45 | #include "mpVariable.h" 46 | #include "mpError.h" 47 | 48 | 49 | MUP_NAMESPACE_START 50 | 51 | //------------------------------------------------------------------------------ 52 | /** \brief Assignement operator. 53 | 54 | This operator can only be applied to variable items. 55 | */ 56 | class OprtAssign : public IOprtBin 57 | { 58 | public: 59 | 60 | OprtAssign(); 61 | 62 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int) override; 63 | virtual const char_type* GetDesc() const override; 64 | virtual IToken* Clone() const override; 65 | }; 66 | 67 | //------------------------------------------------------------------------------ 68 | /** \brief Assignement operator. 69 | 70 | This operator can only be applied to variable items. 71 | */ 72 | class OprtAssignAdd : public IOprtBin 73 | { 74 | public: 75 | OprtAssignAdd(); 76 | virtual void Eval(ptr_val_type& ret, const ptr_val_type *a_pArg, int) override; 77 | virtual const char_type* GetDesc() const override; 78 | virtual IToken* Clone() const override; 79 | }; 80 | 81 | //------------------------------------------------------------------------------ 82 | /** \brief Assignement operator. 83 | 84 | This operator can only be applied to variable items. 85 | */ 86 | class OprtAssignSub : public IOprtBin 87 | { 88 | public: 89 | OprtAssignSub(); 90 | virtual void Eval(ptr_val_type& ret, const ptr_val_type *a_pArg, int) override; 91 | virtual const char_type* GetDesc() const override; 92 | virtual IToken* Clone() const override; 93 | }; 94 | 95 | //------------------------------------------------------------------------------ 96 | /** \brief Assignement operator. 97 | 98 | This operator can only be applied to variable items. 99 | */ 100 | class OprtAssignMul : public IOprtBin 101 | { 102 | public: 103 | OprtAssignMul(); 104 | virtual void Eval(ptr_val_type& ret, const ptr_val_type *a_pArg, int) override; 105 | virtual const char_type* GetDesc() const override; 106 | virtual IToken* Clone() const override; 107 | }; 108 | 109 | //------------------------------------------------------------------------------ 110 | /** \brief Assignement operator. 111 | 112 | This operator can only be applied to variable items. 113 | */ 114 | class OprtAssignDiv : public IOprtBin 115 | { 116 | public: 117 | 118 | OprtAssignDiv(); 119 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int) override; 120 | virtual const char_type* GetDesc() const override; 121 | virtual IToken* Clone() const override; 122 | }; 123 | 124 | MUP_NAMESPACE_END 125 | 126 | #endif 127 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ######################################################################## 2 | # Project setup 3 | ######################################################################## 4 | cmake_minimum_required(VERSION 3.17) 5 | project(muparserx CXX) 6 | 7 | ######################################################################## 8 | # Extract version 9 | ######################################################################## 10 | set(MUPARSERX_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/parser) 11 | file(READ "${MUPARSERX_SOURCE_DIR}/mpDefines.h" mpDefines_h) 12 | string(REGEX MATCH "\\#define MUP_PARSER_VERSION _T\\(\"([0-9]+\\.[0-9]+\\.[0-9]+) \\(" MUPARSERX_VERSION_MATCHES "${mpDefines_h}") 13 | if(NOT MUPARSERX_VERSION_MATCHES) 14 | message(FATAL_ERROR "Failed to extract version number from mpDefines.h") 15 | endif(NOT MUPARSERX_VERSION_MATCHES) 16 | set(MUPARSERX_VERSION ${CMAKE_MATCH_1}) 17 | 18 | ######################################################################## 19 | # Compiler specific flags 20 | ######################################################################## 21 | if(CMAKE_COMPILER_IS_GNUCXX OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) 22 | set(CMAKE_CXX_FLAGS_DEBUG "-D_DEBUG -g3 -gdwarf-3") 23 | set(CMAKE_CXX_FLAGS_COVERAGE "-D_DEBUG -g3 -gdwarf-3 --coverage") 24 | 25 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic") 26 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") 27 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra") 28 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17") 29 | 30 | endif(CMAKE_COMPILER_IS_GNUCXX OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) 31 | 32 | if(MSVC) 33 | add_compile_options(/MP) #multi-core build 34 | add_compile_options(/std:c++17) 35 | endif(MSVC) 36 | 37 | ######################################################################## 38 | # Build library 39 | # Defaults to static, set BUILD_SHARED_LIBS=ON for shared 40 | ######################################################################## 41 | file(GLOB MUPARSERX_SOURCES "${MUPARSERX_SOURCE_DIR}/*.cpp") 42 | include_directories(${MUPARSERX_SOURCE_DIR}) 43 | add_library(muparserx ${MUPARSERX_SOURCES}) 44 | set_target_properties(muparserx PROPERTIES VERSION ${MUPARSERX_VERSION}) 45 | set_property(TARGET muparserx PROPERTY POSITION_INDEPENDENT_CODE TRUE) 46 | set_target_properties(muparserx PROPERTIES SOVERSION ${MUPARSERX_VERSION}) 47 | set_target_properties(muparserx PROPERTIES VERSION ${MUPARSERX_VERSION}) 48 | 49 | #link with lib math when found 50 | find_library( 51 | M_LIBRARY NAMES m 52 | PATHS /usr/lib /usr/lib64 53 | ) 54 | if(M_LIBRARY) 55 | target_link_libraries(muparserx ${M_LIBRARY}) 56 | endif(M_LIBRARY) 57 | 58 | install(TARGETS muparserx 59 | LIBRARY DESTINATION lib${LIB_SUFFIX} # .so file 60 | ARCHIVE DESTINATION lib${LIB_SUFFIX} # .lib file 61 | RUNTIME DESTINATION bin # .dll file 62 | ) 63 | 64 | ######################################################################## 65 | # Build pkg config file 66 | ######################################################################## 67 | configure_file( 68 | ${CMAKE_CURRENT_SOURCE_DIR}/muparserx.in.pc 69 | ${CMAKE_CURRENT_BINARY_DIR}/muparserx.pc 70 | @ONLY) 71 | 72 | install( 73 | FILES ${CMAKE_CURRENT_BINARY_DIR}/muparserx.pc 74 | DESTINATION lib${LIB_SUFFIX}/pkgconfig 75 | ) 76 | 77 | ######################################################################## 78 | # Install project config 79 | ######################################################################## 80 | configure_file( 81 | ${PROJECT_SOURCE_DIR}/cmake/muparserxConfigVersion.in.cmake 82 | ${PROJECT_BINARY_DIR}/muparserxConfigVersion.cmake 83 | @ONLY) 84 | set(cmake_files 85 | ${PROJECT_SOURCE_DIR}/cmake/muparserxConfig.cmake 86 | ${PROJECT_BINARY_DIR}/muparserxConfigVersion.cmake) 87 | if (UNIX) 88 | install(FILES ${cmake_files} DESTINATION share/cmake/muparserx) 89 | elseif (WIN32) 90 | install(FILES ${cmake_files} DESTINATION cmake) 91 | endif () 92 | 93 | ######################################################################## 94 | # Install headers 95 | ######################################################################## 96 | file(GLOB MUPARSERX_HEADERS "${MUPARSERX_SOURCE_DIR}/*.h") 97 | install( 98 | FILES ${MUPARSERX_HEADERS} 99 | DESTINATION include/muparserx 100 | ) 101 | 102 | ######################################################################## 103 | # Options 104 | ######################################################################## 105 | 106 | option(BUILD_EXAMPLES "enable building example applications" ON) 107 | if(BUILD_EXAMPLES) 108 | add_executable(example sample/example.cpp sample/timer.cpp) 109 | target_link_libraries(example muparserx) 110 | endif(BUILD_EXAMPLES) 111 | 112 | option(USE_WIDE_STRING "use UNICODE characters" OFF) 113 | if(USE_WIDE_STRING) 114 | add_compile_definitions(MUP_USE_WIDE_STRING) 115 | endif(USE_WIDE_STRING) 116 | 117 | ######################################################################## 118 | # Print summary 119 | ######################################################################## 120 | 121 | message(STATUS "Building muparserx version: ${MUPARSERX_VERSION}") 122 | message(STATUS "Using install prefix: ${CMAKE_INSTALL_PREFIX}") 123 | -------------------------------------------------------------------------------- /parser/mpOprtIndex.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #include "mpOprtIndex.h" 32 | #include "mpVariable.h" 33 | 34 | MUP_NAMESPACE_START 35 | 36 | //----------------------------------------------------------------------------------------------- 37 | // 38 | // class OprtIndex 39 | // 40 | //----------------------------------------------------------------------------------------------- 41 | 42 | OprtIndex::OprtIndex() 43 | :ICallback(cmIC, _T("Index operator"), -1) 44 | {} 45 | 46 | //----------------------------------------------------------------------------------------------- 47 | /** \brief Index operator implementation 48 | \param ret A reference to the return value 49 | \param a_pArg Pointer to an array with the indices as ptr_val_type 50 | \param a_iArgc Number of indices (=dimension) actully used in the expression found. This must 51 | be 1 or 2 since three dimensional data structures are not supported by muParserX. 52 | */ 53 | void OprtIndex::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) 54 | { 55 | try 56 | { 57 | int rows = a_pArg[-1]->GetRows(); 58 | int cols = a_pArg[-1]->GetCols(); 59 | bool bArgIsVariable = a_pArg[-1]->IsVariable(); 60 | 61 | // If the index operator is applied to a variable the return value is also a variable 62 | // pointing to a specific cell in the matrix. If the operator is applied to a value 63 | // the return value is also a value. 64 | switch (a_iArgc) 65 | { 66 | case 1: 67 | if (cols == 1) 68 | { 69 | if (bArgIsVariable) 70 | ret.Reset(new Variable(&(ret->At(*a_pArg[0], Value(0.0))))); 71 | else 72 | *ret = ret->At(*a_pArg[0], Value(0.0)); 73 | } 74 | else if (rows == 1) 75 | { 76 | if (bArgIsVariable) 77 | ret.Reset(new Variable(&(ret->At(Value(0.0), *a_pArg[0])))); 78 | else 79 | *ret = ret->At(Value(0.0), *a_pArg[0]); 80 | } 81 | else 82 | { 83 | throw ParserError(ErrorContext(ecINDEX_DIMENSION, -1, GetIdent())); 84 | } 85 | break; 86 | 87 | case 2: 88 | if (bArgIsVariable) 89 | ret.Reset(new Variable(&(ret->At(*a_pArg[0], *a_pArg[1])))); 90 | else 91 | *ret = ret->At(*a_pArg[0], *a_pArg[1]); 92 | break; 93 | 94 | default: 95 | throw ParserError(ErrorContext(ecINDEX_DIMENSION, -1, GetIdent())); 96 | } 97 | } 98 | catch(ParserError &exc) 99 | { 100 | exc.GetContext().Pos = GetExprPos(); 101 | throw exc; 102 | } 103 | } 104 | 105 | //----------------------------------------------------------------------------------------------- 106 | const char_type* OprtIndex::GetDesc() const 107 | { 108 | return _T("[,] - The index operator."); 109 | } 110 | 111 | //----------------------------------------------------------------------------------------------- 112 | IToken* OprtIndex::Clone() const 113 | { 114 | return new OprtIndex(*this); 115 | } 116 | 117 | MUP_NAMESPACE_END 118 | -------------------------------------------------------------------------------- /parser/mpOprtCmplx.h: -------------------------------------------------------------------------------- 1 | /* 2 | __________ ____ ___ 3 | _____ __ _\______ \_____ _______ ______ __________\ \/ / 4 | / \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ / 5 | | Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \ 6 | |__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \ 7 | \/ \/ \/ \/ \_/ 8 | Copyright (C) 2023, Ingo Berg 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | * Redistributions of source code must retain the above copyright notice, 15 | this list of conditions and the following disclaimer. 16 | * Redistributions in binary form must reproduce the above copyright notice, 17 | this list of conditions and the following disclaimer in the documentation 18 | and/or other materials provided with the distribution. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #ifndef MP_OPRT_CMPLX_H 32 | #define MP_OPRT_CMPLX_H 33 | 34 | /** \file 35 | \brief Definitions of classes used as callbacks for standard binary operators. 36 | */ 37 | 38 | /** \defgroup binop Binary operator callbacks 39 | 40 | This group lists the objects representing the binary operators of muParserX. 41 | */ 42 | 43 | #include 44 | #include "mpIOprt.h" 45 | #include "mpValue.h" 46 | #include "mpError.h" 47 | 48 | 49 | MUP_NAMESPACE_START 50 | 51 | //--------------------------------------------------------------------------- 52 | /** \brief Callback for the negative sign operator. 53 | \ingroup infix 54 | */ 55 | class OprtSignCmplx : public IOprtInfix 56 | { 57 | public: 58 | OprtSignCmplx(); 59 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; 60 | virtual const char_type* GetDesc() const override; 61 | virtual IToken* Clone() const override; 62 | }; // class OprtSignCmplx 63 | 64 | //------------------------------------------------------------------------------ 65 | /** \brief Parser callback for implementing an addition of two complex values. 66 | \ingroup binop 67 | */ 68 | class OprtAddCmplx : public IOprtBin 69 | { 70 | public: 71 | OprtAddCmplx(); 72 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int) override; 73 | virtual const char_type* GetDesc() const override; 74 | virtual IToken* Clone() const override; 75 | }; 76 | 77 | //------------------------------------------------------------------------------ 78 | /** \brief Parser callback for implementing the subtraction of two complex values. 79 | \ingroup binop 80 | */ 81 | class OprtSubCmplx : public IOprtBin 82 | { 83 | public: 84 | OprtSubCmplx(); 85 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int) override; 86 | virtual const char_type* GetDesc() const override; 87 | virtual IToken* Clone() const override; 88 | }; 89 | 90 | //------------------------------------------------------------------------------ 91 | /** \brief Callback object for implementing the multiplications of complex values. 92 | \ingroup binop 93 | */ 94 | class OprtMulCmplx : public IOprtBin 95 | { 96 | public: 97 | OprtMulCmplx(); 98 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int) override; 99 | virtual const char_type* GetDesc() const override; 100 | virtual IToken* Clone() const override; 101 | }; 102 | 103 | //------------------------------------------------------------------------------ 104 | /** \brief Callback object for implementing the division of complex values. 105 | \ingroup binop 106 | */ 107 | class OprtDivCmplx : public IOprtBin 108 | { 109 | public: 110 | OprtDivCmplx(); 111 | virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int) override; 112 | virtual const char_type* GetDesc() const override; 113 | virtual IToken* Clone() const override; 114 | }; 115 | 116 | //------------------------------------------------------------------------------ 117 | /** \brief Raise x to the power of y. 118 | \ingroup binop 119 | */ 120 | class OprtPowCmplx : public IOprtBin 121 | { 122 | public: 123 | OprtPowCmplx(); 124 | virtual void Eval(ptr_val_type& ret, const ptr_val_type *arg, int argc) override; 125 | virtual const char_type* GetDesc() const override; 126 | virtual IToken* Clone() const override; 127 | }; 128 | } // namespace mu 129 | 130 | #endif 131 | -------------------------------------------------------------------------------- /parser/mpICallback.cpp: -------------------------------------------------------------------------------- 1 | /** \file 2 | \brief Implementation of the interface for parser callback objects. 3 | 4 |
  5 |                __________                                 ____  ___
  6 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
  7 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
  8 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
  9 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
 10 |         \/                     \/           \/     \/           \_/
 11 |                                        Copyright (C) 2023 Ingo Berg
 12 |                                        All rights reserved.
 13 | 
 14 |   muParserX - A C++ math parser library with array and string support
 15 |   Copyright (C) 2023, Ingo Berg
 16 |   All rights reserved.
 17 | 
 18 |   Redistribution and use in source and binary forms, with or without 
 19 |   modification, are permitted provided that the following conditions are met:
 20 | 
 21 |    * Redistributions of source code must retain the above copyright notice, 
 22 |      this list of conditions and the following disclaimer.
 23 |    * Redistributions in binary form must reproduce the above copyright notice, 
 24 |      this list of conditions and the following disclaimer in the documentation 
 25 |      and/or other materials provided with the distribution.
 26 | 
 27 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 28 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 29 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 30 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 31 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
 32 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 33 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 34 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 35 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 36 |   POSSIBILITY OF SUCH DAMAGE.
 37 | 
38 | */ 39 | #include "mpICallback.h" 40 | #include 41 | 42 | #include "mpParserBase.h" 43 | 44 | 45 | MUP_NAMESPACE_START 46 | 47 | //------------------------------------------------------------------------------ 48 | ICallback::ICallback(ECmdCode a_iCode, 49 | const char_type *a_szName, 50 | int a_nArgc) 51 | :IToken(a_iCode, a_szName) 52 | ,m_pParent(nullptr) 53 | ,m_nArgc(a_nArgc) 54 | ,m_nArgsPresent(-1) 55 | {} 56 | 57 | //------------------------------------------------------------------------------ 58 | ICallback::~ICallback() 59 | {} 60 | 61 | //--------------------------------------------------------------------------- 62 | ICallback* ICallback::AsICallback() 63 | { 64 | return this; 65 | } 66 | 67 | //--------------------------------------------------------------------------- 68 | IValue* ICallback::AsIValue() 69 | { 70 | return nullptr; 71 | } 72 | 73 | //------------------------------------------------------------------------------ 74 | /** \brief Returns a pointer to the parser object owning this callback. 75 | \pre [assert] m_pParent must be defined 76 | */ 77 | ParserXBase* ICallback::GetParent() 78 | { 79 | assert(m_pParent); 80 | return m_pParent; 81 | } 82 | 83 | //------------------------------------------------------------------------------ 84 | void ICallback::SetArgc(int argc) 85 | { 86 | m_nArgc = argc; 87 | } 88 | 89 | //------------------------------------------------------------------------------ 90 | /** \brief Returns the m´number of arguments required by this callback. 91 | \return Number of arguments or -1 if the number of arguments is variable. 92 | */ 93 | int ICallback::GetArgc() const 94 | { 95 | return m_nArgc; 96 | } 97 | 98 | //------------------------------------------------------------------------------ 99 | /** \brief Assign a parser object to the callback. 100 | \param a_pParent The parser that belongs to this callback object. 101 | 102 | The parent object can be used in order to access internals of the parser 103 | from within a callback object. Thus enabling callbacks to delete 104 | variables or functions if this is desired. 105 | */ 106 | void ICallback::SetParent(parent_type *a_pParent) 107 | { 108 | assert(a_pParent); 109 | m_pParent = a_pParent; 110 | } 111 | 112 | //------------------------------------------------------------------------------ 113 | string_type ICallback::AsciiDump() const 114 | { 115 | stringstream_type ss; 116 | 117 | ss << g_sCmdCode[ GetCode() ]; 118 | ss << _T(" [addr=0x") << std::hex << this << std::dec; 119 | ss << _T("; pos=") << GetExprPos(); 120 | ss << _T("; id=\"") << GetIdent() << "\""; 121 | ss << _T("; argc=") << GetArgc() << " (found: " << m_nArgsPresent << ")"; 122 | ss << _T("]"); 123 | 124 | return ss.str(); 125 | } 126 | 127 | //------------------------------------------------------------------------------ 128 | void ICallback::SetNumArgsPresent(int argc) 129 | { 130 | m_nArgsPresent = argc; 131 | } 132 | 133 | //------------------------------------------------------------------------------ 134 | int ICallback::GetArgsPresent() const 135 | { 136 | if (m_nArgc!=-1) 137 | return m_nArgc; 138 | else 139 | return m_nArgsPresent; 140 | } 141 | } // namespace mu 142 | -------------------------------------------------------------------------------- /parser/mpValReader.h: -------------------------------------------------------------------------------- 1 | /** \file 2 | \brief Definition of classes that interpret values in a string. 3 | 4 |
  5 |                __________                                 ____  ___
  6 |     _____  __ _\______   \_____ _______  ______ __________\   \/  /
  7 |    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
  8 |   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
  9 |   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
 10 |         \/                     \/           \/     \/           \_/
 11 |                                        Copyright (C) 2023, Ingo Berg
 12 |                                        All rights reserved.
 13 | 
 14 |   Redistribution and use in source and binary forms, with or without 
 15 |   modification, are permitted provided that the following conditions are met:
 16 | 
 17 |    * Redistributions of source code must retain the above copyright notice, 
 18 |      this list of conditions and the following disclaimer.
 19 |    * Redistributions in binary form must reproduce the above copyright notice, 
 20 |      this list of conditions and the following disclaimer in the documentation 
 21 |      and/or other materials provided with the distribution.
 22 | 
 23 |   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 24 |   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 25 |   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 26 |   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 27 |   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
 28 |   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 29 |   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 30 |   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 31 |   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 32 |   POSSIBILITY OF SUCH DAMAGE.
 33 |   
34 | */ 35 | #ifndef MU_PARSER_IMPL_READER_H 36 | #define MU_PARSER_IMPL_READER_H 37 | 38 | #include "mpIValReader.h" 39 | 40 | 41 | 42 | MUP_NAMESPACE_START 43 | 44 | //------------------------------------------------------------------------------ 45 | // 46 | // Reader for floating point values 47 | // 48 | //------------------------------------------------------------------------------ 49 | 50 | /** \brief A class for reading floating point values from an expression string. 51 | \ingroup valreader 52 | */ 53 | class DblValReader : public IValueReader 54 | { 55 | public: 56 | DblValReader(); 57 | virtual ~DblValReader(); 58 | virtual bool IsValue(const char_type *a_szExpr, int &a_iPos, Value &a_fVal) override; 59 | virtual IValueReader* Clone(TokenReader *pTokenReader) const override; 60 | }; 61 | 62 | //------------------------------------------------------------------------------ 63 | // 64 | // Reader for boolean values 65 | // 66 | //------------------------------------------------------------------------------ 67 | 68 | /** \brief A class for reading boolean values from an expression string. 69 | \ingroup valreader 70 | */ 71 | class BoolValReader : public IValueReader 72 | { 73 | public: 74 | BoolValReader(); 75 | virtual ~BoolValReader(); 76 | virtual bool IsValue(const char_type *a_szExpr, int &a_iPos, Value &a_fVal) override; 77 | virtual IValueReader* Clone(TokenReader *pTokenReader) const override; 78 | }; 79 | 80 | //------------------------------------------------------------------------------ 81 | // 82 | // Reader for hex values 83 | // 84 | //------------------------------------------------------------------------------ 85 | 86 | /** \brief A class for reading hex values from an expression string. 87 | \ingroup valreader 88 | */ 89 | class HexValReader : public IValueReader 90 | { 91 | public: 92 | HexValReader(); 93 | virtual bool IsValue(const char_type *a_szExpr, int &a_iPos, Value &a_fVal) override; 94 | virtual IValueReader* Clone(TokenReader *pTokenReader) const override; 95 | }; 96 | 97 | //------------------------------------------------------------------------------ 98 | // 99 | // Reader for binary values 100 | // 101 | //------------------------------------------------------------------------------ 102 | 103 | /** \brief A class for reading binary values from an expression string. 104 | \ingroup valreader 105 | */ 106 | class BinValReader : public IValueReader 107 | { 108 | public: 109 | BinValReader(); 110 | virtual ~BinValReader(); 111 | virtual bool IsValue(const char_type *a_szExpr, int &a_iPos, Value &a_fVal) override; 112 | virtual IValueReader* Clone(TokenReader *pTokenReader) const override; 113 | }; 114 | 115 | //------------------------------------------------------------------------------ 116 | // 117 | // Reader for string values 118 | // 119 | //------------------------------------------------------------------------------ 120 | 121 | /** \brief A class for reading strings from an expression string. 122 | \ingroup valreader 123 | */ 124 | class StrValReader : public IValueReader 125 | { 126 | public: 127 | StrValReader(); 128 | virtual ~StrValReader(); 129 | virtual bool IsValue(const char_type *a_szExpr, int &a_iPos, Value &a_fVal) override; 130 | virtual IValueReader* Clone(TokenReader *pTokenReader) const override; 131 | 132 | private: 133 | string_type Unescape(const char_type *szExpr, int &len); 134 | }; 135 | 136 | MUP_NAMESPACE_END 137 | 138 | #endif 139 | --------------------------------------------------------------------------------