コード例 #1
0
  //------------------------------------------------------------------------------
  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 smax(-1e30), sval(0);
	for (int i=0; i<a_iArgc; ++i)
	{
	  switch(a_pArg[i]->GetType())
	  {
	  case 'f': sval = a_pArg[i]->GetFloat();   break;
	  case 'i': sval = 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);
		}
	  }
	  smax = max(smax, sval);
	}

	*ret = smax;
  }
コード例 #2
0
//---------------------------------------------------------------------------
bool IValue::operator<=(const IValue &a_Val) const
{
    char_type type1 = GetType(),
        type2 = a_Val.GetType();

    if (type1 == type2 || (IsScalar() && a_Val.IsScalar()))
    {
        switch (GetType())
        {
        case 's': return GetString() <= a_Val.GetString();
        case 'i':
        case 'f':
        case 'c': return GetFloat() <= a_Val.GetFloat();
        case 'b': return GetBool() <= a_Val.GetBool();
        default:
            ErrorContext err;
            err.Errc = ecINTERNAL_ERROR;
            err.Pos = -1;
            err.Type1 = GetType();
            err.Type2 = a_Val.GetType();
            throw ParserError(err);

        } // switch this type
    }
    else
    {
        ErrorContext err;
        err.Errc = ecTYPE_CONFLICT_FUN;
        err.Arg = (type1 != 'f' && type1 != 'i') ? 1 : 2;
        err.Type1 = type2;
        err.Type2 = type1;
        throw ParserError(err);
    }
}
コード例 #3
0
  /** \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;
  }
コード例 #4
0
ファイル: parseFmttrs.c プロジェクト: acekiller/MasterThesis
/* this parsing rule allows compatibility with RTC fixed array formats
   like [17:char].  note it doesn't handle more general formats like
   [3,4:char]. use of these formats is deprecated: use, for example,
   [char:17] instead */
static FORMAT_PTR Backwards_Fixed_Array_Format(Format_Parse_Ptr parser, 
					       BOOLEAN *error)
{ 
  FORMAT_PTR Form, next_format;
  TokenPtr tmp, arraySizeToken;
  
  arraySizeToken = NextToken(parser);
  if (INT_TOK != arraySizeToken->Type) {
    ParserError(arraySizeToken, parser, "an integer value");
    return NULL;
  }
  tmp = NextToken(parser);
  if (COLON_TOK != tmp->Type) {
    ParserError(tmp, parser, "':'");
    return NULL;
  }
  next_format = Parse(parser, FALSE, error); /* Formatter */
  tmp = NextToken(parser);
  if (RBRACK_TOK != tmp->Type) {
    ParserError(tmp, parser, "']'");
    return NULL;
  }

  Form = new_a_formatter(FixedArrayFMT, 3);
  Form->formatter.a[1].f = next_format;
  Form->formatter.a[2].i = arraySizeToken->value.num;
  
  return Form;
}
コード例 #5
0
  //-----------------------------------------------------------
  void OprtShr::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int num)
  {
	  assert(num==2);

	  if (!a_pArg[0]->IsScalar())
		  throw ParserError(ErrorContext(ecTYPE_CONFLICT_FUN, GetExprPos(), GetIdent(), a_pArg[0]->GetType(), 'i', 1));

	  if (!a_pArg[1]->IsScalar())
		  throw ParserError(ErrorContext(ecTYPE_CONFLICT_FUN, GetExprPos(), GetIdent(), a_pArg[1]->GetType(), 'i', 2));

	  float_type a = a_pArg[0]->GetFloat(),
		  b = a_pArg[1]->GetFloat();

	  if (a!=(int_type)a)
		  throw ParserError( ErrorContext(ecTYPE_CONFLICT_FUN, GetExprPos(), a_pArg[0]->GetIdent(), a_pArg[0]->GetType(), 'i', 1) ); 

	  if (b!=(int_type)b)
		  throw ParserError( ErrorContext(ecTYPE_CONFLICT_FUN, GetExprPos(), a_pArg[1]->GetIdent(), a_pArg[1]->GetType(), 'i', 2) ); 

	  float_type result = a*std::pow(2, -b);
	  int numDigits = std::numeric_limits<float_type>::digits10;

	  if (std::fabs(result) >= std::fabs(std::pow(10.0, numDigits)))
		  throw ParserError(ErrorContext(ecOVERFLOW, GetExprPos(), GetIdent()));

	  if (result>0)
		  *ret = std::floor(result);
	  else
		  *ret = std::ceil(result);
  }
