void ExprParser::parseArguments (const std::string& arguments, Scanner& scanner, std::vector<Interpreter::Type_Code>& code, bool invert) { ExprParser parser (getErrorHandler(), getContext(), mLocals, mLiterals, true); StringParser stringParser (getErrorHandler(), getContext(), mLiterals); std::stack<std::vector<Interpreter::Type_Code> > stack; for (std::string::const_iterator iter (arguments.begin()); iter!=arguments.end(); ++iter) { if (*iter=='S' || *iter=='c') { stringParser.reset(); if (*iter=='c') stringParser.smashCase(); scanner.scan (stringParser); if (invert) { std::vector<Interpreter::Type_Code> tmp; stringParser.append (tmp); stack.push (tmp); } else stringParser.append (code); } else { parser.reset(); scanner.scan (parser); std::vector<Interpreter::Type_Code> tmp; char type = parser.append (tmp); if (type!=*iter) Generator::convert (tmp, type, *iter); if (invert) stack.push (tmp); else std::copy (tmp.begin(), tmp.end(), std::back_inserter (code)); } } while (!stack.empty()) { std::vector<Interpreter::Type_Code>& tmp = stack.top(); std::copy (tmp.begin(), tmp.end(), std::back_inserter (code)); stack.pop(); } }
int main() { while (true){ ExprParser parser; string expr; cout << "Enter expression: "; cin >> expr; double result = 0; try { result = parser.calcExpression(expr); } catch (char c){ cout << "Unsupported symbol: " << c << endl; continue; } catch (int i){ if (i == 0) cout << "Empty expression" << endl; else cout << "Uncorrect expression" << endl; continue; } catch (string s){ cout << s << endl; continue; } catch (...){ cout << "Unexpected error" << endl; continue; } cout << "Result: " << result << endl; } return 0; }
int ExprParser::parseArguments (const std::string& arguments, Scanner& scanner, std::vector<Interpreter::Type_Code>& code) { bool optional = false; int optionalCount = 0; ExprParser parser (getErrorHandler(), getContext(), mLocals, mLiterals, true); StringParser stringParser (getErrorHandler(), getContext(), mLiterals); std::stack<std::vector<Interpreter::Type_Code> > stack; for (std::string::const_iterator iter (arguments.begin()); iter!=arguments.end(); ++iter) { if (*iter=='/') { optional = true; } else if (*iter=='S' || *iter=='c' || *iter=='x') { stringParser.reset(); if (optional || *iter=='x') stringParser.setOptional (true); if (*iter=='c') stringParser.smashCase(); scanner.scan (stringParser); if (optional && stringParser.isEmpty()) break; if (*iter!='x') { std::vector<Interpreter::Type_Code> tmp; stringParser.append (tmp); stack.push (tmp); if (optional) ++optionalCount; } } else { parser.reset(); if (optional || *iter == 'X') parser.setOptional (true); scanner.scan (parser); if (optional && parser.isEmpty()) break; if (*iter != 'X') { std::vector<Interpreter::Type_Code> tmp; char type = parser.append (tmp); if (type!=*iter) Generator::convert (tmp, type, *iter); stack.push (tmp); if (optional) ++optionalCount; } } } while (!stack.empty()) { std::vector<Interpreter::Type_Code>& tmp = stack.top(); std::copy (tmp.begin(), tmp.end(), std::back_inserter (code)); stack.pop(); } return optionalCount; }