Error parseOne() { read(); switch (Tok.K) { case Eof: return Error::success(); case KwExports: for (;;) { read(); if (Tok.K != Identifier) { unget(); return Error::success(); } if (Error Err = parseExport()) return Err; } case KwHeapsize: return parseNumbers(&Info.HeapReserve, &Info.HeapCommit); case KwStacksize: return parseNumbers(&Info.StackReserve, &Info.StackCommit); case KwLibrary: case KwName: { bool IsDll = Tok.K == KwLibrary; // Check before parseName. std::string Name; if (Error Err = parseName(&Name, &Info.ImageBase)) return Err; Info.ImportName = Name; // Set the output file, but don't override /out if it was already passed. if (Info.OutputFile.empty()) { Info.OutputFile = Name; // Append the appropriate file extension if not already present. if (!sys::path::has_extension(Name)) Info.OutputFile += IsDll ? ".dll" : ".exe"; } return Error::success(); } case KwVersion: return parseVersion(&Info.MajorImageVersion, &Info.MinorImageVersion); default: return createError("unknown directive: " + Tok.Value); } }
double Funktion::calculateFunction(double x){ if(function==NULL) return 0; if(function->leaf=="" && function->op<sizeof(binaryFunction)/sizeof(binaryFunction[0])){ Funktion a(function->left); Funktion b(function->right); switch(function->op){ case 0: return a.calculateFunction(x)+b.calculateFunction(x); case 1: return a.calculateFunction(x)-b.calculateFunction(x); case 2: return a.calculateFunction(x)*b.calculateFunction(x); case 3: return a.calculateFunction(x)/b.calculateFunction(x); case 4: return pow(a.calculateFunction(x),b.calculateFunction(x)); } } if(function->leaf=="" && function->op>=sizeof(binaryFunction)/sizeof(binaryFunction[0])){ Funktion a(function->left); switch(function->op){ case sizeof(binaryFunction)/sizeof(binaryFunction[0]): return exp(a.calculateFunction(x)); case sizeof(binaryFunction)/sizeof(binaryFunction[0])+1: return sin(a.calculateFunction(x)); case sizeof(binaryFunction)/sizeof(binaryFunction[0])+2: return cos(a.calculateFunction(x)); case sizeof(binaryFunction)/sizeof(binaryFunction[0])+3: return sqrt(a.calculateFunction(x)); case sizeof(binaryFunction)/sizeof(binaryFunction[0])+4: return log(a.calculateFunction(x)); } } if(function->leaf=="x") return x; return parseNumbers(function->leaf); }
Intervall parseTermOfIntervals(string input){ string::iterator it; int brackets = 0; int closedBrackets = 0; if(input=="") return 0; for(it=input.end();it>=input.begin();it--){ if(*it=='(') brackets++; if(*it==')') brackets--; if(*it=='[') closedBrackets++; if(*it==']') closedBrackets--; if(*it=='|' && brackets==0 && closedBrackets==0){ if(it==input.begin()){ //Bin‰rem Operator fehlt Operand errorWindow("Operand missing!"); return Intervall(NAN); } if(it+1==input.end()){ //Bin‰rem Operator fehlt Operand errorWindow("Operand missing!"); return Intervall(NAN); } string input1(input.begin(),it); string input2(it+1,input.end()); Intervall result = parseTermOfIntervals(input1)||parseTermOfIntervals(input2); if(result.getInf()!=result.getInf()){ errorWindow("Vereinigung disjunkter Intervalle!"); } return result; } } for(it=input.end();it>=input.begin();it--){ if(*it=='(') brackets++; if(*it==')') brackets--; if(*it=='[') closedBrackets++; if(*it==']') closedBrackets--; if(*it=='&' && brackets==0 && closedBrackets==0){ if(it==input.begin()){ //Bin‰rem Operator fehlt Operand errorWindow("Operand missing!"); return Intervall(NAN); } if(it+1==input.end()){ //Bin‰rem Operator fehlt Operand errorWindow("Operand missing!"); return Intervall(NAN); } string input1(input.begin(),it); string input2(it+1,input.end()); Intervall result = parseTermOfIntervals(input1)&&parseTermOfIntervals(input2); if(result.getInf()!=result.getInf()){ errorWindow("Schnitt disjunkter Intervalle!"); } return result; } } for(it=input.end();it>=input.begin();it--){ if(*it=='(') brackets++; if(*it==')') brackets--; if(*it=='[') closedBrackets++; if(*it==']') closedBrackets--; if(*it=='+' && brackets==0 && closedBrackets==0 && *(it-1)!='/' && *(it-1)!='*' && *(it-1)!='-' && *(it-1)!='+'){ if(it==input.begin()){ string input2(it+1,input.end()); return parseTermOfIntervals(input2); } if(it+1==input.end()){ //Bin‰rem Operator fehlt Operand// errorWindow("Operand missing!"); return Intervall(NAN); } string input1(input.begin(),it); string input2(it+1,input.end()); return parseTermOfIntervals(input1)+parseTermOfIntervals(input2); } if(*it=='-' && brackets==0 && closedBrackets==0 && *(it-1)!='/' && *(it-1)!='*' && *(it-1)!='-' && *(it-1)!='+'){ if(it==input.begin()){ string input2(it+1,input.end()); return Intervall(0)-parseTermOfIntervals(input2); } if(it+1==input.end()){ //Bin‰rem Operator fehlt Operand// errorWindow("Operand missing!"); return Intervall(NAN); } string input1(input.begin(),it); string input2(it+1,input.end()); return parseTermOfIntervals(input1)-parseTermOfIntervals(input2); } } it=input.begin(); if(*it=='('){ brackets++; while(brackets>0){ it++; if(*it=='(') brackets++; if(*it==')') brackets--; } it++; if(it==input.end()){ string input1(input.begin()+1,input.end()-1); return parseTermOfIntervals(input1); } if(*it=='*'){ string input1(input.begin()+1,it-1); if(it+1==input.end()){ //Bin‰rem Operator fehlt Operand// errorWindow("Operand missing!"); return Intervall(NAN); } string input2(it+1,input.end()); return parseTermOfIntervals(input1)*parseTermOfIntervals(input2); } if(*it=='/'){ string input1(input.begin()+1,it-1); if(it+1==input.end()){ //Bin‰rem Operator fehlt Operand// errorWindow("Operand missing!"); return Intervall(NAN); } string input2(it+1,input.end()); Intervall result2 = parseTermOfIntervals(input2); if(result2.getInf()<=0 && result2.getSup()>=0){ //Teilen durch 0! errorWindow("Division by zero!"); return Intervall(NAN); } return parseTermOfIntervals(input1)/result2; } } if(*it=='*' || *it=='/'){ //Bin‰rem Operator fehlt Operand// errorWindow("Operand missing!"); return Intervall(NAN); } closedBrackets = 0; if(*it=='['){ for(;it<input.end();it++){ if(*it=='[') closedBrackets++; if(*it==']') closedBrackets--; if(*it=='*' && closedBrackets==0){ string input1(input.begin(),it); string input2(it+1,input.end()); return parseIntervals(input1)*parseTermOfIntervals(input2); } if(*it=='/' && closedBrackets==0){ string input1(input.begin(),it); string input2(it+1,input.end()); Intervall result2 = parseTermOfIntervals(input2); if(result2.getInf()<=0 && result2.getSup()>=0){ //Teilen durch 0! errorWindow("Division by zero!"); return Intervall(NAN); } return parseIntervals(input1)/result2; } } return parseIntervals(input); } size_t found1 = input.find_first_of("*"); if(found1!=string::npos){ string input1 = input.substr(0,found1); string input2 = input.substr(found1+1); return parseNumbers(input1)*parseTermOfIntervals(input2); } size_t found2 = input.find_first_of("/"); if(found2!=string::npos){ string input1 = input.substr(0,found2); string input2 = input.substr(found2+1); Intervall result2 = parseTermOfIntervals(input2); if(result2.getInf()<=0 && result2.getSup()>=0){ //Teilen durch 0! errorWindow("Division by zero!"); return Intervall(NAN); } return parseNumbers(input1)/result2; } if(found1==string::npos && found2==string::npos){ return parseNumbers(input); } errorWindow("42: "+input); return Intervall(NAN); }
double parseTermOfNumbers(string input){ string::iterator it; int brackets = 0; if(input=="") return 0; for(it=input.end();it>=input.begin();it--){ if(*it=='(') brackets++; if(*it==')') brackets--; if(*it=='+' && brackets==0 && *(it-1)!='/' && *(it-1)!='*' && *(it-1)!='-' && *(it-1)!='+' && *(it-1)!='|' && *(it-1)!='&'){ if(it==input.begin()){ string input2(it+1,input.end()); return parseTermOfNumbers(input2); } if(it+1==input.end()){ //Bin‰rem Operator fehlt Operand// errorWindow("Operand missing!"); return double(NAN); } string input1(input.begin(),it); string input2(it+1,input.end()); return parseTermOfNumbers(input1)+parseTermOfNumbers(input2); } if(*it=='-' && brackets==0 && *(it-1)!='/' && *(it-1)!='*' && *(it-1)!='-' && *(it-1)!='+' && *(it-1)!='|' && *(it-1)!='&'){ if(it==input.begin()){ string input2(it+1,input.end()); return -parseTermOfNumbers(input2); } if(it+1==input.end()){ //Bin‰rem Operator fehlt Operand// errorWindow("Operand missing!"); return double(NAN); } string input1(input.begin(),it); string input2(it+1,input.end()); return parseTermOfNumbers(input1)-parseTermOfNumbers(input2); } } it=input.begin(); if(*it=='('){ brackets++; while(brackets>0){ it++; if(*it=='(') brackets++; if(*it==')') brackets--; } it++; if(it==input.end()){ string input1(input.begin()+1,input.end()-1); return parseTermOfNumbers(input1); } if(*it=='*'){ string input1(input.begin()+1,it-1); if(it+1==input.end()){ //Bin‰rem Operator fehlt Operand// errorWindow("Operand missing!"); return double(NAN); } string input2(it+1,input.end()); return parseTermOfNumbers(input1)*parseTermOfNumbers(input2); } if(*it=='/'){ string input1(input.begin()+1,it-1); if(it+1==input.end()){ //Bin‰rem Operator fehlt Operand// errorWindow("Operand missing!"); return double(NAN); } string input2(it+1,input.end()); double result2 = parseTermOfNumbers(input2); if(result2==0){ //Teilen durch 0! errorWindow("Division by zero!"); return double(NAN); } return parseTermOfNumbers(input1)/result2; } } if(*it=='*' || *it=='/'){ //Bin‰rem Operator fehlt Operand// errorWindow("Operand missing!"); return double(NAN); } size_t found1 = input.find_first_of("*"); if(found1!=string::npos){ string input1 = input.substr(0,found1); string input2 = input.substr(found1+1); return parseNumbers(input1)*parseTermOfNumbers(input2); } size_t found2 = input.find_first_of("/"); if(found2!=string::npos){ string input1 = input.substr(0,found2); string input2 = input.substr(found2+1); double result2 = parseTermOfNumbers(input2); if(result2==0){ //Teilen durch 0! errorWindow("Division by zero!"); return double(NAN); } return parseNumbers(input1)/result2; } if(found1==string::npos && found2==string::npos){ return parseNumbers(input); } errorWindow("42!"); return double(NAN); }