コード例 #6
0
ファイル: mpOprtCmplx.cpp プロジェクト: boussaffawalid/OTB
  //-----------------------------------------------------------------------------------------------
  void OprtSubCmplx::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int num)
  { 
    assert(num==2);
    
    const IValue *arg1 = a_pArg[0].Get();
    const IValue *arg2 = a_pArg[1].Get();
    if ( a_pArg[0]->IsNonComplexScalar() && a_pArg[1]->IsNonComplexScalar())
    {
      *ret = arg1->GetFloat() - arg2->GetFloat(); 
    }
    else if (a_pArg[0]->GetType()=='m' && a_pArg[1]->GetType()=='m')
    {
      // Matrix + Matrix
      *ret = arg1->GetArray() - arg2->GetArray();
    }
    else
    {
      if (!a_pArg[0]->IsScalar())
        throw ParserError( ErrorContext(ecTYPE_CONFLICT_FUN, GetExprPos(), GetIdent(), a_pArg[0]->GetType(), 'c', 1)); 

      if (!a_pArg[1]->IsScalar())
        throw ParserError( ErrorContext(ecTYPE_CONFLICT_FUN, GetExprPos(), GetIdent(), a_pArg[1]->GetType(), 'c', 2)); 
      
      *ret = cmplx_type(a_pArg[0]->GetFloat() - a_pArg[1]->GetFloat(),
                        a_pArg[0]->GetImag() - a_pArg[1]->GetImag()); 
    }
  }
コード例 #7
0
ファイル: mpOprtIndex.cpp プロジェクト: QAndot/muparser
  /** \brief Index operator implementation
      \param ret A reference to the return value
      \param a_pArg Pointer to an array with the indices as ptr_val_type
      \param a_iArgc Number of indices (=dimension) actully used in the expression found. This must 
             be 1 or 2 since three dimensional data structures are not supported by muParserX.
  */
  void OprtIndex::At(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc)
  {
    try
    {
      // The index is -1, thats the actual variable reference
      if (a_iArgc!=a_pArg[-1]->GetDim())
      {
        throw ParserError(ErrorContext(ecINDEX_DIMENSION, -1, GetIdent()));
      }

      switch(a_iArgc)
      {
      case 1:
          ret.Reset(new Variable( &(ret->At(*a_pArg[0], Value(0))) ) );
          break;

      case 2:
          ret.Reset(new Variable( &(ret->At(*a_pArg[0], *a_pArg[1])) ) );
          break;

      default:
          throw ParserError(ErrorContext(ecINDEX_DIMENSION, -1, GetIdent()));
      }
    }
    catch(ParserError &exc)
    {
      exc.GetContext().Pos = GetExprPos();
      throw exc;
    }
  }
