MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
MathParser Class Reference

#include <mathp.h>

Collaboration diagram for MathParser:

Classes

class  ErrGeneric
 
class  MathArg_t
 
class  MathArgPriv_t
 
class  MathArgVoid_t
 
struct  MathFunc_t
 
class  NameSpace
 
class  PlugIn
 
struct  PlugInRegister
 
class  PlugInVar
 
class  StaticNameSpace
 
struct  TokenVal
 

Public Types

enum  ArgType {
  AT_PRIVATE, AT_TYPE, AT_ANY, AT_VOID,
  AT_BOOL, AT_INT, AT_REAL, AT_STRING
}
 
enum  ArgFlag { AF_NONE = 0x0U, AF_OPTIONAL = 0x1U, AF_OPTIONAL_NON_PRESENT = 0x2U, AF_CONST = 0x10U }
 
enum  Token {
  ENDOFFILE = -2, UNKNOWNTOKEN = -1, MP_INT = TypedValue::VAR_INT, MP_REAL = TypedValue::VAR_REAL,
  MP_STRING = TypedValue::VAR_STRING, NUM, NAME, EXP,
  MULT, DIV, MOD, MINUS,
  PLUS, GT, GE, EQ,
  LE, LT, NE, NOT,
  AND, OR, XOR, OBR,
  CBR, OPGIN, CPGIN, STMTSEP,
  ARGSEP, NAMESPACESEP, ASSIGN, LASTTOKEN
}
 
enum  DeclarationModifier { DMOD_UNKNOWN = -1, DMOD_IFNDEF, DMOD_LAST }
 
typedef MathArgPriv_t< ArgType,
AT_TYPE
MathArgType_t
 
typedef MathArgPriv_t
< TypedValue, AT_ANY
MathArgAny_t
 
typedef MathArgPriv_t< bool,
AT_BOOL
MathArgBool_t
 
typedef MathArgPriv_t< Int,
AT_INT
MathArgInt_t
 
typedef MathArgPriv_t< Real,
AT_REAL
MathArgReal_t
 
typedef MathArgPriv_t
< std::string, AT_STRING
MathArgString_t
 
typedef std::vector< MathArg_t * > MathArgs
 
typedef int(* MathFunc_f )(const MathArgs &args)
 
typedef int(* MathFuncTest_f )(const MathArgs &args)
 
typedef std::map< std::string,
NameSpace * > 
NameSpaceMap
 

Public Member Functions

TableGetSymbolTable (void) const
 
void PutSymbolTable (Table &T)
 
int GetLineNumber (void) const
 
const NameSpaceMapGetNameSpaceMap (void) const
 
 MathParser (const InputStream &strm, Table &t, bool bRedefineVars=false)
 
 MathParser (Table &t, bool bRedefineVars=false)
 
 ~MathParser (void)
 
NamedValueInsertSym (const char *const s, const Real &v, int redefine=0)
 
NamedValueInsertSym (const char *const s, const Int &v, int redefine=0)
 
Real GetLastStmt (Real d=0., Token t=ARGSEP)
 
Real GetLastStmt (const InputStream &strm, Real d=0., Token t=ARGSEP)
 
Real Get (Real d=0.)
 
Real Get (const InputStream &strm, Real d=0.)
 
TypedValue Get (const TypedValue &v)
 
TypedValue Get (const InputStream &strm, const TypedValue &v)
 
void GetForever (std::ostream &out, const char *const sep="\n")
 
void GetForever (const InputStream &strm, std::ostream &out, const char *const sep="\n")
 
int RegisterPlugIn (const char *name, MathParser::PlugIn *(*)(MathParser &, void *), void *arg)
 
int RegisterNameSpace (NameSpace *ns)
 
NameSpaceGetNameSpace (const std::string &name) const
 
bool bNameValidate (const std::string &s) const
 

Public Attributes

Tabletable
 
bool bRedefineVars
 
InputStreamin
 

Static Public Attributes

static const char ONE_LINE_REMARK = '#'
 

Protected Member Functions

void TokenPush (enum Token t)
 
int TokenPop (void)
 
TypedValue::Type GetType (const char *const s) const
 
TypedValue::TypeModifier GetTypeModifier (const char *const s) const
 
MathParser::DeclarationModifier GetDeclarationModifier (const char *const s) const
 
bool IsType (const char *const s) const
 
bool IsTypeModifier (const char *const s) const
 
bool IsDeclarationModifier (const char *const s) const
 
bool IsKeyWord (NameSpace *ns, const char *const s) const
 
enum Token GetToken (void)
 
void trim_arg (char *const s)
 
TypedValue logical (void)
 
TypedValue logical (TypedValue d)
 
TypedValue logical_int (TypedValue d)
 
TypedValue relational (void)
 
TypedValue relational (TypedValue d)
 
TypedValue relational_int (TypedValue d)
 
TypedValue binary (void)
 
TypedValue binary (TypedValue d)
 
TypedValue binary_int (TypedValue d)
 
TypedValue mult (void)
 
TypedValue mult (TypedValue d)
 
TypedValue mult_int (TypedValue d)
 
TypedValue power (void)
 
TypedValue power (TypedValue d)
 
TypedValue power_int (TypedValue d)
 
TypedValue unary (void)
 
TypedValue evalfunc (MathParser::NameSpace *ns, MathParser::MathFunc_t *f)
 
TypedValue expr (void)
 
TypedValue stmt (void)
 
TypedValue stmtlist (void)
 
TypedValue readplugin (void)
 

Protected Attributes

struct MathParser::PlugInRegisterPlugIns
 
StaticNameSpacedefaultNameSpace
 
NameSpaceMap nameSpaceMap
 
std::string namebuf
 
TypedValue value
 
enum Token currtoken
 
std::stack< TokenValTokenStack
 

Private Member Functions

 MathParser (const MathParser &)
 

Detailed Description

Definition at line 63 of file mathp.h.

Member Typedef Documentation

Definition at line 152 of file mathp.h.

Definition at line 153 of file mathp.h.

Definition at line 154 of file mathp.h.

Definition at line 155 of file mathp.h.

Definition at line 158 of file mathp.h.

Definition at line 156 of file mathp.h.

Definition at line 150 of file mathp.h.

typedef int(* MathParser::MathFunc_f)(const MathArgs &args)

Definition at line 159 of file mathp.h.

typedef int(* MathParser::MathFuncTest_f)(const MathArgs &args)

Definition at line 160 of file mathp.h.

typedef std::map<std::string, NameSpace *> MathParser::NameSpaceMap

Definition at line 351 of file mathp.h.

Member Enumeration Documentation

Enumerator
AF_NONE 
AF_OPTIONAL 
AF_OPTIONAL_NON_PRESENT 
AF_CONST 

Definition at line 81 of file mathp.h.

81  {
82  AF_NONE = 0x0U,
83  AF_OPTIONAL = 0x1U,
85  AF_CONST = 0x10U
86  };
Enumerator
AT_PRIVATE 
AT_TYPE 
AT_ANY 
AT_VOID 
AT_BOOL 
AT_INT 
AT_REAL 
AT_STRING 

Definition at line 65 of file mathp.h.

65  {
66  /* AT_PRIVATE means only who created that type
67  * is supposed to deal with it (the default);
68  * the MathParser only knows how to deal with
69  * the remaining types */
70  AT_PRIVATE,
71 
72  AT_TYPE,
73  AT_ANY,
74  AT_VOID,
75  AT_BOOL,
76  AT_INT,
77  AT_REAL,
78  AT_STRING
79  };
Enumerator
DMOD_UNKNOWN 
DMOD_IFNDEF 
DMOD_LAST 

Definition at line 340 of file mathp.h.

Enumerator
ENDOFFILE 
UNKNOWNTOKEN 
MP_INT 
MP_REAL 
MP_STRING 
NUM 
NAME 
EXP 
MULT 
DIV 
MOD 
MINUS 
PLUS 
GT 
GE 
EQ 
LE 
LT 
NE 
NOT 
AND 
OR 
XOR 
OBR 
CBR 
OPGIN 
CPGIN 
STMTSEP 
ARGSEP 
NAMESPACESEP 
ASSIGN 
LASTTOKEN 

Definition at line 301 of file mathp.h.

301  {
302  ENDOFFILE = -2,
303  UNKNOWNTOKEN = -1,
304 
308 
309  NUM, /* Numero */
310  NAME, /* Nome */
311  EXP, /* '^' : Elevamento a potenza */
312  MULT, /* '*' : Moltiplicazione */
313  DIV, /* '/' : Divisione */
314  MOD, /* '%' : Divisione */
315  MINUS, /* '-' : Meno */
316  PLUS, /* '+' : Piu' */
317  GT, /* '>' : Maggiore di */
318  GE, /* '>=' : Maggiore o uguale */
319  EQ, /* '==' : Uguale */
320  LE, /* '<=' : Minore o uguale */
321  LT, /* '<' : Minore di */
322  NE, /* '!=' : Diverso da */
323  NOT, /* '!' : Negazione (operatore logico) */
324  AND, /* '&&' : AND (operatore logico) */
325  OR, /* '||' : OR (operatore logico) */
326  XOR, /* '~|' : XOR, o OR esclusivo (op. logico) */
327 
328  OBR, /* '(' : Parentesi aperta */
329  CBR, /* ')' : Parentesi chiusa */
330  OPGIN, /* '[' : Apertura di plugin statement */
331  CPGIN, /* ']' : Chiusura di plugin statement */
332  STMTSEP, /* ';' : Separatore di statements */
333  ARGSEP, /* ',' : Separatore di argomenti */
334  NAMESPACESEP, /* '::' : Separatore di namespace */
335  ASSIGN, /* '=' : Assegnazione */
336 
337  LASTTOKEN
338  };

Constructor & Destructor Documentation

MathParser::MathParser ( const MathParser )
private
MathParser::MathParser ( const InputStream strm,
Table t,
bool  bRedefineVars = false 
)

Definition at line 4266 of file mathp.cc.

References DEBUGCOUTFNAME, defaultNameSpace, MBDYN_EXCEPT_ARGS, RegisterNameSpace(), and MathParser::NameSpace::sGetName().

4267 : PlugIns(0),
4268 table(t),
4270 in(const_cast<InputStream*>(&strm)),
4271 defaultNameSpace(0),
4272 value(),
4274 {
4275  DEBUGCOUTFNAME("MathParser::MathParser");
4276 
4277  // namebuf.resize(4);
4278 
4279  defaultNameSpace = new StaticNameSpace(&t);
4281  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
4282  std::string("unable to register namespace \"") + defaultNameSpace->sGetName() + "\"");
4283  }
4284 
4285  time_t tm;
4286  time(&tm);
4287  srand(tm);
4288 }
InputStream * in
Definition: mathp.h:295
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
#define DEBUGCOUTFNAME(fname)
Definition: myassert.h:256
int RegisterNameSpace(NameSpace *ns)
Definition: mathp.cc:4602
bool bRedefineVars
Definition: mathp.h:288
enum Token currtoken
Definition: mathp.h:364
StaticNameSpace * defaultNameSpace
Definition: mathp.h:348
virtual const std::string & sGetName(void) const
Definition: mathp.cc:2001
TypedValue value
Definition: mathp.h:361
Table & table
Definition: mathp.h:287
struct MathParser::PlugInRegister * PlugIns

Here is the call graph for this function:

MathParser::MathParser ( Table t,
bool  bRedefineVars = false 
)

Definition at line 4290 of file mathp.cc.

