summaryrefslogtreecommitdiffstats
path: root/scilab/modules/ast/src/cpp/ast/consoledebugger.cpp
blob: f520159435bcbfdae3b097c6fad8ee16f9feeaa3 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2015 - Scilab Enterprises - Antoine ELIAS
 *
 *  This file must be used under the terms of the CeCILL.
 *  This source file is licensed as described in the file COPYING, which
 *  you should have received as part of this distribution.  The terms
 *  are also available at
 *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 *
 */

#include "consoledebugger.hxx"
#include "debugmanager.hxx"
#include "printvisitor.hxx"

extern "C"
{
#include "sciprint.h"
#include "prompt.h"
}

namespace debugger
{
void ConsoleDebugger::onStop(int index)
{
    if (index >= 0)
    {
        debugger::DebuggerMagager* manager = debugger::DebuggerMagager::getInstance();
        debugger::Breakpoint* bp = manager->getBreakPoint(index);
        if (bp)
        {
            sciprint(_("debugger stop on breakpoint(%d) in function %s line %d\n"), index, bp->getFunctioName().data(), bp->getMacroLine());
        }
    }

    printExp();
}

void ConsoleDebugger::onResume()
{
    //sciprint("ConsoleDebugger::onResume.\n");
}

void ConsoleDebugger::onAbort()
{
    sciprint(_("Execution aborted.\n"));
}

void ConsoleDebugger::onErrorInFile(const std::string& filename)
{
    debugger::DebuggerMagager* manager = debugger::DebuggerMagager::getInstance();
    ast::Exp* exp = manager->getExp();
    sciprint(_("debugger stop on error in file %s line %d\n"), filename.data(), exp->getLocation().first_line);
    printExp();
}

void ConsoleDebugger::onErrorInScript(const std::string& funcname)
{
    debugger::DebuggerMagager* manager = debugger::DebuggerMagager::getInstance();
    ast::Exp* exp = manager->getExp();
    sciprint(_("debugger stop on error in function %s line %d\n"), funcname.data(), exp->getLocation().first_line);
    printExp();
}

void ConsoleDebugger::onQuit()
{
    sciprint(_("Leave debugger.\n"));
}

void ConsoleDebugger::updateBreakpoints()
{
    debugger::DebuggerMagager* manager = debugger::DebuggerMagager::getInstance();
    debugger::Breakpoints& brks = manager->getAllBreakPoint();

    if (brks.size() == 0)
    {
        sciprint("No breakpoint\n");
        return;
    }

    sciprint("% 3s % 7s %24s % 5s %s\n\n", "num", "enable", "function", "line", "condition");
    int i = 0;
    for (const auto& b : brks)
    {
        if (b->isMacro())
        {
            std::string condition = b->getCondition();
            sciprint("% 3d % 7s %24s % 5d %s\n", i, b->isEnable() ? "true" : "false", b->getFunctioName().c_str(), b->getMacroLine(),
                     condition.size() < 30 ? condition.c_str() :
                     (std::string(condition.begin(), condition.begin() + 27) + "...").c_str());
        }

        ++i;
    }
}

void ConsoleDebugger::printExp()
{
    debugger::DebuggerMagager* manager = debugger::DebuggerMagager::getInstance();
    std::ostringstream ostr;
    ast::PrintVisitor pp(ostr, true, true, true);
    manager->getExp()->accept(pp);
    sciprint("%s%s\n", SCIPROMPT_PAUSE, ostr.str().data());
}
}