Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- `:?s` to format as an escaped quoted string,
- `:s` to format as a quoted string
- `format` can use format specifiers for integers: `b`, `#b`, `B`, `#B`, `c`, `d`, `o`, `x`, `#x`, `X`, and `#X` if the argument is an integer
- display a warning to `stderr` when using a deprecated function/value (checks for `@deprecated` inside the attached comment of functions / values)

### Changed

Expand Down
4 changes: 2 additions & 2 deletions include/Ark/Compiler/AST/Optimizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ namespace Ark::internal
*
* @param ast
*/
void process(const Node& ast) override;
void process(const Node& ast);

/**
* @brief Returns the modified AST
*
* @return const Node&
*/
[[nodiscard]] const Node& ast() const noexcept override;
[[nodiscard]] const Node& ast() const noexcept;

private:
Node m_ast;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
#include <Ark/Utils/Platform.hpp>
#include <Ark/Utils/Logger.hpp>
#include <Ark/Compiler/Common.hpp>
#include <Ark/Compiler/Pass.hpp>
#include <Ark/Compiler/ValTableElem.hpp>
#include <Ark/Compiler/IntermediateRepresentation/Entity.hpp>

namespace Ark::internal
{
class ARK_API IRCompiler final
class ARK_API IRCompiler final : public Pass
{
public:
/**
Expand Down Expand Up @@ -56,7 +57,6 @@ namespace Ark::internal
[[nodiscard]] const bytecode_t& bytecode() const noexcept;

private:
Logger m_logger;
bytecode_t m_bytecode;
std::vector<IR::Block> m_ir;
std::vector<std::string> m_filenames;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <Ark/Utils/Platform.hpp>
#include <Ark/Utils/Logger.hpp>
#include <Ark/Compiler/ValTableElem.hpp>
#include <Ark/Compiler/Pass.hpp>
#include <Ark/Compiler/IntermediateRepresentation/Entity.hpp>

#include <span>
Expand All @@ -27,7 +28,7 @@ namespace Ark::internal
std::size_t offset;
};

class ARK_API IROptimizer final
class ARK_API IROptimizer final : public Pass
{
public:
/**
Expand Down Expand Up @@ -87,7 +88,6 @@ namespace Ark::internal

std::vector<Rule> m_ruleset;

Logger m_logger;
std::vector<IR::Block> m_ir;
std::vector<std::string> m_symbols;
std::vector<ValTableElem> m_values;
Expand Down
7 changes: 3 additions & 4 deletions include/Ark/Compiler/Lowerer/ASTLowerer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <Ark/Utils/Platform.hpp>
#include <Ark/Utils/Logger.hpp>
#include <Ark/Compiler/Pass.hpp>
#include <Ark/Compiler/Instructions.hpp>
#include <Ark/Compiler/IntermediateRepresentation/Entity.hpp>
#include <Ark/Compiler/AST/Node.hpp>
Expand All @@ -37,7 +38,7 @@ namespace Ark::internal
* @brief The ArkScript AST to IR compiler
*
*/
class ARK_API ASTLowerer final
class ARK_API ASTLowerer final : public Pass
{
public:
/**
Expand Down Expand Up @@ -108,8 +109,6 @@ namespace Ark::internal
IR::label_t m_current_label = 0;
std::stack<std::string> m_opened_vars; ///< stack of vars we are currently declaring

Logger m_logger;

enum class ErrorKind
{
InvalidNodeMacro,
Expand Down Expand Up @@ -224,7 +223,7 @@ namespace Ark::internal
* @param message
* @param node
*/
static void warning(const std::string& message, const Node& node);
void warning(const std::string& message, const Node& node);

/**
* @brief Throw a nice error message
Expand Down
4 changes: 2 additions & 2 deletions include/Ark/Compiler/Macros/Processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ namespace Ark::internal
*
* @param ast
*/
void process(const Node& ast) override;
void process(const Node& ast);

/**
* @brief Return the modified AST
*
* @return Node&
*/
[[nodiscard]] const Node& ast() const noexcept override;
[[nodiscard]] const Node& ast() const noexcept;

friend class MacroExecutor;

Expand Down
5 changes: 3 additions & 2 deletions include/Ark/Compiler/NameResolution/NameResolutionPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <Ark/Utils/Platform.hpp>
#include <Ark/Compiler/Pass.hpp>
#include <Ark/Compiler/AST/Node.hpp>
#include <Ark/Compiler/NameResolution/ScopeResolver.hpp>

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

/**
* @brief Unused overload that return the input AST (untouched as this pass only generates errors)
* @return const Node& ast
*/
[[nodiscard]] const Node& ast() const noexcept override;
[[nodiscard]] const Node& ast() const noexcept;

/**
* @brief Register a symbol as defined, so that later we can throw errors on undefined symbols
Expand Down
4 changes: 2 additions & 2 deletions include/Ark/Compiler/Package/ImportSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ namespace Ark::internal
*/
ImportSolver& setup(const std::filesystem::path& root, const std::vector<Import>& origin_imports);

void process(const Node& origin_ast) override;
void process(const Node& origin_ast);

[[nodiscard]] const Node& ast() const noexcept override;
[[nodiscard]] const Node& ast() const noexcept;

private:
struct ImportWithSource
Expand Down
17 changes: 6 additions & 11 deletions include/Ark/Compiler/Pass.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @file Pass.hpp
* @author Lexy Plateau (lexplt.dev@gmail.com)
* @brief Interface for a compiler pass (take in an AST, output an AST)
* @brief Interface for a compiler pass
* @date 2024-07-21
*
* @copyright Copyright (c) 2024-2026
Expand All @@ -11,9 +11,10 @@
#define ARK_COMPILER_PASS_HPP

#include <Ark/Utils/Platform.hpp>
#include <Ark/Compiler/AST/Node.hpp>
#include <Ark/Utils/Logger.hpp>

#include <ostream>

namespace Ark::internal
{
/**
Expand All @@ -33,17 +34,11 @@ namespace Ark::internal
virtual ~Pass() = default;

/**
* @brief Start processing the given AST
* @param ast
*/
virtual void process(const Node& ast) = 0;

/**
* @brief Output of the compiler pass
* @brief Set a custom output stream for the logger
*
* @return const Node& the modified AST
* @param os output stream
*/
[[nodiscard]] virtual const Node& ast() const noexcept = 0;
void configureLogger(std::ostream& os);

protected:
Logger m_logger;
Expand Down
6 changes: 6 additions & 0 deletions include/Ark/Compiler/Welder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ namespace Ark
*/
bool saveBytecodeToFile(const std::string& filename);

/**
* @brief Redirect the logs to a given stream
* @param os output stream
*/
void redirectLogsTo(std::ostream& os);

[[nodiscard]] const internal::Node& ast() const noexcept;
[[nodiscard]] std::string textualIR() const noexcept;
[[nodiscard]] const bytecode_t& bytecode() const noexcept;
Expand Down
3 changes: 2 additions & 1 deletion include/Ark/Error/Diagnostics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ namespace Ark::Diagnostics
*
* @param message error message to be included in the context
* @param node AST node with the error
* @param colorize toggle context colors (default: true)
* @return std::string
*/
std::string makeContextWithNode(const std::string& message, const internal::Node& node);
std::string makeContextWithNode(const std::string& message, const internal::Node& node, bool colorize = true);

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

Expand Down
9 changes: 6 additions & 3 deletions include/Ark/State.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,22 @@ namespace Ark
*
* @param file_path path to an ArkScript code file
* @param features compiler features to enable/disable
* @param stream optional output stream for the logger
* @return true on success
* @return false on failure
*/
bool doFile(const std::string& file_path, uint16_t features = DefaultFeatures);
bool doFile(const std::string& file_path, uint16_t features = DefaultFeatures, std::ostream* stream = nullptr);

/**
* @brief Compile a string (representing ArkScript code) and store resulting bytecode in m_bytecode
*
* @param code the ArkScript code
* @param features compiler features to enable/disable
* @param stream optional output stream for the logger
* @return true on success
* @return false on failure
*/
bool doString(const std::string& code, uint16_t features = DefaultFeatures);
bool doString(const std::string& code, uint16_t features = DefaultFeatures, std::ostream* stream = nullptr);

/**
* @brief Register a function in the virtual machine
Expand Down Expand Up @@ -146,10 +148,11 @@ namespace Ark
*
* @param file the path of file code to compile
* @param output set path of .arkc file
* @param stream output stream for the logger
* @return true on success
* @return false on failure and raise an exception
*/
[[nodiscard]] bool compile(const std::string& file, const std::string& output);
[[nodiscard]] bool compile(const std::string& file, const std::string& output, std::ostream* stream = nullptr);

static void throwStateError(const std::string& message)
{
Expand Down
46 changes: 43 additions & 3 deletions include/Ark/Utils/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@
* @brief Internal logger
* @date 2024-08-30
*
* @copyright Copyright (c) 2024-2026)
* @copyright Copyright (c) 2024-2026
*/
#ifndef ARK_LOGGER_HPP
#define ARK_LOGGER_HPP

#include <iostream>
#include <fmt/format.h>
#include <fmt/color.h>
#include <fmt/ostream.h>

#include <string>
#include <chrono>
#include <source_location>
#include <vector>
#include <unordered_map>
#include <fmt/color.h>

#include <Ark/Utils/Platform.hpp>

namespace Ark::internal
{
class Logger
class ARK_API Logger
{
public:
struct MessageAndLocation
Expand Down Expand Up @@ -67,6 +70,22 @@ namespace Ark::internal
fmt::vformat(fmt, fmt::make_format_args(args...)));
}

/**
* @brief Write a warn level log using fmtlib
* @tparam Args
* @param fmt format string
* @param args
*/
template <typename... Args>
void warn(const char* fmt, Args&&... args)
{
fmt::println(
m_stream == nullptr ? std::cerr : *m_stream,
"{}: {}",
fmt::styled("Warning", colorize() ? fmt::fg(fmt::color::dark_orange) : fmt::text_style()),
fmt::vformat(fmt, fmt::make_format_args(args...)));
}

/**
* @brief Write a debug level log using fmtlib
* @tparam Args
Expand Down Expand Up @@ -120,12 +139,33 @@ namespace Ark::internal
fmt::vformat(fmt, fmt::make_format_args(args...)));
}

/**
* @brief Set a custom output stream to use for warnings. This will disable colors.
*
* @param os output stream
*/
void configureOutputStream(std::ostream* os)
{
m_stream = os;
}

/**
* @brief Check if logs can be colorized
*
* @return true if logs can be colorized
*/
inline bool colorize() const noexcept
{
return m_stream == nullptr;
}

private:
unsigned m_debug;
std::string m_name;
fmt::color m_pass_color;
std::unordered_map<std::string, std::chrono::time_point<std::chrono::high_resolution_clock>> m_trace_starts;
std::vector<std::string> m_active_traces;
std::ostream* m_stream;
};
}

Expand Down
Loading
Loading