References DEBUGCOUTFNAME, defaultNameSpace, MBDYN_EXCEPT_ARGS, RegisterNameSpace(), and MathParser::NameSpace::sGetName().

4291 : PlugIns(0),
4292 table(t),
4294 in(0),
4295 defaultNameSpace(0),
4296 value(),
4298 {
4299  DEBUGCOUTFNAME("MathParser::MathParser");
4300 
4301  // namebuf.resize(4);
4302 
4303  defaultNameSpace = new StaticNameSpace(&t);
4305  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
4306  std::string("unable to register namespace \"") + defaultNameSpace->sGetName() + "\"");
4307  }
4308 
4309  time_t tm;
4310  time(&tm);
4311  srand(tm);
4312 }
InputStream * in
Definition: mathp.h:295
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
#define DEBUGCOUTFNAME(fname)
Definition: myassert.h:256
int RegisterNameSpace(NameSpace *ns)
Definition: mathp.cc:4602
bool bRedefineVars
Definition: mathp.h:288
enum Token currtoken
Definition: mathp.h:364
StaticNameSpace * defaultNameSpace
Definition: mathp.h:348
virtual const std::string & sGetName(void) const
Definition: mathp.cc:2001
TypedValue value
Definition: mathp.h:361
Table & table
Definition: mathp.h:287
struct MathParser::PlugInRegister * PlugIns

Here is the call graph for this function:

MathParser::~MathParser ( void  )

Definition at line 4385 of file mathp.cc.

References DEBUGCOUTFNAME, MathParser::PlugInRegister::name, nameSpaceMap, MathParser::PlugInRegister::next, PlugIns, SAFEDELETE, and SAFEDELETEARR.

4386 {
4387  DEBUGCOUTFNAME("MathParser::~MathParser");
4388 
4389  while (PlugIns) {
4390  PlugInRegister *next = PlugIns->next;
4393  PlugIns = next;
4394  }
4395 
4396  for (NameSpaceMap::iterator i = nameSpaceMap.begin(); i != nameSpaceMap.end(); ++i) {
4397  delete i->second;
4398  }
4399 }
#define DEBUGCOUTFNAME(fname)
Definition: myassert.h:256
#define SAFEDELETEARR(pnt)
Definition: mynewmem.h:713
const char * name
Definition: mathp.h:269
struct PlugInRegister * next
Definition: mathp.h:272
NameSpaceMap nameSpaceMap
Definition: mathp.h:355
#define SAFEDELETE(pnt)
Definition: mynewmem.h:710
struct MathParser::PlugInRegister * PlugIns

Member Function Documentation

TypedValue MathParser::binary ( void  )
protected

Definition at line 3403 of file mathp.cc.

References binary_int(), and mult().

Referenced by relational(), and relational_int().

3404 {
3405  return binary_int(mult());
3406 }
TypedValue binary_int(TypedValue d)
Definition: mathp.cc:3415
TypedValue mult(void)
Definition: mathp.cc:3436

Here is the call graph for this function:

TypedValue MathParser::binary ( TypedValue  d)
protected

Definition at line 3409 of file mathp.cc.

References binary_int(), and mult().

3410 {
3411  return binary_int(mult(d));
3412 }
TypedValue binary_int(TypedValue d)
Definition: mathp.cc:3415
TypedValue mult(void)
Definition: mathp.cc:3436

Here is the call graph for this function:

TypedValue MathParser::binary_int ( TypedValue  d)
protected

Definition at line 3415 of file mathp.cc.

References currtoken, GetToken(), MINUS, mult(), and PLUS.

Referenced by binary().

3416 {
3417  while (true) {
3418  switch (currtoken) {
3419  case PLUS:
3420  GetToken();
3421  d += mult();
3422  break;
3423 
3424  case MINUS:
3425  GetToken();
3426  d -= mult();
3427  break;
3428 
3429  default:
3430  return d;
3431  }
3432  }
3433 }
TypedValue mult(void)
Definition: mathp.cc:3436
enum Token GetToken(void)
Definition: mathp.cc:2960
enum Token currtoken
Definition: mathp.h:364

Here is the call graph for this function:

bool MathParser::bNameValidate ( const std::string &  s) const

Definition at line 3292 of file mathp.cc.

Referenced by module_init(), and NameSpaceDR::Read().

3293 {
3294  std::string::const_iterator i = s.begin();
3295  if (*i != '_' && !isalpha(*i)) {
3296  return false;
3297  }
3298 
3299  while (++i != s.end()) {
3300  if (*i != '_' && !isalnum(*i)) {
3301  return false;
3302  }
3303  }
3304 
3305  return true;
3306 }
TypedValue MathParser::evalfunc ( MathParser::NameSpace ns,
MathParser::MathFunc_t f 
)
protected

Definition at line 3576 of file mathp.cc.

References AF_OPTIONAL, AF_OPTIONAL_NON_PRESENT, MathParser::MathFunc_t::args, ARGSEP, AT_ANY, AT_BOOL, AT_INT, AT_PRIVATE, AT_REAL, AT_STRING, CBR, currtoken, DEBUGCERR, MathParser::MathFunc_t::errmsg, MathParser::NameSpace::EvalFunc(), MathParser::MathFunc_t::fname, TypedValue::GetBool(), TypedValue::GetInt(), TypedValue::GetReal(), TypedValue::GetString(), GetToken(), MBDYN_EXCEPT_ARGS, MathParser::NameSpace::sGetName(), stmtlist(), and MathParser::MathFunc_t::t.

Referenced by expr(), and stmt().

3577 {
3578  for (unsigned i = 1; i < f->args.size(); i++) {
3579  switch (f->args[i]->Type()) {
3580  case MathParser::AT_ANY:
3581  if (currtoken == CBR) {
3582  if (!f->args[i]->IsFlag(MathParser::AF_OPTIONAL)) {
3583  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "argument expected");
3584  }
3585  (*dynamic_cast<MathArgAny_t*>(f->args[i]))() = (*dynamic_cast<MathArgAny_t*>(f->args[i]))();
3586  f->args[i]->SetFlag(MathParser::AF_OPTIONAL_NON_PRESENT);
3587 
3588  } else {
3589  (*dynamic_cast<MathArgAny_t*>(f->args[i]))() = stmtlist();
3590  }
3591  break;
3592 
3593  case MathParser::AT_BOOL:
3594  if (currtoken == CBR) {
3595  if (!f->args[i]->IsFlag(MathParser::AF_OPTIONAL)) {
3596  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "bool argument expected");
3597  }
3598  (*dynamic_cast<MathArgBool_t*>(f->args[i]))() = (*dynamic_cast<MathArgBool_t*>(f->args[i]))();
3599  f->args[i]->SetFlag(MathParser::AF_OPTIONAL_NON_PRESENT);
3600 
3601  } else {
3602  (*dynamic_cast<MathArgBool_t*>(f->args[i]))() = stmtlist().GetBool();
3603  }
3604  break;
3605 
3606  case MathParser::AT_INT:
3607  if (currtoken == CBR) {
3608  if (!f->args[i]->IsFlag(MathParser::AF_OPTIONAL)) {
3609  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "integer argument expected");
3610  }
3611  (*dynamic_cast<MathArgInt_t*>(f->args[i]))() = (*dynamic_cast<MathArgInt_t*>(f->args[i]))();
3612  f->args[i]->SetFlag(MathParser::AF_OPTIONAL_NON_PRESENT);
3613 
3614  } else {
3615  (*dynamic_cast<MathArgInt_t*>(f->args[i]))() = stmtlist().GetInt();
3616  }
3617  break;
3618 
3619  case MathParser::AT_REAL:
3620  if (currtoken == CBR) {
3621  if (!f->args[i]->IsFlag(MathParser::AF_OPTIONAL)) {
3622  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "real argument expected");
3623  }
3624  (*dynamic_cast<MathArgReal_t*>(f->args[i]))() = (*dynamic_cast<MathArgReal_t*>(f->args[i]))();
3625  f->args[i]->SetFlag(MathParser::AF_OPTIONAL_NON_PRESENT);
3626 
3627  } else {
3628  (*dynamic_cast<MathArgReal_t*>(f->args[i]))() = stmtlist().GetReal();
3629  }
3630  break;
3631 
3632  case MathParser::AT_STRING:
3633  if (currtoken == CBR) {
3634  if (!f->args[i]->IsFlag(MathParser::AF_OPTIONAL)) {
3635  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "string argument expected");
3636  }
3637  (*dynamic_cast<MathArgString_t*>(f->args[i]))() = (*dynamic_cast<MathArgString_t*>(f->args[i]))();
3638  f->args[i]->SetFlag(MathParser::AF_OPTIONAL_NON_PRESENT);
3639 
3640  } else {
3641  (*dynamic_cast<MathArgString_t*>(f->args[i]))() = stmtlist().GetString();
3642  }
3643  break;
3644 
3646  /* ignore */
3647  break;
3648 
3649  default:
3651  }
3652 
3653  if (i < f->args.size() - 1) {
3654  if (f->args[i + 1]->Type() != AT_PRIVATE) {
3655  switch (currtoken) {
3656  case CBR:
3657  if (!f->args[i + 1]->IsFlag(MathParser::AF_OPTIONAL)) {
3658  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
3659  "mandatory argument expected");
3660  }
3661  break;
3662 
3663  case ARGSEP:
3664  GetToken();
3665  break;
3666 
3667  default:
3668  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
3669  "argument separator expected");
3670  }
3671  }
3672  }
3673  }
3674 
3675  if (f->t != 0) {
3676  if (f->t(f->args)) {
3677  DEBUGCERR("error in function "
3678  << ns->sGetName() << "::" << f->fname
3679  << " " "(msg: " << f->errmsg << ")"
3680  << " in evalfunc()" << std::endl);
3681  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, f->fname + ": error " + f->errmsg);
3682  }
3683  }
3684 
3685  TypedValue val = ns->EvalFunc(f);
3686 
3687  return val;
3688 }
MathArgPriv_t< Real, AT_REAL > MathArgReal_t
Definition: mathp.h:155
TypedValue stmtlist(void)
Definition: mathp.cc:4253
Real GetReal(void) const
Definition: mathp.cc:1228
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
std::string errmsg
Definition: mathp.h:172
#define DEBUGCERR(msg)
Definition: myassert.h:235
MathArgPriv_t< bool, AT_BOOL > MathArgBool_t
Definition: mathp.h:153
enum Token GetToken(void)
Definition: mathp.cc:2960
MathArgPriv_t< TypedValue, AT_ANY > MathArgAny_t
Definition: mathp.h:152
MathFuncTest_f t
Definition: mathp.h:171
enum Token currtoken
Definition: mathp.h:364
virtual const std::string & sGetName(void) const
Definition: mathp.cc:2001
const std::string & GetString(void) const
Definition: mathp.cc:1247
virtual TypedValue EvalFunc(MathParser::MathFunc_t *f) const =0
MathArgPriv_t< std::string, AT_STRING > MathArgString_t
Definition: mathp.h:156
MathArgPriv_t< Int, AT_INT > MathArgInt_t
Definition: mathp.h:154
Int GetInt(void) const
Definition: mathp.cc:1209
bool GetBool(void) const
Definition: mathp.cc:1188
std::string fname
Definition: mathp.h:167

Here is the call graph for this function:

TypedValue MathParser::expr ( void  )
protected

Definition at line 3691 of file mathp.cc.

