aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/python-console/test_cli.cpp
blob: 76d0f251a401e044417e6f03053071329f62ff85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
#include "ParseHelper.h"
#include "ParseListener.h"
#include "Interpreter.h"

const std::string STD_PROMPT = ">>> ";
const std::string MULTILINE_PROMPT = "... ";

struct InterpreterRelay : public ParseListener
{
    Interpreter* m_interpreter;

    InterpreterRelay( ):
        m_interpreter( new Interpreter )
    { }

    virtual void parseEvent( const ParseMessage& msg )
    {
        if ( msg.errorCode )
        {
            std::cout << "(" << msg.errorCode << ") " << msg.message << "\n";
            return;
        }
        else
        {
            int err;
            std::string res = m_interpreter->interpret( msg.message, &err );
            std::cout << "(" << msg.errorCode << ") " << res << "\n";
        }
    }
};

int main( int argc, char *argv[] )
{
    Interpreter::Initialize( );
    const std::string* prompt = &STD_PROMPT;
    ParseHelper helper;
    ParseListener* listener = new InterpreterRelay;
    helper.subscribe( listener );

    std::string str;
    std::cout << *prompt;
    std::getline( std::cin, str );
    while ( str != "quit" )
    {
        std::cout << str << "\n";
        helper.process( str );
        if ( helper.buffered( ) )
        {
            prompt = &MULTILINE_PROMPT;
        }
        else
        {
            prompt = &STD_PROMPT;
        }
        std::cout << *prompt;
        std::getline( std::cin, str );
    }

    Interpreter::Finalize( );
    return 0;
}