Skip to content
This repository was archived by the owner on Oct 12, 2026. It is now read-only.

Commit 091fa99

Browse files
committed
feat: new library for better diagnostics support
1 parent fd93484 commit 091fa99

78 files changed

Lines changed: 1451 additions & 973 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
cmake-build-debug/
33
venv/
44
**__pycache__/
5+
dump/

‎CMakeLists.txt‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ set(CMAKE_CXX_STANDARD 23)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66
set(CMAKE_CXX_EXTENSIONS OFF)
77

8+
find_package(PkgConfig REQUIRED)
9+
810
###### SOURCE ######
911
file(GLOB_RECURSE SOURCE_SOURCE_FILES CONFIGURE_DEPENDS
1012
${PROJECT_SOURCE_DIR}/src/source/*.cpp
@@ -13,17 +15,33 @@ add_library(lbdSource STATIC)
1315
target_sources(lbdSource PRIVATE ${SOURCE_SOURCE_FILES})
1416
target_include_directories(lbdSource PUBLIC ${PROJECT_SOURCE_DIR}/include)
1517

18+
###### DIAGNOSTICS ######
19+
file(GLOB_RECURSE DIAGNOSTICS_SOURCE_FILES CONFIGURE_DEPENDS
20+
${PROJECT_SOURCE_DIR}/src/diagnostics/*.cpp
21+
)
22+
add_library(lbdDiagnostics STATIC)
23+
target_sources(lbdDiagnostics PRIVATE ${DIAGNOSTICS_SOURCE_FILES})
24+
target_link_libraries(lbdDiagnostics PUBLIC
25+
lbdSource
26+
)
27+
target_include_directories(lbdDiagnostics PUBLIC ${PROJECT_SOURCE_DIR}/include)
28+
29+
# Fmt.
30+
pkg_check_modules(FMT REQUIRED fmt)
31+
1632
####### FRONTEND #######
1733
file(GLOB_RECURSE FRONTEND_SOURCE_FILES CONFIGURE_DEPENDS
1834
${PROJECT_SOURCE_DIR}/src/frontend/*.cpp
1935
)
2036
add_library(lbdFrontend STATIC)
2137
target_sources(lbdFrontend PRIVATE ${FRONTEND_SOURCE_FILES})
22-
target_link_libraries(lbdFrontend PUBLIC
23-
lbdSource
24-
)
2538
target_include_directories(lbdFrontend PUBLIC
2639
${PROJECT_SOURCE_DIR}/include
40+
${FMT_INCLUDE_DIRS}
41+
)
42+
target_link_libraries(lbdFrontend PRIVATE
43+
lbdDiagnostics
44+
${FMT_LIBRARIES}
2745
)
2846

2947
####### RUNTIME #######
@@ -32,25 +50,28 @@ file(GLOB_RECURSE RUNTIME_SOURCE_FILES CONFIGURE_DEPENDS
3250
)
3351
add_library(lbdRuntime STATIC)
3452
target_sources(lbdRuntime PRIVATE ${RUNTIME_SOURCE_FILES})
35-
target_link_libraries(lbdRuntime PUBLIC
36-
lbdSource
37-
)
3853
target_include_directories(lbdRuntime PUBLIC
3954
${PROJECT_SOURCE_DIR}/include
55+
${FMT_INCLUDE_DIRS}
56+
)
57+
target_link_libraries(lbdRuntime PRIVATE
58+
lbdDiagnostics
59+
${FMT_LIBRARIES}
4060
)
4161

4262
###### MAIN EXECUTABLE ######
4363
file(GLOB LBD_SOURCE_FILES CONFIGURE_DEPENDS
4464
${PROJECT_SOURCE_DIR}/src/*.cpp
65+
${PROJECT_SOURCE_DIR}/src/utils/terminal/*.cpp
4566
)
4667
add_executable(lbd)
4768
target_sources(lbd PRIVATE ${LBD_SOURCE_FILES})
4869
target_link_libraries(lbd PRIVATE
70+
lbdDiagnostics
4971
lbdFrontend
5072
lbdRuntime
5173
)
5274

53-
find_package(PkgConfig REQUIRED)
5475
# Readline.
5576
pkg_check_modules(READLINE REQUIRED readline)
5677
target_include_directories(lbd PRIVATE ${READLINE_INCLUDE_DIRS})

‎README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88

99
### Prerequisites
1010

11-
- **CMake** (version 3.16+ recommended)
12-
- **Python 3.8+** (for development environment and tooling)
13-
- A C++17 capable compiler (e.g. `g++`, `clang++`, or MSVC on Windows)
11+
- **CMake** (version 3.20+ recommended)
12+
- A C++23 capable compiler (e.g. `g++`, `clang++`, or MSVC on Windows)
1413

1514
### Setup
1615

@@ -117,4 +116,5 @@ lbd [options]
117116

118117
## Dependencies
119118

120-
1. [readline](https://tiswww.case.edu/php/chet/readline/rltop.html): For better REPL user experience.
119+
1. [readline](https://man7.org/linux/man-pages/man3/readline.3.html): For better REPL user experience.
120+
2. [fmt](https://github.com/fmtlib/fmt): For formatting strings.

‎include/lbd/Context.hpp‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
#pragma once
22

3+
#include <lbd/Logger.hpp>
34
#include <lbd/Options.hpp>
4-
#include <lbd/source/SourceManager.hpp>
5+
#include <lbd/diagnostics/DiagnosticEmitter.hpp>
6+
#include <lbd/source/BufferManager.hpp>
57

68
namespace lbd
79
{
810
class Context
911
{
1012
public:
11-
explicit Context(Options options);
13+
explicit Context(std::ostream& outputStream, Options options = {});
1214

13-
[[nodiscard]] source::SourceManager& getSourceManager();
15+
[[nodiscard]] source::BufferManager& getBufferManager();
16+
17+
[[nodiscard]] diagnostics::DiagnosticEmitter& getDiagnosticEmitter();
1418

1519
[[nodiscard]] Options& getOptions();
1620

17-
// TODO: Add diagnostic emitter and renderer.
21+
[[nodiscard]] Logger& getLogger();
22+
23+
/// Emits error upon unable to load the file.
24+
[[nodiscard]] source::BufferId loadFile(const std::string& path, const source::Range& range);
1825

1926
private:
2027
Options options;
21-
source::SourceManager sourceManager;
28+
Logger logger;
29+
source::BufferManager bufferManager;
30+
diagnostics::DiagnosticRenderer diagnosticRenderer;
31+
diagnostics::DiagnosticEmitter diagnosticEmitter;
2232
};
2333
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <iostream>
44

5-
namespace lbd::docs
5+
namespace lbd
66
{
77
void dumpDocs(std::ostream& outputStream);
88
}

‎include/lbd/Driver.hpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <lbd/Context.hpp>
4+
#include <lbd/runtime/interpreter.h>
5+
#include <lbd/source/Buffer.hpp>
6+
7+
namespace lbd
8+
{
9+
class Driver
10+
{
11+
public:
12+
explicit Driver(Context& context) noexcept;
13+
14+
[[nodiscard]] runtime::Result run(source::BufferId bufferId) const noexcept;
15+
16+
private:
17+
Context& context;
18+
};
19+
}
Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include <optional>
44
#include <lbd/exceptions.h>
55
#include <lbd/source/Range.hpp>
6-
#include <lbd/utils/terminal.h>
6+
#include <lbd/utils/terminal/Colors.hpp>
7+
#include <lbd/utils/terminal/Terminal.hpp>
78

89
namespace lbd
910
{
@@ -20,28 +21,14 @@ namespace lbd
2021
{
2122
if (useColor)
2223
{
23-
enableVirtualTerminal();
24+
utils::terminal::enableVirtualTerminal();
2425
}
2526
}
2627

2728
template <typename... Arguments>
28-
[[noreturn]] void error(const std::optional<source::Location> location, Arguments&&... arguments) const
29+
[[noreturn]] void error(Arguments&&... arguments) const
2930
{
30-
if (useColor)
31-
{
32-
std::cerr << colors::RED;
33-
}
34-
if (showLocation && location.has_value())
35-
{
36-
if (location->getFileId().has_value()) std::cerr << location->getFileId().has_value() << ":";
37-
std::cerr << location->getRow() << ":" << location->getColumn() << ": ";
38-
}
39-
(std::cerr << ... << std::forward<Arguments>(arguments));
40-
if (useColor)
41-
{
42-
std::cerr << colors::RESET;
43-
}
44-
std::cerr << std::endl;
31+
logImplementation(utils::terminal::Colors().red, std::cout, std::forward<Arguments>(arguments)...);
4532
if (exitOnError)
4633
{
4734
exit(EXIT_FAILURE);
@@ -52,13 +39,13 @@ namespace lbd
5239
template <typename... Arguments>
5340
void info(Arguments&&... arguments) const
5441
{
55-
logImplementation(colors::BLUE, std::cout, std::forward<Arguments>(arguments)...);
42+
logImplementation(utils::terminal::Colors().blue, std::cout, std::forward<Arguments>(arguments)...);
5643
}
5744

5845
template <typename... Arguments>
5946
void debug(Arguments&&... arguments) const
6047
{
61-
logImplementation(colors::YELLOW, std::cout, std::forward<Arguments>(arguments)...);
48+
logImplementation(utils::terminal::Colors().yellow, std::cout, std::forward<Arguments>(arguments)...);
6249
}
6350

6451
private:
@@ -72,7 +59,7 @@ namespace lbd
7259
(stream << ... << std::forward<Arguments>(arguments));
7360
if (useColor)
7461
{
75-
stream << colors::RESET;
62+
stream << utils::terminal::Colors().reset;
7663
}
7764
stream << std::endl;
7865
}

‎include/lbd/Options.hpp‎

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,13 @@
11
#pragma once
22

3-
#include <lbd/logs.h>
4-
53
namespace lbd
64
{
7-
class Options
5+
// TODO: Ensure these fields are private.
6+
struct Options
87
{
9-
public:
10-
explicit Options(bool ownExpression = false, bool forceOnEnvironmentDump = false, bool debug = false);
11-
12-
[[nodiscard]] bool shouldOwnExpression() const;
13-
14-
[[nodiscard]] bool shouldForceOnEnvironmentDump() const;
15-
16-
[[nodiscard]] bool isDebug() const;
17-
18-
void toggleForceOnEnvironmentDump();
19-
20-
void toggleDebug();
21-
22-
// TODO: Instead use diagnostic emitter and renderer.
23-
Logger logger;
24-
25-
private:
26-
bool ownExpression; /// Owning Expression inside Thunk. Turned on for REPL
27-
bool forceOnEnvironmentDump;
28-
bool debug;
8+
/// Owning Expression inside Thunk. Turned on for REPL
9+
bool ownExpression = false;
10+
bool forceOnEnvironmentDump = false;
11+
bool debug = false;
2912
};
3013
}
File renamed without changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include <lbd/Context.hpp>
4+
5+
namespace lbd
6+
{
7+
class ScopedOptionsOverride
8+
{
9+
public:
10+
explicit ScopedOptionsOverride(Context& context);
11+
12+
~ScopedOptionsOverride();
13+
14+
Options& get() const;
15+
16+
private:
17+
Context& context;
18+
Options oldOptions;
19+
};
20+
}

0 commit comments

Comments
 (0)