References CBR, CPGIN, currtoken, defaultNameSpace, ENDOFFILE, evalfunc(), MathParser::MathFunc_t::fname, Table::Get(), MathParser::NameSpace::GetFunc(), MathParser::NameSpace::GetTable(), GetToken(), NamedValue::GetVal(), MBDYN_EXCEPT_ARGS, NAME, namebuf, nameSpaceMap, NAMESPACESEP, NUM, OBR, OPGIN, readplugin(), stmtlist(), table, and value.

Referenced by unary().

3692 {
3693  if (currtoken == NUM) {
3694  GetToken();
3695  return value;
3696  }
3697 
3698  if (currtoken == OBR) {
3699  GetToken();
3700  TypedValue d = stmtlist();
3701  if (currtoken != CBR) {
3702  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "closing parenthesis expected");
3703  }
3704  GetToken();
3705  return d;
3706  }
3707 
3708  if (currtoken == OPGIN) {
3709  TypedValue d = readplugin();
3710  if (currtoken != CPGIN) {
3711  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "closing plugin expected");
3712  }
3713  GetToken();
3714  return d;
3715  }
3716 
3717  if (currtoken == NAME) {
3718  std::string name(namebuf);
3719  MathParser::NameSpace *currNameSpace = defaultNameSpace;
3720  Table *currTable = &table;
3721 
3722  GetToken();
3723  if (currtoken == NAMESPACESEP) {
3724  NameSpaceMap::iterator i = nameSpaceMap.find(name);
3725  if (i == nameSpaceMap.end()) {
3726  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
3727  std::string("unable to find namespace \"") + namebuf + "\"");
3728  }
3729  currNameSpace = i->second;
3730  currTable = currNameSpace->GetTable();
3731  GetToken();
3732  if (currtoken != NAME) {
3733  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "name expected after namespace");
3734  }
3735  name += "::";
3736  name += namebuf;
3737  GetToken();
3738  }
3739 
3740  if (currtoken == OBR) {
3741  /* in futuro ci potranno essere magari i dati strutturati */
3742  MathParser::MathFunc_t* f = currNameSpace->GetFunc(namebuf);
3743  if (f == NULL) {
3744  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
3745  std::string("function \"") + namebuf + "\" not found");
3746  }
3747  GetToken();
3748  TypedValue d = evalfunc(currNameSpace, f);
3749  delete f;
3750  if (currtoken != CBR) {
3751  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
3752  std::string("closing parenthesis expected after function \"")
3753  + f->fname + "\" in expr()");
3754  }
3755  GetToken();
3756  return d;
3757 
3758  } else {
3759  NamedValue* v = 0;
3760  if (currTable) {
3761  v = currTable->Get(namebuf);
3762  }
3763 
3764  if (v != NULL) {
3765  return v->GetVal();
3766  }
3767  }
3768 
3769  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, std::string("unknown name \"") + name + "\"");
3770  }
3771 
3772  /* invalid expr */
3773  if (currtoken != ENDOFFILE) {
3774  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "unknown token");
3775  }
3776 
3777  return TypedValue(0.);
3778 }
TypedValue stmtlist(void)
Definition: mathp.cc:4253
std::string namebuf
Definition: mathp.h:358
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual TypedValue GetVal(void) const =0
enum Token GetToken(void)
Definition: mathp.cc:2960
virtual MathParser::MathFunc_t * GetFunc(const std::string &fname) const =0
enum Token currtoken
Definition: mathp.h:364
StaticNameSpace * defaultNameSpace
Definition: mathp.h:348
TypedValue evalfunc(MathParser::NameSpace *ns, MathParser::MathFunc_t *f)
Definition: mathp.cc:3576
TypedValue readplugin(void)
Definition: mathp.cc:4063
NamedValue * Get(const std::string &name) const
Definition: table.cc:150
Definition: table.h:43
virtual Table * GetTable(void)=0
TypedValue value
Definition: mathp.h:361
NameSpaceMap nameSpaceMap
Definition: mathp.h:355
Table & table
Definition: mathp.h:287
std::string fname
Definition: mathp.h:167

Here is the call graph for this function:

Real MathParser::Get ( Real  d = 0.)

Definition at line 4496 of file mathp.cc.

References TypedValue::GetReal().

Referenced by Get(), GetForever(), HighParser::GetValue(), DofPlugIn::ReadLabel(), and PrivPlugIn::ReadLabel().

4497 {
4498  TypedValue v(d);
4499  v = Get(v);
4500  return v.GetReal();
4501 }
Real Get(Real d=0.)
Definition: mathp.cc:4496

Here is the call graph for this function:

Real MathParser::Get ( const InputStream strm,
Real  d = 0. 
)

Definition at line 4504 of file mathp.cc.

References Get(), and TypedValue::GetReal().

4505 {
4506  TypedValue v(d);
4507  v = Get(strm, v);
4508  return v.GetReal();
4509 }
Real Get(Real d=0.)
Definition: mathp.cc:4496

Here is the call graph for this function:

TypedValue MathParser::Get ( const TypedValue v)

Definition at line 4512 of file mathp.cc.

References currtoken, ENDOFFILE, ExpressionElement::Eval(), GetToken(), MBDYN_EXCEPT_ARGS, ExpressionElement::Output(), stmt(), and STMTSEP.

4513 {
4514  GetToken();
4515 #ifdef USE_EE
4516  ExpressionElement *e = stmt();
4517  TypedValue vv = e->Eval();
4518  if (pedantic_out) {
4519  std::cout << "Get: \"", e->Output(std::cout) << "\" = " << vv << std::endl;
4520  }
4521  delete e;
4522 #else // ! USE_EE
4523  TypedValue vv = stmt();
4524 #endif // ! USE_EE
4525  if (currtoken != STMTSEP && currtoken != ENDOFFILE) {
4526  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "statement separator expected");
4527  }
4528  return vv;
4529 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual TypedValue Eval(void) const =0
enum Token GetToken(void)
Definition: mathp.cc:2960
TypedValue stmt(void)
Definition: mathp.cc:3781
enum Token currtoken
Definition: mathp.h:364
virtual std::ostream & Output(std::ostream &out) const =0

Here is the call graph for this function:

TypedValue MathParser::Get ( const InputStream strm,
const TypedValue v 
)

Definition at line 4532 of file mathp.cc.

References ARGSEP, currtoken, ExpressionElement::Eval(), GetToken(), in, MBDYN_EXCEPT_ARGS, ExpressionElement::Output(), InputStream::putback(), stmt(), and STMTSEP.

4533 {
4534  const InputStream* p = in;
4535  in = (InputStream*)&strm;
4536  GetToken();
4537  TypedValue vv = v;
4538  if (currtoken != STMTSEP && currtoken != ARGSEP) {
4539 #ifdef USE_EE
4540  ExpressionElement *e = stmt();
4541  vv = e->Eval();
4542  if (pedantic_out) {
4543  std::cout << "Get: \"", e->Output(std::cout) << "\" = " << vv << std::endl;
4544  }
4545  delete e;
4546 #else // ! USE_EE
4547  vv = stmt();
4548 #endif // ! USE_EE
4549  }
4550  if (currtoken == STMTSEP) {
4551  in->putback(';');
4552 
4553  } else if (currtoken == ARGSEP) {
4554  in->putback(',');
4555 
4556  } else {
4557  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "separator expected");
4558  }
4559  in = const_cast<InputStream *>(p);
4560 
4561  return vv;
4562 }
InputStream * in
Definition: mathp.h:295
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual TypedValue Eval(void) const =0
enum Token GetToken(void)
Definition: mathp.cc:2960
TypedValue stmt(void)
Definition: mathp.cc:3781
enum Token currtoken
Definition: mathp.h:364
virtual std::ostream & Output(std::ostream &out) const =0
InputStream & putback(char ch)
Definition: input.h:121

Here is the call graph for this function:

MathParser::DeclarationModifier MathParser::GetDeclarationModifier ( const char *const  s) const
protected

Definition at line 2915 of file mathp.cc.

References DMOD_UNKNOWN, declarationmodifiernames::name, and declarationmodifiernames::type.

Referenced by IsDeclarationModifier(), and stmt().

2916 {
2917  for (Int i = 0; DeclarationModifierNames[i].name != NULL; i++) {
2918  if (strcmp(s, DeclarationModifierNames[i].name) == 0) {
2919  return DeclarationModifierNames[i].type;
2920  }
2921  }
2922 
2923  return DMOD_UNKNOWN;
2924 }
static const declarationmodifiernames DeclarationModifierNames[]
Definition: mathp.cc:860
int Int
Definition: mathtyp.h:40
const char * name
Definition: mathp.cc:856
MathParser::DeclarationModifier type
Definition: mathp.cc:857
void MathParser::GetForever ( std::ostream &  out,
const char *const  sep = "\n" 
)

Definition at line 4565 of file mathp.cc.

References currtoken, Get(), and STMTSEP.

Referenced by GetForever(), and main().

4566 {
4567  do {
4568  TypedValue val(0.);
4569  out << Get(val) << sep;
4570  } while (currtoken == STMTSEP);
4571 }
Real Get(Real d=0.)
Definition: mathp.cc:4496
enum Token currtoken
Definition: mathp.h:364

Here is the call graph for this function:

void MathParser::GetForever ( const InputStream strm,
std::ostream &  out,
const char *const  sep = "\n" 
)

Definition at line 4574 of file mathp.cc.

References GetForever(), and in.

4576 {
4577  InputStream *p = in;
4578  in = const_cast<InputStream *>(&strm);
4579  GetForever(out);
4580  in = p;
4581 }
InputStream * in
Definition: mathp.h:295
void GetForever(std::ostream &out, const char *const sep="\n")
Definition: mathp.cc:4565

Here is the call graph for this function:

Real MathParser::GetLastStmt ( Real  d = 0.,
Token  t = ARGSEP 
)

Definition at line 4402 of file mathp.cc.

References currtoken, ENDOFFILE, ExpressionElement::Eval(), TypedValue::GetReal(), GetToken(), MBDYN_EXCEPT_ARGS, ExpressionElement::Output(), stmtlist(), and STMTSEP.

Referenced by DriveHandler::dGet(), GetEnviron(), and GetLastStmt().

4403 {
4404  if (GetToken() == t) {
4405  return d;
4406  }
4407 
4408  for (;;) {
4409 #ifdef USE_EE
4410  ExpressionElement *e = stmtlist();
4411  d = e->Eval().GetReal();
4412  if (pedantic_out) {
4413  std::cout << "GetLastStmt: \"", e->Output(std::cout) << "\" = " << d << std::endl;
4414  }
4415  delete e;
4416 #else // ! USE_EE
4417  d = stmtlist().GetReal();
4418 #endif // ! USE_EE
4419  if (currtoken == ENDOFFILE || currtoken == t) {
4420  break;
4421  }
4422  if (currtoken != STMTSEP) {
4423  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "statement separator expected");
4424  }
4425  }
4426 
4427  return d;
4428 }
TypedValue stmtlist(void)
Definition: mathp.cc:4253
Real GetReal(void) const
Definition: mathp.cc:1228
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual TypedValue Eval(void) const =0
enum Token GetToken(void)
Definition: mathp.cc:2960
enum Token currtoken
Definition: mathp.h:364
virtual std::ostream & Output(std::ostream &out) const =0

Here is the call graph for this function:

Real MathParser::GetLastStmt ( const InputStream strm,
Real  d = 0.,
Token  t = ARGSEP 
)

Definition at line 4431 of file mathp.cc.

References currtoken, GetLastStmt(), in, and UNKNOWNTOKEN.

