1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/*
__________ ____ ___
_____ __ _\______ \_____ _______ ______ __________\ \/ /
/ \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ /
| Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \
|__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \
\/ \/ \/ \/ \_/
Copyright (C) 2016 Ingo Berg
All rights reserved.
muParserX - A C++ math parser library with array and string support
Copyright (c) 2016, Ingo Berg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "mpFuncCommon.h"
#include <cassert>
#include <string>
#include <iostream>
#include "mpValue.h"
#include "mpParserBase.h"
MUP_NAMESPACE_START
//------------------------------------------------------------------------------
//
// FunParserID
//
//------------------------------------------------------------------------------
FunParserID::FunParserID()
:ICallback(cmFUNC, _T("parserid"), 0)
{}
//------------------------------------------------------------------------------
void FunParserID::Eval(ptr_val_type &ret, const ptr_val_type * /*a_pArg*/, int /*a_iArgc*/)
{
string_type sVer = _T("muParserX V") + GetParent()->GetVersion();
*ret = sVer;
}
//------------------------------------------------------------------------------
const char_type* FunParserID::GetDesc() const
{
return _T("parserid() - muParserX version information");
}
//------------------------------------------------------------------------------
IToken* FunParserID::Clone() const
{
return new FunParserID(*this);
}
//------------------------------------------------------------------------------
//
// Max Function
//
//------------------------------------------------------------------------------
FunMax::FunMax() : ICallback(cmFUNC, _T("max"), -1)
{}
//------------------------------------------------------------------------------
void FunMax::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc)
{
if (a_iArgc < 1)
throw ParserError(ErrorContext(ecTOO_FEW_PARAMS, GetExprPos(), GetIdent()));
float_type max(-1e30), val(0);
for (int i=0; i<a_iArgc; ++i)
{
switch(a_pArg[i]->GetType())
{
case 'f':
case 'i': val = a_pArg[i]->GetFloat(); break;
case 'n': break; // ignore not in list entries (missing parameter)
case 'c':
default:
{
ErrorContext err;
err.Errc = ecTYPE_CONFLICT_FUN;
err.Arg = i+1;
err.Type1 = a_pArg[i]->GetType();
err.Type2 = 'f';
throw ParserError(err);
}
}
max = std::max(max, val);
}
*ret = max;
}
//------------------------------------------------------------------------------
const char_type* FunMax::GetDesc() const
{
return _T("max(x,y,...,z) - Returns the maximum value from all of its function arguments.");
}
//------------------------------------------------------------------------------
IToken* FunMax::Clone() const
{
return new FunMax(*this);
}
//------------------------------------------------------------------------------
//
// Min Function
//
//------------------------------------------------------------------------------
FunMin::FunMin() : ICallback(cmFUNC, _T("min"), -1)
{}
//------------------------------------------------------------------------------
/** \brief Returns the minimum value of all values.
\param a_pArg Pointer to an array of Values
\param a_iArgc Number of values stored in a_pArg
*/
void FunMin::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc)
{
if (a_iArgc < 1)
throw ParserError(ErrorContext(ecTOO_FEW_PARAMS, GetExprPos(), GetIdent()));
float_type min(1e30), val(min);
for (int i=0; i<a_iArgc; ++i)
{
switch(a_pArg[i]->GetType())
{
case 'f':
case 'i': val = a_pArg[i]->GetFloat(); break;
default:
{
ErrorContext err;
err.Errc = ecTYPE_CONFLICT_FUN;
err.Arg = i+1;
err.Type1 = a_pArg[i]->GetType();
err.Type2 = 'f';
throw ParserError(err);
}
}
min = std::min(min, val);
}
*ret = min;
}
//------------------------------------------------------------------------------
const char_type* FunMin::GetDesc() const
{
return _T("min(x,y,...,z) - Returns the minimum value from all of its function arguments.");
}
//------------------------------------------------------------------------------
IToken* FunMin::Clone() const
{
return new FunMin(*this);
}
//------------------------------------------------------------------------------
//
// class FunSum
//
//------------------------------------------------------------------------------
FunSum::FunSum()
:ICallback(cmFUNC, _T("sum"), -1)
{}
//------------------------------------------------------------------------------
/** \brief Returns the minimum value of all values.
\param a_pArg Pointer to an array of Values
\param a_iArgc Number of values stored in a_pArg
*/
void FunSum::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc)
{
if (a_iArgc < 1)
throw ParserError(ErrorContext(ecTOO_FEW_PARAMS, GetExprPos(), GetIdent()));
float_type sum(0);
for (int i=0; i<a_iArgc; ++i)
{
switch(a_pArg[i]->GetType())
{
case 'f':
case 'i': sum += a_pArg[i]->GetFloat(); break;
default:
{
ErrorContext err;
err.Errc = ecTYPE_CONFLICT_FUN;
err.Arg = i+1;
err.Type1 = a_pArg[i]->GetType();
err.Type2 = 'f';
throw ParserError(err);
}
}
}
*ret = sum;
}
//------------------------------------------------------------------------------
const char_type* FunSum::GetDesc() const
{
return _T("sum(x,y,...,z) - Returns the sum of all arguments.");
}
//------------------------------------------------------------------------------
IToken* FunSum::Clone() const
{
return new FunSum(*this);
}
//------------------------------------------------------------------------------
//
// SizeOf
//
//------------------------------------------------------------------------------
FunSizeOf::FunSizeOf()
:ICallback(cmFUNC, _T("sizeof"), 1)
{}
//------------------------------------------------------------------------------
FunSizeOf::~FunSizeOf()
{}
//------------------------------------------------------------------------------
/** \brief Returns the number of elements stored in the first parameter. */
void FunSizeOf::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc)
{
assert(a_iArgc==1);
*ret = (float_type)a_pArg[0]->GetArray().GetRows();
}
//------------------------------------------------------------------------------
const char_type* FunSizeOf::GetDesc() const
{
return _T("sizeof(a) - Returns the number of elements in a.");
}
//------------------------------------------------------------------------------
IToken* FunSizeOf::Clone() const
{
return new FunSizeOf(*this);
}
MUP_NAMESPACE_END
|