コード例 #8
0
ファイル: parseFmttrs.c プロジェクト: acekiller/MasterThesis
static FORMAT_PTR Enum_Format(Format_Parse_Ptr parser, BOOLEAN *error)
{ 
  TokenPtr Token;
  FORMAT_PTR Form, subform;
  LIST_PTR format_list;
  int num_formats, i, maxVal;
  
  num_formats = 0;
  Token = NextToken(parser);
  if (Token->Type == COLON_TOK) {
    Token = NextToken(parser);
    if (Token->Type != INT_TOK) {
      *error = TRUE;
      ParserError(Token, parser, "an integer");
      return NULL;
    } else {
      maxVal = Token->value.num;
    }
  } else {
    format_list = x_ipc_listCreate();
    do {
      if (num_formats > 0) Token = NextToken(parser);
      if (Token->Type != STR_TOK) {
	*error = TRUE;
	ParserError(Token, parser, "a string");
	return NULL;
      } else {
	Form = new_n_formatter(Token->value.str);
	/* More efficient for Lisp if all enum format names are upper case */
	LOCK_M_MUTEX;
	if (IS_LISP_MODULE()) {
	  upcase(Form->formatter.name);
	}
	UNLOCK_M_MUTEX;
	x_ipc_listInsertItem((char *)Form, format_list);
	num_formats++;
      }
      Token = NextToken(parser);
    } while (Token->Type == COMMA_TOK);
    UngetToken(parser, Token);
    maxVal = num_formats - 1;
  }
  
  Form = new_a_formatter(EnumFMT, num_formats+2);
  Form->formatter.a[1].i = maxVal;
  if (num_formats > 0) {
    /* Index from high to low since "format_list" 
       has formatters in reverse order */
    subform = (FORMAT_PTR)x_ipc_listFirst(format_list);
    for(i=num_formats;i>0;i--) {
      Form->formatter.a[i+1].f = subform;
      subform = (FORMAT_PTR)x_ipc_listNext(format_list);
    }
    x_ipc_listFree(&format_list);
  }
  return Form;
}
コード例 #9
0
ファイル: parseFmttrs.c プロジェクト: acekiller/MasterThesis
static FORMAT_PTR Fixed_Array_Format(Format_Parse_Ptr parser, BOOLEAN *error)
{ 
  FORMAT_PTR Form, next_format;
  TokenPtr Token, tmp;
  int NumberOfIndexes, Continue, array_index;
  
  tmp = parser->TokenList;
  if (INT_TOK == tmp->Type) {
    return Backwards_Fixed_Array_Format(parser, error);
  }
  next_format = Parse(parser, FALSE, error); /* Formatter */
  
  Token = NextToken(parser);
  if (Token->Type != COLON_TOK) {
    ParserError(Token, parser, "':'");
    return NULL;
  }
  
  tmp = parser->TokenList;
  NumberOfIndexes = 0;
  Continue = TRUE;
  do {
    if (tmp->Type != INT_TOK) {
      ParserError(tmp, parser, "an integer value");
      return NULL;
    }
    else {
      NumberOfIndexes++;
      tmp = tmp->next;
      if (tmp->Type == COMMA_TOK) {
	tmp = tmp->next;
	Continue = TRUE;
      } else if (tmp->Type == RBRACK_TOK) {
	Continue = FALSE;
      } else {
	ParserError(tmp, parser, "a ',' or ']'");
	return NULL;
      }
    }
  } while (Continue);
  
  Form = new_a_formatter(FixedArrayFMT, NumberOfIndexes+2);
  Form->formatter.a[1].f = next_format;
  
  /* this time munch tokens */
  
  NumberOfIndexes += 2;
  for (array_index=2; array_index < NumberOfIndexes; array_index++) {
    Token = NextToken(parser); /* This is an INT_TOK */
    Form->formatter.a[array_index].i = Token->value.num;
    
    Token = NextToken(parser); /* This is a COMMA_TOK or a RBRACK_TOK */
  }
  
  return Form;
}
コード例 #10
0
ファイル: mpOprtNonCmplx.cpp プロジェクト: QAndot/muparser
  /** \brief Implements the Division operator. 
      \throw ParserError in case one of the arguments if 
             nonnumeric or an array.
  
  */
  void OprtDiv::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int num)
  { 
    assert(num==2);

    if (!a_pArg[0]->IsNonComplexScalar())
      throw ParserError( ErrorContext(ecTYPE_CONFLICT_FUN, -1, GetIdent(), a_pArg[0]->GetType(), 'f', 1)); 

    if (!a_pArg[1]->IsNonComplexScalar())
      throw ParserError( ErrorContext(ecTYPE_CONFLICT_FUN, -1, GetIdent(), a_pArg[1]->GetType(), 'f', 2)); 
    
    *ret = a_pArg[0]->GetFloat() / a_pArg[1]->GetFloat();
  }