4432 {
4433  const InputStream* save_in = in;
4434  Token save_currtoken = currtoken;
4435 
4436  in = const_cast<InputStream *>(&strm);
4438 
4439  d = GetLastStmt(d, t);
4440 
4441  in = const_cast<InputStream *>(save_in);
4442  currtoken = save_currtoken;
4443 
4444  return d;
4445 }
InputStream * in
Definition: mathp.h:295
enum Token currtoken
Definition: mathp.h:364
Real GetLastStmt(Real d=0., Token t=ARGSEP)
Definition: mathp.cc:4402

Here is the call graph for this function:

int MathParser::GetLineNumber ( void  ) const

Definition at line 1943 of file mathp.cc.

References ASSERT, InputStream::GetLineNumber(), and in.

Referenced by MathParser::ErrGeneric::ErrGeneric(), and GetToken().

1944 {
1945  ASSERT(in != NULL);
1946  return in->GetLineNumber();
1947 }
InputStream * in
Definition: mathp.h:295
unsigned long int GetLineNumber(void) const
Definition: input.h:132
#define ASSERT(expression)
Definition: colamd.c:977

Here is the call graph for this function:

MathParser::NameSpace * MathParser::GetNameSpace ( const std::string &  name) const

Definition at line 4620 of file mathp.cc.

References nameSpaceMap.

Referenced by DataManager::PopCurrData(), DataManager::PushCurrData(), and PrintSymbolTableDR::Read().

4621 {
4622  NameSpaceMap::const_iterator i = nameSpaceMap.find(name);
4623  if (i == nameSpaceMap.end()) {
4624  return 0;
4625  }
4626 
4627  return i->second;
4628 }
NameSpaceMap nameSpaceMap
Definition: mathp.h:355
const MathParser::NameSpaceMap & MathParser::GetNameSpaceMap ( void  ) const

Definition at line 1984 of file mathp.cc.

References nameSpaceMap.

Referenced by PrintSymbolTableDR::Read().

1985 {
1986  return nameSpaceMap;
1987 }
NameSpaceMap nameSpaceMap
Definition: mathp.h:355
Table & MathParser::GetSymbolTable ( void  ) const
MathParser::Token MathParser::GetToken ( void  )
protected

Definition at line 2960 of file mathp.cc.

References AND, ARGSEP, ASSERT, ASSIGN, c, CBR, CPGIN, currtoken, DIV, ENDOFFILE, InputStream::eof(), EQ, EXP, GE, InputStream::get(), GetLineNumber(), InputStream::GetStream(), GT, in, LE, LT, MBDYN_EXCEPT_ARGS, MINUS, MOD, MULT, NAME, namebuf, NAMESPACESEP, NE, NO_OP, NOT, NUM, OBR, ONE_LINE_REMARK, OPGIN, OR, PLUS, InputStream::putback(), TypedValue::Set(), TypedValue::SetType(), STMTSEP, TokenPop(), UNKNOWNTOKEN, value, TypedValue::VAR_INT, TypedValue::VAR_REAL, TypedValue::VAR_STRING, and XOR.

Referenced by binary_int(), evalfunc(), expr(), Get(), GetLastStmt(), logical_int(), mult_int(), power_int(), readplugin(), relational_int(), stmt(), stmtlist(), and unary().

2961 {
2962  ASSERT(in != NULL);
2963 
2964  if (TokenPop()) {
2965  /* se lo trova! */
2966  return currtoken;
2967  }
2968 
2969  int c = 0;
2970 
2971 start_parsing:;
2972  /* skip spaces */
2973  while ((c = in->get()), isspace(c)) {
2974  NO_OP;
2975  };
2976 
2977  if (c == EOF || in->eof()) {
2978  return (currtoken = ENDOFFILE);
2979  }
2980 
2981  if (c == ONE_LINE_REMARK) {
2982  for (c = in->get(); c != '\n'; c = in->get()) {
2983  /* a trailing '\' continues the one-line remark
2984  * on the following line
2985  * FIXME: are we sure we want this? */
2986  if (c == '\\') {
2987  c = in->get();
2988  if (c == '\r') {
2989  c = in->get();
2990  }
2991  }
2992  }
2993  goto start_parsing;
2994  }
2995 
2996  /* number? */
2997  if (c == '.' || isdigit(c)) {
2998  // lot of space...
2999  char s[BUFSIZ];
3000  bool f = false;
3001  unsigned i = 0;
3002 
3003  // FIXME: need to check for overflow
3004 
3005  s[i++] = char(c);
3006 
3007  if (c == '.') {
3008  f = true;
3009  }
3010  while ((c = in->get()) == '.' || isdigit(c)) {
3011  s[i++] = char(c);
3012  if (c == '.') {
3013  if (f) {
3014  return (currtoken = UNKNOWNTOKEN);
3015  }
3016  f = true;
3017  }
3018  if (i >= sizeof(s)) {
3019  // buffer about to overflow
3020  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "value too long");
3021  }
3022  }
3023  if (std::strchr("efdgEFDG", c) != 0) {
3024  f = true;
3025  // use 'e' because strtod only understands 'e' or 'E'
3026  s[i++] = 'e';
3027  if (i >= sizeof(s)) {
3028  // buffer about to overflow
3029  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "value too long");
3030  }
3031  if ((c = in->get()) == '-' || c == '+') {
3032  s[i++] = char(c);
3033  if (i >= sizeof(s)) {
3034  // buffer about to overflow
3035  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "value too long");
3036  }
3037  c = in->get();
3038  }
3039  if (isdigit(c)) {
3040  s[i++] = char(c);
3041  if (i >= sizeof(s)) {
3042  // buffer about to overflow
3043  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "value too long");
3044  }
3045 
3046  } else {
3047  return (currtoken = UNKNOWNTOKEN);
3048  }
3049  while (isdigit((c = in->get()))) {
3050  s[i++] = char(c);
3051  if (i >= sizeof(s)) {
3052  // buffer about to overflow
3053  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "value too long");
3054  }
3055  }
3056  }
3057  s[i] = '\0';
3058  if (in->eof()) {
3059  // force EOF because on some archs (e.g. arm) char is unsigned,
3060  // thus putback won't restore EOF
3061  in->putback(char(c));
3062  in->GetStream().setstate(std::ios::eofbit);
3063  } else {
3064  in->putback(char(c));
3065  }
3066  char *endptr = 0;
3067  if (!f) {
3069 #ifdef HAVE_STRTOL
3070  errno = 0;
3071  long l = strtol(s, &endptr, 10);
3072  int save_errno = errno;
3073  if (endptr == s || endptr[0] != '\0') {
3074  silent_cerr(" MathParser - unable to parse \"" << s << "\" as integer"
3075  << " at line " << GetLineNumber() << std::endl);
3076  return (currtoken = UNKNOWNTOKEN);
3077  }
3078 
3079  if (save_errno == ERANGE) {
3080  // over/under-flow
3081  if (l == LONG_MIN) {
3082  throw ErrGeneric(this,
3084  std::string("integer value ") + std::string(s, endptr - s) + " underflow");
3085  }
3086 
3087  if (l == LONG_MAX) {
3088  throw ErrGeneric(this,
3090  std::string("integer value ") + std::string(s, endptr - s) + " overflow");
3091  }
3092 
3094  }
3095  value.Set(Int(l));
3096 #else /* !HAVE_STRTOL */
3097  value.Set(Int(atoi(s)));
3098 #endif /* !HAVE_STRTOL */
3099 
3100  } else {
3102 #ifdef HAVE_STRTOD
3103  errno = 0;
3104  double d = strtod(s, &endptr);
3105  int save_errno = errno;
3106  if (endptr == s || endptr[0] != '\0') {
3107  silent_cerr(" MathParser - unable to parse \"" << s << "\" as real"
3108  << " at line " << GetLineNumber() << std::endl);
3109  return (currtoken = UNKNOWNTOKEN);
3110  }
3111 
3112  if (save_errno == ERANGE) {
3113  // over/under-flow
3114  if (std::abs(d) == HUGE_VAL) {
3115  throw ErrGeneric(this,
3117  std::string("real value ") + std::string(s, endptr - s) + " overflow");
3118  }
3119 
3120  if (d == 0.) {
3121  throw ErrGeneric(this,
3123  std::string("real value ") + std::string(s, endptr - s) + " underflow");
3124  }
3125 
3127  }
3128  value.Set(Real(d));
3129 #else /* !HAVE_STRTOD */
3130  value.Set(Real(atof(s)));
3131 #endif /* !HAVE_STRTOD */
3132  }
3133 
3134  return (currtoken = NUM);
3135  }
3136 
3137  /* name? */
3138  if (isalpha(c) || c == '_') {
3139  namebuf.clear();
3140  namebuf.push_back(char(c));
3141  while ((c = in->get())) {
3142  if (!(c == '_'
3143  || isalnum(c)
3144  || ((currtoken == NAMESPACESEP) && c == ':')))
3145  {
3146  break;
3147  }
3148 
3149  namebuf.push_back(char(c));
3150  }
3151  in->putback(char(c));
3152  return (currtoken = NAME);
3153  }
3154 
3155  switch (c) {
3156  case '^':
3157  return (currtoken = EXP);
3158 
3159  case '*':
3160  return (currtoken = MULT);
3161 
3162  case '/':
3163  if ((c = in->get()) == '*') {
3164  for (c = in->get();; c = in->get()) {
3165  if (c == '*') {
3166 end_of_comment:;
3167  c = in->get();
3168  if (c == '/') {
3169  goto start_parsing;
3170  }
3171  } else if (c == '/') {
3172  c = in->get();
3173  if (c == '*') {
3174  silent_cerr("warning: '/*' "
3175  "inside a comment at line "
3176  << GetLineNumber()
3177  << std::endl);
3178  goto end_of_comment;
3179  }
3180  }
3181  }
3182 
3183  } else {
3184  in->putback(char(c));
3185  return (currtoken = DIV);
3186  }
3187 
3188  case '%':
3189  return (currtoken = MOD);
3190 
3191  case '-':
3192  return (currtoken = MINUS);
3193 
3194  case '+':
3195  return (currtoken = PLUS);
3196 
3197  case '>':
3198  if ((c = in->get()), c == '=') {
3199  return (currtoken = GE);
3200  }
3201  in->putback(char(c));
3202  return (currtoken = GT);
3203 
3204  case '=':
3205  if ((c = in->get()), c == '=') {
3206  return (currtoken = EQ);
3207  }
3208  in->putback(char(c));
3209  return (currtoken = ASSIGN);
3210 
3211  case '<':
3212  if ((c = in->get()), c == '=') {
3213  return (currtoken = LE);
3214  }
3215  in->putback(char(c));
3216  return (currtoken = LT);
3217 
3218  case '!':
3219  if ((c = in->get()), c == '=') {
3220  return (currtoken = NE);
3221  }
3222  in->putback(char(c));
3223  return (currtoken = NOT);
3224 
3225  case '&':
3226  if ((c = in->get()), c != '&') {
3227  return (currtoken = UNKNOWNTOKEN);
3228  }
3229  return (currtoken = AND);
3230 
3231  case '|':
3232  if ((c = in->get()), c != '|') {
3233  return (currtoken = UNKNOWNTOKEN);
3234  }
3235  return (currtoken = OR);
3236 
3237  case '~':
3238  if ((c = in->get()), c != '|') {
3239  return (currtoken = UNKNOWNTOKEN);
3240  }
3241  return (currtoken = XOR);
3242 
3243  case '(':
3244  return (currtoken = OBR);
3245 
3246  case ')':
3247  return (currtoken = CBR);
3248 
3249  case '[':
3250  return (currtoken = OPGIN);
3251 
3252  case ']':
3253  return (currtoken = CPGIN);
3254 
3255  case ';':
3256  return (currtoken = STMTSEP);
3257 
3258  case ',':
3259  return (currtoken = ARGSEP);
3260 
3261  case ':':
3262  if ((c = in->get()), c != ':') {
3263  return (currtoken = UNKNOWNTOKEN);
3264  }
3265  return (currtoken = NAMESPACESEP);
3266 
3267  case '"': {
3268  namebuf.clear();
3269  while ((c = in->get()) != '"') {
3270  if (c == '\\') {
3271  c = in->get();
3272  if (c == '\0') {
3273  return (currtoken = UNKNOWNTOKEN);
3274  }
3275  if (c == EOF || in->eof()) {
3276  return (currtoken = ENDOFFILE);
3277  }
3278  }
3279  namebuf.push_back(char(c));
3280  }
3282  value.Set(namebuf);
3283  return (currtoken = NUM);
3284  }
3285 
3286  default:
3287  return (currtoken = UNKNOWNTOKEN);
3288  }
3289 }
InputStream * in
Definition: mathp.h:295
static const char ONE_LINE_REMARK
Definition: mathp.h:188
std::string namebuf
Definition: mathp.h:358
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
const TypedValue & Set(const bool &b)
Definition: mathp.cc:1285
#define NO_OP
Definition: myassert.h:74
char get(void)
Definition: input.h:99
int Int
Definition: mathtyp.h:40
const std::istream & GetStream(void) const
Definition: input.h:146
enum Token currtoken
Definition: mathp.h:364
InputStream & putback(char ch)
Definition: input.h:121
int TokenPop(void)
Definition: mathp.cc:1963
#define ASSERT(expression)
Definition: colamd.c:977
int GetLineNumber(void) const
Definition: mathp.cc:1943
static std::stack< cleanup * > c
Definition: cleanup.cc:59
TypedValue value
Definition: mathp.h:361
double Real
Definition: mathtyp.h:39
bool eof(void) const
Definition: input.h:139
void SetType(TypedValue::Type t, bool isConst=false)
Definition: mathp.cc:1264

