Skip to content

Commit

Permalink
Source location for: vardecl, function decl, function args
Browse files Browse the repository at this point in the history
  • Loading branch information
wpmed92 committed Aug 7, 2024
1 parent 780b945 commit 178d158
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
13 changes: 8 additions & 5 deletions include/AST/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,9 @@ class Declaration : public ExternalDeclaration, public Statement {
class VariableDeclaration : public Declaration {

public:
VariableDeclaration(std::unique_ptr<Type> type, const std::string &name,
VariableDeclaration(SourceLocation location, std::unique_ptr<Type> type, const std::string &name,
std::unique_ptr<Expression> initializerExpr)
: type(std::move(type)), identifierName(name),
: ASTNode(location), type(std::move(type)), identifierName(name),
initializerExpr(std::move(initializerExpr)) {}

const std::string &getIdentifierName() const { return identifierName; }
Expand Down Expand Up @@ -400,14 +400,17 @@ class VariableDeclarationList : public Declaration {
std::vector<std::unique_ptr<VariableDeclaration>> declarations;
};

class ParameterDeclaration {
class ParameterDeclaration : public ASTNode {

public:
ParameterDeclaration(const std::string &name, std::unique_ptr<Type> type)
: name(name), type(std::move(type)) {}
ParameterDeclaration(SourceLocation location, const std::string &name, std::unique_ptr<Type> type)
: ASTNode(location), name(name), type(std::move(type)) {}

const std::string &getName() { return name; }
Type *getType() { return type.get(); }
void accept(ASTVisitor *visitor) override {

}

private:
std::string name;
Expand Down
11 changes: 8 additions & 3 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) : ""));
print("|-VariableDeclaration: name=" + varDecl->getIdentifierName() + ((typeName != "") ? (", type=" + typeName) : "") + loc(varDecl->getSourceLocation()));

indent();
if (auto exp = varDecl->getInitialzerExpression()) {
Expand Down Expand Up @@ -276,7 +276,7 @@ void PrinterASTVisitor::visit(FunctionDeclaration *funcDecl) {
indent();

for (auto &arg : funcDecl->getParams()) {
print("name=" + arg->getName() + ", type=" + arg->getType()->toString());
print("name=" + arg->getName() + ", type=" + arg->getType()->toString() + loc(arg->getSourceLocation()));
}

resetIndent();
Expand All @@ -299,7 +299,12 @@ void PrinterASTVisitor::visit(CaseLabel *caseLabel) {

std::string PrinterASTVisitor::loc(const SourceLocation &sourceLoc) {
std::stringstream ssLoc;
ssLoc << "[line: " << sourceLoc.startLine << ", col:" << sourceLoc.startCol << "]->" << "[line: " << sourceLoc.endLine << ", col:" << sourceLoc.endCol << "]";
if (sourceLoc.startLine == sourceLoc.endLine) {
ssLoc << "[line: " << sourceLoc.startLine << ", col:" << sourceLoc.startCol << " -> " << "col:" << sourceLoc.endCol << "]";
} else {
ssLoc << "[line: " << sourceLoc.startLine << ", col:" << sourceLoc.startCol << "] -> " << "[line: " << sourceLoc.endLine << ", col:" << sourceLoc.endCol << "]";
}

return ssLoc.str();
}

Expand Down
25 changes: 15 additions & 10 deletions lib/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,28 @@ std::unique_ptr<FunctionDeclaration> Parser::parseFunctionDeclaration() {

std::unique_ptr<Declaration> Parser::parseDeclaration() {
if (auto type = parseType()) {
SourceLocation startLoc = curToken->getSourceLocation();
advanceToken();

// Only type, no variable name
if (curToken->is(TokenKind::semiColon)) {
advanceToken();
return std::make_unique<VariableDeclaration>(std::move(type),
return std::make_unique<VariableDeclaration>(startLoc, std::move(type),
std::string(), nullptr);
}

const std::string &name = curToken->getIdentifierName();

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

// Type and variable name, no initializer expression
if (curToken->is(TokenKind::semiColon)) {
advanceToken();
return std::make_unique<VariableDeclaration>(std::move(type), name,
nullptr);
return std::make_unique<VariableDeclaration>(
SourceLocation(startLoc.startLine, startLoc.startCol, endLoc.startLine, endLoc.endCol),
std::move(type),
name,
nullptr);
} else if (curToken->is(TokenKind::assign)) {
// Initializer expression
advanceToken();
Expand All @@ -124,7 +128,7 @@ std::unique_ptr<Declaration> Parser::parseDeclaration() {
return declarations;
} else if (curToken->is(TokenKind::semiColon)) {
advanceToken();
return std::make_unique<VariableDeclaration>(std::move(type), name,
return std::make_unique<VariableDeclaration>(SourceLocation(), std::move(type), name,
std::move(exp));
} else {
reportError(ParserErrorKind::UnexpectedToken, "Parser error: unexpected token '" + curToken->getRawData() + "'");
Expand All @@ -147,13 +151,12 @@ std::unique_ptr<VariableDeclarationList> Parser::parseVariableDeclarationList(
std::unique_ptr<Type> type, const std::string &identifierName,
std::unique_ptr<Expression> initializerExpression) {
std::vector<std::unique_ptr<VariableDeclaration>> declarations;
declarations.push_back(std::make_unique<VariableDeclaration>(
declarations.push_back(std::make_unique<VariableDeclaration>(SourceLocation(),
nullptr, identifierName, std::move(initializerExpression)));

do {
advanceToken();
if (!curToken->is(TokenKind::Identifier)) {

reportError(ParserErrorKind::ExpectedToken, "Parser error: expected identifier " + curToken->getRawData());
}

Expand All @@ -170,10 +173,10 @@ std::unique_ptr<VariableDeclarationList> Parser::parseVariableDeclarationList(
return nullptr;
}

declarations.push_back(std::make_unique<VariableDeclaration>(
declarations.push_back(std::make_unique<VariableDeclaration>(SourceLocation(),
nullptr, varName, std::move(exp)));
} else {
declarations.push_back(std::make_unique<VariableDeclaration>(
declarations.push_back(std::make_unique<VariableDeclaration>(SourceLocation(),
nullptr, varName, nullptr));
}
} while (curToken->is(TokenKind::comma));
Expand Down Expand Up @@ -679,11 +682,13 @@ Parser::parseFunctionParameters() {
advanceToken();

if (auto type = parseType()) {
SourceLocation startLoc = curToken->getSourceLocation();
advanceToken();

if (curToken->is(TokenKind::Identifier)) {
SourceLocation endLoc = curToken->getSourceLocation();
auto param = std::make_unique<ParameterDeclaration>(
curToken->getIdentifierName(), std::move(type));
SourceLocation(startLoc.startLine, startLoc.startCol, endLoc.startLine, endLoc.endCol), curToken->getIdentifierName(), std::move(type));
advanceToken();
params.push_back(std::move(param));
}
Expand Down

0 comments on commit 178d158

Please sign in to comment.