コード例 #11
0
ファイル: mpOprtNonCmplx.cpp プロジェクト: QAndot/muparser
  //-----------------------------------------------------------
  void OprtMul::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int num)
  { 
    assert(num==2);
    IValue *arg1 = a_pArg[0].Get();
    IValue *arg2 = a_pArg[1].Get();
    if (arg1->GetType()=='m' && arg2->GetType()=='m')
    {
      // Scalar multiplication
      matrix_type a1 = arg1->GetArray();
      matrix_type a2 = arg2->GetArray();

      if (a1.GetRows()!=a2.GetRows())
        throw ParserError(ErrorContext(ecARRAY_SIZE_MISMATCH, -1, GetIdent(), 'm', 'm', 2));

      float_type val(0);
      for (int i=0; i<a1.GetRows(); ++i)
        val += a1.At(i).GetFloat()*a2.At(i).GetFloat();

      *ret = val;
    }
    else if (arg1->GetType()=='m' && arg2->IsNonComplexScalar())
    {
      // Skalar * Vector
      matrix_type out(a_pArg[0]->GetArray());
      for (int i=0; i<out.GetRows(); ++i)
        out.At(i) = out.At(i).GetFloat() * arg2->GetFloat();

      *ret = out; 
    }
    else if (arg2->GetType()=='m' && arg1->IsNonComplexScalar())
    {
      // Vector * Skalar
      matrix_type out(arg2->GetArray());
      for (int i=0; i<out.GetRows(); ++i)
        out.At(i) = out.At(i).GetFloat() * arg1->GetFloat();

      *ret = out; 
    }
    else
    {
      if (!arg1->IsNonComplexScalar())
        throw ParserError( ErrorContext(ecTYPE_CONFLICT_FUN, -1, GetIdent(), arg1->GetType(), 'f', 1)); 

      if (!arg2->IsNonComplexScalar())
        throw ParserError( ErrorContext(ecTYPE_CONFLICT_FUN, -1, GetIdent(), arg2->GetType(), 'f', 2)); 

      *ret = arg1->GetFloat() * arg2->GetFloat(); 
    }
  }
コード例 #12
0
/** \brief Check wheter a token at a given position is an undefined variable.
    \param a_Tok [out] If a variable tom_pParser->m_vStringBufken has been found it will be placed here.
    \return true if a variable token has been found.
    \throw nothrow
    */
bool TokenReader::IsUndefVarTok(ptr_tok_type &a_Tok)
{
    string_type sTok;
    int iEnd = ExtractToken(m_pParser->ValidNameChars(), sTok, m_nPos);
    if (iEnd == m_nPos || (sTok.size() > 0 && sTok[0] >= _T('0') && sTok[0] <= _T('9')))
        return false;

    if (m_nSynFlags & noVAR)
    {
        ErrorContext err;
        err.Errc = ecUNEXPECTED_VAR;
        err.Ident = sTok;
        err.Expr = m_sExpr;
        err.Pos = m_nPos;
        throw ParserError(err);
    }

    // Create a variable token
    if (m_pParser->m_bAutoCreateVar)
    {
        ptr_val_type val(new Value);                   // Create new value token
        m_pDynVarShadowValues->push_back(val);         // push to the vector of shadow values 
        a_Tok = ptr_tok_type(new Variable(val.Get())); // bind variable to the new value item
        (*m_pVarDef)[sTok] = a_Tok;                    // add new variable to the variable list
    }
    else
        a_Tok = ptr_tok_type(new Variable(nullptr));      // bind variable to empty variable

    a_Tok->SetIdent(sTok);
    m_UsedVar[sTok] = a_Tok;     // add new variable to used-var-list

    m_nPos = iEnd;
    m_nSynFlags = noVAL | noVAR | noFUN | noBO | noIFX;
    return true;
}
コード例 #13
0
  void OprtCastToStr::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int /*a_iArgc*/)
  {
	  CodaScriptBackingStore* Store = a_pArg[0]->GetStore();
	  SME_ASSERT(Store);
	  char Buffer[0x512] = {0};

	  switch(Store->GetType())
	  {
	  case ICodaScriptDataStore::kDataType_Numeric:
		  sprintf_s(Buffer, sizeof(Buffer), "%0.6f", Store->GetNumber());
		  *ret = string_type(Buffer);
		  break;
	  case ICodaScriptDataStore::kDataType_Reference:
		  sprintf_s(Buffer, sizeof(Buffer), "%08X", Store->GetFormID());
		  *ret = string_type(Buffer);
		  break;
	  case ICodaScriptDataStore::kDataType_String:
		  *ret = string_type(Store->GetString());
		  break;
	  default:
		  {
			  ErrorContext err;
			  err.Errc = ecINVALID_TYPECAST;
			  err.Type1 =Store->GetType();
			  err.Type2 = ICodaScriptDataStore::kDataType_String;
			  throw ParserError(err);
		  }
	  }
  }
