diff --git a/.clang-format b/.clang-format index 9cda9e8..5f84dc0 100644 --- a/.clang-format +++ b/.clang-format @@ -1,9 +1,43 @@ --- BasedOnStyle: LLVM +AlignAfterOpenBracket: BlockIndent +AlignArrayOfStructures: Left +AlignConsecutiveAssignments: + Enabled: true + AcrossComments: true +AlignConsecutiveBitFields: + Enabled: true + AcrossComments: true +AlignConsecutiveMacros: + Enabled: true + AcrossComments: true +AlignConsecutiveShortCaseStatements: + Enabled: true + AcrossComments: true +AlignEscapedNewlines: Left +AlignOperands: AlignAfterOperator +AllowShortBlocksOnASingleLine: Empty +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: WithoutElse +AllowShortLambdasOnASingleLine: Empty +AlwaysBreakBeforeMultilineStrings: true +BinPackArguments: false +BinPackParameters: false +BreakBeforeBinaryOperators: All +ColumnLimit: 120 +IncludeBlocks: Regroup IndentWidth: 4 ---- -Language: Cpp -Standard: c++17 -UseTab: Never +InsertBraces: true InsertNewlineAtEOF: true -ColumnLimit: 120 +InsertTrailingCommas: Wrapped +LineEnding: LF +RemoveParentheses: MultipleParentheses +RemoveSemicolon: true +SpaceAfterCStyleCast: true +SpaceAfterLogicalNot: true +SpaceAfterTemplateKeyword: false +SpaceBeforeCtorInitializerColon: false +SpacesInContainerLiterals: false +Standard: c++17 +TabWidth: 4 +BracedInitializerIndentWidth: 2 diff --git a/include/filc/filc.h b/include/filc/filc.h index 5e847f9..3dce855 100644 --- a/include/filc/filc.h +++ b/include/filc/filc.h @@ -24,11 +24,9 @@ #ifndef FILC_FILC_H #define FILC_FILC_H -#include "filc/grammar/Parser.h" #include "filc/options/OptionsParser.h" #include "filc/grammar/DumpVisitor.h" #include "filc/validation/ValidationVisitor.h" -#include "filc/llvm/IRGenerator.h" namespace filc { class FilCompiler final { diff --git a/include/filc/grammar/DumpVisitor.h b/include/filc/grammar/DumpVisitor.h index 15173ea..236337f 100644 --- a/include/filc/grammar/DumpVisitor.h +++ b/include/filc/grammar/DumpVisitor.h @@ -56,7 +56,7 @@ class DumpVisitor final: public Visitor { std::ostream &_out; int _indent_level; - auto printIdent() -> void; + auto printIdent() const -> void; }; } diff --git a/include/filc/grammar/Type.h b/include/filc/grammar/Type.h index 0ad6cba..d4b3783 100644 --- a/include/filc/grammar/Type.h +++ b/include/filc/grammar/Type.h @@ -24,13 +24,15 @@ #ifndef FILC_TYPE_H #define FILC_TYPE_H -#include -#include #include +#include +#include namespace filc { class AbstractType { public: + virtual ~AbstractType() = default; + [[nodiscard]] virtual auto getName() const noexcept -> std::string = 0; [[nodiscard]] virtual auto getDisplayName() const noexcept -> std::string = 0; @@ -48,7 +50,7 @@ class AbstractType { llvm::Type *_llvm_type = nullptr; }; -class Type final: public AbstractType { +class Type final : public AbstractType { public: explicit Type(std::string name); @@ -62,7 +64,7 @@ class Type final: public AbstractType { std::string _name; }; -class PointerType final: public AbstractType { +class PointerType final : public AbstractType { public: explicit PointerType(std::shared_ptr pointed_type); @@ -76,7 +78,7 @@ class PointerType final: public AbstractType { std::shared_ptr _pointed_type; }; -class AliasType final: public AbstractType { +class AliasType final : public AbstractType { public: AliasType(std::string name, std::shared_ptr aliased_type); @@ -90,7 +92,7 @@ class AliasType final: public AbstractType { std::string _name; std::shared_ptr _aliased_type; }; -} +} // namespace filc auto operator==(const std::shared_ptr &a, const std::shared_ptr &b) -> bool; auto operator!=(const std::shared_ptr &a, const std::shared_ptr &b) -> bool; diff --git a/include/filc/grammar/Visitor.h b/include/filc/grammar/Visitor.h index 1ad6c65..c309cbf 100644 --- a/include/filc/grammar/Visitor.h +++ b/include/filc/grammar/Visitor.h @@ -25,11 +25,11 @@ #define FILC_VISITOR_H #include "filc/grammar/ast.h" + #include "llvm/IR/Value.h" namespace filc { -template -class Visitor { +template class Visitor { public: virtual ~Visitor() = default; @@ -59,10 +59,12 @@ class Visitor { class Visitable { public: + virtual ~Visitable() = default; + virtual auto acceptVoidVisitor(Visitor *visitor) -> void = 0; - virtual auto acceptIRVisitor(Visitor *visitor) -> llvm::Value * = 0; + virtual auto acceptIRVisitor(Visitor *visitor) -> llvm::Value * = 0; }; -} +} // namespace filc #endif // FILC_VISITOR_H diff --git a/include/filc/grammar/assignation/Assignation.h b/include/filc/grammar/assignation/Assignation.h index 920fa61..818a34b 100644 --- a/include/filc/grammar/assignation/Assignation.h +++ b/include/filc/grammar/assignation/Assignation.h @@ -24,7 +24,6 @@ #ifndef FILC_ASSIGNATION_H #define FILC_ASSIGNATION_H -#include "filc/grammar/ast.h" #include "filc/grammar/expression/Expression.h" #include #include diff --git a/include/filc/grammar/calcul/Calcul.h b/include/filc/grammar/calcul/Calcul.h index 3c56962..0092268 100644 --- a/include/filc/grammar/calcul/Calcul.h +++ b/include/filc/grammar/calcul/Calcul.h @@ -24,7 +24,6 @@ #ifndef FILC_CALCUL_H #define FILC_CALCUL_H -#include "filc/grammar/ast.h" #include "filc/grammar/expression/Expression.h" #include #include diff --git a/include/filc/grammar/expression/Expression.h b/include/filc/grammar/expression/Expression.h index 2827a6f..b7a3d5c 100644 --- a/include/filc/grammar/expression/Expression.h +++ b/include/filc/grammar/expression/Expression.h @@ -24,11 +24,9 @@ #ifndef FILC_EXPRESSION_H #define FILC_EXPRESSION_H -#include "filc/grammar/ast.h" #include "filc/grammar/Visitor.h" #include "filc/grammar/Position.h" #include "filc/grammar/Type.h" -#include namespace filc { class Expression: public Visitable { diff --git a/include/filc/grammar/identifier/Identifier.h b/include/filc/grammar/identifier/Identifier.h index c5a5fcf..9e00eb5 100644 --- a/include/filc/grammar/identifier/Identifier.h +++ b/include/filc/grammar/identifier/Identifier.h @@ -24,7 +24,6 @@ #ifndef FILC_IDENTIFIER_H #define FILC_IDENTIFIER_H -#include "filc/grammar/ast.h" #include "filc/grammar/expression/Expression.h" #include diff --git a/include/filc/grammar/literal/Literal.h b/include/filc/grammar/literal/Literal.h index 0f398b8..fe3cde2 100644 --- a/include/filc/grammar/literal/Literal.h +++ b/include/filc/grammar/literal/Literal.h @@ -24,7 +24,6 @@ #ifndef FILC_LITERAL_H #define FILC_LITERAL_H -#include "filc/grammar/ast.h" #include "filc/grammar/expression/Expression.h" namespace filc { diff --git a/include/filc/grammar/variable/Variable.h b/include/filc/grammar/variable/Variable.h index 341e12f..14a8e3c 100644 --- a/include/filc/grammar/variable/Variable.h +++ b/include/filc/grammar/variable/Variable.h @@ -24,13 +24,12 @@ #ifndef FILC_VARIABLE_H #define FILC_VARIABLE_H -#include "filc/grammar/ast.h" #include "filc/grammar/expression/Expression.h" #include #include namespace filc { -class VariableDeclaration: public Expression { +class VariableDeclaration final: public Expression { public: VariableDeclaration(bool is_constant, std::string name, std::string _type_name, std::shared_ptr value); diff --git a/include/filc/llvm/CalculBuilder.h b/include/filc/llvm/CalculBuilder.h index 88f0bad..b2a08ae 100644 --- a/include/filc/llvm/CalculBuilder.h +++ b/include/filc/llvm/CalculBuilder.h @@ -24,7 +24,6 @@ #ifndef FILC_CALCULBUILDER_H #define FILC_CALCULBUILDER_H -#include "filc/grammar/calcul/Calcul.h" #include "filc/llvm/IRGenerator.h" #include @@ -33,23 +32,23 @@ class CalculBuilder final { public: explicit CalculBuilder(IRGenerator *generator, llvm::IRBuilder<> *builder); - auto buildCalculValue(BinaryCalcul *calcul) const -> llvm::Value *; + auto buildCalculValue(const BinaryCalcul *calcul) const -> llvm::Value *; private: IRGenerator *_generator; llvm::IRBuilder<> *_builder; - auto buildSignedInteger(BinaryCalcul *calcul) const -> llvm::Value *; + auto buildSignedInteger(const BinaryCalcul *calcul) const -> llvm::Value *; - auto buildUnsignedInteger(BinaryCalcul *calcul) const -> llvm::Value *; + auto buildUnsignedInteger(const BinaryCalcul *calcul) const -> llvm::Value *; - auto buildFloat(BinaryCalcul *calcul) const -> llvm::Value *; + auto buildFloat(const BinaryCalcul *calcul) const -> llvm::Value *; - auto buildBool(BinaryCalcul *calcul) const -> llvm::Value *; + auto buildBool(const BinaryCalcul *calcul) const -> llvm::Value *; - auto buildPointer(BinaryCalcul *calcul) const -> llvm::Value *; + auto buildPointer(const BinaryCalcul *calcul) const -> llvm::Value *; - auto static buildError(BinaryCalcul *calcul) -> std::logic_error; + auto static buildError(const BinaryCalcul *calcul) -> std::logic_error; }; } diff --git a/include/filc/options/OptionsParser.h b/include/filc/options/OptionsParser.h index faeee29..de33c4d 100644 --- a/include/filc/options/OptionsParser.h +++ b/include/filc/options/OptionsParser.h @@ -27,7 +27,6 @@ #include #include #include -#include namespace filc { class OptionsParser final { @@ -58,7 +57,7 @@ class OptionsParser final { cxxopts::ParseResult _result; }; -class OptionsParserException : public std::exception { +class OptionsParserException final : public std::exception { public: explicit OptionsParserException(std::string message); diff --git a/main.cpp b/main.cpp index c929002..1e33efd 100644 --- a/main.cpp +++ b/main.cpp @@ -24,7 +24,7 @@ #include auto main(int argc, char **argv) -> int { - auto compiler = - filc::FilCompiler(filc::OptionsParser(), filc::DumpVisitor(std::cout), filc::ValidationVisitor(std::cout)); + auto compiler + = filc::FilCompiler(filc::OptionsParser(), filc::DumpVisitor(std::cout), filc::ValidationVisitor(std::cout)); return compiler.run(argc, argv); } diff --git a/src/filc.cpp b/src/filc.cpp index 26a4cc4..c1aa607 100644 --- a/src/filc.cpp +++ b/src/filc.cpp @@ -22,15 +22,20 @@ * SOFTWARE. */ #include "filc/filc.h" + +#include "filc/grammar/Parser.h" #include "filc/grammar/program/Program.h" +#include "filc/llvm/IRGenerator.h" + #include #include #include using namespace filc; -FilCompiler::FilCompiler(OptionsParser options_parser, DumpVisitor ast_dump_visitor, - ValidationVisitor validation_visitor) +FilCompiler::FilCompiler( + OptionsParser options_parser, DumpVisitor ast_dump_visitor, ValidationVisitor validation_visitor +) : _options_parser(std::move(options_parser)), _ast_dump_visitor(std::move(ast_dump_visitor)), _validation_visitor(std::move(validation_visitor)) {} @@ -46,7 +51,7 @@ auto FilCompiler::run(int argc, char **argv) -> int { } const auto filename = _options_parser.getFile(); - if (!std::filesystem::exists(filename) || !std::filesystem::is_regular_file(filename)) { + if (! std::filesystem::exists(filename) || ! std::filesystem::is_regular_file(filename)) { std::cerr << "File " << filename << " not found"; return 1; } diff --git a/src/grammar/DumpVisitor.cpp b/src/grammar/DumpVisitor.cpp index 7e03a06..8cfa484 100644 --- a/src/grammar/DumpVisitor.cpp +++ b/src/grammar/DumpVisitor.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "filc/grammar/DumpVisitor.h" + #include "filc/grammar/assignation/Assignation.h" #include "filc/grammar/calcul/Calcul.h" #include "filc/grammar/identifier/Identifier.h" @@ -31,7 +32,7 @@ using namespace filc; -DumpVisitor::DumpVisitor(std::ostream &out) : _out(out), _indent_level(0) {} +DumpVisitor::DumpVisitor(std::ostream &out): _out(out), _indent_level(0) {} auto DumpVisitor::visitProgram(Program *program) -> void { _out << "=== Begin AST dump ===\n"; @@ -57,7 +58,7 @@ auto DumpVisitor::visitFloatLiteral(FloatLiteral *literal) -> void { } auto DumpVisitor::visitCharacterLiteral(CharacterLiteral *literal) -> void { - auto value = literal->getValue(); + const auto value = literal->getValue(); std::string to_print; switch (value) { case '\'': @@ -109,7 +110,7 @@ auto DumpVisitor::visitStringLiteral(StringLiteral *literal) -> void { auto DumpVisitor::visitVariableDeclaration(VariableDeclaration *variable) -> void { printIdent(); _out << "[Variable:" << (variable->isConstant() ? "val" : "var") << ":" << variable->getName(); - if (!variable->getTypeName().empty()) { + if (! variable->getTypeName().empty()) { _out << ":" << variable->getTypeName(); } _out << "]\n"; @@ -143,4 +144,6 @@ auto DumpVisitor::visitAssignation(Assignation *assignation) -> void { _indent_level--; } -auto DumpVisitor::printIdent() -> void { _out << std::string(_indent_level, '\t'); } +auto DumpVisitor::printIdent() const -> void { + _out << std::string(_indent_level, '\t'); +} diff --git a/src/grammar/Parser.cpp b/src/grammar/Parser.cpp index 4d1e645..fc04931 100644 --- a/src/grammar/Parser.cpp +++ b/src/grammar/Parser.cpp @@ -22,15 +22,17 @@ * SOFTWARE. */ #include "filc/grammar/Parser.h" + #include "FilLexer.h" #include "FilParser.h" #include "antlr4-runtime.h" + #include using namespace filc; auto ParserProxy::parse(const std::string &filename) -> std::shared_ptr { - if (!std::filesystem::exists(filename)) { + if (! std::filesystem::exists(filename)) { throw std::logic_error("File '" + filename + "' not found"); } diff --git a/src/grammar/Position.cpp b/src/grammar/Position.cpp index 2ea5030..a8fc20d 100644 --- a/src/grammar/Position.cpp +++ b/src/grammar/Position.cpp @@ -22,13 +22,14 @@ * SOFTWARE. */ #include "filc/grammar/Position.h" + #include using namespace filc; #define RESET "\033[0m" -Position::Position() : _start_position(0, 0), _end_position(0, 0) {} +Position::Position(): _start_position(0, 0), _end_position(0, 0) {} Position::Position(const antlr4::Token *start_token, const antlr4::Token *end_token) : _filename(start_token->getTokenSource()->getSourceName()) { @@ -37,14 +38,20 @@ Position::Position(const antlr4::Token *start_token, const antlr4::Token *end_to } _start_position = std::make_pair(start_token->getLine(), start_token->getCharPositionInLine()); - _end_position = std::make_pair(end_token->getLine(), end_token->getCharPositionInLine()); + _end_position = std::make_pair(end_token->getLine(), end_token->getCharPositionInLine()); } -auto Position::getFilename() const -> std::string { return _filename; } +auto Position::getFilename() const -> std::string { + return _filename; +} -auto Position::getStartPosition() const -> std::pair { return _start_position; } +auto Position::getStartPosition() const -> std::pair { + return _start_position; +} -auto Position::getEndPosition() const -> std::pair { return _end_position; } +auto Position::getEndPosition() const -> std::pair { + return _end_position; +} auto Position::getContent() const -> std::vector { if (_filename.empty() || _filename == "") { @@ -52,14 +59,14 @@ auto Position::getContent() const -> std::vector { } std::ifstream file(_filename); - if (!file.is_open() || !file.good()) { + if (! file.is_open() || ! file.good()) { throw std::logic_error("File '" + _filename + "' not found"); } std::vector content; const auto start_line = _start_position.first; - unsigned int index = 0; + unsigned int index = 0; std::string line; for (; index < start_line; index++) { std::getline(file, line); @@ -80,21 +87,21 @@ auto Position::getContent() const -> std::vector { } auto Position::dump(const std::string &color) const -> std::string { - const auto start_line = _start_position.first; + const auto start_line = _start_position.first; const auto start_column = _start_position.second; - const auto end_line = _end_position.first; - const auto end_column = _end_position.second; - const auto content = getContent(); + const auto end_line = _end_position.first; + const auto end_column = _end_position.second; + const auto content = getContent(); if (content.empty()) { return ""; } if (content.size() == 1) { // Single line - const auto nth = " " + std::to_string(start_line) + " "; + const auto nth = " " + std::to_string(start_line) + " "; const auto spaces = start_column > 0 ? std::string(start_column, ' ') : ""; - return std::string(nth.length() - 1, ' ') + "--> " + _filename + ":" + std::to_string(start_line) + ":" + - std::to_string(start_column) + "\n" + nth + "| " + content[0] + "\n" + std::string(nth.length(), ' ') + - "| " + spaces + color + "^" + RESET + "\n"; + return std::string(nth.length() - 1, ' ') + "--> " + _filename + ":" + std::to_string(start_line) + ":" + + std::to_string(start_column) + "\n" + nth + "| " + content[0] + "\n" + std::string(nth.length(), ' ') + + "| " + spaces + color + "^" + RESET + "\n"; } if (content.size() > 1) { // Multi line @@ -104,12 +111,12 @@ auto Position::dump(const std::string &color) const -> std::string { const auto line = " " + std::to_string(i) + " "; nths.push_back(line + std::string(nth_end.length() - line.length(), ' ')); } - const auto nth_spaces = std::string(nth_end.length(), ' ') + "| "; + const auto nth_spaces = std::string(nth_end.length(), ' ') + "| "; const auto start_spaces = start_column > 0 ? std::string(start_column, ' ') : ""; - const auto end_spaces = end_column > 0 ? std::string(end_column, ' ') : ""; + const auto end_spaces = end_column > 0 ? std::string(end_column, ' ') : ""; - auto res = std::string(nth_end.length() - 1, ' ') + "--> " + _filename + ":" + std::to_string(start_line) + - ":" + std::to_string(start_column) + "\n" + nth_spaces + start_spaces + color + "v" + RESET + "\n"; + auto res = std::string(nth_end.length() - 1, ' ') + "--> " + _filename + ":" + std::to_string(start_line) + ":" + + std::to_string(start_column) + "\n" + nth_spaces + start_spaces + color + "v" + RESET + "\n"; for (unsigned int i = 0; i < nths.size(); i++) { res += nths[i] + "| " + content[i] + "\n"; diff --git a/src/grammar/Type.cpp b/src/grammar/Type.cpp index cc8ecc7..0a8bbd5 100644 --- a/src/grammar/Type.cpp +++ b/src/grammar/Type.cpp @@ -22,27 +22,42 @@ * SOFTWARE. */ #include "filc/grammar/Type.h" + #include using namespace filc; -auto AbstractType::setLLVMType(llvm::Type *type) -> void { _llvm_type = type; } +auto AbstractType::setLLVMType(llvm::Type *type) -> void { + _llvm_type = type; +} -auto AbstractType::getLLVMType() const -> llvm::Type * { return _llvm_type; } +auto AbstractType::getLLVMType() const -> llvm::Type * { + return _llvm_type; +} -Type::Type(std::string name) : _name(std::move(name)) {} +Type::Type(std::string name): _name(std::move(name)) {} -auto Type::getName() const noexcept -> std::string { return _name; } +auto Type::getName() const noexcept -> std::string { + return _name; +} -auto Type::getDisplayName() const noexcept -> std::string { return getName(); } +auto Type::getDisplayName() const noexcept -> std::string { + return getName(); +} -auto Type::toDisplay() const noexcept -> std::string { return getName(); } +auto Type::toDisplay() const noexcept -> std::string { + return getName(); +} -PointerType::PointerType(std::shared_ptr pointed_type) : _pointed_type(std::move(pointed_type)) {} +PointerType::PointerType(std::shared_ptr pointed_type): _pointed_type(std::move(pointed_type)) {} -auto PointerType::getName() const noexcept -> std::string { return _pointed_type->getName() + "*"; } +auto PointerType::getName() const noexcept -> std::string { + return _pointed_type->getName() + "*"; +} -auto PointerType::getDisplayName() const noexcept -> std::string { return _pointed_type->getDisplayName() + "*"; } +auto PointerType::getDisplayName() const noexcept -> std::string { + return _pointed_type->getDisplayName() + "*"; +} auto PointerType::toDisplay() const noexcept -> std::string { if (_pointed_type->getName() != _pointed_type->getDisplayName()) { @@ -54,16 +69,22 @@ auto PointerType::toDisplay() const noexcept -> std::string { AliasType::AliasType(std::string name, std::shared_ptr aliased_type) : _name(std::move(name)), _aliased_type(std::move(aliased_type)) {} -auto AliasType::getName() const noexcept -> std::string { return _aliased_type->getName(); } +auto AliasType::getName() const noexcept -> std::string { + return _aliased_type->getName(); +} -auto AliasType::getDisplayName() const noexcept -> std::string { return _name; } +auto AliasType::getDisplayName() const noexcept -> std::string { + return _name; +} -auto AliasType::toDisplay() const noexcept -> std::string { return getDisplayName() + " aka " + getName(); } +auto AliasType::toDisplay() const noexcept -> std::string { + return getDisplayName() + " aka " + getName(); +} auto operator==(const std::shared_ptr &a, const std::shared_ptr &b) -> bool { return a->getName() == b->getName(); } auto operator!=(const std::shared_ptr &a, const std::shared_ptr &b) -> bool { - return !(a == b); + return ! (a == b); } diff --git a/src/grammar/assignation/Assignation.cpp b/src/grammar/assignation/Assignation.cpp index 34ef763..0752cb5 100644 --- a/src/grammar/assignation/Assignation.cpp +++ b/src/grammar/assignation/Assignation.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "filc/grammar/assignation/Assignation.h" + #include using namespace filc; @@ -29,11 +30,17 @@ using namespace filc; Assignation::Assignation(std::string identifier, std::shared_ptr value) : _identifier(std::move(identifier)), _value(std::move(value)) {} -auto Assignation::getIdentifier() const -> std::string { return _identifier; } +auto Assignation::getIdentifier() const -> std::string { + return _identifier; +} -auto Assignation::getValue() const -> std::shared_ptr { return _value; } +auto Assignation::getValue() const -> std::shared_ptr { + return _value; +} -auto Assignation::acceptVoidVisitor(Visitor *visitor) -> void { visitor->visitAssignation(this); } +auto Assignation::acceptVoidVisitor(Visitor *visitor) -> void { + visitor->visitAssignation(this); +} auto Assignation::acceptIRVisitor(Visitor *visitor) -> llvm::Value * { return visitor->visitAssignation(this); diff --git a/src/grammar/calcul/BinaryCalcul.cpp b/src/grammar/calcul/BinaryCalcul.cpp index be5103a..2c0d446 100644 --- a/src/grammar/calcul/BinaryCalcul.cpp +++ b/src/grammar/calcul/BinaryCalcul.cpp @@ -22,22 +22,32 @@ * SOFTWARE. */ #include "filc/grammar/calcul/Calcul.h" + #include using namespace filc; -BinaryCalcul::BinaryCalcul(std::shared_ptr left_expression, std::string op, - std::shared_ptr right_expression) +BinaryCalcul::BinaryCalcul( + std::shared_ptr left_expression, std::string op, std::shared_ptr right_expression +) : _left_expression(std::move(left_expression)), _operator(std::move(op)), _right_expression(std::move(right_expression)) {} -auto BinaryCalcul::getLeftExpression() const -> std::shared_ptr { return _left_expression; } +auto BinaryCalcul::getLeftExpression() const -> std::shared_ptr { + return _left_expression; +} -auto BinaryCalcul::getOperator() const -> std::string { return _operator; } +auto BinaryCalcul::getOperator() const -> std::string { + return _operator; +} -auto BinaryCalcul::getRightExpression() const -> std::shared_ptr { return _right_expression; } +auto BinaryCalcul::getRightExpression() const -> std::shared_ptr { + return _right_expression; +} -auto BinaryCalcul::acceptVoidVisitor(Visitor *visitor) -> void { visitor->visitBinaryCalcul(this); } +auto BinaryCalcul::acceptVoidVisitor(Visitor *visitor) -> void { + visitor->visitBinaryCalcul(this); +} auto BinaryCalcul::acceptIRVisitor(Visitor *visitor) -> llvm::Value * { return visitor->visitBinaryCalcul(this); diff --git a/src/grammar/expression/Expression.cpp b/src/grammar/expression/Expression.cpp index 4a190f9..b129b31 100644 --- a/src/grammar/expression/Expression.cpp +++ b/src/grammar/expression/Expression.cpp @@ -27,10 +27,18 @@ using namespace filc; Expression::Expression() = default; -auto Expression::setPosition(const Position &position) -> void { _position = position; } +auto Expression::setPosition(const Position &position) -> void { + _position = position; +} -auto Expression::getPosition() const -> const Position & { return _position; } +auto Expression::getPosition() const -> const Position & { + return _position; +} -auto Expression::setType(const std::shared_ptr &type) -> void { _type = type; } +auto Expression::setType(const std::shared_ptr &type) -> void { + _type = type; +} -auto Expression::getType() const -> const std::shared_ptr & { return _type; } +auto Expression::getType() const -> const std::shared_ptr & { + return _type; +} diff --git a/src/grammar/identifier/Identifier.cpp b/src/grammar/identifier/Identifier.cpp index 0c07bb6..5c4b883 100644 --- a/src/grammar/identifier/Identifier.cpp +++ b/src/grammar/identifier/Identifier.cpp @@ -22,15 +22,20 @@ * SOFTWARE. */ #include "filc/grammar/identifier/Identifier.h" + #include using namespace filc; -Identifier::Identifier(std::string name) : _name(std::move(name)) {} +Identifier::Identifier(std::string name): _name(std::move(name)) {} -auto Identifier::getName() const -> std::string { return _name; } +auto Identifier::getName() const -> std::string { + return _name; +} -auto Identifier::acceptVoidVisitor(Visitor *visitor) -> void { visitor->visitIdentifier(this); } +auto Identifier::acceptVoidVisitor(Visitor *visitor) -> void { + visitor->visitIdentifier(this); +} auto Identifier::acceptIRVisitor(Visitor *visitor) -> llvm::Value * { return visitor->visitIdentifier(this); diff --git a/src/grammar/literal/BooleanLiteral.cpp b/src/grammar/literal/BooleanLiteral.cpp index acafc53..1b382fc 100644 --- a/src/grammar/literal/BooleanLiteral.cpp +++ b/src/grammar/literal/BooleanLiteral.cpp @@ -25,9 +25,11 @@ using namespace filc; -BooleanLiteral::BooleanLiteral(bool value) : Literal(value) {} +BooleanLiteral::BooleanLiteral(const bool value): Literal(value) {} -auto BooleanLiteral::acceptVoidVisitor(Visitor *visitor) -> void { visitor->visitBooleanLiteral(this); } +auto BooleanLiteral::acceptVoidVisitor(Visitor *visitor) -> void { + visitor->visitBooleanLiteral(this); +} auto BooleanLiteral::acceptIRVisitor(Visitor *visitor) -> llvm::Value * { return visitor->visitBooleanLiteral(this); diff --git a/src/grammar/literal/CharacterLiteral.cpp b/src/grammar/literal/CharacterLiteral.cpp index aa88891..65b930c 100644 --- a/src/grammar/literal/CharacterLiteral.cpp +++ b/src/grammar/literal/CharacterLiteral.cpp @@ -23,14 +23,15 @@ */ #include "filc/grammar/literal/Literal.h" #include "filc/utils/utils.h" + #include using namespace filc; -CharacterLiteral::CharacterLiteral(char value) : Literal(value) {} +CharacterLiteral::CharacterLiteral(const char value): Literal(value) {} auto CharacterLiteral::stringToChar(const std::string &snippet) -> char { - auto value = snippet.substr(1, snippet.length() - 2); + const auto value = snippet.substr(1, snippet.length() - 2); if (value.size() == 1) { return value[0]; @@ -43,7 +44,9 @@ auto CharacterLiteral::stringToChar(const std::string &snippet) -> char { throw std::logic_error("Lexer found a character that is not regular: " + snippet); } -auto CharacterLiteral::acceptVoidVisitor(Visitor *visitor) -> void { visitor->visitCharacterLiteral(this); } +auto CharacterLiteral::acceptVoidVisitor(Visitor *visitor) -> void { + visitor->visitCharacterLiteral(this); +} auto CharacterLiteral::acceptIRVisitor(Visitor *visitor) -> llvm::Value * { return visitor->visitCharacterLiteral(this); diff --git a/src/grammar/literal/FloatLiteral.cpp b/src/grammar/literal/FloatLiteral.cpp index 4fe24dd..b302986 100644 --- a/src/grammar/literal/FloatLiteral.cpp +++ b/src/grammar/literal/FloatLiteral.cpp @@ -25,9 +25,11 @@ using namespace filc; -FloatLiteral::FloatLiteral(double value) : Literal(value) {} +FloatLiteral::FloatLiteral(const double value): Literal(value) {} -auto FloatLiteral::acceptVoidVisitor(Visitor *visitor) -> void { visitor->visitFloatLiteral(this); } +auto FloatLiteral::acceptVoidVisitor(Visitor *visitor) -> void { + visitor->visitFloatLiteral(this); +} auto FloatLiteral::acceptIRVisitor(Visitor *visitor) -> llvm::Value * { return visitor->visitFloatLiteral(this); diff --git a/src/grammar/literal/IntegerLiteral.cpp b/src/grammar/literal/IntegerLiteral.cpp index a3b078b..cbf9934 100644 --- a/src/grammar/literal/IntegerLiteral.cpp +++ b/src/grammar/literal/IntegerLiteral.cpp @@ -25,9 +25,11 @@ using namespace filc; -IntegerLiteral::IntegerLiteral(int value) : Literal(value) {} +IntegerLiteral::IntegerLiteral(const int value): Literal(value) {} -auto IntegerLiteral::acceptVoidVisitor(Visitor *visitor) -> void { visitor->visitIntegerLiteral(this); } +auto IntegerLiteral::acceptVoidVisitor(Visitor *visitor) -> void { + visitor->visitIntegerLiteral(this); +} auto IntegerLiteral::acceptIRVisitor(Visitor *visitor) -> llvm::Value * { return visitor->visitIntegerLiteral(this); diff --git a/src/grammar/literal/StringLiteral.cpp b/src/grammar/literal/StringLiteral.cpp index bb21179..dfb4921 100644 --- a/src/grammar/literal/StringLiteral.cpp +++ b/src/grammar/literal/StringLiteral.cpp @@ -25,9 +25,11 @@ using namespace filc; -StringLiteral::StringLiteral(const std::string &value) : Literal(value.substr(1, value.length() - 2)) {} +StringLiteral::StringLiteral(const std::string &value): Literal(value.substr(1, value.length() - 2)) {} -auto StringLiteral::acceptVoidVisitor(Visitor *visitor) -> void { visitor->visitStringLiteral(this); } +auto StringLiteral::acceptVoidVisitor(Visitor *visitor) -> void { + visitor->visitStringLiteral(this); +} auto StringLiteral::acceptIRVisitor(Visitor *visitor) -> llvm::Value * { return visitor->visitStringLiteral(this); diff --git a/src/grammar/program/Program.cpp b/src/grammar/program/Program.cpp index fd3d037..86c8110 100644 --- a/src/grammar/program/Program.cpp +++ b/src/grammar/program/Program.cpp @@ -25,10 +25,16 @@ using namespace filc; -Program::Program(const std::vector> &expressions) : _expressions(expressions) {} +Program::Program(const std::vector> &expressions): _expressions(expressions) {} -auto Program::getExpressions() const -> const std::vector> & { return _expressions; } +auto Program::getExpressions() const -> const std::vector> & { + return _expressions; +} -auto Program::acceptVoidVisitor(Visitor *visitor) -> void { visitor->visitProgram(this); } +auto Program::acceptVoidVisitor(Visitor *visitor) -> void { + visitor->visitProgram(this); +} -auto Program::acceptIRVisitor(Visitor *visitor) -> llvm::Value * { return visitor->visitProgram(this); } +auto Program::acceptIRVisitor(Visitor *visitor) -> llvm::Value * { + return visitor->visitProgram(this); +} diff --git a/src/grammar/variable/Variable.cpp b/src/grammar/variable/Variable.cpp index 5e7de80..aa45f4a 100644 --- a/src/grammar/variable/Variable.cpp +++ b/src/grammar/variable/Variable.cpp @@ -22,23 +22,35 @@ * SOFTWARE. */ #include "filc/grammar/variable/Variable.h" + #include using namespace filc; -VariableDeclaration::VariableDeclaration(bool is_constant, std::string name, std::string type_name, - std::shared_ptr value) +VariableDeclaration::VariableDeclaration( + const bool is_constant, std::string name, std::string type_name, std::shared_ptr value +) : _constant(is_constant), _name(std::move(name)), _type_name(std::move(type_name)), _value(std::move(value)) {} -auto VariableDeclaration::isConstant() const -> bool { return _constant; } +auto VariableDeclaration::isConstant() const -> bool { + return _constant; +} -auto VariableDeclaration::getName() const -> std::string { return _name; } +auto VariableDeclaration::getName() const -> std::string { + return _name; +} -auto VariableDeclaration::getTypeName() const -> std::string { return _type_name; } +auto VariableDeclaration::getTypeName() const -> std::string { + return _type_name; +} -auto VariableDeclaration::getValue() const -> std::shared_ptr { return _value; } +auto VariableDeclaration::getValue() const -> std::shared_ptr { + return _value; +} -auto VariableDeclaration::acceptVoidVisitor(Visitor *visitor) -> void { visitor->visitVariableDeclaration(this); } +auto VariableDeclaration::acceptVoidVisitor(Visitor *visitor) -> void { + visitor->visitVariableDeclaration(this); +} auto VariableDeclaration::acceptIRVisitor(Visitor *visitor) -> llvm::Value * { return visitor->visitVariableDeclaration(this); diff --git a/src/llvm/CalculBuilder.cpp b/src/llvm/CalculBuilder.cpp index dd75a59..7fb145d 100644 --- a/src/llvm/CalculBuilder.cpp +++ b/src/llvm/CalculBuilder.cpp @@ -23,14 +23,16 @@ */ #include "filc/llvm/CalculBuilder.h" +#include "filc/grammar/calcul/Calcul.h" + using namespace filc; CalculBuilder::CalculBuilder(IRGenerator *generator, llvm::IRBuilder<> *builder) : _generator(generator), _builder(builder) {} -auto CalculBuilder::buildCalculValue(BinaryCalcul *calcul) const -> llvm::Value * { +auto CalculBuilder::buildCalculValue(const BinaryCalcul *calcul) const -> llvm::Value * { const auto left_type_name = calcul->getLeftExpression()->getType()->getName(); - const auto operation = calcul->getOperator(); + const auto operation = calcul->getOperator(); const std::vector signed_integers = {"i8", "i16", "i32", "i64", "i128"}; if (std::find(signed_integers.begin(), signed_integers.end(), left_type_name) != signed_integers.end()) { @@ -57,189 +59,308 @@ auto CalculBuilder::buildCalculValue(BinaryCalcul *calcul) const -> llvm::Value throw buildError(calcul); } -auto CalculBuilder::buildSignedInteger(BinaryCalcul *calcul) const -> llvm::Value * { +auto CalculBuilder::buildSignedInteger(const BinaryCalcul *calcul) const -> llvm::Value * { const auto operation = calcul->getOperator(); if (operation == "%") { - return _builder->CreateSRem(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_mod"); + return _builder->CreateSRem( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_mod" + ); } if (operation == "+") { - return _builder->CreateAdd(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_add"); + return _builder->CreateAdd( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_add" + ); } if (operation == "-") { - return _builder->CreateSub(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_sub"); + return _builder->CreateSub( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_sub" + ); } if (operation == "/") { - return _builder->CreateSDiv(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_div"); + return _builder->CreateSDiv( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_div" + ); } if (operation == "*") { - return _builder->CreateMul(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_mul"); + return _builder->CreateMul( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_mul" + ); } if (operation == "<") { - return _builder->CreateICmpSLT(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_lt"); + return _builder->CreateICmpSLT( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_lt" + ); } if (operation == "<=") { - return _builder->CreateICmpSLE(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_le"); + return _builder->CreateICmpSLE( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_le" + ); } if (operation == ">") { - return _builder->CreateICmpSGT(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_gt"); + return _builder->CreateICmpSGT( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_gt" + ); } if (operation == ">=") { - return _builder->CreateICmpSGE(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_ge"); + return _builder->CreateICmpSGE( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_ge" + ); } if (operation == "==") { - return _builder->CreateICmpEQ(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_equality"); + return _builder->CreateICmpEQ( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_equality" + ); } if (operation == "!=") { - return _builder->CreateICmpNE(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_inequality"); + return _builder->CreateICmpNE( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_inequality" + ); } throw buildError(calcul); } -auto CalculBuilder::buildUnsignedInteger(BinaryCalcul *calcul) const -> llvm::Value * { +auto CalculBuilder::buildUnsignedInteger(const BinaryCalcul *calcul) const -> llvm::Value * { const auto operation = calcul->getOperator(); if (operation == "%") { - return _builder->CreateURem(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_mod"); + return _builder->CreateURem( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_mod" + ); } if (operation == "+") { - return _builder->CreateAdd(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_add"); + return _builder->CreateAdd( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_add" + ); } if (operation == "-") { - return _builder->CreateSub(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_sub"); + return _builder->CreateSub( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_sub" + ); } if (operation == "/") { - return _builder->CreateUDiv(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_div"); + return _builder->CreateUDiv( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_div" + ); } if (operation == "*") { - return _builder->CreateMul(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_mul"); + return _builder->CreateMul( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_mul" + ); } if (operation == "<") { - return _builder->CreateICmpULT(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_lt"); + return _builder->CreateICmpULT( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_lt" + ); } if (operation == "<=") { - return _builder->CreateICmpULE(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_le"); + return _builder->CreateICmpULE( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_le" + ); } if (operation == ">") { - return _builder->CreateICmpUGT(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_gt"); + return _builder->CreateICmpUGT( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_gt" + ); } if (operation == ">=") { - return _builder->CreateICmpUGE(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_ge"); + return _builder->CreateICmpUGE( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_ge" + ); } if (operation == "==") { - return _builder->CreateICmpEQ(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_equality"); + return _builder->CreateICmpEQ( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_equality" + ); } if (operation == "!=") { - return _builder->CreateICmpNE(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "int_inequality"); + return _builder->CreateICmpNE( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "int_inequality" + ); } throw buildError(calcul); } -auto CalculBuilder::buildFloat(BinaryCalcul *calcul) const -> llvm::Value * { +auto CalculBuilder::buildFloat(const BinaryCalcul *calcul) const -> llvm::Value * { const auto operation = calcul->getOperator(); if (operation == "%") { - return _builder->CreateFRem(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "float_mod"); + return _builder->CreateFRem( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "float_mod" + ); } if (operation == "+") { - return _builder->CreateFAdd(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "float_add"); + return _builder->CreateFAdd( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "float_add" + ); } if (operation == "-") { - return _builder->CreateFSub(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "float_sub"); + return _builder->CreateFSub( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "float_sub" + ); } if (operation == "/") { - return _builder->CreateFDiv(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "float_div"); + return _builder->CreateFDiv( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "float_div" + ); } if (operation == "*") { - return _builder->CreateFMul(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "float_mul"); + return _builder->CreateFMul( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "float_mul" + ); } if (operation == "<") { - return _builder->CreateFCmpOLT(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "float_lt"); + return _builder->CreateFCmpOLT( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "float_lt" + ); } if (operation == "<=") { - return _builder->CreateFCmpOLE(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "float_le"); + return _builder->CreateFCmpOLE( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "float_le" + ); } if (operation == ">") { - return _builder->CreateFCmpOGT(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "float_gt"); + return _builder->CreateFCmpOGT( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "float_gt" + ); } if (operation == ">=") { - return _builder->CreateFCmpOGE(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "float_ge"); + return _builder->CreateFCmpOGE( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "float_ge" + ); } if (operation == "==") { - return _builder->CreateFCmpOEQ(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "float_equality"); + return _builder->CreateFCmpOEQ( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "float_equality" + ); } if (operation == "!=") { - return _builder->CreateFCmpONE(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "float_inequality"); + return _builder->CreateFCmpONE( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "float_inequality" + ); } throw buildError(calcul); } -auto CalculBuilder::buildBool(BinaryCalcul *calcul) const -> llvm::Value * { +auto CalculBuilder::buildBool(const BinaryCalcul *calcul) const -> llvm::Value * { const auto operation = calcul->getOperator(); if (operation == "&&") { - return _builder->CreateAnd(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "bool_and"); + return _builder->CreateAnd( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "bool_and" + ); } if (operation == "||") { - return _builder->CreateOr(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "bool_or"); + return _builder->CreateOr( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "bool_or" + ); } if (operation == "==") { - return _builder->CreateICmpEQ(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "bool_equality"); + return _builder->CreateICmpEQ( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "bool_equality" + ); } if (operation == "!=") { - return _builder->CreateICmpNE(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "bool_inequality"); + return _builder->CreateICmpNE( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "bool_inequality" + ); } throw buildError(calcul); } -auto CalculBuilder::buildPointer(BinaryCalcul *calcul) const -> llvm::Value * { +auto CalculBuilder::buildPointer(const BinaryCalcul *calcul) const -> llvm::Value * { const auto operation = calcul->getOperator(); if (operation == "==") { - return _builder->CreateICmpEQ(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "pointer_equality"); + return _builder->CreateICmpEQ( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "pointer_equality" + ); } if (operation == "!=") { - return _builder->CreateICmpNE(calcul->getLeftExpression()->acceptIRVisitor(_generator), - calcul->getRightExpression()->acceptIRVisitor(_generator), "pointer_inequality"); + return _builder->CreateICmpNE( + calcul->getLeftExpression()->acceptIRVisitor(_generator), + calcul->getRightExpression()->acceptIRVisitor(_generator), + "pointer_inequality" + ); } throw buildError(calcul); } -auto CalculBuilder::buildError(BinaryCalcul *calcul) -> std::logic_error { - return std::logic_error("Should be caught by validation, got operation (" + calcul->getOperator() + ") with:\n" + - " - " + calcul->getLeftExpression()->getType()->toDisplay() + "\n" + " - " + - calcul->getRightExpression()->getType()->toDisplay()); +auto CalculBuilder::buildError(const BinaryCalcul *calcul) -> std::logic_error { + return std::logic_error( + "Should be caught by validation, got operation (" + calcul->getOperator() + ") with:\n" + " - " + + calcul->getLeftExpression()->getType()->toDisplay() + "\n" + " - " + + calcul->getRightExpression()->getType()->toDisplay() + ); } diff --git a/src/llvm/IRGenerator.cpp b/src/llvm/IRGenerator.cpp index b06c841..9a1e66e 100644 --- a/src/llvm/IRGenerator.cpp +++ b/src/llvm/IRGenerator.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "filc/llvm/IRGenerator.h" + #include "filc/grammar/assignation/Assignation.h" #include "filc/grammar/calcul/Calcul.h" #include "filc/grammar/identifier/Identifier.h" @@ -29,6 +30,7 @@ #include "filc/grammar/program/Program.h" #include "filc/grammar/variable/Variable.h" #include "filc/llvm/CalculBuilder.h" + #include #include #include @@ -42,8 +44,8 @@ using namespace filc; IRGenerator::IRGenerator(const std::string &filename, const Environment *environment) { _llvm_context = std::make_unique(); - _module = std::make_unique(llvm::StringRef(filename), *_llvm_context); - _builder = std::make_unique>(*_llvm_context); + _module = std::make_unique(llvm::StringRef(filename), *_llvm_context); + _builder = std::make_unique>(*_llvm_context); environment->prepareLLVMTypes(_llvm_context.get()); } @@ -66,7 +68,7 @@ auto IRGenerator::toTarget(const std::string &output_file, const std::string &ta std::string error; const auto target = llvm::TargetRegistry::lookupTarget(used_target_triple, error); - if (!target) { + if (! target) { std::cerr << error; return 1; } @@ -96,9 +98,9 @@ auto IRGenerator::toTarget(const std::string &output_file, const std::string &ta } auto IRGenerator::visitProgram(Program *program) -> llvm::Value * { - auto function_type = llvm::FunctionType::get(llvm::Type::getInt32Ty(*_llvm_context), {}, false); - auto function = llvm::Function::Create(function_type, llvm::Function::ExternalLinkage, "main", _module.get()); - auto basic_block = llvm::BasicBlock::Create(*_llvm_context, "entry", function); + const auto function_type = llvm::FunctionType::get(llvm::Type::getInt32Ty(*_llvm_context), {}, false); + const auto function = llvm::Function::Create(function_type, llvm::Function::ExternalLinkage, "main", _module.get()); + const auto basic_block = llvm::BasicBlock::Create(*_llvm_context, "entry", function); _builder->SetInsertPoint(basic_block); const auto &expressions = program->getExpressions(); @@ -106,7 +108,7 @@ auto IRGenerator::visitProgram(Program *program) -> llvm::Value * { const auto return_value = llvm::ConstantInt::get(*_llvm_context, llvm::APInt(32, 0, true)); _builder->CreateRet(return_value); } else { - for (auto it = expressions.begin(); it != expressions.end(); it++) { + for (auto it = expressions.begin(); it != expressions.end(); ++it) { if (it + 1 != expressions.end()) { (*it)->acceptIRVisitor(this); } else { @@ -128,14 +130,13 @@ auto IRGenerator::visitBooleanLiteral(BooleanLiteral *literal) -> llvm::Value * auto IRGenerator::visitIntegerLiteral(IntegerLiteral *literal) -> llvm::Value * { const auto type_name = literal->getType()->getName(); const auto is_signed = type_name[0] == 'i'; - const auto size = stoi(type_name.substr(1)); + const auto size = stoi(type_name.substr(1)); return llvm::ConstantInt::get(*_llvm_context, llvm::APInt(size, literal->getValue(), is_signed)); } auto IRGenerator::visitFloatLiteral(FloatLiteral *literal) -> llvm::Value * { - const auto type_name = literal->getType()->getName(); - if (type_name == "f32") { - return llvm::ConstantFP::get(*_llvm_context, llvm::APFloat((float)literal->getValue())); + if (literal->getType()->getName() == "f32") { + return llvm::ConstantFP::get(*_llvm_context, llvm::APFloat(static_cast(literal->getValue()))); } return llvm::ConstantFP::get(*_llvm_context, llvm::APFloat(literal->getValue())); } @@ -149,14 +150,15 @@ auto IRGenerator::visitStringLiteral(StringLiteral *literal) -> llvm::Value * { } auto IRGenerator::visitVariableDeclaration(VariableDeclaration *variable) -> llvm::Value * { - const auto global = new llvm::GlobalVariable(variable->getType()->getLLVMType(), variable->isConstant(), - llvm::GlobalValue::InternalLinkage); + const auto global = new llvm::GlobalVariable( + variable->getType()->getLLVMType(), variable->isConstant(), llvm::GlobalValue::InternalLinkage + ); global->setName(variable->getName()); _module->insertGlobalVariable(global); if (variable->getValue() != nullptr) { const auto value = variable->getValue()->acceptIRVisitor(this); - global->setInitializer((llvm::Constant *)value); + global->setInitializer((llvm::Constant *) value); return value; } @@ -169,13 +171,13 @@ auto IRGenerator::visitIdentifier(Identifier *identifier) -> llvm::Value * { } auto IRGenerator::visitBinaryCalcul(BinaryCalcul *calcul) -> llvm::Value * { - CalculBuilder builder(this, _builder.get()); + const CalculBuilder builder(this, _builder.get()); return builder.buildCalculValue(calcul); } auto IRGenerator::visitAssignation(Assignation *assignation) -> llvm::Value * { const auto variable = _module->getNamedGlobal(assignation->getIdentifier()); - const auto value = assignation->getValue()->acceptIRVisitor(this); + const auto value = assignation->getValue()->acceptIRVisitor(this); _builder->CreateStore(value, variable); return value; } diff --git a/src/options/OptionsParser.cpp b/src/options/OptionsParser.cpp index c4f20d2..de2b448 100644 --- a/src/options/OptionsParser.cpp +++ b/src/options/OptionsParser.cpp @@ -22,29 +22,34 @@ * SOFTWARE. */ #include "filc/options/OptionsParser.h" + #include using namespace filc; #define NOT_PARSED_MESSAGE "You should call parse() before getting results" -OptionsParser::OptionsParser() : _options("filc", "Fil compiler"), _parsed(false) { +OptionsParser::OptionsParser(): _options("filc", "Fil compiler"), _parsed(false) { _options.add_options()("file", "Path of file to compile.", cxxopts::value()->default_value("")); _options.parse_positional("file"); _options.positional_help("file"); - _options.add_options("General")("out,o", "Write output to file", - cxxopts::value()->default_value("a.out"), "")( - "target", "Generate code for the given target", cxxopts::value(), ""); + _options.add_options("General") + ("out,o", "Write output to file", cxxopts::value()->default_value("a.out"), "") + ("target", "Generate code for the given target", cxxopts::value(), ""); - _options.add_options("Troubleshooting")("help", "Show this help message and exit.")("version", - "Show version and exit."); + _options.add_options("Troubleshooting")("help", "Show this help message and exit.")( + "version", "Show version and exit." + ); - _options.add_options("Debug")("dump", "Dump some data. One of these values: ast, ir.", - cxxopts::value()->implicit_value("all")->default_value("none")); + _options.add_options("Debug")( + "dump", + "Dump some data. One of these values: ast, ir.", + cxxopts::value()->implicit_value("all")->default_value("none") + ); } -auto OptionsParser::parse(int argc, char **argv) -> void { +auto OptionsParser::parse(const int argc, char **argv) -> void { try { _result = _options.parse(argc, argv); } catch (std::exception &error) { @@ -54,17 +59,19 @@ auto OptionsParser::parse(int argc, char **argv) -> void { } auto OptionsParser::isHelp() const -> bool { - if (!_parsed) { + if (! _parsed) { throw OptionsParserException(NOT_PARSED_MESSAGE); } return _result.count("help") > 0 || _result.arguments().empty(); } -auto OptionsParser::showHelp(std::ostream &out) const -> void { out << _options.help() << "\n"; } +auto OptionsParser::showHelp(std::ostream &out) const -> void { + out << _options.help() << "\n"; +} auto OptionsParser::isVersion() const -> bool { - if (!_parsed) { + if (! _parsed) { throw OptionsParserException(NOT_PARSED_MESSAGE); } @@ -72,7 +79,7 @@ auto OptionsParser::isVersion() const -> bool { } auto OptionsParser::getFile() const -> std::string { - if (!_parsed) { + if (! _parsed) { throw OptionsParserException(NOT_PARSED_MESSAGE); } @@ -80,12 +87,12 @@ auto OptionsParser::getFile() const -> std::string { } auto OptionsParser::getDump() const -> std::string { - if (!_parsed) { + if (! _parsed) { throw OptionsParserException(NOT_PARSED_MESSAGE); } - auto dump = _result["dump"].as(); - auto valid = {"none", "all", "ast", "ir"}; + auto dump = _result["dump"].as(); + const auto valid = {"none", "all", "ast", "ir"}; if (std::find(valid.begin(), valid.end(), dump) == valid.end()) { throw OptionsParserException("Dump option value '" + dump + "' is not a valid value"); } @@ -93,10 +100,12 @@ auto OptionsParser::getDump() const -> std::string { return dump; } -auto OptionsParser::showVersion(std::ostream &out) -> void { out << FILC_VERSION << "\n"; } +auto OptionsParser::showVersion(std::ostream &out) -> void { + out << FILC_VERSION << "\n"; +} auto OptionsParser::getOutputFile() const -> std::string { - if (!_parsed) { + if (! _parsed) { throw OptionsParserException(NOT_PARSED_MESSAGE); } @@ -104,7 +113,7 @@ auto OptionsParser::getOutputFile() const -> std::string { } auto OptionsParser::getTarget() const -> std::string { - if (!_parsed) { + if (! _parsed) { throw OptionsParserException(NOT_PARSED_MESSAGE); } @@ -114,6 +123,8 @@ auto OptionsParser::getTarget() const -> std::string { return _result["target"].as(); } -OptionsParserException::OptionsParserException(std::string message) : _message(std::move(message)) {} +OptionsParserException::OptionsParserException(std::string message): _message(std::move(message)) {} -const char *OptionsParserException::what() const noexcept { return _message.c_str(); } +const char *OptionsParserException::what() const noexcept { + return _message.c_str(); +} diff --git a/src/utils/Message.cpp b/src/utils/Message.cpp index dfbe213..b6cf3d6 100644 --- a/src/utils/Message.cpp +++ b/src/utils/Message.cpp @@ -22,11 +22,12 @@ * SOFTWARE. */ #include "filc/utils/Message.h" + #include using namespace filc; -#define BOLD "\033[1m" +#define BOLD "\033[1m" #define RESET "\033[0m" Message::Message(std::string tag, std::string message, Position position, std::string color) @@ -36,4 +37,6 @@ auto Message::write(std::ostream &out) const -> std::ostream & { return out << BOLD << _color << "[" << _tag << "] " << RESET << _message << "\n" << _position.dump(_color); } -auto operator<<(std::ostream &out, const filc::Message &message) -> std::ostream & { return message.write(out); } +auto operator<<(std::ostream &out, const filc::Message &message) -> std::ostream & { + return message.write(out); +} diff --git a/src/validation/CalculValidator.cpp b/src/validation/CalculValidator.cpp index ed791d6..eceef91 100644 --- a/src/validation/CalculValidator.cpp +++ b/src/validation/CalculValidator.cpp @@ -22,23 +22,37 @@ * SOFTWARE. */ #include "filc/validation/CalculValidator.h" + #include #include using namespace filc; -CalculValidator::CalculValidator(Environment *environment) : _environment(environment) {} +CalculValidator::CalculValidator(Environment *environment): _environment(environment) {} -auto CalculValidator::isCalculValid(const std::shared_ptr &left_type, const std::string &op, - const std::shared_ptr &right_type) const - -> std::shared_ptr { +auto CalculValidator::isCalculValid( + const std::shared_ptr &left_type, + const std::string &op, + const std::shared_ptr &right_type +) const -> std::shared_ptr { if (left_type != right_type) { return nullptr; } const auto type = left_type->getName(); const std::vector numeric_type = { - "i8", "i16", "i32", "i64", "i128", "u8", "u16", "u32", "u64", "u128", "f32", "f64", + "i8", + "i16", + "i32", + "i64", + "i128", + "u8", + "u16", + "u32", + "u64", + "u128", + "f32", + "f64", }; if (std::find(numeric_type.begin(), numeric_type.end(), type) != numeric_type.end()) { return isNumericOperatorValid(left_type, op); @@ -56,8 +70,8 @@ auto CalculValidator::isCalculValid(const std::shared_ptr &left_ty return nullptr; } -auto CalculValidator::isNumericOperatorValid(const std::shared_ptr &left_type, - const std::string &op) const -> std::shared_ptr { +auto CalculValidator::isNumericOperatorValid(const std::shared_ptr &left_type, const std::string &op) + const -> std::shared_ptr { const std::vector numeric_op = {"%", "+", "-", "/", "*"}; const std::vector boolean_op = {"<", "<=", ">", ">=", "==", "!="}; diff --git a/src/validation/Environment.cpp b/src/validation/Environment.cpp index ffb6012..ee5e22b 100644 --- a/src/validation/Environment.cpp +++ b/src/validation/Environment.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "filc/validation/Environment.h" + #include #include @@ -75,10 +76,12 @@ auto Environment::prepareLLVMTypes(llvm::LLVMContext *context) const -> void { getType("char*")->setLLVMType(llvm::PointerType::get(llvm::Type::getInt8Ty(*context), 0)); } -auto Environment::hasType(const std::string &name) const -> bool { return _types.find(name) != _types.end(); } +auto Environment::hasType(const std::string &name) const -> bool { + return _types.find(name) != _types.end(); +} auto Environment::getType(const std::string &name) const -> const std::shared_ptr & { - if (!hasType(name)) { + if (! hasType(name)) { throw std::logic_error("Environment doesn't have type " + name); } return _types.at(name); @@ -91,10 +94,12 @@ auto Environment::addType(const std::shared_ptr &type) -> void { _types[type->getDisplayName()] = type; } -auto Environment::hasName(const std::string &name) const -> bool { return _names.find(name) != _names.end(); } +auto Environment::hasName(const std::string &name) const -> bool { + return _names.find(name) != _names.end(); +} auto Environment::getName(const std::string &name) const -> const Name & { - if (!hasName(name)) { + if (! hasName(name)) { throw std::logic_error("Environment doesn't have name " + name); } return _names.at(name); diff --git a/src/validation/Name.cpp b/src/validation/Name.cpp index 8c3c835..190447e 100644 --- a/src/validation/Name.cpp +++ b/src/validation/Name.cpp @@ -22,17 +22,24 @@ * SOFTWARE. */ #include "filc/validation/Name.h" + #include using namespace filc; -Name::Name() : _constant(true) {} +Name::Name(): _constant(true) {} -Name::Name(bool constant, std::string name, std::shared_ptr type) +Name::Name(const bool constant, std::string name, std::shared_ptr type) : _constant(constant), _name(std::move(name)), _type(std::move(type)) {} -auto Name::isConstant() const -> bool { return _constant; } +auto Name::isConstant() const -> bool { + return _constant; +} -auto Name::getName() const -> const std::string & { return _name; } +auto Name::getName() const -> const std::string & { + return _name; +} -auto Name::getType() const -> std::shared_ptr { return _type; } +auto Name::getType() const -> std::shared_ptr { + return _type; +} diff --git a/src/validation/ValidationContext.cpp b/src/validation/ValidationContext.cpp index 89a7c5a..1ae9df5 100644 --- a/src/validation/ValidationContext.cpp +++ b/src/validation/ValidationContext.cpp @@ -25,9 +25,13 @@ using namespace filc; -ValidationContext::ValidationContext() { stack(); } +ValidationContext::ValidationContext() { + stack(); +} -auto ValidationContext::stack() -> void { _values.emplace(); } +auto ValidationContext::stack() -> void { + _values.emplace(); +} auto ValidationContext::unstack() -> void { if (_values.size() > 1) { @@ -35,10 +39,14 @@ auto ValidationContext::unstack() -> void { } } -auto ValidationContext::set(const std::string &key, const std::any &value) -> void { _values.top()[key] = value; } +auto ValidationContext::set(const std::string &key, const std::any &value) -> void { + _values.top()[key] = value; +} auto ValidationContext::has(const std::string &key) const -> bool { return _values.top().find(key) != _values.top().end(); } -auto ValidationContext::clear() -> void { _values.top().clear(); } +auto ValidationContext::clear() -> void { + _values.top().clear(); +} diff --git a/src/validation/ValidationVisitor.cpp b/src/validation/ValidationVisitor.cpp index 64696e4..91f3537 100644 --- a/src/validation/ValidationVisitor.cpp +++ b/src/validation/ValidationVisitor.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "filc/validation/ValidationVisitor.h" + #include "filc/grammar/assignation/Assignation.h" #include "filc/grammar/calcul/Calcul.h" #include "filc/grammar/identifier/Identifier.h" @@ -30,6 +31,7 @@ #include "filc/grammar/variable/Variable.h" #include "filc/utils/Message.h" #include "filc/validation/CalculValidator.h" + #include using namespace filc; @@ -37,9 +39,13 @@ using namespace filc; ValidationVisitor::ValidationVisitor(std::ostream &out) : _context(new ValidationContext()), _environment(new Environment()), _out(out), _error(false) {} -auto ValidationVisitor::getEnvironment() const -> const Environment * { return _environment.get(); } +auto ValidationVisitor::getEnvironment() const -> const Environment * { + return _environment.get(); +} -auto ValidationVisitor::hasError() const -> bool { return _error; } +auto ValidationVisitor::hasError() const -> bool { + return _error; +} auto ValidationVisitor::displayError(const std::string &message, const Position &position) -> void { _error = true; @@ -52,7 +58,7 @@ auto ValidationVisitor::displayWarning(const std::string &message, const Positio auto ValidationVisitor::visitProgram(Program *program) -> void { auto expressions = program->getExpressions(); - for (auto it = expressions.begin(); it != expressions.end(); it++) { + for (auto it = expressions.begin(); it != expressions.end(); ++it) { if (it + 1 == expressions.end()) { _context->set("return", true); } @@ -61,16 +67,18 @@ auto ValidationVisitor::visitProgram(Program *program) -> void { if (it + 1 == expressions.end()) { const auto expected = _environment->getType("int"); - const std::vector allowed_types = {"i8", "i16", "i32", "i64", "i128", "u8", - "u16", "u32", "u64", "u128", "bool"}; + const std::vector allowed_types + = {"i8", "i16", "i32", "i64", "i128", "u8", "u16", "u32", "u64", "u128", "bool"}; const auto found_type = (*it)->getType(); if (found_type == nullptr) { return; } if (std::find(allowed_types.begin(), allowed_types.end(), found_type->getName()) == allowed_types.end()) { - displayError("Expected type " + expected->toDisplay() + " but got " + found_type->toDisplay(), - (*it)->getPosition()); + displayError( + "Expected type " + expected->toDisplay() + " but got " + found_type->toDisplay(), + (*it)->getPosition() + ); } } @@ -81,14 +89,14 @@ auto ValidationVisitor::visitProgram(Program *program) -> void { auto ValidationVisitor::visitBooleanLiteral(BooleanLiteral *literal) -> void { literal->setType(_environment->getType("bool")); - if (!_context->has("return") || !_context->get("return")) { + if (! _context->has("return") || ! _context->get("return")) { displayWarning("Boolean value not used", literal->getPosition()); } } auto ValidationVisitor::visitIntegerLiteral(IntegerLiteral *literal) -> void { if (_context->has("cast_type")) { - const auto cast_type = _context->get>("cast_type"); + const auto cast_type = _context->get>("cast_type"); std::vector allowed_casts = {"i8", "i16", "i32", "i64", "i128", "u8", "u16", "u32", "u64", "u128"}; if (std::find(allowed_casts.begin(), allowed_casts.end(), cast_type->getName()) != allowed_casts.end()) { literal->setType(cast_type); @@ -99,14 +107,14 @@ auto ValidationVisitor::visitIntegerLiteral(IntegerLiteral *literal) -> void { literal->setType(_environment->getType("int")); } - if (!_context->has("return") || !_context->get("return")) { + if (! _context->has("return") || ! _context->get("return")) { displayWarning("Integer value not used", literal->getPosition()); } } auto ValidationVisitor::visitFloatLiteral(FloatLiteral *literal) -> void { if (_context->has("cast_type")) { - const auto cast_type = _context->get>("cast_type"); + const auto cast_type = _context->get>("cast_type"); std::vector allowed_casts = {"f32", "f64"}; if (std::find(allowed_casts.begin(), allowed_casts.end(), cast_type->getName()) != allowed_casts.end()) { literal->setType(cast_type); @@ -117,7 +125,7 @@ auto ValidationVisitor::visitFloatLiteral(FloatLiteral *literal) -> void { literal->setType(_environment->getType("f64")); } - if (!_context->has("return") || !_context->get("return")) { + if (! _context->has("return") || ! _context->get("return")) { displayWarning("Float value not used", literal->getPosition()); } } @@ -125,7 +133,7 @@ auto ValidationVisitor::visitFloatLiteral(FloatLiteral *literal) -> void { auto ValidationVisitor::visitCharacterLiteral(CharacterLiteral *literal) -> void { literal->setType(_environment->getType("char")); - if (!_context->has("return") || !_context->get("return")) { + if (! _context->has("return") || ! _context->get("return")) { displayWarning("Character value not used", literal->getPosition()); } } @@ -133,7 +141,7 @@ auto ValidationVisitor::visitCharacterLiteral(CharacterLiteral *literal) -> void auto ValidationVisitor::visitStringLiteral(StringLiteral *literal) -> void { literal->setType(_environment->getType("char*")); - if (!_context->has("return") || !_context->get("return")) { + if (! _context->has("return") || ! _context->get("return")) { displayWarning("String value not used", literal->getPosition()); } } @@ -150,8 +158,8 @@ auto ValidationVisitor::visitVariableDeclaration(VariableDeclaration *variable) } std::shared_ptr variable_type = nullptr; - if (!variable->getTypeName().empty()) { - if (!_environment->hasType(variable->getTypeName())) { + if (! variable->getTypeName().empty()) { + if (! _environment->hasType(variable->getTypeName())) { displayError("Unknown type: " + variable->getTypeName(), variable->getPosition()); return; } @@ -171,9 +179,11 @@ auto ValidationVisitor::visitVariableDeclaration(VariableDeclaration *variable) return; } if (variable_type != nullptr && variable_type->getName() != value_type->getName()) { - displayError("Cannot assign value of type " + value_type->toDisplay() + " to a variable of type " + - variable_type->toDisplay(), - variable->getPosition()); + displayError( + "Cannot assign value of type " + value_type->toDisplay() + " to a variable of type " + + variable_type->toDisplay(), + variable->getPosition() + ); return; } else if (variable_type == nullptr) { variable_type = value_type; @@ -190,7 +200,7 @@ auto ValidationVisitor::visitVariableDeclaration(VariableDeclaration *variable) } auto ValidationVisitor::visitIdentifier(Identifier *identifier) -> void { - if (!_environment->hasName(identifier->getName())) { + if (! _environment->hasName(identifier->getName())) { displayError("Unknown name, don't know what it refers to: " + identifier->getName(), identifier->getPosition()); return; } @@ -198,7 +208,7 @@ auto ValidationVisitor::visitIdentifier(Identifier *identifier) -> void { const auto name = _environment->getName(identifier->getName()); identifier->setType(name.getType()); - if (!_context->has("return") || !_context->get("return")) { + if (! _context->has("return") || ! _context->get("return")) { displayWarning("Value not used", identifier->getPosition()); } } @@ -220,26 +230,29 @@ auto ValidationVisitor::visitBinaryCalcul(BinaryCalcul *calcul) -> void { return; } - CalculValidator validator(_environment.get()); + const CalculValidator validator(_environment.get()); const auto found_type = validator.isCalculValid(left_type, calcul->getOperator(), right_type); if (found_type == nullptr) { - displayError("You cannot use operator " + calcul->getOperator() + " with " + left_type->toDisplay() + " and " + - right_type->toDisplay(), - calcul->getPosition()); + displayError( + "You cannot use operator " + calcul->getOperator() + " with " + left_type->toDisplay() + " and " + + right_type->toDisplay(), + calcul->getPosition() + ); return; } calcul->setType(found_type); - if (!_context->has("return") || !_context->get("return")) { + if (! _context->has("return") || ! _context->get("return")) { displayWarning("Value not used", calcul->getPosition()); } } auto ValidationVisitor::visitAssignation(Assignation *assignation) -> void { - if (!_environment->hasName(assignation->getIdentifier())) { - displayError("Unknown name, don't know what it refers to: " + assignation->getIdentifier(), - assignation->getPosition()); + if (! _environment->hasName(assignation->getIdentifier())) { + displayError( + "Unknown name, don't know what it refers to: " + assignation->getIdentifier(), assignation->getPosition() + ); return; } const auto name = _environment->getName(assignation->getIdentifier()); @@ -258,9 +271,11 @@ auto ValidationVisitor::visitAssignation(Assignation *assignation) -> void { return; } if (value_type->getName() != name.getType()->getName()) { - displayError("Cannot assign value of type " + value_type->toDisplay() + " to a variable of type " + - name.getType()->toDisplay(), - assignation->getPosition()); + displayError( + "Cannot assign value of type " + value_type->toDisplay() + " to a variable of type " + + name.getType()->toDisplay(), + assignation->getPosition() + ); return; } diff --git a/tests/e2e/filc.cpp b/tests/e2e/filc.cpp index 7bee2e1..b0cefe3 100644 --- a/tests/e2e/filc.cpp +++ b/tests/e2e/filc.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include TEST(filc, run_help) { diff --git a/tests/e2e/llvm_ir.cpp b/tests/e2e/llvm_ir.cpp index 55d47ad..5081b3b 100644 --- a/tests/e2e/llvm_ir.cpp +++ b/tests/e2e/llvm_ir.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include @@ -52,4 +53,6 @@ TEST(ir_dump, calcul_program) { ASSERT_EQ(5, getProgramResult("(3 * 2 + 4) / 2")); } -TEST(ir_dump, variable_program) { ASSERT_EQ(2, getProgramResult("val foo = 2\nfoo")); } +TEST(ir_dump, variable_program) { + ASSERT_EQ(2, getProgramResult("val foo = 2\nfoo")); +} diff --git a/tests/e2e/memory.cpp b/tests/e2e/memory.cpp index 348322c..291eb90 100644 --- a/tests/e2e/memory.cpp +++ b/tests/e2e/memory.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include diff --git a/tests/e2e/test_tools.cpp b/tests/e2e/test_tools.cpp index a48e3fe..cccb3bc 100644 --- a/tests/e2e/test_tools.cpp +++ b/tests/e2e/test_tools.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include @@ -30,7 +31,7 @@ auto exec_output(const char *cmd) -> std::string { std::string result; std::unique_ptr pipe(popen(cmd, "r"), pclose); - if (!pipe) { + if (! pipe) { throw std::runtime_error("popen() failed!"); } diff --git a/tests/unit/FilCompilerTest.cpp b/tests/unit/FilCompilerTest.cpp index e4632f3..fdeb8db 100644 --- a/tests/unit/FilCompilerTest.cpp +++ b/tests/unit/FilCompilerTest.cpp @@ -22,13 +22,14 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include TEST(FilCompiler, run) { - auto compiler = - filc::FilCompiler(filc::OptionsParser(), filc::DumpVisitor(std::cout), filc::ValidationVisitor(std::cout)); + auto compiler + = filc::FilCompiler(filc::OptionsParser(), filc::DumpVisitor(std::cout), filc::ValidationVisitor(std::cout)); SCOPED_TRACE("No argument"); ASSERT_EQ(0, compiler.run(1, toStringArray({"filc"}).data())); @@ -45,32 +46,34 @@ TEST(FilCompiler, dumpAST) { auto compiler = filc::FilCompiler(filc::OptionsParser(), filc::DumpVisitor(ss), filc::ValidationVisitor(std::cout)); ASSERT_EQ(0, compiler.run(3, toStringArray({"filc", "--dump=ast", FIXTURES_PATH "/sample.fil"}).data())); std::string result(std::istreambuf_iterator(ss), {}); - ASSERT_STREQ("=== Begin AST dump ===\n" - "[Boolean:true]\n" - "[Float:6.82]\n" - "[String:\"hEllO\"]\n" - "[Variable:val:some_constant_73]\n" - "[Variable:var:myAweSOMeVariable:i32]\n" - "[Variable:val:anotherConst]\n" - "\t[Integer:73]\n" - "[Variable:var:my_var:char]\n" - "\t[Character:'c']\n" - "[Identifier:_some_varWhichUses_Some_CHARACTERS]\n" - "[BinaryCalcul:<=]\n" - "\t[BinaryCalcul:+]\n" - "\t\t[Integer:2]\n" - "\t\t[Integer:4]\n" - "\t[BinaryCalcul:*]\n" - "\t\t[Integer:3]\n" - "\t\t[Integer:2]\n" - "[Assignation:my_var]\n" - "\t[Integer:2]\n" - "[Assignation:my_var]\n" - "\t[BinaryCalcul:+]\n" - "\t\t[Identifier:my_var]\n" - "\t\t[Integer:2]\n" - "=== End AST dump ===\n", - result.c_str()); + ASSERT_STREQ( + "=== Begin AST dump ===\n" + "[Boolean:true]\n" + "[Float:6.82]\n" + "[String:\"hEllO\"]\n" + "[Variable:val:some_constant_73]\n" + "[Variable:var:myAweSOMeVariable:i32]\n" + "[Variable:val:anotherConst]\n" + "\t[Integer:73]\n" + "[Variable:var:my_var:char]\n" + "\t[Character:'c']\n" + "[Identifier:_some_varWhichUses_Some_CHARACTERS]\n" + "[BinaryCalcul:<=]\n" + "\t[BinaryCalcul:+]\n" + "\t\t[Integer:2]\n" + "\t\t[Integer:4]\n" + "\t[BinaryCalcul:*]\n" + "\t\t[Integer:3]\n" + "\t\t[Integer:2]\n" + "[Assignation:my_var]\n" + "\t[Integer:2]\n" + "[Assignation:my_var]\n" + "\t[BinaryCalcul:+]\n" + "\t\t[Identifier:my_var]\n" + "\t\t[Integer:2]\n" + "=== End AST dump ===\n", + result.c_str() + ); } TEST(FilCompiler, fullRun) { diff --git a/tests/unit/grammar/DumpVisitorTest.cpp b/tests/unit/grammar/DumpVisitorTest.cpp index 1dd6c03..09b73b8 100644 --- a/tests/unit/grammar/DumpVisitorTest.cpp +++ b/tests/unit/grammar/DumpVisitorTest.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include diff --git a/tests/unit/grammar/ParserTest.cpp b/tests/unit/grammar/ParserTest.cpp index ba8f2d3..b6145ba 100644 --- a/tests/unit/grammar/ParserTest.cpp +++ b/tests/unit/grammar/ParserTest.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include @@ -32,7 +33,9 @@ using namespace ::testing; -TEST(Parser, nonExistingFile) { ASSERT_THROW(filc::ParserProxy::parse("non-existing-file"), std::logic_error); } +TEST(Parser, nonExistingFile) { + ASSERT_THROW(filc::ParserProxy::parse("non-existing-file"), std::logic_error); +} TEST(Parser, parseSample) { const auto program = filc::ParserProxy::parse(FIXTURES_PATH "/sample.fil"); diff --git a/tests/unit/grammar/PositionTest.cpp b/tests/unit/grammar/PositionTest.cpp index 7bc5d90..d519d03 100644 --- a/tests/unit/grammar/PositionTest.cpp +++ b/tests/unit/grammar/PositionTest.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include @@ -47,8 +48,9 @@ TEST(Position, tokenConstructor) { } TEST(Position, tokenConstructor_throw) { - ASSERT_THROW(filc::Position position(new TokenStub("source1", {2, 3}), new TokenStub("source2", {5, 6})), - std::logic_error); + ASSERT_THROW( + filc::Position position(new TokenStub("source1", {2, 3}), new TokenStub("source2", {5, 6})), std::logic_error + ); } TEST(Position, getContent) { @@ -56,38 +58,49 @@ TEST(Position, getContent) { const auto content = position.getContent(); ASSERT_THAT( content, - ElementsAre("Phasellus fringilla sem vel lorem scelerisque, non commodo augue consectetur. Donec", // 2 - "malesuada erat vitae egestas euismod. Fusce in cursus justo, non egestas ipsum. Pellentesque", // 3 - "at dui nunc. Nam ligula augue, tempus id vestibulum nec, rutrum porttitor nibh. Nullam non" // 4 - )); + ElementsAre( + "Phasellus fringilla sem vel lorem scelerisque, non commodo augue consectetur. Donec", // 2 + "malesuada erat vitae egestas euismod. Fusce in cursus justo, non egestas ipsum. Pellentesque", // 3 + "at dui nunc. Nam ligula augue, tempus id vestibulum nec, rutrum porttitor nibh. Nullam non" // 4 + ) + ); } TEST(Position, dump_multiline) { filc::Position position(new TokenStub(FILENAME, {2, 3}), new TokenStub(FILENAME, {4, 6})); const auto result = position.dump(""); - ASSERT_STREQ(" --> " FILENAME ":2:3\n" - " | v\x1B[0m\n" - " 2 | Phasellus fringilla sem vel lorem scelerisque, non commodo augue consectetur. Donec\n" - " 3 | malesuada erat vitae egestas euismod. Fusce in cursus justo, non egestas ipsum. Pellentesque\n" - " 4 | at dui nunc. Nam ligula augue, tempus id vestibulum nec, rutrum porttitor nibh. Nullam non\n" - " | ^\x1B[0m\n", - result.c_str()); + ASSERT_STREQ( + " --> " FILENAME + ":2:3\n" + " | v\x1B[0m\n" + " 2 | Phasellus fringilla sem vel lorem scelerisque, non commodo augue consectetur. Donec\n" + " 3 | malesuada erat vitae egestas euismod. Fusce in cursus justo, non egestas ipsum. Pellentesque\n" + " 4 | at dui nunc. Nam ligula augue, tempus id vestibulum nec, rutrum porttitor nibh. Nullam non\n" + " | ^\x1B[0m\n", + result.c_str() + ); } TEST(Position, dump_oneline) { filc::Position position(new TokenStub(FILENAME, {1, 7}), new TokenStub(FILENAME, {1, 10})); const auto result = position.dump(""); - ASSERT_STREQ(" --> " FILENAME ":1:7\n" - " 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut pharetra volutpat fermentum.\n" - " | ^\x1B[0m\n", - result.c_str()); + ASSERT_STREQ( + " --> " FILENAME + ":1:7\n" + " 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut pharetra volutpat fermentum.\n" + " | ^\x1B[0m\n", + result.c_str() + ); } TEST(Position, dump_color) { filc::Position position(new TokenStub(FILENAME, {1, 7}), new TokenStub(FILENAME, {1, 10})); const auto result = position.dump("\033[31m"); - ASSERT_STREQ(" --> " FILENAME ":1:7\n" - " 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut pharetra volutpat fermentum.\n" - " | \x1B[31m^\x1B[0m\n", - result.c_str()); + ASSERT_STREQ( + " --> " FILENAME + ":1:7\n" + " 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut pharetra volutpat fermentum.\n" + " | \x1B[31m^\x1B[0m\n", + result.c_str() + ); } diff --git a/tests/unit/grammar/assignation/AssignationTest.cpp b/tests/unit/grammar/assignation/AssignationTest.cpp index 1228742..6adfb79 100644 --- a/tests/unit/grammar/assignation/AssignationTest.cpp +++ b/tests/unit/grammar/assignation/AssignationTest.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include @@ -31,7 +32,7 @@ using namespace ::testing; TEST(Assignation, parsingSimple) { - const auto program = parseString("foo = \"bar\""); + const auto program = parseString("foo = \"bar\""); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); const auto assignation = std::dynamic_pointer_cast(expressions[0]); @@ -43,7 +44,7 @@ TEST(Assignation, parsingSimple) { } TEST(Assignation, parsingCalcul) { - const auto program = parseString("bar ||= true"); + const auto program = parseString("bar ||= true"); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); const auto assignation = std::dynamic_pointer_cast(expressions[0]); diff --git a/tests/unit/grammar/calcul/BinaryCalculTest.cpp b/tests/unit/grammar/calcul/BinaryCalculTest.cpp index 1bafc45..ead7d3c 100644 --- a/tests/unit/grammar/calcul/BinaryCalculTest.cpp +++ b/tests/unit/grammar/calcul/BinaryCalculTest.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include @@ -30,7 +31,7 @@ using namespace ::testing; TEST(BinaryCalcul, parsingAddition) { - const auto program = parseString("1 + 2"); + const auto program = parseString("1 + 2"); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto calcul = std::dynamic_pointer_cast(expressions[0]); diff --git a/tests/unit/grammar/identifier/IdentiferTest.cpp b/tests/unit/grammar/identifier/IdentiferTest.cpp index 62b3533..482050e 100644 --- a/tests/unit/grammar/identifier/IdentiferTest.cpp +++ b/tests/unit/grammar/identifier/IdentiferTest.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include @@ -29,7 +30,7 @@ using namespace ::testing; TEST(Identifier, parsing) { - const auto program = parseString("myAwesome_var3"); + const auto program = parseString("myAwesome_var3"); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); const auto identifier = std::dynamic_pointer_cast(expressions[0]); diff --git a/tests/unit/grammar/literal/BooleanLiteralTest.cpp b/tests/unit/grammar/literal/BooleanLiteralTest.cpp index 4a32b06..adc026b 100644 --- a/tests/unit/grammar/literal/BooleanLiteralTest.cpp +++ b/tests/unit/grammar/literal/BooleanLiteralTest.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include @@ -29,7 +30,7 @@ using namespace ::testing; TEST(BooleanLiteral, parsingTrue) { - const auto program = parseString("true"); + const auto program = parseString("true"); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto literal = std::dynamic_pointer_cast(expressions[0]); @@ -38,7 +39,7 @@ TEST(BooleanLiteral, parsingTrue) { } TEST(BooleanLiteral, parsingFalse) { - const auto program = parseString("false"); + const auto program = parseString("false"); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto literal = std::dynamic_pointer_cast(expressions[0]); diff --git a/tests/unit/grammar/literal/CharacterLiteralTest.cpp b/tests/unit/grammar/literal/CharacterLiteralTest.cpp index 149ab9b..0db933c 100644 --- a/tests/unit/grammar/literal/CharacterLiteralTest.cpp +++ b/tests/unit/grammar/literal/CharacterLiteralTest.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include @@ -30,7 +31,7 @@ using namespace ::testing; TEST(CharacterLiteral, parsingSimpleChar) { - const auto program = parseString("'a'"); + const auto program = parseString("'a'"); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto literal = std::dynamic_pointer_cast(expressions[0]); @@ -42,7 +43,7 @@ TEST(CharacterLiteral, parsingEscapedChar) { for (const std::string content : {"'\\''", "'\\\"'", "'\\?'", "'\\a'", "'\\b'", "'\\f'", "'\\n'", "'\\r'", "'\\t'", "'\\v'", "'\\\\'"}) { SCOPED_TRACE(content); - const auto program = parseString(content); + const auto program = parseString(content); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto literal = std::dynamic_pointer_cast(expressions[0]); diff --git a/tests/unit/grammar/literal/FloatLiteralTest.cpp b/tests/unit/grammar/literal/FloatLiteralTest.cpp index 26b102d..8247681 100644 --- a/tests/unit/grammar/literal/FloatLiteralTest.cpp +++ b/tests/unit/grammar/literal/FloatLiteralTest.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include @@ -29,7 +30,7 @@ using namespace ::testing; TEST(FloatLiteral, parsingFullFloat) { - const auto program = parseString("3.14"); + const auto program = parseString("3.14"); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto literal = std::dynamic_pointer_cast(expressions[0]); @@ -38,7 +39,7 @@ TEST(FloatLiteral, parsingFullFloat) { } TEST(FloatLiteral, parsingSemiFloat) { - const auto program = parseString(".2"); + const auto program = parseString(".2"); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto literal = std::dynamic_pointer_cast(expressions[0]); diff --git a/tests/unit/grammar/literal/IntegerLiteralTest.cpp b/tests/unit/grammar/literal/IntegerLiteralTest.cpp index 034d0ed..4793ac5 100644 --- a/tests/unit/grammar/literal/IntegerLiteralTest.cpp +++ b/tests/unit/grammar/literal/IntegerLiteralTest.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include @@ -29,7 +30,7 @@ using namespace ::testing; TEST(IntegerLiteral, parsing) { - const auto program = parseString("73"); + const auto program = parseString("73"); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto literal = std::dynamic_pointer_cast(expressions[0]); diff --git a/tests/unit/grammar/literal/StringLiteralTest.cpp b/tests/unit/grammar/literal/StringLiteralTest.cpp index 6ef57a9..daa8fa4 100644 --- a/tests/unit/grammar/literal/StringLiteralTest.cpp +++ b/tests/unit/grammar/literal/StringLiteralTest.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include @@ -29,7 +30,7 @@ using namespace ::testing; TEST(StringLiteral, parsingEmptyString) { - const auto program = parseString("\"\""); + const auto program = parseString("\"\""); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto literal = std::dynamic_pointer_cast(expressions[0]); @@ -38,7 +39,7 @@ TEST(StringLiteral, parsingEmptyString) { } TEST(StringLiteral, parsingSomeString) { - const auto program = parseString("\"Hello World!\""); + const auto program = parseString("\"Hello World!\""); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto literal = std::dynamic_pointer_cast(expressions[0]); diff --git a/tests/unit/grammar/variable/VariableDeclarationTest.cpp b/tests/unit/grammar/variable/VariableDeclarationTest.cpp index e1c67f1..a219e65 100644 --- a/tests/unit/grammar/variable/VariableDeclarationTest.cpp +++ b/tests/unit/grammar/variable/VariableDeclarationTest.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include @@ -30,7 +31,7 @@ using namespace ::testing; TEST(VariableDeclaration, parsingSimpleConstant) { - const auto program = parseString("val foo"); + const auto program = parseString("val foo"); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto variable = std::dynamic_pointer_cast(expressions[0]); @@ -42,7 +43,7 @@ TEST(VariableDeclaration, parsingSimpleConstant) { } TEST(VariableDeclaration, parsingSimpleVariable) { - const auto program = parseString("var bar"); + const auto program = parseString("var bar"); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto variable = std::dynamic_pointer_cast(expressions[0]); @@ -54,7 +55,7 @@ TEST(VariableDeclaration, parsingSimpleVariable) { } TEST(VariableDeclaration, parsingWithType) { - const auto program = parseString("var bar: i32"); + const auto program = parseString("var bar: i32"); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto variable = std::dynamic_pointer_cast(expressions[0]); @@ -66,7 +67,7 @@ TEST(VariableDeclaration, parsingWithType) { } TEST(VariableDeclaration, parsingWithValue) { - const auto program = parseString("val foo = 'a'"); + const auto program = parseString("val foo = 'a'"); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto variable = std::dynamic_pointer_cast(expressions[0]); @@ -78,7 +79,7 @@ TEST(VariableDeclaration, parsingWithValue) { } TEST(VariableDeclaration, parsingWithTypeAndValue) { - const auto program = parseString("val foo: f64 = 3.1415"); + const auto program = parseString("val foo: f64 = 3.1415"); const auto expressions = program->getExpressions(); ASSERT_THAT(expressions, SizeIs(1)); auto variable = std::dynamic_pointer_cast(expressions[0]); diff --git a/tests/unit/llvm/IRGeneratorTest.cpp b/tests/unit/llvm/IRGeneratorTest.cpp index 5dff967..01805e7 100644 --- a/tests/unit/llvm/IRGeneratorTest.cpp +++ b/tests/unit/llvm/IRGeneratorTest.cpp @@ -27,6 +27,7 @@ #include "filc/grammar/variable/Variable.h" #include "filc/validation/ValidationVisitor.h" #include "test_tools.h" + #include #include #include @@ -47,18 +48,24 @@ auto getIR(const std::string &content) -> std::string { TEST(IRGenerator, program_empty) { const auto ir = getIR(""); - ASSERT_THAT(ir, HasSubstr("define i32 @main() {\n" - "entry:\n" - " ret i32 0\n" - "}")); + ASSERT_THAT( + ir, + HasSubstr("define i32 @main() {\n" + "entry:\n" + " ret i32 0\n" + "}") + ); } TEST(IRGenerator, program_nonEmpty) { const auto ir = getIR("1"); - ASSERT_THAT(ir, HasSubstr("define i32 @main() {\n" - "entry:\n" - " ret i32 1\n" - "}")); + ASSERT_THAT( + ir, + HasSubstr("define i32 @main() {\n" + "entry:\n" + " ret i32 1\n" + "}") + ); } TEST(IRGenerator, booleanLiteral_true) { diff --git a/tests/unit/options/OptionsParserTest.cpp b/tests/unit/options/OptionsParserTest.cpp index d58248e..eed1f66 100644 --- a/tests/unit/options/OptionsParserTest.cpp +++ b/tests/unit/options/OptionsParserTest.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include @@ -53,7 +54,7 @@ TEST(OptionsParser, showHelp) { options_parser.showHelp(stream); std::string result; stream >> result; - ASSERT_TRUE(!result.empty()); + ASSERT_TRUE(! result.empty()); } TEST(OptionsParser, showVersion) { diff --git a/tests/unit/test_tools.cpp b/tests/unit/test_tools.cpp index dfaa743..b9e5032 100644 --- a/tests/unit/test_tools.cpp +++ b/tests/unit/test_tools.cpp @@ -22,9 +22,11 @@ * SOFTWARE. */ #include "test_tools.h" + #include "FilLexer.h" #include "FilParser.h" #include "antlr4-runtime.h" + #include auto toStringArray(const std::vector &data) -> std::vector { @@ -56,7 +58,7 @@ auto parseAndValidateString(const std::string &content) -> std::shared_ptr std::string { std::string result(std::istreambuf_iterator(_out), {}); @@ -74,9 +76,13 @@ auto PrinterVisitor::visitBooleanLiteral(filc::BooleanLiteral *literal) -> void _out << (literal->getValue() ? "true" : "false"); } -auto PrinterVisitor::visitIntegerLiteral(filc::IntegerLiteral *literal) -> void { _out << literal->getValue(); } +auto PrinterVisitor::visitIntegerLiteral(filc::IntegerLiteral *literal) -> void { + _out << literal->getValue(); +} -auto PrinterVisitor::visitFloatLiteral(filc::FloatLiteral *literal) -> void { _out << literal->getValue(); } +auto PrinterVisitor::visitFloatLiteral(filc::FloatLiteral *literal) -> void { + _out << literal->getValue(); +} auto PrinterVisitor::visitCharacterLiteral(filc::CharacterLiteral *literal) -> void { _out << "'" << literal->getValue() << "'"; @@ -88,7 +94,7 @@ auto PrinterVisitor::visitStringLiteral(filc::StringLiteral *literal) -> void { auto PrinterVisitor::visitVariableDeclaration(filc::VariableDeclaration *variable) -> void { _out << (variable->isConstant() ? "val " : "var ") << variable->getName(); - if (!variable->getTypeName().empty()) { + if (! variable->getTypeName().empty()) { _out << ": " << variable->getTypeName(); } if (variable->getValue() != nullptr) { @@ -97,7 +103,9 @@ auto PrinterVisitor::visitVariableDeclaration(filc::VariableDeclaration *variabl } } -auto PrinterVisitor::visitIdentifier(filc::Identifier *identifier) -> void { _out << identifier->getName(); } +auto PrinterVisitor::visitIdentifier(filc::Identifier *identifier) -> void { + _out << identifier->getName(); +} auto PrinterVisitor::visitBinaryCalcul(filc::BinaryCalcul *calcul) -> void { _out << "("; @@ -112,41 +120,75 @@ auto PrinterVisitor::visitAssignation(filc::Assignation *assignation) -> void { assignation->getValue()->acceptVoidVisitor(this); } -TokenSourceStub::TokenSourceStub(std::string filename) : _filename(std::move(filename)) {} +TokenSourceStub::TokenSourceStub(std::string filename): _filename(std::move(filename)) {} -auto TokenSourceStub::nextToken() -> std::unique_ptr { return nullptr; } +auto TokenSourceStub::nextToken() -> std::unique_ptr { + return nullptr; +} -auto TokenSourceStub::getLine() const -> size_t { return 0; } +auto TokenSourceStub::getLine() const -> size_t { + return 0; +} -auto TokenSourceStub::getCharPositionInLine() -> size_t { return 0; } +auto TokenSourceStub::getCharPositionInLine() -> size_t { + return 0; +} -auto TokenSourceStub::getInputStream() -> antlr4::CharStream * { return nullptr; } +auto TokenSourceStub::getInputStream() -> antlr4::CharStream * { + return nullptr; +} -auto TokenSourceStub::getSourceName() -> std::string { return _filename; } +auto TokenSourceStub::getSourceName() -> std::string { + return _filename; +} -auto TokenSourceStub::getTokenFactory() -> antlr4::TokenFactory * { return nullptr; } +auto TokenSourceStub::getTokenFactory() -> antlr4::TokenFactory * { + return nullptr; +} TokenStub::TokenStub(const std::string &filename, const std::pair &position) : _source(new TokenSourceStub(filename)), _position(position) {} -auto TokenStub::getText() const -> std::string { return ""; } +auto TokenStub::getText() const -> std::string { + return ""; +} -auto TokenStub::getType() const -> size_t { return 0; } +auto TokenStub::getType() const -> size_t { + return 0; +} -auto TokenStub::getLine() const -> size_t { return _position.first; } +auto TokenStub::getLine() const -> size_t { + return _position.first; +} -auto TokenStub::getCharPositionInLine() const -> size_t { return _position.second; } +auto TokenStub::getCharPositionInLine() const -> size_t { + return _position.second; +} -auto TokenStub::getChannel() const -> size_t { return 0; } +auto TokenStub::getChannel() const -> size_t { + return 0; +} -auto TokenStub::getTokenIndex() const -> size_t { return 0; } +auto TokenStub::getTokenIndex() const -> size_t { + return 0; +} -auto TokenStub::getStartIndex() const -> size_t { return 0; } +auto TokenStub::getStartIndex() const -> size_t { + return 0; +} -auto TokenStub::getStopIndex() const -> size_t { return 0; } +auto TokenStub::getStopIndex() const -> size_t { + return 0; +} -auto TokenStub::getTokenSource() const -> antlr4::TokenSource * { return _source; } +auto TokenStub::getTokenSource() const -> antlr4::TokenSource * { + return _source; +} -auto TokenStub::getInputStream() const -> antlr4::CharStream * { return nullptr; } +auto TokenStub::getInputStream() const -> antlr4::CharStream * { + return nullptr; +} -auto TokenStub::toString() const -> std::string { return ""; } +auto TokenStub::toString() const -> std::string { + return ""; +} diff --git a/tests/unit/utils/MessageTest.cpp b/tests/unit/utils/MessageTest.cpp index 6f2263d..5d343c5 100644 --- a/tests/unit/utils/MessageTest.cpp +++ b/tests/unit/utils/MessageTest.cpp @@ -22,21 +22,28 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #define FILENAME FIXTURES_PATH "/ipsum.txt" TEST(Message, write) { - filc::Message message(WARNING, "This is a warning message", - filc::Position(new TokenStub(FILENAME, {1, 7}), new TokenStub(FILENAME, {1, 7})), - WARNING_COLOR); + filc::Message message( + WARNING, + "This is a warning message", + filc::Position(new TokenStub(FILENAME, {1, 7}), new TokenStub(FILENAME, {1, 7})), + WARNING_COLOR + ); std::stringstream ss; ss << message; std::string dump(std::istreambuf_iterator(ss), {}); - ASSERT_STREQ("\x1B[1m\x1B[33m[WARNING] \x1B[0mThis is a warning message\n" - " --> " FILENAME ":1:7\n" - " 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut pharetra volutpat fermentum.\n" - " | \x1B[33m^\x1B[0m\n", - dump.c_str()); + ASSERT_STREQ( + "\x1B[1m\x1B[33m[WARNING] \x1B[0mThis is a warning message\n" + " --> " FILENAME + ":1:7\n" + " 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut pharetra volutpat fermentum.\n" + " | \x1B[33m^\x1B[0m\n", + dump.c_str() + ); } diff --git a/tests/unit/validation/CalculValidatorTest.cpp b/tests/unit/validation/CalculValidatorTest.cpp index bd45ebb..345177f 100644 --- a/tests/unit/validation/CalculValidatorTest.cpp +++ b/tests/unit/validation/CalculValidatorTest.cpp @@ -26,8 +26,8 @@ #include #include -#define VALIDATOR \ - auto env = new filc::Environment(); \ +#define VALIDATOR \ + auto env = new filc::Environment(); \ filc::CalculValidator validator(env) TEST(CalculValidator, invalidDifferentType) { @@ -52,15 +52,22 @@ TEST(CalculValidator, validBool) { TEST(CalculValidator, validPointer) { VALIDATOR; - ASSERT_STREQ("bool", validator - .isCalculValid(std::make_shared(env->getType("i32")), - "==", std::make_shared(env->getType("i32"))) - ->getName() - .c_str()); + ASSERT_STREQ( + "bool", + validator + .isCalculValid( + std::make_shared(env->getType("i32")), + "==", + std::make_shared(env->getType("i32")) + ) + ->getName() + .c_str() + ); } TEST(CalculValidator, invalidUnknown) { VALIDATOR; - ASSERT_EQ(nullptr, - validator.isCalculValid(std::make_shared("foo"), "+", std::make_shared("foo"))); + ASSERT_EQ( + nullptr, validator.isCalculValid(std::make_shared("foo"), "+", std::make_shared("foo")) + ); } diff --git a/tests/unit/validation/ValidationContextTest.cpp b/tests/unit/validation/ValidationContextTest.cpp index 40b67df..828666b 100644 --- a/tests/unit/validation/ValidationContextTest.cpp +++ b/tests/unit/validation/ValidationContextTest.cpp @@ -66,7 +66,9 @@ typedef struct { TEST(ValidationContext, get_set_structure) { filc::ValidationContext context; - SomeStructure value = {2, "Hello World", {'a', 'b', 'c', 'd', 'e'}}; + SomeStructure value = { + 2, "Hello World", {'a', 'b', 'c', 'd', 'e'} + }; context.set("struct_value", value); ASSERT_TRUE(context.has("struct_value")); auto found = context.get("struct_value"); @@ -77,9 +79,11 @@ TEST(ValidationContext, get_set_structure) { class SomeClass { public: - SomeClass() : _a(12), _b("foo") {} + SomeClass(): _a(12), _b("foo") {} - [[nodiscard]] auto equals(const SomeClass &other) const -> bool { return _a == other._a && _b == other._b; } + [[nodiscard]] auto equals(const SomeClass &other) const -> bool { + return _a == other._a && _b == other._b; + } private: int _a; diff --git a/tests/unit/validation/ValidationVisitorTest.cpp b/tests/unit/validation/ValidationVisitorTest.cpp index f604d25..184eaf4 100644 --- a/tests/unit/validation/ValidationVisitorTest.cpp +++ b/tests/unit/validation/ValidationVisitorTest.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ #include "test_tools.h" + #include #include #include @@ -30,8 +31,8 @@ using namespace ::testing; -#define VISITOR \ - std::stringstream ss; \ +#define VISITOR \ + std::stringstream ss; \ filc::ValidationVisitor visitor(ss) TEST(ValidationVisitor, program_valid) { @@ -46,8 +47,9 @@ TEST(ValidationVisitor, program_invalid) { VISITOR; const auto program = parseString("3.4"); program->acceptVoidVisitor(&visitor); - ASSERT_THAT(std::string(std::istreambuf_iterator(ss), {}), - HasSubstr("Expected type int aka i32 but got f64")); + ASSERT_THAT( + std::string(std::istreambuf_iterator(ss), {}), HasSubstr("Expected type int aka i32 but got f64") + ); ASSERT_TRUE(visitor.hasError()); } @@ -125,8 +127,10 @@ TEST(ValidationVisitor, variable_constantWithoutValue) { VISITOR; const auto program = parseString("val my_constant"); program->acceptVoidVisitor(&visitor); - ASSERT_THAT(std::string(std::istreambuf_iterator(ss), {}), - HasSubstr("When declaring a constant, you must provide it a value")); + ASSERT_THAT( + std::string(std::istreambuf_iterator(ss), {}), + HasSubstr("When declaring a constant, you must provide it a value") + ); ASSERT_TRUE(visitor.hasError()); ASSERT_EQ(nullptr, program->getExpressions()[0]->getType()); } @@ -144,8 +148,10 @@ TEST(ValidationVisitor, variable_differentValueType) { VISITOR; const auto program = parseString("var my_var: i32 = 'a'"); program->acceptVoidVisitor(&visitor); - ASSERT_THAT(std::string(std::istreambuf_iterator(ss), {}), - HasSubstr("Cannot assign value of type char aka u8 to a variable of type i32")); + ASSERT_THAT( + std::string(std::istreambuf_iterator(ss), {}), + HasSubstr("Cannot assign value of type char aka u8 to a variable of type i32") + ); ASSERT_TRUE(visitor.hasError()); ASSERT_EQ(nullptr, program->getExpressions()[0]->getType()); } @@ -163,8 +169,10 @@ TEST(ValidationVisitor, variable_withoutTypeAndValue) { VISITOR; const auto program = parseString("var my_var"); program->acceptVoidVisitor(&visitor); - ASSERT_THAT(std::string(std::istreambuf_iterator(ss), {}), - HasSubstr("When declaring a variable, you must provide at least a type or a value")); + ASSERT_THAT( + std::string(std::istreambuf_iterator(ss), {}), + HasSubstr("When declaring a variable, you must provide at least a type or a value") + ); ASSERT_TRUE(visitor.hasError()); ASSERT_EQ(nullptr, program->getExpressions()[0]->getType()); } @@ -191,8 +199,10 @@ TEST(ValidationVisitor, identifier_nonExisting) { VISITOR; const auto program = parseString("bar"); program->acceptVoidVisitor(&visitor); - ASSERT_THAT(std::string(std::istreambuf_iterator(ss), {}), - HasSubstr("Unknown name, don't know what it refers to: bar")); + ASSERT_THAT( + std::string(std::istreambuf_iterator(ss), {}), + HasSubstr("Unknown name, don't know what it refers to: bar") + ); ASSERT_TRUE(visitor.hasError()); ASSERT_EQ(nullptr, program->getExpressions()[0]->getType()); } @@ -210,8 +220,10 @@ TEST(ValidationVisitor, calcul_invalidLeft) { VISITOR; const auto program = parseString("(val foo) + 2"); program->acceptVoidVisitor(&visitor); - ASSERT_THAT(std::string(std::istreambuf_iterator(ss), {}), - HasSubstr("When declaring a constant, you must provide it a value")); + ASSERT_THAT( + std::string(std::istreambuf_iterator(ss), {}), + HasSubstr("When declaring a constant, you must provide it a value") + ); ASSERT_TRUE(visitor.hasError()); ASSERT_EQ(nullptr, program->getExpressions()[0]->getType()); } @@ -220,8 +232,10 @@ TEST(ValidationVisitor, calcul_invalidRight) { VISITOR; const auto program = parseString("2 + (val foo)"); program->acceptVoidVisitor(&visitor); - ASSERT_THAT(std::string(std::istreambuf_iterator(ss), {}), - HasSubstr("When declaring a constant, you must provide it a value")); + ASSERT_THAT( + std::string(std::istreambuf_iterator(ss), {}), + HasSubstr("When declaring a constant, you must provide it a value") + ); ASSERT_TRUE(visitor.hasError()); ASSERT_EQ(nullptr, program->getExpressions()[0]->getType()); } @@ -230,8 +244,10 @@ TEST(ValidationVisitor, calcul_invalid) { VISITOR; const auto program = parseString("'a' && 3"); program->acceptVoidVisitor(&visitor); - ASSERT_THAT(std::string(std::istreambuf_iterator(ss), {}), - HasSubstr("You cannot use operator && with char aka u8 and int aka i32")); + ASSERT_THAT( + std::string(std::istreambuf_iterator(ss), {}), + HasSubstr("You cannot use operator && with char aka u8 and int aka i32") + ); ASSERT_TRUE(visitor.hasError()); ASSERT_EQ(nullptr, program->getExpressions()[0]->getType()); } @@ -249,8 +265,10 @@ TEST(ValidationVisitor, assignation_nonExisting) { VISITOR; const auto program = parseString("foo = 3"); program->acceptVoidVisitor(&visitor); - ASSERT_THAT(std::string(std::istreambuf_iterator(ss), {}), - HasSubstr("Unknown name, don't know what it refers to: foo")); + ASSERT_THAT( + std::string(std::istreambuf_iterator(ss), {}), + HasSubstr("Unknown name, don't know what it refers to: foo") + ); ASSERT_TRUE(visitor.hasError()); ASSERT_EQ(nullptr, program->getExpressions()[0]->getType()); } @@ -268,8 +286,10 @@ TEST(ValidationVisitor, assignation_differentType) { VISITOR; const auto program = parseString("var foo = 3\nfoo = false"); program->acceptVoidVisitor(&visitor); - ASSERT_THAT(std::string(std::istreambuf_iterator(ss), {}), - HasSubstr("Cannot assign value of type bool to a variable of type int aka i32")); + ASSERT_THAT( + std::string(std::istreambuf_iterator(ss), {}), + HasSubstr("Cannot assign value of type bool to a variable of type int aka i32") + ); ASSERT_TRUE(visitor.hasError()); ASSERT_EQ(nullptr, program->getExpressions()[1]->getType()); }