Skip to content

Commit

Permalink
Merge pull request #9 from blorente/gdb
Browse files Browse the repository at this point in the history
FInish basic debugging commands
  • Loading branch information
blorente committed Apr 28, 2017
2 parents 48e900e + 73c9948 commit 9e516d9
Show file tree
Hide file tree
Showing 67 changed files with 1,009 additions and 265 deletions.
11 changes: 6 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@

# Build files
bin/
cmake-build-debug/
/cmake-build-debug/
cmake-build-beginDebug/
/cmake-build-beginDebug/
*.make
cmake-build-debug/CMakeFiles/Makefile2
cmake-build-beginDebug/CMakeFiles/Makefile2

/interpreter/src/core/parser/generated/
/deps/antlr/build/
/build/
/deps/antlr/run/
/deps/antlr/cmake/
.idea
/cmake-build-debug/antlr4cpp_generated_src/
/cmake-build-debug/locals/
/cmake-build-debug/
/docs/page/_site
/cmake-build-debugclang/
/cmake-build-release/
8 changes: 8 additions & 0 deletions examples/Debug.grace
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def out1 = 4;
def obj = object {
var in1 := 3;
var in2 := 5;
var in3 := 8;
};
def out2 = 6;
def out3 = 6;
3 changes: 3 additions & 0 deletions examples/Lines.grace
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var in1 := 3;
var in2 := 5;
var in3 := 8;
4 changes: 4 additions & 0 deletions examples/Method.grace
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
method add(a)to(b) {
a + b;
}
var x := {ba, bb -> add(ba)to(bb);}.apply(2, 4);
2 changes: 1 addition & 1 deletion grammars/GraceLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tokens {
DUMMY
}

WS : [ \r\t\n]+ -> skip ;
WS : [ \r\t\n]+ -> channel(HIDDEN);
INT: Digit+;
Digit: [0-9];

Expand Down
15 changes: 15 additions & 0 deletions interpreter/src/core/control/DebugState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Copyright (c) 2017 by Borja Lorente.
// Distributed under the GPLv3 license.
//

#ifndef NAYLANG_DEBUGSTATE_H
#define NAYLANG_DEBUGSTATE_H
enum DebugState {
CONTINUE,
STOP,
STEP_IN,
STEP_OVER,
STEP_OVER_SKIP
};
#endif //NAYLANG_DEBUGSTATE_H
59 changes: 59 additions & 0 deletions interpreter/src/core/control/Debugger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Copyright (c) 2016 by Borja Lorente.
// Distributed under the GPLv3 license.
//

#include "Debugger.h"

namespace naylang {

Debugger::Debugger(DebugMode *mode, const std::string &code) :
Interpreter(std::make_unique<ExecutionEvaluator>(this)),
_frontend{mode},
_AST{parse(code)}{}

void Debugger::run() {
_eval->setDebugState(CONTINUE);
_eval->evaluateAST(_AST);
finish();
}

void Debugger::setBreakpoint(int line) {
_breakpoints.insert(line);
std::cout << "Breakpoint set at line " << line << std::endl;
}

void Debugger::printEnvironment() {
std::cout << "Current environment: " << std::endl;
std::cout << _eval->currentScope()->prettyPrint(0) << std::endl;
}

void Debugger::resume() {
_eval->setDebugState(CONTINUE);
}

void Debugger::debug(Statement *node) {
if (node->stoppable()) {
if (_breakpoints.count(node->line()) != 0) {
std::cout << "Breakpoint found at line " << node->line() << ". Stop." << std::endl;
_eval->setDebugState(STOP);
}
while(_eval->getDebugState() == STOP) {
_frontend->executeNextCommand();
}
}
}

void Debugger::stepIn() {
_eval->setDebugState(STEP_IN);
}

void Debugger::stepOver() {
_eval->setDebugState(STEP_OVER);
}

void Debugger::finish() {
std::cout << "Process finished. Resulting environment: " << std::endl;
std::cout << _eval->currentScope()->prettyPrint(0) << std::endl;
}
}
40 changes: 40 additions & 0 deletions interpreter/src/core/control/Debugger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Copyright (c) 2016 by Borja Lorente.
// Distributed under the GPLv3 license.
//

#ifndef NAYLANG_DEBUGGER_H
#define NAYLANG_DEBUGGER_H

#include <frontends/modes/debug/DebugMode.h>
#include <core/control/DebugState.h>
#include "Interpreter.h"

namespace naylang {
class DebugMode;
class Debugger : public Interpreter {

GraceAST _AST;
std::set<int> _breakpoints;
DebugMode *_frontend;

public:

Debugger(DebugMode *frontend, const std::string &filename);

void run();
void setBreakpoint(int line);
void printEnvironment();
void resume();
void stepIn();
void stepOver();

void debug(Statement *node);

private:

void finish();
};
}

#endif //NAYLANG_DEBUGGER_H
23 changes: 13 additions & 10 deletions interpreter/src/core/control/Interpreter.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
//
// Copyright (c) 2016 by Borja Lorente.
// Distributed under the GPLv3 license.
// Created by borja on 4/14/17.
//

#include "REPLInterpreter.h"
#include "Interpreter.h"

