Skip to content

Commit

Permalink
Add SourceLocation support for some Expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
wpmed92 committed Aug 11, 2024
1 parent e861031 commit c82928e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 40 deletions.
36 changes: 17 additions & 19 deletions include/AST/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ASTNode {

class Expression : public ASTNode {
public:
Expression(bool lhs = false) : lhs(lhs) { }
Expression(SourceLocation location = SourceLocation(), bool lhs = false) : ASTNode(location), lhs(lhs) { }
virtual ~Expression() = default;
bool isRhs() const { return !lhs; }
bool isLhs() const { return lhs; }
Expand All @@ -85,7 +85,7 @@ class Expression : public ASTNode {
class IntegerConstantExpression : public Expression {

public:
IntegerConstantExpression(int32_t val) : val(val), type(std::make_unique<Type>(TypeKind::Integer)) {}
IntegerConstantExpression(SourceLocation location, int32_t val) : Expression(location), val(val), type(std::make_unique<Type>(TypeKind::Integer)) {}

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

Expand All @@ -101,7 +101,8 @@ class IntegerConstantExpression : public Expression {
class UnsignedIntegerConstantExpression : public Expression {

public:
UnsignedIntegerConstantExpression(uint32_t val) : val(val), type(std::make_unique<Type>(TypeKind::UnsignedInteger)) {}
UnsignedIntegerConstantExpression(SourceLocation location, uint32_t val) :
Expression(location, false), val(val), type(std::make_unique<Type>(TypeKind::UnsignedInteger)) {}

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

Expand All @@ -116,7 +117,7 @@ class UnsignedIntegerConstantExpression : public Expression {

class FloatConstantExpression : public Expression {
public:
FloatConstantExpression(float val) : val(val), type(std::make_unique<Type>(TypeKind::Float)) {}
FloatConstantExpression(SourceLocation location, float val) : Expression(location), val(val), type(std::make_unique<Type>(TypeKind::Float)) {}

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

Expand All @@ -131,7 +132,7 @@ class FloatConstantExpression : public Expression {

class DoubleConstantExpression : public Expression {
public:
DoubleConstantExpression(double val) : val(val), type(std::make_unique<Type>(TypeKind::Double)) {}
DoubleConstantExpression(SourceLocation location, double val) : Expression(location), val(val), type(std::make_unique<Type>(TypeKind::Double)) {}

void accept(ASTVisitor *visitor) override { visitor->visit(this); }
Type *getType() const { return type.get(); }
Expand All @@ -145,7 +146,7 @@ class DoubleConstantExpression : public Expression {

class BoolConstantExpression : public Expression {
public:
BoolConstantExpression(bool val) : val(val), type(std::make_unique<Type>(TypeKind::Bool)) {}
BoolConstantExpression(SourceLocation location, bool val) : Expression(location), val(val), type(std::make_unique<Type>(TypeKind::Bool)) {}

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

Expand All @@ -160,7 +161,8 @@ class BoolConstantExpression : public Expression {

class VariableExpression : public Expression {
public:
VariableExpression(const std::string &name, bool isLhs = false) : Expression(isLhs), name(name) {}
VariableExpression(SourceLocation location, const std::string &name, bool isLhs = false) :
Expression(location, isLhs), name(name) {}

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

Expand All @@ -174,7 +176,7 @@ class MemberAccessExpression : public Expression {

public:
MemberAccessExpression(std::unique_ptr<Expression> baseComposite, std::vector<std::unique_ptr<Expression>> members, bool lhs = false) :
Expression(lhs), baseComposite(std::move(baseComposite)), members(std::move(members)) {
Expression(SourceLocation(), lhs), baseComposite(std::move(baseComposite)), members(std::move(members)) {

}

Expand All @@ -191,7 +193,7 @@ class ArrayAccessExpression : public Expression {

public:
ArrayAccessExpression(std::unique_ptr<Expression> array, std::vector<std::unique_ptr<Expression>> accessChain, bool lhs = false) :
Expression(lhs), array(std::move(array)), accessChain(std::move(accessChain)) {
Expression(SourceLocation(), lhs), array(std::move(array)), accessChain(std::move(accessChain)) {

}

Expand All @@ -207,12 +209,12 @@ class ArrayAccessExpression : public Expression {
class CallExpression : public Expression {

public:
CallExpression(const std::string &functionName)
: functionName(functionName) {}
CallExpression(SourceLocation location, const std::string &functionName)
: Expression(location), functionName(functionName) {}

CallExpression(const std::string &functionName,
CallExpression(SourceLocation location, const std::string &functionName,
std::vector<std::unique_ptr<Expression>> arguments)
: functionName(functionName), arguments(std::move(arguments)) {}
: Expression(location), functionName(functionName), arguments(std::move(arguments)) {}

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

Expand All @@ -230,16 +232,12 @@ class ConstructorExpression : public Expression {

public:
ConstructorExpression(std::unique_ptr<Type> type) :
type(std::move(type)) {

}
type(std::move(type)) { }

ConstructorExpression(std::unique_ptr<Type> type,
std::vector<std::unique_ptr<Expression>> arguments) :
type(std::move(type)),
arguments(std::move(arguments)) {

}
arguments(std::move(arguments)) { }

void accept(ASTVisitor *visitor) override {
visitor->visit(this);
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/PrinterASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void PrinterASTVisitor::visit(ConditionalExpression *condExp) {
}

void PrinterASTVisitor::visit(CallExpression *callee) {
print("|-CallExpression: name=" + callee->getFunctionName());
print("|-CallExpression: name=" + callee->getFunctionName() + " " + loc(callee->getSourceLocation()));

indent();

Expand Down
34 changes: 14 additions & 20 deletions lib/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1217,30 +1217,21 @@ std::unique_ptr<Expression> Parser::parsePrimaryExpression() {
} else if (auto callExp = parseCallExpression()) {
return callExp;
} else if (curToken->is(TokenKind::Identifier)) {
return std::make_unique<VariableExpression>(curToken->getIdentifierName(), parsingLhsExpression);
return std::make_unique<VariableExpression>(curToken->getSourceLocation(), curToken->getIdentifierName(), parsingLhsExpression);
} else if (curToken->is(TokenKind::IntegerConstant)) {
auto int_const = dynamic_cast<IntegerLiteral *>(curToken->getLiteralData());

return std::make_unique<IntegerConstantExpression>(int_const->getVal());
return std::make_unique<IntegerConstantExpression>(curToken->getSourceLocation(), int_const->getVal());
} else if (curToken->is(TokenKind::UnsignedIntegerConstant)) {
auto uint_const =
dynamic_cast<UnsignedIntegerLiteral *>(curToken->getLiteralData());

return std::make_unique<UnsignedIntegerConstantExpression>(
uint_const->getVal());
auto uint_const = dynamic_cast<UnsignedIntegerLiteral *>(curToken->getLiteralData());
return std::make_unique<UnsignedIntegerConstantExpression>(curToken->getSourceLocation(), uint_const->getVal());
} else if (curToken->is(TokenKind::FloatConstant)) {
auto float_const = dynamic_cast<FloatLiteral *>(curToken->getLiteralData());

return std::make_unique<FloatConstantExpression>(float_const->getVal());
return std::make_unique<FloatConstantExpression>(curToken->getSourceLocation(), float_const->getVal());
} else if (curToken->is(TokenKind::DoubleConstant)) {
auto double_const =
dynamic_cast<DoubleLiteral *>(curToken->getLiteralData());

return std::make_unique<DoubleConstantExpression>(double_const->getVal());
} else if (curToken->is(TokenKind::kw_true) ||
curToken->is(TokenKind::kw_false)) {
return std::make_unique<BoolConstantExpression>(
curToken->is(TokenKind::kw_true));
auto double_const = dynamic_cast<DoubleLiteral *>(curToken->getLiteralData());
return std::make_unique<DoubleConstantExpression>(curToken->getSourceLocation(), double_const->getVal());
} else if (curToken->is(TokenKind::kw_true) || curToken->is(TokenKind::kw_false)) {
return std::make_unique<BoolConstantExpression>(curToken->getSourceLocation(), curToken->is(TokenKind::kw_true));
} else if (curToken->is(TokenKind::lParen)) {
advanceToken();
auto exp = parseConditionalExpression();
Expand Down Expand Up @@ -1430,13 +1421,15 @@ std::unique_ptr<CallExpression> Parser::parseCallExpression() {
return nullptr;
}

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

advanceToken();
advanceToken();

if (curToken->is(TokenKind::rParen)) {
return std::make_unique<CallExpression>(name);
auto endLoc = curToken->getSourceLocation();
return std::make_unique<CallExpression>(SourceLocation(startLoc.startLine, startLoc.startCol, endLoc.endLine, endLoc.endCol), name);
}

std::vector<std::unique_ptr<Expression>> arguments;
Expand All @@ -1462,7 +1455,8 @@ std::unique_ptr<CallExpression> Parser::parseCallExpression() {
return nullptr;
}

return std::make_unique<CallExpression>(name, std::move(arguments));
auto endLoc = curToken->getSourceLocation();
return std::make_unique<CallExpression>(SourceLocation(startLoc.startLine, startLoc.startCol, endLoc.endLine, endLoc.endCol), name, std::move(arguments));
}

std::unique_ptr<Expression> Parser::parseConditionalExpression() {
Expand Down

0 comments on commit c82928e

Please sign in to comment.