move to c++11 remove console writes

This commit is contained in:
Miodrag Milanovic 2018-06-28 13:32:06 +02:00
parent 9347c8742d
commit 9cf23d1010
6 changed files with 28 additions and 96 deletions

View File

@ -1,7 +1,7 @@
#include "Interpreter.h" #include "Interpreter.h"
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <boost/format.hpp> #include <memory>
PyThreadState* Interpreter::MainThreadState = NULL; PyThreadState* Interpreter::MainThreadState = NULL;
@ -27,9 +27,6 @@ Interpreter::Interpreter( )
Interpreter::~Interpreter( ) Interpreter::~Interpreter( )
{ {
#ifndef NDEBUG
std::cout << "delete interpreter\n";
#endif
PyEval_AcquireThread( m_threadState ); PyEval_AcquireThread( m_threadState );
Py_EndInterpreter( m_threadState ); Py_EndInterpreter( m_threadState );
PyEval_ReleaseLock( ); PyEval_ReleaseLock( );
@ -60,6 +57,15 @@ Interpreter::test( )
PyEval_ReleaseThread( m_threadState ); PyEval_ReleaseThread( m_threadState );
} }
template<typename ... Args>
std::string string_format( const std::string& format, Args ... args )
{
size_t size = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
std::unique_ptr<char[]> buf( new char[ size ] );
std::snprintf( buf.get(), size, format.c_str(), args ... );
return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
}
std::string std::string
Interpreter::interpret( const std::string& command, int* errorCode ) Interpreter::interpret( const std::string& command, int* errorCode )
{ {
@ -69,15 +75,9 @@ Interpreter::interpret( const std::string& command, int* errorCode )
PyObject* py_result; PyObject* py_result;
PyObject* dum; PyObject* dum;
std::string res; std::string res;
#ifndef NDEBUG
std::cout << "interpreting (" << command << ")\n";
#endif
py_result = Py_CompileString(command.c_str(), "<stdin>", Py_single_input); py_result = Py_CompileString(command.c_str(), "<stdin>", Py_single_input);
if ( py_result == 0 ) if ( py_result == 0 )
{ {
#ifndef NDEBUG
std::cout << "Huh?\n";
#endif
if ( PyErr_Occurred( ) ) if ( PyErr_Occurred( ) )
{ {
*errorCode = 1; *errorCode = 1;
@ -110,13 +110,7 @@ const std::list<std::string>& Interpreter::suggest( const std::string& hint )
PyEval_AcquireThread( m_threadState ); PyEval_AcquireThread( m_threadState );
m_suggestions.clear(); m_suggestions.clear();
int i = 0; int i = 0;
std::string command = boost::str( std::string command = string_format("sys.completer.complete('%s', %d)\n", hint.c_str(),i);
boost::format("sys.completer.complete('%1%', %2%)\n")
% hint
% i);
#ifndef NDEBUG
std::cout << command << "\n";
#endif
std::string res; std::string res;
do do
{ {
@ -129,10 +123,7 @@ const std::list<std::string>& Interpreter::suggest( const std::string& hint )
res = GetResultString( m_threadState ); res = GetResultString( m_threadState );
GetResultString( m_threadState ) = ""; GetResultString( m_threadState ) = "";
++i; ++i;
command = boost::str( command = string_format("sys.completer.complete('%s', %d)\n", hint.c_str(),i);
boost::format("sys.completer.complete('%1%', %2%)\n")
% hint
% i);
if (res.size()) if (res.size())
{ {
// throw away the newline // throw away the newline

View File

@ -20,7 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
*/ */
#include "ParseHelper.h" #include "ParseHelper.h"
#include <iostream>
ParseHelper::BlockParseState:: ParseHelper::BlockParseState::
BlockParseState( ParseHelper& parent ): BlockParseState( ParseHelper& parent ):
@ -47,10 +46,6 @@ process(const std::string& str)
bool isIndented = PeekIndent( str, &ind ); bool isIndented = PeekIndent( str, &ind );
if ( isIndented ) if ( isIndented )
{ {
#ifndef NDEBUG
std::cout << "current line indent: ";
print( ind );
#endif
// check if indent matches // check if indent matches
if ( ind.Token != indent.Token ) if ( ind.Token != indent.Token )
{ {
@ -61,17 +56,14 @@ process(const std::string& str)
parent.stateStack.pop_back( ); parent.stateStack.pop_back( );
if ( !parent.stateStack.size( ) ) if ( !parent.stateStack.size( ) )
break; break;
boost::shared_ptr<BlockParseState> parseState = std::shared_ptr<BlockParseState> parseState =
boost::dynamic_pointer_cast<BlockParseState>( std::dynamic_pointer_cast<BlockParseState>(
parent.stateStack.back( )); parent.stateStack.back( ));
found = ( ind.Token == parseState->indent.Token ); found = ( ind.Token == parseState->indent.Token );
} }
if ( ! found ) if ( ! found )
{ {
#ifndef NDEBUG
std::cout << "indent mismatch\n";
#endif
parent.reset( ); parent.reset( );
ParseMessage msg( 1, "IndentationError: unexpected indent"); ParseMessage msg( 1, "IndentationError: unexpected indent");
parent.broadcast( msg ); parent.broadcast( msg );
@ -85,11 +77,11 @@ process(const std::string& str)
if ( str[str.size()-1] == ':' ) if ( str[str.size()-1] == ':' )
{ {
parent.commandBuffer.push_back( str ); parent.commandBuffer.push_back( str );
//parent.inBlock = (boost::dynamic_pointer_cast<BlockParseState>( //parent.inBlock = (std::dynamic_pointer_cast<BlockParseState>(
// parent.stateStack.back())); // parent.stateStack.back()));
//expectingIndent = true; //expectingIndent = true;
boost::shared_ptr<ParseState> parseState( std::shared_ptr<ParseState> parseState(
new BlockParseState( parent ) ); new BlockParseState( parent ) );
parent.stateStack.push_back( parseState ); parent.stateStack.push_back( parseState );
return true; return true;
@ -98,7 +90,7 @@ process(const std::string& str)
if ( str[str.size()-1] == '\\' ) if ( str[str.size()-1] == '\\' )
{ {
parent.commandBuffer.push_back( str ); parent.commandBuffer.push_back( str );
boost::shared_ptr<ParseState> parseState( std::shared_ptr<ParseState> parseState(
new ContinuationParseState( parent ) ); new ContinuationParseState( parent ) );
parent.stateStack.push_back( parseState ); parent.stateStack.push_back( parseState );
return true; return true;
@ -107,7 +99,7 @@ process(const std::string& str)
if (BracketParseState::HasOpenBrackets( str )) if (BracketParseState::HasOpenBrackets( str ))
{ {
// FIXME: Every parse state should have its own local buffer // FIXME: Every parse state should have its own local buffer
boost::shared_ptr<ParseState> parseState( std::shared_ptr<ParseState> parseState(
new BracketParseState( parent, str ) ); new BracketParseState( parent, str ) );
parent.stateStack.push_back( parseState ); parent.stateStack.push_back( parseState );
return true; return true;
@ -121,18 +113,12 @@ process(const std::string& str)
if ( str.size() ) if ( str.size() )
{ {
{ {
#ifndef NDEBUG
std::cout << "Expected indented block\n";
#endif
parent.reset( ); parent.reset( );
ParseMessage msg( 1, "IndentationError: expected an indented block" ); ParseMessage msg( 1, "IndentationError: expected an indented block" );
parent.broadcast( msg ); parent.broadcast( msg );
return false; return false;
} }
} }
#ifndef NDEBUG
std::cout << "Leaving block\n";
#endif
parent.stateStack.pop_back(); parent.stateStack.pop_back();
parent.flush( ); parent.flush( );
parent.reset( ); parent.reset( );
@ -146,8 +132,6 @@ initializeIndent(const std::string& str)
bool expectingIndent = (indent.Token == ""); bool expectingIndent = (indent.Token == "");
if ( !expectingIndent ) if ( !expectingIndent )
{ {
std::cout << "already initialized indent: ";
print( indent );
return true; return true;
} }
@ -155,9 +139,6 @@ initializeIndent(const std::string& str)
bool isIndented = parent.PeekIndent( str, &ind ); bool isIndented = parent.PeekIndent( str, &ind );
if ( !isIndented ) if ( !isIndented )
{ {
#ifndef NDEBUG
std::cout << "Expected indented block\n";
#endif
parent.reset( ); parent.reset( );
ParseMessage msg( 1, "IndentationError: expected an indented block" ); ParseMessage msg( 1, "IndentationError: expected an indented block" );
parent.broadcast( msg ); parent.broadcast( msg );
@ -167,9 +148,5 @@ initializeIndent(const std::string& str)
//parent.currentIndent = ind; //parent.currentIndent = ind;
//parent.indentStack.push_back( parent.currentIndent ); //parent.indentStack.push_back( parent.currentIndent );
//parent.expectingIndent = false; //parent.expectingIndent = false;
#ifndef NDEBUG
std::cout << "initializing indent: ";
print( ind );
#endif
return true; return true;
} }

View File

@ -22,7 +22,7 @@ THE SOFTWARE.
#include "ParseHelper.h" #include "ParseHelper.h"
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <iostream>
const std::string ParseHelper::BracketParseState::OpeningBrackets = "[({"; const std::string ParseHelper::BracketParseState::OpeningBrackets = "[({";
const std::string ParseHelper::BracketParseState::ClosingBrackets = "])}"; const std::string ParseHelper::BracketParseState::ClosingBrackets = "])}";
@ -64,23 +64,17 @@ ParseHelper::BracketParseState::BracketParseState( ParseHelper& parent, const st
ParseState( parent ) ParseState( parent )
{ {
bool hasOpenBrackets = LoadBrackets( firstLine, &brackets ); bool hasOpenBrackets = LoadBrackets( firstLine, &brackets );
assert( hasOpenBrackets ); //assert( hasOpenBrackets );
m_buffer.push_back( firstLine ); m_buffer.push_back( firstLine );
} }
bool ParseHelper::BracketParseState::process(const std::string& str) bool ParseHelper::BracketParseState::process(const std::string& str)
{ {
#ifndef NDEBUG
std::cout << "(BracketParseState) processing " << str << "\n";
#endif
// update brackets stack // update brackets stack
for (int i = 0; i < str.size(); ++i) for (int i = 0; i < str.size(); ++i)
{ {
if (OpeningBrackets.find_first_of(str[i]) != std::string::npos) if (OpeningBrackets.find_first_of(str[i]) != std::string::npos)
{ {
#ifndef NDEBUG
std::cout << "push " << str[i] << "\n";
#endif
brackets.push_back(str[i]); brackets.push_back(str[i]);
} }
else else
@ -88,9 +82,6 @@ bool ParseHelper::BracketParseState::process(const std::string& str)
size_t t = ClosingBrackets.find_first_of(str[i]); size_t t = ClosingBrackets.find_first_of(str[i]);
if (t != std::string::npos) if (t != std::string::npos)
{ {
#ifndef NDEBUG
std::cout << "pop " << str[i] << "\n";
#endif
// reset state if unmatched brackets seen // reset state if unmatched brackets seen
if (t != OpeningBrackets.find_first_of(brackets.back())) if (t != OpeningBrackets.find_first_of(brackets.back()))
{ {

View File

@ -28,26 +28,6 @@ THE SOFTWARE.
#include <cstdlib> #include <cstdlib>
#include "ParseListener.h" #include "ParseListener.h"
#ifndef NDEBUG
void print(const ParseHelper::Indent& indent)
{
std::string str = indent.Token;
for (int i = 0; i < str.size(); ++i)
{
switch (str.at(i))
{
case ' ':
str[i] = 's';
break;
case '\t':
str[i] = 't';
break;
}
}
std::cout << str << "\n";
}
#endif
ParseHelper::Indent:: ParseHelper::Indent::
Indent( ) Indent( )
{ } { }
@ -93,15 +73,11 @@ ParseHelper::ParseHelper( )
void ParseHelper::process( const std::string& str ) void ParseHelper::process( const std::string& str )
{ {
#ifndef NDEBUG
std::cout << "processing: (" << str << ")\n";
#endif
std::string top; std::string top;
commandBuffer.push_back(str); commandBuffer.push_back(str);
//std::string top = commandBuffer.back(); //std::string top = commandBuffer.back();
//commandBuffer.pop_back(); //commandBuffer.pop_back();
boost::shared_ptr<ParseState> blockStatePtr; std::shared_ptr<ParseState> blockStatePtr;
while (stateStack.size()) while (stateStack.size())
{ {
top = commandBuffer.back(); top = commandBuffer.back();
@ -122,9 +98,6 @@ void ParseHelper::process( const std::string& str )
broadcast( std::string() ); broadcast( std::string() );
return; return;
} }
#ifndef NDEBUG
std::cout << "now processing: (" << top << ")\n";
#endif
{ // check for unexpected indent { // check for unexpected indent
Indent ind; Indent ind;
@ -142,7 +115,7 @@ void ParseHelper::process( const std::string& str )
// enter indented block state // enter indented block state
if ( top[top.size()-1] == ':' ) if ( top[top.size()-1] == ':' )
{ {
boost::shared_ptr<ParseState> parseState( std::shared_ptr<ParseState> parseState(
new BlockParseState( *this ) ); new BlockParseState( *this ) );
stateStack.push_back( parseState ); stateStack.push_back( parseState );
return; return;
@ -150,7 +123,7 @@ void ParseHelper::process( const std::string& str )
if ( top[top.size()-1] == '\\' ) if ( top[top.size()-1] == '\\' )
{ {
boost::shared_ptr<ParseState> parseState( std::shared_ptr<ParseState> parseState(
new ContinuationParseState( *this ) ); new ContinuationParseState( *this ) );
stateStack.push_back( parseState ); stateStack.push_back( parseState );
return; return;
@ -160,7 +133,7 @@ void ParseHelper::process( const std::string& str )
{ {
// FIXME: Every parse state should have its own local buffer // FIXME: Every parse state should have its own local buffer
commandBuffer.pop_back( ); commandBuffer.pop_back( );
boost::shared_ptr<ParseState> parseState( std::shared_ptr<ParseState> parseState(
new BracketParseState( *this, top ) ); new BracketParseState( *this, top ) );
stateStack.push_back( parseState ); stateStack.push_back( parseState );
return; return;
@ -198,7 +171,7 @@ void ParseHelper::reset( )
bool ParseHelper::isInContinuation( ) const bool ParseHelper::isInContinuation( ) const
{ {
return (stateStack.size() return (stateStack.size()
&& (boost::dynamic_pointer_cast<ContinuationParseState>( && (std::dynamic_pointer_cast<ContinuationParseState>(
stateStack.back()))); stateStack.back())));
} }

View File

@ -25,7 +25,7 @@ THE SOFTWARE.
#include <string> #include <string>
#include <vector> #include <vector>
#include <list> #include <list>
#include <boost/shared_ptr.hpp> #include <memory>
#include "ParseMessage.h" #include "ParseMessage.h"
class ParseListener; class ParseListener;
@ -120,7 +120,7 @@ protected:
// TODO: Create a ContinuationParseState to handle this // TODO: Create a ContinuationParseState to handle this
bool inContinuation; bool inContinuation;
std::vector< ParseListener* > listeners; std::vector< ParseListener* > listeners;
std::vector< boost::shared_ptr< ParseState > > stateStack; std::vector< std::shared_ptr< ParseState > > stateStack;
std::vector< std::string > commandBuffer; std::vector< std::string > commandBuffer;
public: public:

View File

@ -5,7 +5,7 @@ int main( int argc, char *argv[] )
{ {
std::string commands[] = { std::string commands[] = {
"from time import time,ctime\n", "from time import time,ctime\n",
"print 'Today is',ctime(time())\n" "print('Today is',ctime(time()))\n"
}; };
Interpreter::Initialize( ); Interpreter::Initialize( );
Interpreter* interpreter = new Interpreter; Interpreter* interpreter = new Interpreter;