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

Commit 0bb165f

Browse files
committed
Add logger
1 parent 50854a1 commit 0bb165f

14 files changed

Lines changed: 131 additions & 94 deletions

File tree

‎include/lbd/error.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ inline constexpr bool always_false_v = false;
2525
#define UNREACHABLE_OP ((void)0)
2626
#endif
2727

28-
#define UNREACHABLE(MSG) (assert(false && (MSG)), UNREACHABLE_OP)
28+
#define UNREACHABLE(MSG) (assert(false && (MSG)), UNREACHABLE_OP)

‎include/lbd/exceptions.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
#include <exception>
3+
4+
struct ControlledExit final : std::exception {
5+
const char *what() const noexcept override {
6+
return "Controlled non-fatal exit";
7+
}
8+
};

‎include/lbd/fe/lexer.h‎

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

33
#include <lbd/fe/loc.h>
44
#include <lbd/fe/token.h>
5+
#include <lbd/options.h>
56
#include <string>
67
#include <vector>
78

@@ -19,9 +20,9 @@ namespace fe::lexer {
1920
size_t col = 1;
2021
std::string filepath;
2122

22-
explicit Lexer(const std::string &filepath, FromFile);
23+
Lexer(const std::string &filepath, FromFile, options::Options options_ = {});
2324

24-
explicit Lexer(const std::string &str, FromRepl);
25+
Lexer(std::string str, FromRepl, options::Options options_ = {});
2526

2627
token::Token next_token();
2728

‎include/lbd/fe/parser.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <lbd/options.h>
34
#include <lbd/fe/ast.h>
45
#include <lbd/fe/token.h>
56
#include <lbd/intp/types.h>
@@ -9,7 +10,7 @@ namespace fe::parser {
910
struct Parser {
1011
ast::Program program;
1112

12-
explicit Parser(const std::vector<token::Token> &tokens);
13+
explicit Parser(const std::vector<token::Token> &tokens, options::Options options_ = {});
1314

1415
private:
1516
// TODO: Add checks for T to be a variant of fe::token::TokenType

‎include/lbd/intp/interpreter.h‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <variant>
77
#include <lbd/fe/ast.h>
88
#include <lbd/fe/parser.h>
9+
#include <lbd/options.h>
910

1011
namespace intp::interp {
1112
struct NativeFunction;
@@ -113,12 +114,8 @@ namespace intp::interp {
113114
Value value;
114115
};
115116

116-
struct Options {
117-
bool own_expr = false;
118-
};
119-
120117
Result interpret(fe::ast::Program &program, std::optional<std::shared_ptr<Env> > global_env = std::nullopt,
121-
Options options = {});
118+
options::Options options_ = {});
122119

123120
/// Add builtins Native Functions into Environment
124121
void install_builtins(const std::shared_ptr<Env> &env);

‎include/lbd/logs.h‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include <optional>
4+
#include <lbd/fe/loc.h>
5+
#include <lbd/utils/term.h>
6+
#include <lbd/exceptions.h>
7+
8+
namespace logs {
9+
class Logger {
10+
bool exit_on_error = true;
11+
bool use_color = false;
12+
bool show_loc = true;
13+
14+
public:
15+
Logger() = default;
16+
17+
Logger(const bool exit_on_error, const bool use_color, const bool show_loc) : exit_on_error(exit_on_error),
18+
use_color(use_color), show_loc(show_loc) {
19+
if (use_color) {
20+
enable_virtual_terminal();
21+
}
22+
}
23+
24+
template<typename... Args>
25+
[[noreturn]] void error(const std::optional<fe::loc::Loc> &loc, Args &&... args) const {
26+
if (use_color) {
27+
std::cerr << colors::RED;
28+
}
29+
if (show_loc && loc.has_value()) {
30+
std::cerr << loc.value() << ": ";
31+
}
32+
(std::cerr << ... << std::forward<Args>(args));
33+
if (use_color) {
34+
std::cerr << colors::RESET;
35+
}
36+
std::cerr << std::endl;
37+
if (exit_on_error) {
38+
exit(EXIT_FAILURE);
39+
}
40+
throw ControlledExit{};
41+
}
42+
};
43+
}

‎include/lbd/options.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
#include "logs.h"
3+
4+
namespace options {
5+
struct Options {
6+
bool own_expr = false; /// Owning Expression inside Thunk. Turned on for REPL
7+
bool debug = false;
8+
logs::Logger logger;
9+
};
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <iostream>
4+
#include <iomanip>
45
#include <vector>
56

67
#if defined(_WIN32)

‎src/fe/ast.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <lbd/error.h>
22
#include <lbd/fe/ast.h>
33

4-
#include "lbd/string_escape.h"
4+
#include "../../include/lbd/utils/string_escape.h"
55

66
namespace fe::ast {
77
static void print_indent(std::ostream &os, const size_t indent) {

0 commit comments

Comments
 (0)