Here is the call graph for this function:

TypedValue::Type MathParser::GetType ( const char *const  s) const
protected

Definition at line 2891 of file mathp.cc.

References TypeName_t::name, TypeName_t::type, and TypedValue::VAR_UNKNOWN.

Referenced by IsType(), and stmt().

2892 {
2893  for (Int i = 0; TypeNames[i].name != NULL; i++) {
2894  if (strcmp(s, TypeNames[i].name) == 0) {
2895  return TypeNames[i].type;
2896  }
2897  }
2898 
2899  return TypedValue::VAR_UNKNOWN;
2900 }
const char * name
Definition: mathp.cc:832
TypedValue::Type type
Definition: mathp.cc:833
int Int
Definition: mathtyp.h:40
static const TypeName_t TypeNames[]
Definition: mathp.cc:836
TypedValue::TypeModifier MathParser::GetTypeModifier ( const char *const  s) const
protected

Definition at line 2903 of file mathp.cc.

References TypedValue::MOD_UNKNOWN, typemodifiernames::name, and typemodifiernames::type.

Referenced by IsTypeModifier(), and stmt().

2904 {
2905  for (Int i = 0; TypeModifierNames[i].name != NULL; i++) {
2906  if (strcmp(s, TypeModifierNames[i].name) == 0) {
2907  return TypeModifierNames[i].type;
2908  }
2909  }
2910 
2911  return TypedValue::MOD_UNKNOWN;
2912 }
const char * name
Definition: mathp.cc:846
int Int
Definition: mathtyp.h:40
static const typemodifiernames TypeModifierNames[]
Definition: mathp.cc:850
TypedValue::TypeModifier type
Definition: mathp.cc:847
NamedValue * MathParser::InsertSym ( const char *const  s,
const Real v,
int  redefine = 0 
)

Definition at line 4315 of file mathp.cc.

References Table::Get(), NamedValue::GetName(), NamedValue::IsVar(), MBDYN_EXCEPT_ARGS, Table::Put(), and table.

Referenced by DataManager::InsertSym().

4316 {
4317  /* cerco la variabile */
4318  NamedValue* var = table.Get(s);
4319 
4320  if (var == 0) {
4321  /* Se non c'e' la inserisco */
4322  var = table.Put(s, TypedValue(v));
4323  } else {
4324  /* altrimenti, se la posso ridefinire, mi limito
4325  * ad assegnarle il nuovo valore */
4326  if (redefine) {
4327  if (var->IsVar()) {
4328  dynamic_cast<Var *>(var)->SetVal(TypedValue(v));
4329  } else {
4331  std::string("cannot redefine non-var named value \"") + var->GetName() + "\"");
4332  }
4333 
4334  } else {
4335  /* altrimenti la reinserisco, cosi'
4336  * da provocare l'errore di table */
4337  var = table.Put(s, TypedValue(v));
4338  }
4339  }
4340 
4341  if (var == NULL) {
4342  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, std::string("error while adding real var \"") + s + "\"");
4343  }
4344 
4345  return var;
4346 }
Definition: mathtyp.h:175
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
Var * Put(const std::string &name, const TypedValue &v)
Definition: table.cc:110
NamedValue * Get(const std::string &name) const
Definition: table.cc:150
const char * GetName(void) const
Definition: mathp.cc:1751
Table & table
Definition: mathp.h:287
virtual bool IsVar(void) const
Definition: mathp.cc:1745

Here is the call graph for this function:

NamedValue * MathParser::InsertSym ( const char *const  s,
const Int v,
int  redefine = 0 
)

Definition at line 4349 of file mathp.cc.

References Table::Get(), NamedValue::GetName(), NamedValue::IsVar(), MBDYN_EXCEPT_ARGS, Table::Put(), and table.

4350 {
4351  /* cerco la variabile */
4352  NamedValue* var = table.Get(s);
4353 
4354  if (var == NULL) {
4355  /* Se non c'e' la inserisco */
4356  var = table.Put(s, TypedValue(v));
4357 
4358  } else {
4359  /* altrimenti, se la posso ridefinire, mi limito
4360  * ad assegnarle il nuovo valore */
4361  if (redefine) {
4362  if (var->IsVar()) {
4363  dynamic_cast<Var *>(var)->SetVal(TypedValue(v));
4364 
4365  } else {
4367  std::string("cannot redefine non-var named value \"") + var->GetName() + "\"");
4368  }
4369 
4370  } else {
4371  /* altrimenti la reinserisco, cosi'
4372  * da provocare l'errore di table */
4373  var = table.Put(s, TypedValue(v));
4374  }
4375  }
4376 
4377  if (var == NULL) {
4378  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
4379  std::string("error while adding integer var \"") + s + "\"");
4380  }
4381 
4382  return var;
4383 }
Definition: mathtyp.h:175
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
Var * Put(const std::string &name, const TypedValue &v)
Definition: table.cc:110
NamedValue * Get(const std::string &name) const
Definition: table.cc:150
const char * GetName(void) const
Definition: mathp.cc:1751
Table & table
Definition: mathp.h:287
virtual bool IsVar(void) const
Definition: mathp.cc:1745

Here is the call graph for this function:

bool MathParser::IsDeclarationModifier ( const char *const  s) const
protected

Definition at line 2939 of file mathp.cc.

References DMOD_UNKNOWN, and GetDeclarationModifier().

2940 {
2941  return GetDeclarationModifier(s) != DMOD_UNKNOWN;
2942 }
MathParser::DeclarationModifier GetDeclarationModifier(const char *const s) const
Definition: mathp.cc:2915

Here is the call graph for this function:

bool MathParser::IsKeyWord ( MathParser::NameSpace ns,
const char *const  s 
) const
protected

Definition at line 2945 of file mathp.cc.

References MathParser::NameSpace::IsFunc(), IsType(), and IsTypeModifier().

Referenced by stmt().

2946 {
2947  if (IsTypeModifier(s)) {
2948  return true;
2949  }
2950  if (IsType(s)) {
2951  return true;
2952  }
2953  if (ns->IsFunc(s)) {
2954  return true;
2955  }
2956  return false;
2957 }
bool IsTypeModifier(const char *const s) const
Definition: mathp.cc:2933
bool IsType(const char *const s) const
Definition: mathp.cc:2927
virtual bool IsFunc(const std::string &fname) const =0

Here is the call graph for this function:

bool MathParser::IsType ( const char *const  s) const
protected

Definition at line 2927 of file mathp.cc.

References GetType(), and TypedValue::VAR_UNKNOWN.

Referenced by IsKeyWord().

2928 {
2929  return GetType(s) != TypedValue::VAR_UNKNOWN;
2930 }
TypedValue::Type GetType(const char *const s) const
Definition: mathp.cc:2891

Here is the call graph for this function:

bool MathParser::IsTypeModifier ( const char *const  s) const
protected

Definition at line 2933 of file mathp.cc.

References GetTypeModifier(), and TypedValue::MOD_UNKNOWN.

Referenced by IsKeyWord().

2934 {
2936 }
TypedValue::TypeModifier GetTypeModifier(const char *const s) const
Definition: mathp.cc:2903

Here is the call graph for this function:

TypedValue MathParser::logical ( void  )
protected

Definition at line 3310 of file mathp.cc.

References logical_int(), and relational().

Referenced by stmt().

3311 {
3312  return logical_int(relational());
3313 }
TypedValue logical_int(TypedValue d)
Definition: mathp.cc:3322
TypedValue relational(void)
Definition: mathp.cc:3350

Here is the call graph for this function:

TypedValue MathParser::logical ( TypedValue  d)
protected

Definition at line 3316 of file mathp.cc.

References logical_int(), and relational().

3317 {
3318  return logical_int(relational(d));
3319 }
TypedValue logical_int(TypedValue d)
Definition: mathp.cc:3322
TypedValue relational(void)
Definition: mathp.cc:3350

Here is the call graph for this function:

TypedValue MathParser::logical_int ( TypedValue  d)
protected

Definition at line 3322 of file mathp.cc.

References AND, currtoken, GetToken(), OR, relational(), and XOR.

Referenced by logical().

3323 {
3324  while (true) {
3325  switch (currtoken) {
3326  case AND:
3327  GetToken();
3328  d = (d && relational());
3329  break;
3330 
3331  case OR:
3332  GetToken();
3333  d = (d || relational());
3334  break;
3335 
3336  case XOR: {
3337  GetToken();
3338  TypedValue e = relational();
3339  d = ((!(d && e)) && (d || e));
3340  break;
3341  }
3342 
3343  default:
3344  return d;
3345  }
3346  }
3347 }
enum Token GetToken(void)
Definition: mathp.cc:2960
enum Token currtoken
Definition: mathp.h:364
TypedValue relational(void)
Definition: mathp.cc:3350

Here is the call graph for this function:

TypedValue MathParser::mult ( void  )
protected

Definition at line 3436 of file mathp.cc.

References mult_int(), and power().

Referenced by binary(), and binary_int().

3437 {
3438  return mult_int(power());
3439 }
TypedValue power(void)
Definition: mathp.cc:3485
TypedValue mult_int(TypedValue d)
Definition: mathp.cc:3448

Here is the call graph for this function:

TypedValue MathParser::mult ( TypedValue  d)
protected

