Skip to content

Commit 1ba43e0

Browse files
committed
feat(logs): allow using a custom output stream for warnings
1 parent 0f94b18 commit 1ba43e0

File tree

27 files changed

+140
-43
lines changed

27 files changed

+140
-43
lines changed

include/Ark/Compiler/AST/Optimizer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ namespace Ark::internal
4040
*
4141
* @param ast
4242
*/
43-
void process(const Node& ast) override;
43+
void process(const Node& ast);
4444

4545
/**
4646
* @brief Returns the modified AST
4747
*
4848
* @return const Node&
4949
*/
50-
[[nodiscard]] const Node& ast() const noexcept override;
50+
[[nodiscard]] const Node& ast() const noexcept;
5151

5252
private:
5353
Node m_ast;

include/Ark/Compiler/IntermediateRepresentation/IRCompiler.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
#include <Ark/Utils/Platform.hpp>
1818
#include <Ark/Utils/Logger.hpp>
1919
#include <Ark/Compiler/Common.hpp>
20+
#include <Ark/Compiler/Pass.hpp>
2021
#include <Ark/Compiler/ValTableElem.hpp>
2122
#include <Ark/Compiler/IntermediateRepresentation/Entity.hpp>
2223

2324
namespace Ark::internal
2425
{
25-
class ARK_API IRCompiler final
26+
class ARK_API IRCompiler final : public Pass
2627
{
2728
public:
2829
/**
@@ -56,7 +57,6 @@ namespace Ark::internal
5657
[[nodiscard]] const bytecode_t& bytecode() const noexcept;
5758

5859
private:
59-
Logger m_logger;
6060
bytecode_t m_bytecode;
6161
std::vector<IR::Block> m_ir;
6262
std::vector<std::string> m_filenames;

include/Ark/Compiler/IntermediateRepresentation/IROptimizer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <Ark/Utils/Platform.hpp>
1414
#include <Ark/Utils/Logger.hpp>
1515
#include <Ark/Compiler/ValTableElem.hpp>
16+
#include <Ark/Compiler/Pass.hpp>
1617
#include <Ark/Compiler/IntermediateRepresentation/Entity.hpp>
1718

1819
#include <span>
@@ -27,7 +28,7 @@ namespace Ark::internal
2728
std::size_t offset;
2829
};
2930

30-
class ARK_API IROptimizer final
31+
class ARK_API IROptimizer final : public Pass
3132
{
3233
public:
3334
/**
@@ -87,7 +88,6 @@ namespace Ark::internal
8788

8889
std::vector<Rule> m_ruleset;
8990

90-
Logger m_logger;
9191
std::vector<IR::Block> m_ir;
9292
std::vector<std::string> m_symbols;
9393
std::vector<ValTableElem> m_values;

include/Ark/Compiler/Lowerer/ASTLowerer.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <Ark/Utils/Platform.hpp>
2121
#include <Ark/Utils/Logger.hpp>
22+
#include <Ark/Compiler/Pass.hpp>
2223
#include <Ark/Compiler/Instructions.hpp>
2324
#include <Ark/Compiler/IntermediateRepresentation/Entity.hpp>
2425
#include <Ark/Compiler/AST/Node.hpp>
@@ -37,7 +38,7 @@ namespace Ark::internal
3738
* @brief The ArkScript AST to IR compiler
3839
*
3940
*/
40-
class ARK_API ASTLowerer final
41+
class ARK_API ASTLowerer final : public Pass
4142
{
4243
public:
4344
/**
@@ -108,8 +109,6 @@ namespace Ark::internal
108109
IR::label_t m_current_label = 0;
109110
std::stack<std::string> m_opened_vars; ///< stack of vars we are currently declaring
110111

111-
Logger m_logger;
112-
113112
enum class ErrorKind
114113
{
115114
InvalidNodeMacro,

include/Ark/Compiler/Macros/Processor.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ namespace Ark::internal
4343
*
4444
* @param ast
4545
*/
46-
void process(const Node& ast) override;
46+
void process(const Node& ast);
4747

4848
/**
4949
* @brief Return the modified AST
5050
*
5151
* @return Node&
5252
*/
53-
[[nodiscard]] const Node& ast() const noexcept override;
53+
[[nodiscard]] const Node& ast() const noexcept;
5454

5555
friend class MacroExecutor;
5656

include/Ark/Compiler/NameResolution/NameResolutionPass.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <Ark/Utils/Platform.hpp>
1919
#include <Ark/Compiler/Pass.hpp>
20+
#include <Ark/Compiler/AST/Node.hpp>
2021
#include <Ark/Compiler/NameResolution/ScopeResolver.hpp>
2122

2223
namespace Ark::internal
@@ -34,13 +35,13 @@ namespace Ark::internal
3435
* @brief Start visiting the given AST, checking for mutability violation and unbound variables
3536
* @param ast AST to analyze
3637
*/
37-
void process(const Node& ast) override;
38+
void process(const Node& ast);
3839

3940
/**
4041
* @brief Unused overload that return the input AST (untouched as this pass only generates errors)
4142
* @return const Node& ast
4243
*/
43-
[[nodiscard]] const Node& ast() const noexcept override;
44+
[[nodiscard]] const Node& ast() const noexcept;
4445

4546
/**
4647
* @brief Register a symbol as defined, so that later we can throw errors on undefined symbols

include/Ark/Compiler/Package/ImportSolver.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ namespace Ark::internal
4343
*/
4444
ImportSolver& setup(const std::filesystem::path& root, const std::vector<Import>& origin_imports);
4545

46-
void process(const Node& origin_ast) override;
46+
void process(const Node& origin_ast);
4747

48-
[[nodiscard]] const Node& ast() const noexcept override;
48+
[[nodiscard]] const Node& ast() const noexcept;
4949

5050
private:
5151
struct ImportWithSource

include/Ark/Compiler/Pass.hpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @file Pass.hpp
33
* @author Lexy Plateau (lexplt.dev@gmail.com)
4-
* @brief Interface for a compiler pass (take in an AST, output an AST)
4+
* @brief Interface for a compiler pass
55
* @date 2024-07-21
66
*
77
* @copyright Copyright (c) 2024-2026
@@ -11,9 +11,10 @@
1111
#define ARK_COMPILER_PASS_HPP
1212

1313
#include <Ark/Utils/Platform.hpp>
14-
#include <Ark/Compiler/AST/Node.hpp>
1514
#include <Ark/Utils/Logger.hpp>
1615

16+
#include <ostream>
17+
1718
namespace Ark::internal
1819
{
1920
/**
@@ -33,17 +34,11 @@ namespace Ark::internal
3334
virtual ~Pass() = default;
3435

3536
/**
36-
* @brief Start processing the given AST
37-
* @param ast
38-
*/
39-
virtual void process(const Node& ast) = 0;
40-
41-
/**
42-
* @brief Output of the compiler pass
37+
* @brief Set a custom output stream for the logger
4338
*
44-
* @return const Node& the modified AST
39+
* @param os output stream
4540
*/
46-
[[nodiscard]] virtual const Node& ast() const noexcept = 0;
41+
void configureLogger(std::ostream& os);
4742

4843
protected:
4944
Logger m_logger;

include/Ark/Compiler/Welder.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ namespace Ark
100100
*/
101101
bool saveBytecodeToFile(const std::string& filename);
102102

103+
/**
104+
* @brief Redirect the logs to a given stream
105+
* @param os output stream
106+
*/
107+
void redirectLogsTo(std::ostream& os);
108+
103109
[[nodiscard]] const internal::Node& ast() const noexcept;
104110
[[nodiscard]] std::string textualIR() const noexcept;
105111
[[nodiscard]] const bytecode_t& bytecode() const noexcept;

include/Ark/Error/Diagnostics.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ namespace Ark::Diagnostics
6060
*
6161
* @param message error message to be included in the context
6262
* @param node AST node with the error
63+
* @param colorize toggle context colors (default: true)
6364
* @return std::string
6465
*/
65-
std::string makeContextWithNode(const std::string& message, const internal::Node& node);
66+
std::string makeContextWithNode(const std::string& message, const internal::Node& node, bool colorize = true);
6667

6768
ARK_API void generateWithCode(const CodeError& e, const std::string& code, std::ostream& os = std::cerr, bool colorize = true);
6869

0 commit comments

Comments
 (0)