Skip to content

Commit

Permalink
Add SourceLocation support for DoStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
wpmed92 committed Aug 8, 2024
1 parent c9a47bb commit e861031
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions include/AST/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,9 @@ class WhileStatement : public Statement {
class DoStatement : public Statement {

public:
DoStatement(std::unique_ptr<Statement> body,
DoStatement(SourceLocation location, std::unique_ptr<Statement> body,
std::unique_ptr<Expression> condition)
: body(std::move(body)), condition(std::move(condition)) {}
: ASTNode(location), body(std::move(body)), condition(std::move(condition)) {}

void accept(ASTVisitor *visitor) override { visitor->visit(this); }

Expand Down
8 changes: 4 additions & 4 deletions lib/AST/PrinterASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void PrinterASTVisitor::visit(VariableDeclarationList *varDeclList) {

void PrinterASTVisitor::visit(VariableDeclaration *varDecl) {
auto typeName = varDecl->getType() != nullptr ? varDecl->getType()->toString() : "";
print("|-VariableDeclaration: name=" + varDecl->getIdentifierName() + ((typeName != "") ? (", type=" + typeName) : "") + loc(varDecl->getSourceLocation()));
print("|-VariableDeclaration: name=" + varDecl->getIdentifierName() + ((typeName != "") ? (", type=" + typeName) : "") + " " + loc(varDecl->getSourceLocation()));

indent();
if (auto exp = varDecl->getInitialzerExpression()) {
Expand All @@ -81,7 +81,7 @@ void PrinterASTVisitor::visit(WhileStatement *whileStmt) {
}

void PrinterASTVisitor::visit(DoStatement *doStmt) {
print("|-DoStatement");
print("|-DoStatement " + loc(doStmt->getSourceLocation()));

doStmt->getCondition()->accept(this);
indent();
Expand Down Expand Up @@ -300,9 +300,9 @@ void PrinterASTVisitor::visit(CaseLabel *caseLabel) {
std::string PrinterASTVisitor::loc(const SourceLocation &sourceLoc) {
std::stringstream ssLoc;
if (sourceLoc.startLine == sourceLoc.endLine) {
ssLoc << "[line: " << sourceLoc.startLine << ", col:" << sourceLoc.startCol << " -> " << "col:" << sourceLoc.endCol << "]";
ssLoc << "[line:" << sourceLoc.startLine << ", col:" << sourceLoc.startCol << " -> " << "col:" << sourceLoc.endCol << "]";
} else {
ssLoc << "[line: " << sourceLoc.startLine << ", col:" << sourceLoc.startCol << "] -> " << "[line: " << sourceLoc.endLine << ", col:" << sourceLoc.endCol << "]";
ssLoc << "[line:" << sourceLoc.startLine << ", col:" << sourceLoc.startCol << "] -> " << "[line: " << sourceLoc.endLine << ", col:" << sourceLoc.endCol << "]";
}

return ssLoc.str();
Expand Down
8 changes: 7 additions & 1 deletion lib/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ std::unique_ptr<DoStatement> Parser::parseDoStatement() {
return nullptr;
}

auto startLoc = curToken->getSourceLocation();
advanceToken();

if (auto stmt = parseStatement()) {
Expand Down Expand Up @@ -868,9 +869,14 @@ std::unique_ptr<DoStatement> Parser::parseDoStatement() {
return nullptr;
}

auto endLoc = curToken->getSourceLocation();
advanceToken();

return std::make_unique<DoStatement>(std::move(stmt), std::move(exp));
return std::make_unique<DoStatement>(
SourceLocation(startLoc.startLine, startLoc.startCol, endLoc.endLine, endLoc.endCol),
std::move(stmt),
std::move(exp)
);
} else {
return nullptr;
}
Expand Down

0 comments on commit e861031

Please sign in to comment.