コード例 #14
0
ファイル: mpOprtNonCmplx.cpp プロジェクト: QAndot/muparser
  //------------------------------------------------------------------------------
  void OprtSign::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc)  
  { 
    MUP_ASSERT(a_iArgc==1);

    if (a_pArg[0]->IsScalar())
    {
      *ret = -a_pArg[0]->GetFloat();
    }
    else if (a_pArg[0]->GetType()=='m')
    {
      Value v(a_pArg[0]->GetRows(), 0);
      for (int i=0; i<a_pArg[0]->GetRows(); ++i)
      {
        v.At(i) = -a_pArg[0]->At(i).GetFloat();
      }
      *ret = v;
    }
    else
    {
        ErrorContext err;
        err.Errc = ecINVALID_TYPE;
        err.Type1 = a_pArg[0]->GetType();
        err.Type2 = 's';
        throw ParserError(err);
    }
  }
コード例 #15
0
  /** \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)
  {
    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;
  }
コード例 #16
0
ファイル: typeparser.cpp プロジェクト: qyvlik/LearnCompilers
void TypeParser::TypeDefineStatement(TokenStream *lexerStream, TypeSystem *__typeSystem)  throw(ParserError)
{
    // current is `LET
    // read next
    lexerStream->next();

    // 这里指向下一个token
    TypeSpecifier(lexerStream, __typeSystem);

    lexerStream->next();

    if(lexerStream->current().value() == "as") {
        lexerStream->next();

        if(__typeSystem->isRegisterType(lexerStream->current().value())) {
            __typeSystem->registerType(lexerStream->current().value(),
                                       __typeSystem->getHelper()->takeCurrentArgumentTypeMetaData());
            __typeSystem->mapTypeName(lexerStream->current().value(),
                                      __typeSystem->getHelper()->takeFullTypeName());

        } else {
            throw ParserError(-1, "Type "+lexerStream->current().value()+" Already exist.");
        }
        TypeName(lexerStream, __typeSystem);
        lexerStream->next();
    }
}
コード例 #17
0
 //---------------------------------------------------------------------------
 const SToken* ParserByteCode::GetBase() const
 {
   if (m_vRPN.size()==0)
     throw ParserError(ecINTERNAL_ERROR);
   else
     return &m_vRPN[0];
 }
コード例 #18
0
ファイル: mpValue.cpp プロジェクト: boussaffawalid/OTB
  /** \brief Return the value as an integer. 
    
    This function should only be called if you really need an integer value and
    want to make sure your either get one or throw an exception if the value
    can not be implicitely converted into an integer.
  */
  int_type Value::GetInteger() const
  {
    float_type v = m_val.real();

    if (m_cType!='i') //!IsScalar() || (int_type)v-v!=0)
    {
      ErrorContext err;
      err.Errc  = ecTYPE_CONFLICT;
      err.Type1 = m_cType;
      err.Type2 = 'i';
      
      if (GetIdent().length())
      {
        err.Ident = GetIdent();
      }
      else
      {
        stringstream_type ss;
        ss << *this;
        err.Ident = ss.str();
      }

      throw ParserError(err);
    }

    return (int_type)v;
  }
コード例 #19
0
ファイル: mpValue.cpp プロジェクト: boussaffawalid/OTB
  /** \brief Get the imaginary part of the value. 
      \throw ParserError in case this value represents a string or a matrix
  */
  float_type Value::GetImag() const
  {
    if (!IsScalar())
    {
      ErrorContext err;
      err.Errc  = ecTYPE_CONFLICT;
      err.Type1 = m_cType;
      err.Type2 = 'c';
      
      if (GetIdent().length())
      {
        err.Ident = GetIdent();
      }
      else
      {
        stringstream_type ss;
        ss << *this;
        err.Ident = ss.str();
      }

      throw ParserError(err);
    }

    return m_val.imag();
  }
コード例 #20
0
ファイル: mpValue.cpp プロジェクト: boussaffawalid/OTB
  //---------------------------------------------------------------------------
  IValue& Value::At(int nRow, int nCol)
  {
    if (IsMatrix())
    {
      if (nRow>=m_pvVal->GetRows() || nCol>=m_pvVal->GetCols() || nRow<0 || nCol<0)
        throw ParserError( ErrorContext(ecINDEX_OUT_OF_BOUNDS, -1, GetIdent()) ); 

      return m_pvVal->At(nRow, nCol);
    }
    else if (nRow==0 && nCol==0)
    {
      return *this;
    }
    else
      throw ParserError( ErrorContext(ecINDEX_OUT_OF_BOUNDS) ); 
  }
コード例 #21
0
ファイル: mpValue.cpp プロジェクト: boussaffawalid/OTB
  //---------------------------------------------------------------------------
  IValue& Value::operator+=(const IValue &val)
  {
    if (IsScalar() && val.IsScalar())
    {
      // Scalar/Scalar addition
      m_val += val.GetComplex();
      m_cType = (m_val.imag()==0) ? ( (m_val.real()==(int)m_val.real()) ? 'i' : 'f' ) : 'c';
    }
    else if (IsMatrix() && val.IsMatrix())
    {
      // Matrix/Matrix addition
      assert(m_pvVal);
      *m_pvVal += val.GetArray();
    }
    else if (IsString() && val.IsString())
    {
      // string/string addition
      assert(m_psVal);
      *m_psVal += val.GetString();
    }
    else
    {
      // Type conflict
      throw ParserError(ErrorContext(ecTYPE_CONFLICT_FUN, -1, _T("+"), GetType(), val.GetType(), 2));
    }

    return *this;
  }
