diff --git a/include/AST/AST.h b/include/AST/AST.h index 6daccd3..cae0a0f 100644 --- a/include/AST/AST.h +++ b/include/AST/AST.h @@ -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; } @@ -85,7 +85,7 @@ class Expression : public ASTNode { class IntegerConstantExpression : public Expression { public: - IntegerConstantExpression(int32_t val) : val(val), type(std::make_unique(TypeKind::Integer)) {} + IntegerConstantExpression(SourceLocation location, int32_t val) : Expression(location), val(val), type(std::make_unique(TypeKind::Integer)) {} void accept(ASTVisitor *visitor) override { visitor->visit(this); } @@ -101,7 +101,8 @@ class IntegerConstantExpression : public Expression { class UnsignedIntegerConstantExpression : public Expression { public: - UnsignedIntegerConstantExpression(uint32_t val) : val(val), type(std::make_unique(TypeKind::UnsignedInteger)) {} + UnsignedIntegerConstantExpression(SourceLocation location, uint32_t val) : + Expression(location, false), val(val), type(std::make_unique(TypeKind::UnsignedInteger)) {} void accept(ASTVisitor *visitor) override { visitor->visit(this); } @@ -116,7 +117,7 @@ class UnsignedIntegerConstantExpression : public Expression { class FloatConstantExpression : public Expression { public: - FloatConstantExpression(float val) : val(val), type(std::make_unique(TypeKind::Float)) {} + FloatConstantExpression(SourceLocation location, float val) : Expression(location), val(val), type(std::make_unique(TypeKind::Float)) {} void accept(ASTVisitor *visitor) override { visitor->visit(this); } @@ -131,7 +132,7 @@ class FloatConstantExpression : public Expression { class DoubleConstantExpression : public Expression { public: - DoubleConstantExpression(double val) : val(val), type(std::make_unique(TypeKind::Double)) {} + DoubleConstantExpression(SourceLocation location, double val) : Expression(location), val(val), type(std::make_unique(TypeKind::Double)) {} void accept(ASTVisitor *visitor) override { visitor->visit(this); } Type *getType() const { return type.get(); } @@ -145,7 +146,7 @@ class DoubleConstantExpression : public Expression { class BoolConstantExpression : public Expression { public: - BoolConstantExpression(bool val) : val(val), type(std::make_unique(TypeKind::Bool)) {} + BoolConstantExpression(SourceLocation location, bool val) : Expression(location), val(val), type(std::make_unique(TypeKind::Bool)) {} void accept(ASTVisitor *visitor) override { visitor->visit(this); } @@ -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); } @@ -174,7 +176,7 @@ class MemberAccessExpression : public Expression { public: MemberAccessExpression(std::unique_ptr baseComposite, std::vector> 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)) { } @@ -191,7 +193,7 @@ class ArrayAccessExpression : public Expression { public: ArrayAccessExpression(std::unique_ptr array, std::vector> 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)) { } @@ -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> arguments) - : functionName(functionName), arguments(std::move(arguments)) {} + : Expression(location), functionName(functionName), arguments(std::move(arguments)) {} void accept(ASTVisitor *visitor) override { visitor->visit(this); } @@ -230,16 +232,12 @@ class ConstructorExpression : public Expression { public: ConstructorExpression(std::unique_ptr type) : - type(std::move(type)) { - - } + type(std::move(type)) { } ConstructorExpression(std::unique_ptr type, std::vector> arguments) : type(std::move(type)), - arguments(std::move(arguments)) { - - } + arguments(std::move(arguments)) { } void accept(ASTVisitor *visitor) override { visitor->visit(this); diff --git a/lib/AST/PrinterASTVisitor.cpp b/lib/AST/PrinterASTVisitor.cpp index 9568272..70683bb 100644 --- a/lib/AST/PrinterASTVisitor.cpp +++ b/lib/AST/PrinterASTVisitor.cpp @@ -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(); diff --git a/lib/Parser/Parser.cpp b/lib/Parser/Parser.cpp index 3d6d43d..a4fe8a9 100644 --- a/lib/Parser/Parser.cpp +++ b/lib/Parser/Parser.cpp @@ -1217,30 +1217,21 @@ std::unique_ptr Parser::parsePrimaryExpression() { } else if (auto callExp = parseCallExpression()) { return callExp; } else if (curToken->is(TokenKind::Identifier)) { - return std::make_unique(curToken->getIdentifierName(), parsingLhsExpression); + return std::make_unique(curToken->getSourceLocation(), curToken->getIdentifierName(), parsingLhsExpression); } else if (curToken->is(TokenKind::IntegerConstant)) { auto int_const = dynamic_cast(curToken->getLiteralData()); - - return std::make_unique(int_const->getVal()); + return std::make_unique(curToken->getSourceLocation(), int_const->getVal()); } else if (curToken->is(TokenKind::UnsignedIntegerConstant)) { - auto uint_const = - dynamic_cast(curToken->getLiteralData()); - - return std::make_unique( - uint_const->getVal()); + auto uint_const = dynamic_cast(curToken->getLiteralData()); + return std::make_unique(curToken->getSourceLocation(), uint_const->getVal()); } else if (curToken->is(TokenKind::FloatConstant)) { auto float_const = dynamic_cast(curToken->getLiteralData()); - - return std::make_unique(float_const->getVal()); + return std::make_unique(curToken->getSourceLocation(), float_const->getVal()); } else if (curToken->is(TokenKind::DoubleConstant)) { - auto double_const = - dynamic_cast(curToken->getLiteralData()); - - return std::make_unique(double_const->getVal()); - } else if (curToken->is(TokenKind::kw_true) || - curToken->is(TokenKind::kw_false)) { - return std::make_unique( - curToken->is(TokenKind::kw_true)); + auto double_const = dynamic_cast(curToken->getLiteralData()); + return std::make_unique(curToken->getSourceLocation(), double_const->getVal()); + } else if (curToken->is(TokenKind::kw_true) || curToken->is(TokenKind::kw_false)) { + return std::make_unique(curToken->getSourceLocation(), curToken->is(TokenKind::kw_true)); } else if (curToken->is(TokenKind::lParen)) { advanceToken(); auto exp = parseConditionalExpression(); @@ -1430,13 +1421,15 @@ std::unique_ptr 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(name); + auto endLoc = curToken->getSourceLocation(); + return std::make_unique(SourceLocation(startLoc.startLine, startLoc.startCol, endLoc.endLine, endLoc.endCol), name); } std::vector> arguments; @@ -1462,7 +1455,8 @@ std::unique_ptr Parser::parseCallExpression() { return nullptr; } - return std::make_unique(name, std::move(arguments)); + auto endLoc = curToken->getSourceLocation(); + return std::make_unique(SourceLocation(startLoc.startLine, startLoc.startCol, endLoc.endLine, endLoc.endCol), name, std::move(arguments)); } std::unique_ptr Parser::parseConditionalExpression() {