Definition at line 3442 of file mathp.cc.

References mult_int(), and power().

3443 {
3444  return mult_int(power(d));
3445 }
TypedValue power(void)
Definition: mathp.cc:3485
TypedValue mult_int(TypedValue d)
Definition: mathp.cc:3448

Here is the call graph for this function:

TypedValue MathParser::mult_int ( TypedValue  d)
protected

Definition at line 3448 of file mathp.cc.

References currtoken, DIV, GetToken(), MBDYN_EXCEPT_ARGS, MOD, MULT, and power().

Referenced by mult().

3449 {
3450  while (true) {
3451  switch (currtoken) {
3452  case MULT:
3453  GetToken();
3454  d *= power();
3455  break;
3456 
3457  case DIV: {
3458  GetToken();
3459  TypedValue e = power();
3460  if (e == 0.) {
3461  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "divide by zero in mult()");
3462  }
3463  d /= e;
3464  break;
3465  }
3466 
3467  case MOD: {
3468  GetToken();
3469  TypedValue e = power();
3470  if (e == 0.) {
3471  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "divide by zero in mult()");
3472  }
3473  d %= e;
3474  break;
3475  }
3476 
3477  default:
3478  return d;
3479  }
3480  }
3481 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
enum Token GetToken(void)
Definition: mathp.cc:2960
enum Token currtoken
Definition: mathp.h:364
TypedValue power(void)
Definition: mathp.cc:3485

Here is the call graph for this function:

TypedValue MathParser::power ( void  )
protected

Definition at line 3485 of file mathp.cc.

References power_int(), and unary().

Referenced by mult(), mult_int(), and power_int().

3486 {
3487  return power_int(unary());
3488 }
TypedValue power_int(TypedValue d)
Definition: mathp.cc:3497
TypedValue unary(void)
Definition: mathp.cc:3555

Here is the call graph for this function:

TypedValue MathParser::power ( TypedValue  d)
protected

Definition at line 3491 of file mathp.cc.

References power_int().

3492 {
3493  return power_int(d);
3494 }
TypedValue power_int(TypedValue d)
Definition: mathp.cc:3497

Here is the call graph for this function:

TypedValue MathParser::power_int ( TypedValue  d)
protected

Definition at line 3497 of file mathp.cc.

References currtoken, DEBUGCERR, EXP, TypedValue::GetInt(), TypedValue::GetReal(), GetToken(), TypedValue::GetType(), MBDYN_EXCEPT_ARGS, grad::pow(), power(), TypedValue::SetType(), TypedValue::VAR_INT, and TypedValue::VAR_REAL.

Referenced by power().

3498 {
3499  if (currtoken == EXP) {
3500  GetToken();
3501 
3502  /*
3503  * Per l'esponente chiamo di nuovo power cosi' richiama unary;
3504  * se per caso dopo unary c'e' di nuovo un esponente,
3505  * l'associazione avviene correttamente da destra:
3506  *
3507  * d^e1^e2 == d^(e1^e2)
3508  */
3509  TypedValue e = power();
3510 
3511  if (d < 0. && e <= 0.) {
3512  DEBUGCERR("can't compute (" << d << ")^("
3513  << e << ") in power()" << std::endl);
3514  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "invalid operands in power()");
3515  }
3516 
3517  /*
3518  * Se sono entrambi interi, uso la sequenza di prodotti
3519  * (maggiore accuratezza? comunque va in overflow
3520  * correttamente)
3521  */
3522  if (e.GetType() == TypedValue::VAR_INT
3523  && d.GetType() == TypedValue::VAR_INT)
3524  {
3525  Int i = e.GetInt();
3526  Int j = d.GetInt();
3527  Int r = j;
3528  if (i == 0) {
3529  r = 1;
3530  } else if (i < 0) {
3531  r = 0;
3532  } else {
3533  for (Int k = i-1; k-- > 0; ) {
3534  r *= j;
3535  }
3536  }
3537  d = TypedValue(r);
3538  /*
3539  * Altrimenti li forzo entrambi a reale e uso pow
3540  * (ottimizzata di suo)
3541  */
3542  } else {
3543  Real r = e.GetReal();
3544  Real b = d.GetReal();
3546  d = TypedValue(Real(std::pow(b, r)));
3547  }
3548  }
3549 
3550  return d;
3551 }
Real GetReal(void) const
Definition: mathp.cc:1228
GradientExpression< BinaryExpr< FuncPow, LhsExpr, RhsExpr > > pow(const GradientExpression< LhsExpr > &u, const GradientExpression< RhsExpr > &v)
Definition: gradient.h:2961
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
TypedValue::Type GetType(void) const
Definition: mathp.cc:1155
#define DEBUGCERR(msg)
Definition: myassert.h:235
int Int
Definition: mathtyp.h:40
enum Token GetToken(void)
Definition: mathp.cc:2960
enum Token currtoken
Definition: mathp.h:364
TypedValue power(void)
Definition: mathp.cc:3485
Int GetInt(void) const
Definition: mathp.cc:1209
double Real
Definition: mathtyp.h:39
void SetType(TypedValue::Type t, bool isConst=false)
Definition: mathp.cc:1264

Here is the call graph for this function:

void MathParser::PutSymbolTable ( Table T)

Definition at line 1937 of file mathp.cc.

References table.

Referenced by DriveHandler::PutSymbolTable().

1938 {
1939  table = T;
1940 }
Table & table
Definition: mathp.h:287
TypedValue MathParser::readplugin ( void  )
protected

Definition at line 4063 of file mathp.cc.

References buf, c, InputStream::eof(), Table::Get(), InputStream::get(), GetToken(), NamedValue::GetVal(), in, MBDYN_EXCEPT_ARGS, MathParser::PlugInRegister::next, NO_OP, PlugIns, Table::Put(), InputStream::putback(), MathParser::PlugIn::Read(), SAFEDELETEARR, SAFENEWWITHCONSTRUCTOR, SAFESTRDUP, table, and trim_arg().

Referenced by expr().

4064 {
4065  /*
4066  * parse plugin:
4067  * - arg[0]: nome plugin
4068  * - arg[1]: nome variabile
4069  * - arg[2]->arg[n]: dati da passare al costrutture
4070  */
4071  std::vector<char *> argv(1);
4072  char c, buf[1024];
4073  int argc = 0;
4074  unsigned int i = 0, in_quotes = 0;
4075 
4076  /*
4077  * inizializzo l'array degli argomenti
4078  */
4079  argv[0] = NULL;
4080 
4081  /*
4082  * parserizzo la stringa:
4083  * <plugin> ::= '[' <type> ',' <var_name> <list_of_args> ']'
4084  * <type> ::= <registered_type>
4085  * <var_name> ::= <legal_var_name>
4086  * <list_of_args> ::= ',' <arg> <list_of_args> | ''
4087  * <arg> ::= <legal_string> (no unescaped ']' or ','!)
4088  */
4089  while ((c = in->get()), !in->eof()) {
4090  switch (c) {
4091  case '\\':
4092  c = in->get();
4093  if (in_quotes) {
4094  switch (c) {
4095  case 'n':
4096  c = '\n';
4097  break;
4098 
4099  default:
4100  in->putback(c);
4101  c = '\\';
4102  break;
4103  }
4104  }
4105  buf[i++] = c;
4106  break;
4107 
4108  case '"':
4109  if (in_quotes == 0) {
4110  in_quotes = 1;
4111  break;
4112  }
4113  in_quotes = 0;
4114  while (isspace((c = in->get()))) {
4115  NO_OP;
4116  }
4117  if (c != ',' && c != ']') {
4118  silent_cerr("need a separator "
4119  "after closing quotes" << std::endl);
4121  }
4122  in->putback(c);
4123  break;
4124 
4125  case ',':
4126  case ']':
4127  if (in_quotes) {
4128  buf[i++] = c;
4129  break;
4130  }
4131  buf[i] = '\0';
4132  argv.resize(argc + 2);
4133  trim_arg(buf);
4134  SAFESTRDUP(argv[argc], buf);
4135  ++argc;
4136  argv[argc] = NULL;
4137  if (c == ']') {
4138  goto last_arg;
4139  }
4140  i = 0;
4141  break;
4142 
4143  default:
4144  buf[i++] = c;
4145  break;
4146  }
4147 
4148  /*
4149  * FIXME: rendere dinamico il buffer ...
4150  */
4151  if (i >= sizeof(buf)) {
4152  silent_cerr("MathParser::readplugin(): buffer overflow" << std::endl);
4154  }
4155  }
4156 
4157 last_arg:
4158  if (in->eof()) {
4159  silent_cerr("eof encountered while parsing plugin"
4160  << std::endl);
4162  }
4163 
4164  /*
4165  * put the close plugin token back
4166  */
4167  in->putback(c);
4168  buf[i] = '\0';
4169 
4170  /*
4171  * argomenti comuni a tutti i plugin
4172  */
4173  char *pginname = argv[0];
4174  char *varname = argv[1];
4175  trim_arg(pginname);
4176  trim_arg(varname);
4177 
4178  /*
4179  * verifiche di validita' argomenti
4180  */
4181  if (pginname == NULL || *pginname == '\0') {
4182  silent_cerr("illegal or missing plugin name" << std::endl);
4184  }
4185 
4186  if (varname == NULL || *varname == '\0') {
4187  silent_cerr("illegal or missing plugin variable name"
4188  << std::endl);
4190  }
4191 
4192  /*
4193  * verifica esistenza nome
4194  */
4195  NamedValue* v = table.Get(varname);
4196  if (v != NULL) {
4197  silent_cerr("variable \"" << varname << "\" already defined"
4198  << std::endl);
4200  }
4201 
4202  /*
4203  * ricerca registrazione plugin
4204  */
4205  for (struct PlugInRegister *p = PlugIns; p != NULL; p = p->next) {
4206  if (strcasecmp(p->name, pginname) != 0) {
4207  continue;
4208  }
4209 #ifdef DEBUG
4210  for (int i = 0; argv[i] != NULL; i++) {
4211  silent_cout("argv[" << i << "]=" << argv[i]
4212  << std::endl);
4213  }
4214 #endif /* DEBUG */
4215 
4216  /*
4217  * costruisce il plugin e gli fa interpretare gli argomenti
4218  */
4219  MathParser::PlugIn *pgin = (*p->constructor)(*this, p->arg);
4220  pgin->Read(argc - 2, &argv[2]);
4221 
4222  /*
4223  * riporta il parser nello stato corretto
4224  */
4225  GetToken();
4226 
4227  /*
4228  * costruisce la variabile, la inserisce nella tabella
4229  * e ne ritorna il valore (prima esecuzione)
4230  */
4231  SAFENEWWITHCONSTRUCTOR(v, PlugInVar,
4232  PlugInVar(varname, pgin));
4233  table.Put(v);
4234 
4235  /*
4236  * pulizia ...
4237  */
4238  for (int i = 0; argv[i] != NULL; i++) {
4239  SAFEDELETEARR(argv[i]);
4240  }
4241 
4242  return v->GetVal();
4243  }
4244 
4245  /*
4246  * si arriva qui solo se il plugin non e' stato registrato
4247  */
4248  silent_cerr(" plugin '" << pginname << "' not supported" << std::endl);
4250 }
void trim_arg(char *const s)
Definition: mathp.cc:1865
InputStream * in
Definition: mathp.h:295
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
Var * Put(const std::string &name, const TypedValue &v)
Definition: table.cc:110
virtual TypedValue GetVal(void) const =0
#define SAFEDELETEARR(pnt)
Definition: mynewmem.h:713
#define NO_OP
Definition: myassert.h:74
char get(void)
Definition: input.h:99
enum Token GetToken(void)
Definition: mathp.cc:2960
struct PlugInRegister * next
Definition: mathp.h:272
InputStream & putback(char ch)
Definition: input.h:121
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
static std::stack< cleanup * > c
Definition: cleanup.cc:59
NamedValue * Get(const std::string &name) const
Definition: table.cc:150
#define SAFESTRDUP(pnt, src)
Definition: mynewmem.h:707
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
virtual int Read(int argc, char *argv[])=0
Table & table
Definition: mathp.h:287
bool eof(void) const
Definition: input.h:139
struct MathParser::PlugInRegister * PlugIns