コード例 #22
0
 //------------------------------------------------------------------------------
 void FunMax::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc)
 {
   float_type max(-1e30), val(0);
   for (int i=0; i<a_iArgc; ++i)
   {
     switch(a_pArg[i]->GetType())
     {
     case 'f': val = a_pArg[i]->GetFloat();   break;
     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;
 }
コード例 #23
0
ファイル: mpIOprt.cpp プロジェクト: QAndot/muparser
/** \brief Verify the operator prototype.

  Binary operators have the additional constraint that return type and the types
  of both arguments must be the same. So adding to floats can not produce a string
  and adding a number to a string is impossible.
*/
void IOprtBin::CheckPrototype(const string_type &a_sProt)
{
    if (a_sProt.length()!=4)
        throw ParserError( ErrorContext(ecAPI_INVALID_PROTOTYPE, -1, GetIdent() ) );

    //if (a_sProt[0]!=a_sProt[2] || a_sProt[0]!=a_sProt[3])
    //  throw ParserError( ErrorContext(ecAPI_INVALID_PROTOTYPE, -1, GetIdent() ) );
}
コード例 #24
0
StorageClassSpecifierNode::StorageClassSpecifierNode(int storageSpecKind ) 
{
	if ( StorageSpecifierKindStart  <= storageSpecKind && storageSpecKind <= StorageSpecifierKindEnd ) {
		specifier = storageSpecKind;
	}
	else {
		throw ParserError(ParserError::Whatever, "StorageClassSpecifierNode");
	}
}
コード例 #25
0
/** \brief Check if a string position contains a binary operator. */
bool TokenReader::IsOprt(ptr_tok_type &a_Tok)
{
    string_type sTok;
    int iEnd = ExtractToken(m_pParser->ValidOprtChars(), sTok, m_nPos);
    if (iEnd == m_nPos)
        return false;

    oprt_bin_maptype::reverse_iterator item;
    try
    {
        // Note:
        // All tokens in oprt_bin_maptype are have been sorted by their length
        // Long operators must come first! Otherwise short names (like: "add") that
        // are part of long token names (like: "add123") will be found instead 
        // of the long ones.
        // Length sorting is done with ascending length so we use a reverse iterator here.
        for (item = m_pOprtDef->rbegin(); item != m_pOprtDef->rend(); ++item)
        {
            if (sTok.find(item->first) != 0)
                continue;

            // operator found, check if we expect one...
            if (m_nSynFlags & noOPT)
            {
                // An operator was found but is not expected to occur at
                // this position of the formula, maybe it is an infix 
                // operator, not a binary operator. Both operator types
                // can use the same characters in their identifiers.
                if (IsInfixOpTok(a_Tok))
                    return true;

                // nope, it's no infix operator and we dont expect 
                // an operator
                throw ecUNEXPECTED_OPERATOR;
            }
            else
            {
                a_Tok = ptr_tok_type(item->second->Clone());

                m_nPos += (int)a_Tok->GetIdent().length();
                m_nSynFlags = noBC | noIO | noIC | noOPT | noCOMMA | noEND | noNEWLINE | noPFX | noIF | noELSE;
                return true;
            }
        }

        return false;
    }
    catch (EErrorCodes e)
    {
        ErrorContext err;
        err.Errc = e;
        err.Pos = m_nPos; // - (int)item->first.length();
        err.Ident = item->first;
        err.Expr = m_sExpr;
        throw ParserError(err);
    }
}
コード例 #26
0
/** \brief Check if a string position contains a unary post value operator. */
bool TokenReader::IsPostOpTok(ptr_tok_type &a_Tok)
{
    if (m_nSynFlags & noPFX)
    {
        // <ibg 2014-05-30/> Only look for postfix operators if they are allowed at the given position.
        //                   This will prevent conflicts with variable names such as: 
        //                   "sin(n)" where n is the postfix for "nano"
        return false;
        // </ibg>
    }

    // Tricky problem with equations like "3m+5":
    //     m is a postfix operator, + is a valid sign for postfix operators and 
    //     for binary operators parser detects "m+" as operator string and 
    //     finds no matching postfix operator.
    // 
    // This is a special case so this routine slightly differs from the other
    // token readers.

    // Test if there could be a postfix operator
    string_type sTok;
    int iEnd = ExtractToken(m_pParser->ValidOprtChars(), sTok, m_nPos);
    if (iEnd == m_nPos)
        return false;

    try
    {
        // iteraterate over all postfix operator strings
        oprt_pfx_maptype::const_iterator item;
        for (item = m_pPostOprtDef->begin(); item != m_pPostOprtDef->end(); ++item)
        {
            if (sTok.find(item->first) != 0)
                continue;

            a_Tok = ptr_tok_type(item->second->Clone());
            m_nPos += (int)item->first.length();

            if (m_nSynFlags & noPFX)
                throw ecUNEXPECTED_OPERATOR;

            m_nSynFlags = noVAL | noVAR | noFUN | noBO | noPFX /*| noIO*/ | noIF;
            return true;
        }

        return false;
    }
    catch (EErrorCodes e)
    {
        ErrorContext err;
        err.Errc = e;
        err.Pos = m_nPos - (int)a_Tok->GetIdent().length();
        err.Ident = a_Tok->GetIdent();
        err.Expr = m_sExpr;
        throw ParserError(err);
    }
}
コード例 #27
0
//---------------------------------------------------------------------------
IValue& IValue::operator=(const IValue &ref)
{
    if (this == &ref)
        return *this;

    switch (ref.GetType())
    {
    case 'i':
    case 'f':
    case 'c': return *this = cmplx_type(ref.GetFloat(), ref.GetImag());
    case 's': return *this = ref.GetString();
    case 'm': return *this = ref.GetArray();
    case 'b': return *this = ref.GetBool();
    case 'v':
        throw ParserError(_T("Assignment from void type is not possible"));

    default:
        throw ParserError(_T("Internal error: unexpected data type identifier in IValue& operator=(const IValue &ref)"));
    }
}
コード例 #28
0
/** \brief Check wheter a token at a given position is a variable token.
    \param a_Tok [out] If a variable token has been found it will be placed here.
    \return true if a variable token has been found.
    */
bool TokenReader::IsVarOrConstTok(ptr_tok_type &a_Tok)
{
    if (!m_pVarDef->size() && !m_pConstDef->size() && !m_pFunDef->size())
        return false;

    string_type sTok;
    int iEnd;
    try
    {
        iEnd = ExtractToken(m_pParser->ValidNameChars(), sTok, m_nPos);
        if (iEnd == m_nPos || (sTok.size() > 0 && sTok[0] >= _T('0') && sTok[0] <= _T('9')))
            return false;

        // Check for variables
        var_maptype::const_iterator item = m_pVarDef->find(sTok);
        if (item != m_pVarDef->end())
        {
            if (m_nSynFlags & noVAR)
                throw ecUNEXPECTED_VAR;

            m_nPos = iEnd;
            m_nSynFlags = noVAL | noVAR | noFUN | noBO | noIFX;
            a_Tok = ptr_tok_type(item->second->Clone());
            a_Tok->SetIdent(sTok);
            m_UsedVar[item->first] = item->second;  // Add variable to used-var-list
            return true;
        }

        // Check for constants
        item = m_pConstDef->find(sTok);
        if (item != m_pConstDef->end())
        {
            if (m_nSynFlags & noVAL)
                throw ecUNEXPECTED_VAL;

            m_nPos = iEnd;
            m_nSynFlags = noVAL | noVAR | noFUN | noBO | noIFX | noIO;
            a_Tok = ptr_tok_type(item->second->Clone());
            a_Tok->SetIdent(sTok);
            return true;
        }
    }
    catch (EErrorCodes e)
    {
        ErrorContext err;
        err.Errc = e;
        err.Pos = m_nPos;
        err.Ident = sTok;
        err.Expr = m_sExpr;
        throw ParserError(err);
    }

    return false;
}
コード例 #29
0
ファイル: XmlParser.cpp プロジェクト: makeclean/helios
void XmlParser::parseInputFile(const string& file) {
	/* Open document */
	ticpp::Document doc(file.c_str());
	try {
		/* Load document */
		doc.LoadFile();
		rootNode(doc.GetTiXmlPointer(),file);
	} catch(ticpp::Exception& ex) {
		throw(ParserError(ex.details.description + " at line " +
				toString(ex.details.line) + " in column " + toString(ex.details.column)));
	}
}
コード例 #30
0
ファイル: typeparser.cpp プロジェクト: qyvlik/LearnCompilers
void TypeParser::TypeDefinesStatement(TokenStream *lexerStream, TypeSystem *__typeSystem) throw(ParserError)
{
    while(lexerStream->current().value() == "let") {
        TypeDefineStatement(lexerStream, __typeSystem);
        if(lexerStream->current().value() != ";") {
            throw ParserError(-1, "Lost `;");
        } else {
            lexerStream->next();
        }
    }
    qDebug() << "OK";
}