+ InterpreterError::InterpreterError( const std::string& message,
+ bool in_script_file,
+ const std::string& script_file,
+ int script_line
+ )
+ : Exception("Interpreter",0,message),
+ mInScriptFile(in_script_file),
+ mScriptFile(script_file),
+ mScriptLine(script_line)
+ {
+ }
+ InterpreterError::InterpreterError( const Exception& excep,
+ bool in_script_file,
+ const std::string& script_file,
+ int script_line
+ )
+ : Exception(excep),
+ mInScriptFile(in_script_file),
+ mScriptFile(script_file),
+ mScriptLine(script_line)
+ {
+ }
+ //=======================================================================
+ void Interpreter::CatchBbtkException( const bbtk::Exception& e )
+ {
+ if (mThrow)
+ {
+ bool in_script = false;
+ std::string file("");
+ int line = 0;
+ if (mFileName.size()) {
+ std::ifstream* fs = dynamic_cast<std::ifstream*>(mFile.back());
+ if (fs!=0) in_script = true;
+ file = mFileName.back();
+ line = mLine.back();
+ }
+ CloseAllFiles();
+ throw InterpreterError(e,in_script,file,line);
+ }
+ else
+ {
+ std::stringstream mess;
+ mess << "* ERROR : "<<e.GetMessage()<<std::endl;
+ if (mFileName.size()) {
+ mess << "* FILE : \""<<mFileName.back()<<"\""<<std::endl;
+ mess << "* LINE : "<<mLine.back()<<std::endl;
+ }
+ std::cerr << mess.str();
+ }
+ }
+ //=======================================================================
+
+ //=======================================================================
+ void Interpreter::CatchStdException( const std::exception& e )
+ {
+ if (mThrow)
+ {
+ bool in_script = false;
+ std::string file("");
+ int line = 0;
+ if (mFileName.size()) {
+ std::ifstream* fs = dynamic_cast<std::ifstream*>(mFile.back());
+ if (fs!=0) in_script = true;
+ file = mFileName.back();
+ line = mLine.back();
+ }
+ CloseAllFiles();
+ throw InterpreterError(e.what(),in_script,file,line);
+ }
+ else
+ {
+ std::stringstream mess;
+ mess << "* ERROR : "<<e.what()<<std::endl;
+ if (mFileName.size()) {
+ mess << "* FILE : \""<<mFileName.back()<<"\""<<std::endl;
+ mess << "* LINE : "<<mLine.back()<<std::endl;
+ }
+ std::cerr << mess.str();
+ }
+ }
+ //=======================================================================
+
+ //=======================================================================
+ void Interpreter::CatchUnknownException()
+ {
+ if (mThrow)
+ {
+ bool in_script = false;
+ std::string file("");
+ int line = 0;
+ if (mFileName.size()) {
+ std::ifstream* fs = dynamic_cast<std::ifstream*>(mFile.back());
+ if (fs!=0) in_script = true;
+ file = mFileName.back();
+ line = mLine.back();
+ }
+ CloseAllFiles();
+ throw InterpreterError("Unknown exception caught",
+ in_script,file,line);
+ }
+ else
+ {
+ std::stringstream mess;
+ mess << "* UNDEFINED ERROR (not a bbtk nor a std exception)"
+ << std::endl;
+ if (mFileName.size()) {
+ mess << "* FILE : \""<<mFileName.back()<<"\""<<std::endl;
+ mess << "* LINE : "<<mLine.back()<<std::endl;
+ }
+ std::cerr << mess.str();
+ }
+ }
+ //=======================================================================
+
+ //=======================================================================
+
+#define CATCH_MACRO \
+ catch (QuitException e) \
+ { \
+ status = Interpreter_QUIT; \
+ if (mThrow) throw QuitException(); \
+ } \
+ catch (bbtk::Exception e) \
+ { \
+ status = Interpreter_ERROR; \
+ CatchBbtkException(e); \
+ } \
+ catch (std::exception& e) \
+ { \
+ status = Interpreter_ERROR; \
+ CatchStdException(e); \
+ } \
+ catch (...) \
+ { \
+ status = Interpreter_ERROR; \
+ CatchUnknownException(); \
+ }
+ //=======================================================================
+
+