Here is the call graph for this function:

int MathParser::RegisterNameSpace ( MathParser::NameSpace ns)

Definition at line 4602 of file mathp.cc.

References ASSERT, nameSpaceMap, and MathParser::NameSpace::sGetName().

Referenced by DataManager::DataManager(), MathParser(), module_init(), and NameSpaceDR::Read().

4603 {
4604  ASSERT(ns != 0);
4605 
4606  pedantic_cout("MathParser::RegisterNameSpace: "
4607  "registering namespace \"" << ns->sGetName() << "\""
4608  << std::endl);
4609 
4610  if (nameSpaceMap.find(ns->sGetName()) != nameSpaceMap.end()) {
4611  return 1;
4612  }
4613 
4614  nameSpaceMap[ns->sGetName()] = ns;
4615 
4616  return 0;
4617 }
virtual const std::string & sGetName(void) const
Definition: mathp.cc:2001
#define ASSERT(expression)
Definition: colamd.c:977
NameSpaceMap nameSpaceMap
Definition: mathp.h:355

Here is the call graph for this function:

int MathParser::RegisterPlugIn ( const char *  name,
MathParser::PlugIn *)(MathParser &, void *,
void *  arg 
)

Definition at line 4584 of file mathp.cc.

References MathParser::PlugInRegister::arg, MathParser::PlugInRegister::constructor, MathParser::PlugInRegister::name, MathParser::PlugInRegister::next, PlugIns, SAFENEW, and SAFESTRDUP.

Referenced by DataManager::DataManager(), and module_init().

4587 {
4588  pedantic_cout("registering plugin \"" << name << "\"" << std::endl);
4589 
4590  PlugInRegister *p = NULL;
4591  SAFENEW(p, PlugInRegister);
4592  SAFESTRDUP(p->name, name);
4593  p->constructor = constructor;
4594  p->arg = arg;
4595  p->next = PlugIns;
4596  PlugIns = p;
4597 
4598  return 0;
4599 }
#define SAFENEW(pnt, item)
Definition: mynewmem.h:695
#define SAFESTRDUP(pnt, src)
Definition: mynewmem.h:707
struct MathParser::PlugInRegister * PlugIns
TypedValue MathParser::relational ( void  )
protected

Definition at line 3350 of file mathp.cc.

References binary(), and relational_int().

Referenced by logical(), and logical_int().

3351 {
3352  return relational_int(binary());
3353 }
TypedValue relational_int(TypedValue d)
Definition: mathp.cc:3362
TypedValue binary(void)
Definition: mathp.cc:3403

Here is the call graph for this function:

TypedValue MathParser::relational ( TypedValue  d)
protected

Definition at line 3356 of file mathp.cc.

References binary(), and relational_int().

3357 {
3358  return relational_int(binary(d));
3359 }
TypedValue relational_int(TypedValue d)
Definition: mathp.cc:3362
TypedValue binary(void)
Definition: mathp.cc:3403

Here is the call graph for this function:

TypedValue MathParser::relational_int ( TypedValue  d)
protected

Definition at line 3362 of file mathp.cc.

References binary(), currtoken, EQ, GE, GetToken(), GT, LE, LT, and NE.

Referenced by relational().

3363 {
3364  while (true) {
3365  switch (currtoken) {
3366  case GT:
3367  GetToken();
3368  d = (d > binary());
3369  break;
3370 
3371  case GE:
3372  GetToken();
3373  d = (d >= binary());
3374  break;
3375 
3376  case EQ:
3377  GetToken();
3378  d = (d == binary());
3379  break;
3380 
3381  case LE:
3382  GetToken();
3383  d = (d <= binary());
3384  break;
3385 
3386  case LT:
3387  GetToken();
3388  d = (d < binary());
3389  break;
3390 
3391  case NE:
3392  GetToken();
3393  d = (d != binary());
3394  break;
3395 
3396  default:
3397  return d;
3398  }
3399  }
3400 }
enum Token GetToken(void)
Definition: mathp.cc:2960
enum Token currtoken
Definition: mathp.h:364
TypedValue binary(void)
Definition: mathp.cc:3403

Here is the call graph for this function:

TypedValue MathParser::stmt ( void  )
protected

Definition at line 3781 of file mathp.cc.

References ASSIGN, bRedefineVars, TypedValue::Cast(), CBR, NamedValue::Const(), currtoken, defaultNameSpace, DMOD_IFNDEF, DMOD_UNKNOWN, evalfunc(), MathParser::MathFunc_t::fname, Table::Get(), GetDeclarationModifier(), MathParser::NameSpace::GetFunc(), MathParser::NameSpace::GetTable(), GetToken(), NamedValue::GetType(), GetType(), GetTypeModifier(), TypedValue::GetTypeName(), NamedValue::GetTypeName(), NamedValue::GetVal(), IsKeyWord(), NamedValue::IsVar(), logical(), MBDYN_EXCEPT_ARGS, mbdyn_get_line_data(), TypedValue::MOD_CONST, TypedValue::MOD_UNKNOWN, NAME, namebuf, nameSpaceMap, NAMESPACESEP, OBR, Table::Put(), TypedValue::SetConst(), MathParser::NameSpace::sGetName(), STMTSEP, table, TokenPush(), and TypedValue::VAR_UNKNOWN.

Referenced by Get(), and stmtlist().