namespace naylang {
void Interpreter::execute(const std::string &line) {
eval.evaluateAST(parse(line));
std::cout << eval.currentScope()->prettyPrint(0) << std::endl;
}

void Interpreter::printResult(const std::string &line) {
eval.evaluateAST(parse(line));
std::cout << eval.partial()->prettyPrint(0) << std::endl;
void Interpreter::printResult(std::string line) {
colonize(line);
auto res = _eval->evaluateSandbox(parse(line));
std::cout << res->prettyPrint(0) << std::endl;
}

GraceAST Interpreter::parse(const std::string &line) const {
Expand All @@ -25,4 +22,10 @@ GraceAST Interpreter::parse(const std::string &line) const {
parserVisitor.visit(parser.program());
return parserVisitor.AST();
}
}

void Interpreter::colonize(std::string &line) {
if (line.at(line.length() - 1) != ';') {
line.append(";");
}
}
}
17 changes: 8 additions & 9 deletions interpreter/src/core/control/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#ifndef NAYLANG_INTERPRETER_H
#define NAYLANG_INTERPRETER_H

#include <core/parser/NaylangParserVisitor.h>
#include <core/model/evaluators/ExecutionEvaluator.h>
#include <core/model/execution/objects/GraceObject.h>
#include <GraceLexer.h>
Expand All @@ -16,17 +15,17 @@ using namespace antlr4::tree;

namespace naylang {
class Interpreter {

ExecutionEvaluator eval;
protected:
std::unique_ptr<ExecutionEvaluator> _eval;

public:
Interpreter(std::unique_ptr<ExecutionEvaluator> eval) : _eval{std::move(eval)} {}
void printResult(std::string line);

void execute(const std::string &line);
void printResult(const std::string &line);

private:
protected:
GraceAST parse(const std::string &line) const;
void colonize(std::string &line);
};
}

#endif //NAYLANG__INTERPRETER_H
}
#endif //NAYLANG_INTERPRETER_H
17 changes: 17 additions & 0 deletions interpreter/src/core/control/REPLInterpreter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Copyright (c) 2016 by Borja Lorente.
// Distributed under the GPLv3 license.
//

#include "REPLInterpreter.h"
#include <core/control/Debugger.h>

namespace naylang {
void REPLInterpreter::execute(std::string line) {
colonize(line);
_eval->evaluateAST(parse(line));
std::cout << _eval->currentScope()->prettyPrint(0) << std::endl;
}

REPLInterpreter::REPLInterpreter() : Interpreter(std::make_unique<ExecutionEvaluator>()) {}
}
22 changes: 22 additions & 0 deletions interpreter/src/core/control/REPLInterpreter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Copyright (c) 2016 by Borja Lorente.
// Distributed under the GPLv3 license.
//

#ifndef NAYLANG_REPLINTERPRETER_H
#define NAYLANG_REPLINTERPRETER_H

#include <core/parser/NaylangParserVisitor.h>
#include "Interpreter.h"

namespace naylang {
class REPLInterpreter : public Interpreter {

public:
REPLInterpreter();

void execute(std::string line);
};
}

#endif //NAYLANG_REPLINTERPRETER_H
1 change: 1 addition & 0 deletions interpreter/src/core/model/ast/ASTNodeDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <core/model/ast/control/IfThen.h>
#include <core/model/ast/control/IfThenElse.h>
#include <core/model/ast/control/While.h>
#include <core/model/ast/control/Return.h>

#include <core/model/ast/declarations/Declaration.h>
#include <core/model/ast/declarations/ConstantDeclaration.h>
Expand Down
14 changes: 0 additions & 14 deletions interpreter/src/core/model/ast/ASTTreeDefinition.h

This file was deleted.

28 changes: 28 additions & 0 deletions interpreter/src/core/model/ast/GraceAST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Copyright (c) 2017 by Borja Lorente.
// Distributed under the GPLv3 license.
//

#include "GraceAST.h"

namespace naylang {
StatementPtr GraceAST::operator[](int index) {
return _nodes[index];
}

void GraceAST::addNode(StatementPtr node) {
_nodes.push_back(node);
}

const std::vector<StatementPtr> &GraceAST::nodes() const {
return _nodes;
}

void GraceAST::addLineLink(StatementPtr node) {
if (_nodeLinks.count(node->line()) == 0) {
_nodeLinks[node->line()] = node;
_lastLine = node->line() > _lastLine ? node->line() : _lastLine;
}
}
}

27 changes: 27 additions & 0 deletions interpreter/src/core/model/ast/GraceAST.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Copyright (c) 2017 by Borja Lorente.
// Distributed under the GPLv3 license.
//

#ifndef NAYLANG_ASTTREEDEFINITION_H
#define NAYLANG_ASTTREEDEFINITION_H

#include <core/model/ast/ASTNodeDefinitions.h>
#include <map>

namespace naylang {
class GraceAST {
std::vector<StatementPtr> _nodes;
std::map<int, StatementPtr> _nodeLinks;
int _lastLine = 0;

public:

virtual StatementPtr operator[](int index);
void addNode(StatementPtr node);
void addLineLink(StatementPtr node);
const std::vector<StatementPtr> &nodes() const;

};
}
#endif //NAYLANG_ASTTREEDEFINITION_H
12 changes: 8 additions & 4 deletions interpreter/src/core/model/ast/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ class Statement {

int _line;
int _col;
bool _stoppable;

public:

Statement() : _line{-1}, _col{-1} {}
Statement(int line, int col) : _line{line}, _col{col} {}
Statement() : _line{-1}, _col{-1}, _stoppable{false}{}
Statement(int line, int col) : _line{line}, _col{col}, _stoppable{false}{}

virtual void accept(Evaluator &evaluator) = 0;

int line() {return _line;}
int col() {return _col;}
int line() const {return _line;}
int col() const {return _col;}

bool stoppable() {return _stoppable;}
void makeStoppable() {_stoppable = true;}
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ const std::string & ConstantDeclaration::name() const {
ExpressionPtr ConstantDeclaration::value() const {
return _value;
}

}
Loading

0 comments on commit 9e516d9

Please sign in to comment.