/* reads the math expresion (in values) and its size*/ void readExpression(char **values, int *size) { fprintf(stdout, "[INFO] [READ]\tIntroduce the mathematical expression separated by space in\n"); fprintf(stdout, "\t\tpostfix form with operands in [a-zA-Z], operations in '+-*/'\n"); fprintf(stdout, "\t\tand use '#' for unary operations:\n"); fprintf(stdout, "[INFO] [READ]\t"); char *buffer = (char*) calloc(1024, sizeof(char)); fgets(buffer, 1024, stdin); *size = 0; while (*(buffer+*size) != '\n') { (*size)++; } *size = (*size+1)/2; char *result = (char*) calloc((*size+1), sizeof(char)); for (int i=0; i<*size; i++) { *(result+i) = *(buffer+i*2); } free(buffer); if (checkExpression(result)) { *values = result; } else { exitError("[READ]\tThe read expression contains invalid characters.\n"); } }
int main(int argc, char **argv){ std::string evalexp(argv[1]); // Check input expression for unhandling symbols if(!checkExpression(evalexp)) return -1; // Clear expression from spaces for(uint i=0 ; i < evalexp.length(); i++){ if(evalexp[i] == ' '){ evalexp.erase(evalexp.begin() + i); if(i > 0) i--; } } std::cout << "Evaluating expression is: \"" << evalexp << "\"" << std::endl; std::cout << "Result is: " << evalExpression<int>(evalexp) << std::endl; return 0; }
HRESULT STDMETHODCALLTYPE CAAFEssenceFileContainer::CreateEssenceStream (const aafCharacter * pName, aafMobID_constptr pMobID, IAAFEssenceStream ** ppEssenceStream) { HRESULT hr = S_OK; CAAFEssenceFileStream *pEssenceFileStream = NULL; // Validate return argument. if (NULL == ppEssenceStream) return E_INVALIDARG; try { // First see if the stream has already been opened. checkResult(CheckExistingStreams(pName, openNew)); // Create file stream object. pEssenceFileStream = CAAFEssenceFileStream::CreateFileStream(this); checkExpression(NULL != pEssenceFileStream, E_OUTOFMEMORY); // Temporarily reuse code for obsolete CAAFEssenceFileScream checkResult(pEssenceFileStream->Create(pName, pMobID)); // Return the interface to the stream to the caller. checkResult(pEssenceFileStream->QueryInterface(IID_IAAFEssenceStream, (void **)ppEssenceStream)); } catch (HRESULT& rResult) { hr = rResult; } // // If an error occurs the following release will delete the object. if (pEssenceFileStream) pEssenceFileStream->Release(); return hr; }
void Check_PriorDeclaration_Base::checkBaseExpression(const AST_BaseExpression::shared_ptr &stm) { switch (stm->baseExpressionType()) { case AST_BaseExpressionType::betStatements: { const AST_BaseStatement::shared_ptr& local = boost::dynamic_pointer_cast<AST_BaseStatement>(stm); checkStatement(local); break; } case AST_BaseExpressionType::betIdentifier: { const AST_Identifier::shared_ptr& local = boost::dynamic_pointer_cast<AST_Identifier>(stm); checkIdentifier(local); break; } case AST_BaseExpressionType::betExpression: { const AST_Expression::shared_ptr& local = boost::dynamic_pointer_cast<AST_Expression>(stm); checkExpression(local); break; } case AST_BaseExpressionType::betBoolExpression: { const AST_Boolean_Expression::shared_ptr& local = boost::dynamic_pointer_cast<AST_Boolean_Expression>(stm); checkBoolExpression(local); break; } case AST_BaseExpressionType::betBoolValue: { const AST_Boolean::shared_ptr& local = boost::dynamic_pointer_cast<AST_Boolean>(stm); checkBoolValue(local); break; } case AST_BaseExpressionType::betDoubleValue: { const AST_Double::shared_ptr& local = boost::dynamic_pointer_cast<AST_Double>(stm); checkDoubleValue(local); break; } case AST_BaseExpressionType::betIntegerValue: { const AST_Integer::shared_ptr& local = boost::dynamic_pointer_cast<AST_Integer>(stm); checkIntegerValue(local); break; } case AST_BaseExpressionType::betStringValue: { const AST_String::shared_ptr& local = boost::dynamic_pointer_cast<AST_String>(stm); checkStringValue(local); break; } default: break; } }
bool Mission::doTest(missionNode *node,int mode){ if(mode==SCRIPT_PARSE){ string teststr=node->attr_value("test"); if(teststr.empty()){ fatalError(node,mode,"you have to give test an argument what to test"); assert(0); } if(teststr=="gt"){ node->script.tester=TEST_GT; } else if(teststr=="lt"){ node->script.tester=TEST_LT; } else if(teststr=="eq"){ node->script.tester=TEST_EQ; } else if(teststr=="ne"){ node->script.tester=TEST_NE; } else if(teststr=="ge"){ node->script.tester=TEST_GE; } else if(teststr=="le"){ node->script.tester=TEST_LE; } #if 0 else if(teststr=="between"){ node->script.tester=TEST_BETWEEN; } #endif else { fatalError(node,mode,"unknown test argument for test"); assert(0); } vector<easyDomNode *>::const_iterator siter; #if 0 int i=0; for(siter= node->subnodes.begin() ; siter!=node->subnodes.end() && i<2; siter++){ missionNode *snode=(missionNode *)*siter; (node->script.test_arg)[i]=snode; } if(i<2){ fatalError(node,mode,"a test-expr needs exact two subnodes"); assert(0); } #endif int len=node->subnodes.size(); if(len!=2){ fatalError(node,mode,"a test-expr needs exact two subnodes"); assert(0); } node->script.test_arg[0]=(missionNode *)node->subnodes[0]; node->script.test_arg[1]=(missionNode *)node->subnodes[1]; } // end of parse varInst * arg1_vi=checkExpression(node->script.test_arg[0],mode); varInst * arg2_vi=checkExpression(node->script.test_arg[1],mode); bool res=false; if(arg1_vi->type!=arg2_vi->type){ fatalError(node,mode,"test is getting not the same types"); assert(0); } if(mode==SCRIPT_RUN){ if(arg1_vi->type==VAR_FLOAT){ double arg1=arg1_vi->float_val; double arg2=arg2_vi->float_val; switch(node->script.tester){ case TEST_GT: res=(arg1>arg2); break; case TEST_LT: res=(arg1<arg2); break; case TEST_EQ: res=(arg1==arg2); break; case TEST_NE: res=(arg1!=arg2); break; case TEST_GE: res=(arg1>=arg2); break; case TEST_LE: res=(arg1<=arg2); break; default: fatalError(node,mode,"no valid tester"); assert(0); } } else if(arg1_vi->type==VAR_INT){ int arg1=arg1_vi->int_val; int arg2=arg2_vi->int_val; switch(node->script.tester){ case TEST_GT: res=(arg1>arg2); break; case TEST_LT: res=(arg1<arg2); break; case TEST_EQ: res=(arg1==arg2); break; case TEST_NE: res=(arg1!=arg2); break; case TEST_GE: res=(arg1>=arg2); break; case TEST_LE: res=(arg1<=arg2); break; default: fatalError(node,mode,"no valid tester"); assert(0); } } else{ fatalError(node,mode,"no such type allowed for test"); assert(0); } }// SCRIPT_RUN deleteVarInst(arg1_vi); deleteVarInst(arg2_vi); return res; }
varInst * Mission::doMath(missionNode *node,int mode){ // if(mode==SCRIPT_PARSE){ string mathname=node->attr_value("math"); int len=node->subnodes.size(); if(len<2){ fatalError(node,mode,"math needs at least 2 arguments"); assert(0); } varInst *res_vi=newVarInst(VI_TEMP); varInst* res1_vi=checkExpression((missionNode *)node->subnodes[0],mode); if(res1_vi->type!=VAR_INT && res1_vi->type!=VAR_FLOAT && res1_vi->type!=VAR_ANY){ printf("res1_vi=%d\n",res1_vi->type); fatalError(node,mode,"only int or float expr allowed for math"); assert(0); } res_vi->type=res1_vi->type; assignVariable(res_vi,res1_vi); if(res_vi->type==VAR_ANY){ res_vi->type=VAR_FLOAT; } deleteVarInst(res1_vi); // char buffer[200]; //sprintf(buffer,"fmath: 1st expr returns %f",res); //debug(4,node,mode,buffer); for(int i=1;i<len;i++){ varInst * res2_vi=checkExpression((missionNode *)node->subnodes[i],mode); var_type res2_type=res2_vi->type; if(res2_type==VAR_INT && res_vi->type==VAR_FLOAT){ res2_type=VAR_FLOAT; if(mode==SCRIPT_RUN){ float res2=(float)res2_vi->int_val; float res=floatMath(mathname,res_vi->float_val,res2); res_vi->float_val=res; } } else if(res2_type==VAR_FLOAT && res_vi->type==VAR_INT){ res_vi->type=VAR_FLOAT; if(mode==SCRIPT_RUN){ res_vi->float_val=(float)res_vi->int_val; float res2=res2_vi->float_val; float res=floatMath(mathname,res_vi->float_val,res2); res_vi->float_val=res; } } else{ if(res_vi->type!=res2_type){ fatalError(node,mode,"can't do math on such types"); assert(0); } if(mode==SCRIPT_RUN){ if(res_vi->type==VAR_INT){ int res=intMath(mathname,res_vi->int_val,res2_vi->int_val); res_vi->int_val=res; } else if(res_vi->type==VAR_FLOAT){ float res=floatMath(mathname,res_vi->float_val,res2_vi->float_val); res_vi->float_val=res; } else{ if(res_vi->type!=res2_type){ fatalError(node,mode,"can't do math on such types"); assert(0); } } } // of SCRIPT_RUN } // else deleteVarInst(res2_vi); } // for arguments return res_vi; }
void TextModelLavaan::checkExpression(const QList<Token> &tokens, int &i, TextModelLavaan::BlockStatus *status) { const Token &token = tokens[i]; if (token.type == Variable) { i++; return; } else if (token.type == Number) { i++; return; } else if (token.type == FunctionOpen) { i++; if (i >= tokens.size()) { status->setError("Missing closing bracket", token.pos, token.text.length()); return; } Token next = tokens[i]; if (next.type == FunctionClose) { status->setError("Missing function arguments", token.pos, token.text.length()); return; } else if (next.type == FunctionOpen) { checkExpression(tokens, i, status); if (status->isError()) return; } else if (next.type == Number) { i++; for (;;) { if (i >= tokens.length()) break; next = tokens[i]; if (next.type != Comma) break; i++; if (i >= tokens.length()) break; next = tokens[i]; if (next.type != Number) { status->setError("Expected a number", next.pos, next.text.length()); return; } i++; } } else { status->setError("Expected a number (or vector)", next.pos, next.text.length()); } if (i >= tokens.length()) { status->setError("Missing closing bracket", token.pos, token.text.length()); return; } next = tokens[i]; if (next.type != FunctionClose) { status->setError("Expecting a closing bracket", next.pos, next.text.length()); return; } i++; } else { status->setError("Expected an expression", token.pos, token.text.length()); return; } }
QList<Token> TextModelLavaan::parse(QTextBlock &block) { BlockStatus *status = blockStatus(block); status->clearError(); QList<Token> tokens = tokenise(block); if (status->isError()) { qDebug() << "error : " << status->message << "\n"; return tokens; } if (tokens.length() == 0 || tokens.at(0).type == Comment) return tokens; Token &first = tokens[0]; if (first.type != Variable) { status->setError("Expected a variable", first.pos, first.text.length()); return tokens; } else if (tokens.length() == 1) { status->setError("Expected an operator", first.pos + first.text.length(), -1); return tokens; } if (tokens.length() >= 2) { Token &second = tokens[1]; if (second.type == Comment) { status->setError("Expected an operator", first.pos + first.text.length(), -1); return tokens; } else if (second.type == Operator) { if (second.text != "~~" && second.text != "~" && second.text != "=~" && second.text != "==" && second.text != ">" && second.text != "<" && second.text != ":=") { status->setError("Unrecognised operator", second.pos, second.text.length()); return tokens; } else if (tokens.length() == 2) { status->setError("Expected an expression", second.pos + second.text.length(), -1); return tokens; } } else { status->setError("Expected an operator", second.pos, second.text.length()); return tokens; } } int i = 2; checkExpression(tokens, i, status); if (status->isError()) return tokens; while (i < tokens.length()) { Token token = tokens.at(i); if (token.type == Comment) break; if (token.type != Plus) { status->setError("Expected a plus", token.pos, token.text.length()); return tokens; } i++; if (i >= tokens.length()) { status->setError("Expected an expression", token.pos + token.text.length(), -1); return tokens; } checkExpression(tokens, i, status); if (status->isError()) return tokens; } return tokens; }
varInst *Mission::call_omap(missionNode *node,int mode){ //varInst *viret=new varInst; varInst *viret=NULL; if(mode==SCRIPT_PARSE){ string cmd=node->attr_value("name"); node->script.method_id=module_omap_map[cmd]; } callback_module_omap_type method_id=(callback_module_omap_type) node->script.method_id; if(method_id==CMT_OMAP_new){ viret=call_omap_new(node,mode); return viret; } else{ varInst *ovi=getObjectArg(node,mode); omap_t *my_object=getOMapObject(node,mode,ovi); if(method_id==CMT_OMAP_delete){ if(mode==SCRIPT_RUN){ omap_t::iterator iter; for(iter=my_object->begin();iter!=my_object->end();iter++){ string varname=(*iter).first ; varInst *vi=(*iter).second; deleteVarInst(vi,true); } my_object->clear(); delete my_object; } viret=newVarInst(VI_TEMP); viret->type=VAR_VOID; } else if(method_id==CMT_OMAP_set){ missionNode *snode=getArgument(node,mode,2); //varInst *vi=doVariable(snode,mode); // should be getObjExpr varInst *var_vi=checkExpression(snode,mode); // should be getObjExpr string name=getStringArgument(node,mode,1); debug(3,node,mode,"omap set"); if(mode==SCRIPT_RUN){ varInst *push_vi=newVarInst(VI_IN_OBJECT); push_vi->type=var_vi->type; assignVariable(push_vi,var_vi); (*my_object)[name]=push_vi; //printf("setting [%s] type %d\n",name.c_str(),push_vi->type); } deleteVarInst(var_vi); viret=newVarInst(VI_TEMP); viret->type=VAR_VOID; //return viret; } else if(method_id==CMT_OMAP_get){ debug(3,node,mode,"omap.get"); string name=getStringArgument(node,mode,1); viret=newVarInst(VI_TEMP); viret->type=VAR_ANY; if(mode==SCRIPT_RUN){ varInst *back_vi=(*my_object)[name]; assignVariable(viret,back_vi); // printf("viret type=%d back_vi type=%d\n",viret->type,back_vi->type); if(back_vi->type>10){ assert(0); } deleteVarInst(back_vi); // this won't delete it } } else if(method_id==CMT_OMAP_toxml){ if(node->subnodes.size()!=1){ fatalError(node,mode,"olist.toxml needs no arguments"); assert(0); } debug(3,node,mode,"omap.toxml"); if(mode==SCRIPT_RUN){ //call_olist_toxml(node,mode,ovi); } viret =newVarInst(VI_TEMP); viret->type=VAR_VOID; } else if(method_id==CMT_OMAP_size){ if(node->subnodes.size()!=1){ fatalError(node,mode,"olist.size needs one arguments"); assert(0); } debug(3,node,mode,"omap.size"); viret=newVarInst(VI_TEMP); if(mode==SCRIPT_RUN){ int len=my_object->size(); viret->int_val=len; } viret->type=VAR_INT; //return viret; } else{ fatalError(node,mode,"unknown command "+node->script.name+" for callback omap"); assert(0); } deleteVarInst(ovi); return viret; } // else (objects) return NULL; // never reach }