3782 {
3783  if (currtoken == NAME) {
3784  bool isIfndef = false;
3785  bool isConst = false;
3786 
3787  DeclarationModifier declarationmodifier = GetDeclarationModifier(namebuf.c_str());
3788  if (declarationmodifier != DMOD_UNKNOWN) {
3789  switch (declarationmodifier) {
3790  case DMOD_IFNDEF:
3791  isIfndef = true;
3792  break;
3793 
3794  default:
3795  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
3796  std::string("unhandled definition modifier \"") + namebuf + "\"");
3797  }
3798 
3799  if (GetToken() != NAME) {
3800  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "type (modifier) expected "
3801  "after definition modifier in declaration");
3802  }
3803  }
3804 
3805  TypedValue::TypeModifier typemodifier = GetTypeModifier(namebuf.c_str());
3806  if (typemodifier != TypedValue::MOD_UNKNOWN) {
3807  switch (typemodifier) {
3808  case TypedValue::MOD_CONST:
3809  isConst = true;
3810  break;
3811 
3812  default:
3813  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
3814  std::string("unhandled type modifier \"") + namebuf + "\"");
3815  }
3816 
3817  if (GetToken() != NAME) {
3818  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "type expected "
3819  "after type modifier in declaration");
3820  }
3821  }
3822 
3823  /* declaration? */
3824  TypedValue::Type type = GetType(namebuf.c_str());
3825  if (type != TypedValue::VAR_UNKNOWN) {
3826  switch (GetToken()) {
3827  case OBR:
3828  // explicit cast?
3830  currtoken = NAME;
3831  return logical();
3832 
3833  case NAME:
3834  break;
3835 
3836  default:
3837  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "name expected "
3838  "after type in declaration");
3839  }
3840 
3841  if (IsKeyWord(defaultNameSpace, namebuf.c_str())) {
3842  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, std::string("name \"")
3843  + namebuf + "\" is a keyword");
3844  }
3845 
3846  MathParser::NameSpace *currNameSpace = defaultNameSpace;
3847  Table *currTable = &table;
3848  std::string name(namebuf);
3849 
3850  GetToken();
3851  if (currtoken == NAMESPACESEP) {
3852  NameSpaceMap::iterator i = nameSpaceMap.find(name);
3853  if (i == nameSpaceMap.end()) {
3854  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
3855  std::string("unable to find namespace \"") + namebuf + "\"");
3856  }
3857  currNameSpace = i->second;
3858  currTable = currNameSpace->GetTable();
3859  GetToken();
3860  if (currtoken != NAME) {
3861  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "name expected after namespace");
3862  }
3863  name += "::";
3864  name += namebuf;
3865  GetToken();
3866  }
3867 
3868  /* with assign? */
3869  if (currtoken == ASSIGN) {
3870  if (currTable == 0) {
3871  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
3872  std::string("namespace \"") + currNameSpace->sGetName()
3873  + "\" does not support variables");
3874  }
3875 
3876  /* faccio una copia del nome! */
3877  std::string varname(namebuf);
3878 
3879  GetToken();
3880  TypedValue d = logical();
3881  /* set const'ness of newly gotten value */
3882  if (isConst) {
3883  d.SetConst();
3884  }
3885  /* cerco la variabile */
3886  NamedValue* v = currTable->Get(varname.c_str());
3887 
3888  if (v == NULL) {
3889  /* create new var with assigned type */
3890  TypedValue newvar(type);
3891  /* assign new var, so that it internally
3892  * takes care of casting, while it inherits
3893  * const'ness from d */
3894  newvar.Cast(d);
3895  v = currTable->Put(varname.c_str(), newvar);
3896 
3897  if (isIfndef) {
3898  silent_cerr("warning, ifndef variable " << v->GetTypeName() << " \"" << name
3899  << "\" not yet defined; set to \"" << newvar << "\" at line " << mbdyn_get_line_data() << std::endl);
3900  }
3901 
3902  } else {
3903  /* altrimenti, se la posso ridefinire, mi limito
3904  * ad assegnarle il nuovo valore */
3905  if (!bRedefineVars && !isIfndef) {
3907  std::string("cannot redefine var \"") + name + "\"");
3908  }
3909 
3910  if (v->Const() && !isIfndef) {
3911  // TODO: check redefinition of const'ness
3913  std::string("cannot redefine const named value \"") + name + "\"");
3914  }
3915 
3916  if (!v->IsVar()) {
3918  std::string("cannot redefine non-var named value \"") + name + "\"");
3919  }
3920 
3921  if (!isIfndef) {
3922  dynamic_cast<Var *>(v)->SetVal(d);
3923 
3924  } else {
3925  if (v->GetType() != type) {
3926  silent_cerr("warning, skipping redefinition of \"" << name.c_str() << "\""
3927  << " from " << v->GetTypeName() << " to " << TypedValue::GetTypeName(type)
3928  << " (orig=" << v->GetVal() << ", unchanged; new=" << d << ")"
3929  << " at line " << mbdyn_get_line_data() << std::endl);
3930 
3931  } else {
3932  silent_cerr("warning, skipping redefinition of " << v->GetTypeName() << " \"" << name.c_str() << "\""
3933  << " (orig=" << v->GetVal() << ", unchanged; new=" << d << ")"
3934  << " at line " << mbdyn_get_line_data() << std::endl);
3935  }
3936  }
3937  }
3938  return v->GetVal();
3939 
3940  } else if (currtoken == STMTSEP) {
3941  if (currTable == 0) {
3942  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
3943  std::string("namespace \"") + currNameSpace->sGetName()
3944  + "\" does not support variables");
3945  }
3946 
3947  NamedValue* v = currTable->Get(namebuf);
3948  if (v == NULL || (!bRedefineVars && !isIfndef)) {
3949  if (isConst) {
3950  /* cannot insert a const var
3951  * with no value */
3953  std::string("cannot create const named value \"")
3954  + namebuf + "\" with no value");
3955  }
3956  /* se la var non esiste, la inserisco;
3957  * se invece esiste e non vale
3958  * la ridefinizione, tento
3959  * di inserirla comunque, cosi'
3960  * table da' errore */
3961  v = currTable->Put(namebuf, TypedValue(type));
3962  }
3963 
3964  return v->GetVal();
3965  }
3966 
3967  } else {
3968  if (declarationmodifier != DMOD_UNKNOWN) {
3970  "definition modifier without type");
3971  }
3972 
3973  if (typemodifier != TypedValue::MOD_UNKNOWN) {
3975  "type modifier without type");
3976  }
3977 
3978  MathParser::NameSpace *currNameSpace = defaultNameSpace;
3979  Table *currTable = &table;
3980  std::string name(namebuf);
3981 
3982  GetToken();
3983  if (currtoken == NAMESPACESEP) {
3984  NameSpaceMap::iterator i = nameSpaceMap.find(name);
3985  if (i == nameSpaceMap.end()) {
3986  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
3987  std::string("unable to find namespace \"") + namebuf + "\"");
3988  }
3989  currNameSpace = i->second;
3990  currTable = currNameSpace->GetTable();
3991  GetToken();
3992  if (currtoken != NAME) {
3993  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, "name expected after namespace");
3994  }
3995  name += "::";
3996  name += namebuf;
3997  GetToken();
3998  }
3999 
4000  if (currtoken == OBR) {
4001  /* in futuro ci potranno essere magari i dati strutturati */
4002  MathParser::MathFunc_t* f = currNameSpace->GetFunc(namebuf);
4003  if (f == NULL) {
4004  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
4005  std::string("function \"") + namebuf + "\" not found");
4006  }
4007  GetToken();
4008  TypedValue d = evalfunc(currNameSpace, f);
4009  delete f;
4010  if (currtoken != CBR) {
4011  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
4012  std::string("closing parenthesis expected after function \"")
4013  + f->fname + "\" in expr()");
4014  }
4015  GetToken();
4016 
4017  return logical(d);
4018  }
4019 
4020  /* assignment? */
4021  if (currTable == 0) {
4022  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS,
4023  std::string("namespace \"") + currNameSpace->sGetName()
4024  + "\" does not support variables");
4025  }
4026  NamedValue* v = currTable->Get(namebuf);
4027  if (v == NULL) {
4028  throw ErrGeneric(this, MBDYN_EXCEPT_ARGS, std::string("var \"") + namebuf + "\" not found");
4029  }
4030 
4031  if (currtoken == ASSIGN) {
4032  GetToken();
4033  TypedValue d = logical();
4034  if (v->Const()) {
4036  std::string("cannot assign const named value \"") + name + "\"");
4037  }
4038 
4039  if (!v->IsVar()) {
4041  std::string("cannot assign non-var named value \"") + name + "\"");
4042  }
4043  dynamic_cast<Var *>(v)->Cast(d);
4044  return v->GetVal();
4045 
4046  } else {
4047  // NOTE: fails if <name> is actually <namespace>::<name>
4048  // ASSERT(currtoken != NAME);
4049  // TokenPush(currtoken);
4050  // currtoken = NAME;
4051 
4052  // NOTE: fails if <name> is not the complete <stmt>
4053  // return v->GetVal();
4054 
4055  return logical(v->GetVal());
4056  }
4057  }
4058  }
4059  return logical();
4060 }
TypedValue logical(void)
Definition: mathp.cc:3310
Definition: mathtyp.h:175
bool IsKeyWord(NameSpace *ns, const char *const s) const
Definition: mathp.cc:2945
std::string namebuf
Definition: mathp.h:358
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual TypedValue::Type GetType(void) const =0
Var * Put(const std::string &name, const TypedValue &v)
Definition: table.cc:110
virtual TypedValue GetVal(void) const =0
DeclarationModifier
Definition: mathp.h:340
const char *const GetTypeName(void) const
Definition: mathp.cc:1176
enum Token GetToken(void)
Definition: mathp.cc:2960
bool bRedefineVars
Definition: mathp.h:288
virtual MathParser::MathFunc_t * GetFunc(const std::string &fname) const =0
enum Token currtoken
Definition: mathp.h:364
StaticNameSpace * defaultNameSpace
Definition: mathp.h:348
TypedValue::TypeModifier GetTypeModifier(const char *const s) const
Definition: mathp.cc:2903
virtual const std::string & sGetName(void) const
Definition: mathp.cc:2001
TypedValue evalfunc(MathParser::NameSpace *ns, MathParser::MathFunc_t *f)
Definition: mathp.cc:3576
void TokenPush(enum Token t)
Definition: mathp.cc:1950
virtual bool Const(void) const =0
NamedValue * Get(const std::string &name) const
Definition: table.cc:150
HighParser::ErrOut mbdyn_get_line_data(void)
Definition: parser.cc:603
Definition: table.h:43
virtual Table * GetTable(void)=0
virtual const char *const GetTypeName(void) const
Definition: mathp.cc:1758
void SetConst(bool isConst=true, bool bForce=false)
Definition: mathp.cc:1275
MathParser::DeclarationModifier GetDeclarationModifier(const char *const s) const
Definition: mathp.cc:2915
TypedValue::Type GetType(const char *const s) const
Definition: mathp.cc:2891
NameSpaceMap nameSpaceMap
Definition: mathp.h:355
Table & table
Definition: mathp.h:287
virtual bool IsVar(void) const
Definition: mathp.cc:1745
std::string fname
Definition: mathp.h:167
TypeModifier
Definition: mathtyp.h:56

Here is the call graph for this function:

TypedValue MathParser::stmtlist ( void  )
protected

Definition at line 4253 of file mathp.cc.

References currtoken, GetToken(), stmt(), and STMTSEP.

Referenced by evalfunc(), expr(), and GetLastStmt().

4254 {
4255  TypedValue d = stmt();
4256  if (currtoken == STMTSEP) {
4257  GetToken();
4258  return stmtlist();
4259  }
4260  return d;
4261 }
TypedValue stmtlist(void)
Definition: mathp.cc:4253
enum Token GetToken(void)
Definition: mathp.cc:2960
TypedValue stmt(void)
Definition: mathp.cc:3781
enum Token currtoken
Definition: mathp.h:364

Here is the call graph for this function:

int MathParser::TokenPop ( void  )
protected

Definition at line 1963 of file mathp.cc.

References currtoken, TypedValue::GetString(), MathParser::TokenVal::m_t, MathParser::TokenVal::m_v, NAME, namebuf, NUM, TokenStack, and value.

Referenced by GetToken().

1964 {
1965  if (TokenStack.empty()) {
1966  return 0;
1967  }
1968 
1969  TokenVal tv = TokenStack.top();
1970  TokenStack.pop();
1971 
1972  currtoken = tv.m_t;
1973  if (currtoken == NUM) {
1974  value = tv.m_v;
1975  } else if (currtoken == NAME) {
1976  // note: namebuf is expected to be large enough for value
1977  namebuf = tv.m_v.GetString();
1978  }
1979 
1980  return 1;
1981 }
std::string namebuf
Definition: mathp.h:358
enum Token currtoken
Definition: mathp.h:364
TypedValue value
Definition: mathp.h:361
std::stack< TokenVal > TokenStack
Definition: mathp.h:370

Here is the call graph for this function:

void MathParser::TokenPush ( enum Token  t)
protected

Definition at line 1950 of file mathp.cc.

References MathParser::TokenVal::m_t, MathParser::TokenVal::m_v, NAME, namebuf, NUM, TokenStack, and value.

Referenced by stmt().

1951 {
1952  TokenVal tv;
1953  tv.m_t = t;
1954  if (t == NUM) {
1955  tv.m_v = value;
1956  } else if (t == NAME) {
1957  tv.m_v = TypedValue(namebuf);
1958  }
1959  TokenStack.push(tv);
1960 }
std::string namebuf
Definition: mathp.h:358
TypedValue value
Definition: mathp.h:361
std::stack< TokenVal > TokenStack
Definition: mathp.h:370
void MathParser::trim_arg ( char *const  s)
protected

Definition at line 1865 of file mathp.cc.

References NO_OP.

Referenced by readplugin().

1866 {
1867  int i, l;
1868 
1869  for (i = 0; isspace(s[i]); ++i) {
1870  NO_OP;
1871  }
1872 
1873  l = strlen(&s[i]);
1874  if (i > 0) {
1875  memmove(s, &s[i], l + 1);
1876  }
1877 
1878  for (i = l - 1; isspace(s[i]); --i) {
1879  NO_OP;
1880  }
1881  s[i + 1] = '\0';
1882 }
#define NO_OP
Definition: myassert.h:74
TypedValue MathParser::unary ( void  )
protected

Definition at line 3555 of file mathp.cc.

References currtoken, expr(), GetToken(), MINUS, NOT, and PLUS.

Referenced by power().

3556 {
3557  switch (currtoken) {
3558  case MINUS:
3559  GetToken();
3560  return -expr();
3561 
3562  case PLUS:
3563  GetToken();
3564  return expr();
3565 
3566  case NOT:
3567  GetToken();
3568  return !expr();
3569 
3570  default:
3571  return expr();
3572  }
3573 }
enum Token GetToken(void)
Definition: mathp.cc:2960
enum Token currtoken
Definition: mathp.h:364
TypedValue expr(void)
Definition: mathp.cc:3691

Here is the call graph for this function:

Member Data Documentation

bool MathParser::bRedefineVars

Definition at line 288 of file mathp.h.

Referenced by stmt().

enum Token MathParser::currtoken
protected
StaticNameSpace* MathParser::defaultNameSpace
protected

Definition at line 348 of file mathp.h.

Referenced by expr(), MathParser(), and stmt().

InputStream* MathParser::in

Definition at line 295 of file mathp.h.

Referenced by Get(), GetForever(), GetLastStmt(), GetLineNumber(), GetToken(), and readplugin().

std::string MathParser::namebuf
protected

Definition at line 358 of file mathp.h.

Referenced by expr(), GetToken(), stmt(), TokenPop(), and TokenPush().

NameSpaceMap MathParser::nameSpaceMap
protected

Definition at line 355 of file mathp.h.

Referenced by expr(), GetNameSpace(), GetNameSpaceMap(), RegisterNameSpace(), stmt(), and ~MathParser().

const char MathParser::ONE_LINE_REMARK = '#'
static

Definition at line 188 of file mathp.h.

Referenced by GetToken(), and skip_remarks().

struct MathParser::PlugInRegister * MathParser::PlugIns
protected
Table& MathParser::table

Definition at line 287 of file mathp.h.

Referenced by expr(), GetSymbolTable(), InsertSym(), PutSymbolTable(), readplugin(), and stmt().

std::stack<TokenVal> MathParser::TokenStack
protected

Definition at line 370 of file mathp.h.

Referenced by TokenPop(), and TokenPush().

TypedValue MathParser::value
protected

Definition at line 361 of file mathp.h.

Referenced by expr(), GetToken(), TokenPop(), and TokenPush().


The documentation for this class was generated from the following files: