void kexLexer::GetLetterToken(char initial) { int c = initial; int i = 0; bool haschar = false; while(parser.CharCode()[c] == CHAR_LETTER || (haschar && parser.CharCode()[c] == CHAR_NUMBER)) { token[i++] = c; c = GetChar(); haschar = true; } tokentype = TK_IDENIFIER; #ifdef SC_DEBUG SC_DebugPrintf("get letter (%s)\n", token); #endif Rewind(); CheckKeywords(); }
//--------------------------------------------------------------------------- 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); }
//--------------------------------------------------------------------------- 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); }
//--------------------------------------------------------------------------- void Calc() { mecChar_t szLine[100]; mecFloat_t fVal = 0, afVarVal[] = { 1, 2, 7.2f, -2.1f }; // Values of the parser variables mecParserHandle_t hParser; mecEvalFun_t pFunEval = NULL; hParser = mecCreate(); // initialize the parser Intro(hParser); // Set an error handler [optional] // the only function that does not take a parser instance handle mecSetErrorHandler(hParser, OnError); //#define GERMAN_LOCALS #ifdef GERMAN_LOCALS mecSetArgSep(hParser, ';'); mecSetDecSep(hParser, ','); mecSetThousandsSep(hParser, '.'); #else mecSetArgSep(hParser, ','); mecSetDecSep(hParser, '.'); #endif // Define parser variables and bind them to C++ variables [optional] mecDefineConst(hParser, _T("const1"), 1); mecDefineConst(hParser, _T("const2"), 2); // Define parser variables and bind them to C++ variables [optional] mecDefineVar(hParser, _T("a"), &afVarVal[0]); mecDefineVar(hParser, _T("b"), &afVarVal[1]); mecDefineVar(hParser, _T("c"), &afVarVal[2]); mecDefineVar(hParser, _T("d"), &afVarVal[3]); // Define infix operator [optional] mecDefineInfixOprt(hParser, _T("!"), Not, 0); // Define functions [optional] mecDefineFun0(hParser, _T("zero"), ZeroArg, 0); mecDefineFun1(hParser, _T("rnd"), Rnd, 0); // Add an unoptimizeable function mecDefineFun2(hParser, _T("dump"), DebugDump, 0); // Define binary operators [optional] mecDefineOprt(hParser, _T("add"), Add, 0, mecOPRT_ASCT_LEFT, 0); mecDefineOprt(hParser, _T("mul"), Mul, 1, mecOPRT_ASCT_LEFT, 0); #ifdef _DEBUG mecDebugDump(1, 0); #endif while (myfgets(szLine, 99, stdin)) { szLine[mystrlen(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 } // Set the expression mecSetExpr(hParser, szLine); // Compile the expression and get the pointer to the // just in time compiled eval function pFunEval = mecDbgCompile(hParser, -1); if (pFunEval == NULL) { continue; } // calculate the expression fVal = pFunEval(); /* alternative: fVal = mecEval(hParser); // 1st time parse from string and compile expression fVal = mecEval(hParser); // 2nd time parse from JIT code (handled internally) */ // Without an Error handler function // you must use this for error treatment: //if (mecError(hParser)) //{ // printf("\nError:\n"); // printf("------\n"); // printf("Message: %s\n", mecGetErrorMsg(hParser) ); // printf("Token: %s\n", mecGetErrorToken(hParser) ); // printf("Position: %s\n", mecGetErrorPos(hParser) ); // printf("Errc: %s\n", mecGetErrorCode(hParser) ); // continue; //} if (!mecError(hParser)) { myprintf(_T("%f\n"), fVal); } } // while // finalle free the parser ressources mecRelease(hParser); }