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

Commit f32d264

Browse files
committed
feat: add rudimentary for logging
1 parent cacc9c3 commit f32d264

20 files changed

Lines changed: 329 additions & 244 deletions

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ if_then: Any = \condition: Any.
4343
```
4444

4545
```console
46-
$ ./cmake-build-debug/lbd.exe -f ./examples/if_then.lbd
46+
$ ./cmake-build-debug/lbd -f ./examples/if_then.lbd
4747
True
4848
False
4949
```

‎examples/shapes.lbd‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ volume_of_cylinder: Float -> Float -> Float = \r: Float.
1313
\h: Float. (mul (area_of_circle r) h)
1414

1515
-- Calculate the volume of a cylinder
16-
(print "Volume of cylinder is: ")
17-
(print (volume_of_cylinder 5.0 10.0))
16+
(print "Volume of cylinder is: " (volume_of_cylinder 5.0 10.0) "\n")

‎examples/std.lbd‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ lte: Float -> Float -> Bool = \x: Float. \y: Float.
2828
(if_zero (cmp x y) true (if_zero (cmp (cmp x y) -1) true false))
2929

3030
gte: Float -> Float -> Bool = \x: Float. \y: Float.
31-
(if_zero (cmp x y) true (if_zero (cmp (cmp x y) 1) true false))
31+
(if_zero (cmp x y) true (if_zero (cmp (cmp x y) 1) true false))

‎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/builtins.h‎

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

33
#include <lbd/intp/interpreter.h>
4+
#include <lbd/options.h>
45

56
namespace intp::interp {
6-
std::vector<NativeFunction> get_builtins();
7+
std::vector<NativeFunction> get_builtins(options::Options options_ = {});
78
}

‎include/lbd/intp/interpreter.h‎

Lines changed: 3 additions & 6 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;
@@ -54,7 +55,7 @@ namespace intp::interp {
5455
using Impl = std::function<Value(
5556
const std::vector<std::shared_ptr<Thunk> > &, const std::shared_ptr<Env> &)>;
5657

57-
std::size_t arity;
58+
int arity;
5859
std::string name;
5960
Impl impl;
6061

@@ -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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
struct Logger {
10+
bool exit_on_error = true;
11+
bool use_color = false;
12+
bool show_loc = true;
13+
14+
Logger() = default;
15+
16+
Logger(const bool exit_on_error, const bool use_color, const bool show_loc) : exit_on_error(exit_on_error),
17+
use_color(use_color), show_loc(show_loc) {
18+
if (use_color) {
19+
enable_virtual_terminal();
20+
}
21+
}
22+
23+
template<typename... Args>
24+
[[noreturn]] void error(const std::optional<fe::loc::Loc> &loc, Args &&... args) const {
25+
if (use_color) {
26+
std::cerr << colors::RED;
27+
}
28+
if (show_loc && loc.has_value()) {
29+
std::cerr << loc.value() << ": ";
30+
}
31+
(std::cerr << ... << std::forward<Args>(args));
32+
if (use_color) {
33+
std::cerr << colors::RESET;
34+
}
35+
std::cerr << std::endl;
36+
if (exit_on_error) {
37+
exit(EXIT_FAILURE);
38+
}
39+
throw ControlledExit{};
40+
}
41+
42+
template<typename... Args>
43+
void info(Args &&... args) const {
44+
log_impl(colors::BLUE, std::cout, std::forward<Args>(args)...);
45+
}
46+
47+
template<typename... Args>
48+
void debug(Args &&... args) const {
49+
log_impl(colors::YELLOW, std::cout, std::forward<Args>(args)...);
50+
}
51+
52+
private:
53+
template<typename... Args>
54+
void log_impl(const std::string &color, std::ostream &os, Args &&... args) const {
55+
if (use_color) {
56+
os << color;
57+
}
58+
(os << ... << std::forward<Args>(args));
59+
if (use_color) {
60+
os << colors::RESET;
61+
}
62+
os << std::endl;
63+
}
64+
};
65+
}

0 commit comments

Comments
 (0)