Esempio n. 1
0
muParserHandle_t InitCalc()
{
	muParserHandle_t hParser = mupCreate(muBASETYPE_FLOAT);              // initialize the parser

	// Set an error handler [optional]
	mupSetErrorHandler(hParser, OnError);

	// Define postfix operators [optional]
	mupDefinePostfixOprt(hParser, "!", Fact, 0);

	// Define infix operator [optional]
	mupDefineInfixOprt(hParser, "~", Not, 0);

	// Define binary operators [optional]
	mupDefineOprt(hParser, "%", Mod, 0, muOPRT_ASCT_LEFT, 0);

	// Define functions [optional]
	mupDefineFun2(hParser, "angle", Angle, 2);
	mupDefineFun4(hParser, "pramp", PRamp, 4);
	mupDefineFun4(hParser, "periodic1", Periodic1, 4);
	mupDefineFun4(hParser, "periodic2", Periodic2, 4);
	mupDefineFun3(hParser, "pulse", Pulse, 3);

	return hParser;
}
Esempio n. 2
0
void muexpr_init (MuExpr *me, const char *exprstr)
{
    me->nvars=0;
    me->npars=0;
    me->hparser=mupCreate(muBASETYPE_FLOAT);
    mupSetExpr (me->hparser, exprstr);
}
Esempio n. 3
0
//---------------------------------------------------------------------------
void CalcBulk()
{
  int nBulkSize = 200, i;
  muFloat_t *x = (muFloat_t*)malloc(nBulkSize * sizeof(muFloat_t));
  muFloat_t *y = (muFloat_t*)malloc(nBulkSize * sizeof(muFloat_t));
  muFloat_t *r = (muFloat_t*)malloc(nBulkSize * sizeof(muFloat_t));

  muParserHandle_t hParser = mupCreate(muBASETYPE_FLOAT);              // initialize the parser
  
  for (i=0; i<nBulkSize; ++i)
  {
    x[i] = i;
    y[i] = i;
    r[i] = 0;
  }

  mupDefineVar(hParser, "x", x);  
  mupDefineVar(hParser, "y", y);
  mupDefineBulkFun1(hParser, "bulktest", BulkTest);
  mupSetExpr(hParser, "bulktest(x+y)");
  mupEvalBulk(hParser, r, nBulkSize);
  if (mupError(hParser))
  {
    printf("\nError:\n");
    printf("------\n");
    printf("Message:  %s\n", mupGetErrorMsg(hParser) );
    printf("Token:    %s\n", mupGetErrorToken(hParser) );
    printf("Position: %d\n", mupGetErrorPos(hParser) );
    printf("Errc:     %d\n", mupGetErrorCode(hParser) );
    return;
  }

  for (i=0; i<nBulkSize; ++i)
  {
    printf("%d: bulkfun(%2.2f + %2.2f) = %2.2f\n", i, x[i], y[i], r[i]);
    x[i] = i;
    y[i] = (muFloat_t)i/10;
  }

  free(x);
  free(y);
  free(r);
}
Esempio n. 4
0
//---------------------------------------------------------------------------
void Calc()
{
  muChar_t szLine[100];
  muFloat_t fVal = 0,
            afVarVal[] = { 1, 2 }; // Values of the parser variables
  muParserHandle_t hParser;

  hParser = mupCreate(muBASETYPE_FLOAT);              // initialize the parser
  Intro(hParser);

  // Set an error handler [optional]
  // the only function that does not take a parser instance handle
  mupSetErrorHandler(hParser, OnError);

//#define GERMAN_LOCALS
#ifdef GERMAN_LOCALS
  mupSetArgSep(hParser, ';');
  mupSetDecSep(hParser, ',');
  mupSetThousandsSep(hParser, '.');
#else
  mupSetArgSep(hParser, ',');
  mupSetDecSep(hParser, '.');
#endif

  // Set a variable factory
  mupSetVarFactory(hParser, AddVariable, NULL);

  // Define parser variables and bind them to C++ variables [optional]
  mupDefineConst(hParser, "const1", 1);  
  mupDefineConst(hParser, "const2", 2);
  mupDefineStrConst(hParser, "strBuf", "Hallo welt");

  // Define parser variables and bind them to C++ variables [optional]
  mupDefineVar(hParser, "a", &afVarVal[0]);  
  mupDefineVar(hParser, "b", &afVarVal[1]);

  // Define postfix operators [optional]
  mupDefinePostfixOprt(hParser, "M", Mega, 0);
  mupDefinePostfixOprt(hParser, "m", Milli, 0);

  // Define infix operator [optional]
  mupDefineInfixOprt(hParser, "!", Not, 0);

  // Define functions [optional]
//  mupDefineStrFun(hParser, "query", SampleQuery, 0); // Add an unoptimizeable function 
  mupDefineFun0(hParser, "zero", ZeroArg, 0);
  mupDefineFun1(hParser, "rnd", Rnd, 0);             // Add an unoptimizeable function
  mupDefineFun1(hParser, "rnd2", Rnd, 1); 
  mupDefineMultFun(hParser, "_sum", Sum, 0);  // "sum" is already a default function

  // Define binary operators [optional]
  mupDefineOprt(hParser, "add", Add, 0, muOPRT_ASCT_LEFT, 0);
  mupDefineOprt(hParser, "mul", Mul, 1, muOPRT_ASCT_LEFT, 0);

  while ( fgets(szLine, 99, stdin) )
  {
    szLine[strlen(szLine)-1] = 0; // overwrite the newline

    switch(CheckKeywords(szLine, hParser))
    {
    case  0:  break;       // no keyword found; parse the line
    case  1:  continue;    // A Keyword was found do not parse the line
    case -1:  return;      // abort the application
    }

    mupSetExpr(hParser, szLine);

    fVal = mupEval(hParser);

  
    // Without an Error handler function 
    // you must use this for error treatment:
    //if (mupError(hParser))
    //{
    //  printf("\nError:\n");
    //  printf("------\n");
    //  printf("Message:  %s\n", mupGetErrorMsg(hParser) );
    //  printf("Token:    %s\n", mupGetErrorToken(hParser) );
    //  printf("Position: %s\n", mupGetErrorPos(hParser) );
    //  printf("Errc:     %d\n", mupGetErrorCode(hParser) );
    //  continue;
    //}

    if (!mupError(hParser))
      printf("%f\n", fVal);

  } // while 

  // finalle free the parser ressources
  mupRelease(hParser);
}
Esempio n. 5
0
//---------------------------------------------------------------------------
void Calc()
{
  muChar_t szLine[100];
  muFloat_t fVal = 0,
            afVarVal[] = { 1, 2 }; // Values of the parser variables
  muParserHandle_t hParser;

  hParser = mupCreate();              // initialize the parser

  // Set an error handler [optional]
  // the only function that does not take a parser instance handle
  mupSetErrorHandler(OnError);
  
  // Set a variable factory
  mupSetVarFactory(hParser, AddVariable, NULL);

  // Define parser variables and bind them to C++ variables [optional]
  mupDefineConst(hParser, "const1", 1);  
  mupDefineConst(hParser, "const2", 2);
  mupDefineStrConst(hParser, "strBuf", "Hallo welt");

  // Define parser variables and bind them to C++ variables [optional]
  mupDefineVar(hParser, "a", &afVarVal[0]);  
  mupDefineVar(hParser, "b", &afVarVal[1]);

  // Define postfix operators [optional]
  mupDefinePostfixOprt(hParser, "M", Mega, 0);
  mupDefinePostfixOprt(hParser, "m", Milli, 0);

  // Define infix operator [optional]
  mupDefineInfixOprt(hParser, "!", Not, 0);

  // Define functions [optional]
//  mupDefineStrFun(hParser, "query", SampleQuery, 0); // Add an unoptimizeable function 
  mupDefineFun1(hParser, "rnd", Rnd, 0);             // Add an unoptimizeable function
  mupDefineFun1(hParser, "rnd2", Rnd, 1); 
  mupDefineMultFun(hParser, "_sum", Sum, 0);  // "sum" is already a default function

  // Define binary operators [optional]
  mupDefineOprt(hParser, "add", Add, 0, 0);
  mupDefineOprt(hParser, "mul", Mul, 1, 0);

  while ( fgets(szLine, 99, stdin) )
  {
    szLine[strlen(szLine)-1] = 0; // overwrite the newline

    if (CheckKeywords(szLine, hParser)) 
      continue;

    mupSetExpr(hParser, szLine);

    fVal = mupEval(hParser);

/*  
    // Without an Error handler function 
    // you must use this for error treatment:
    if (mupError())
    {
      printf("\nError:\n");
      printf("------\n");
      printf("Message:  %s\n", mupGetErrorMsg() );
      printf("Token:    %s\n", mupGetErrorToken() );
      printf("Position: %s\n", mupGetErrorPos() );
      printf("Errc:     %s\n", mupGetErrorCode() );
      continue;
    }
*/
    if (!mupError())
      printf("%f\n", fVal);

  } // while 

  // finalle free the parser ressources
  mupRelease(hParser);
}