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

Commit 559fac9

Browse files
committed
chore: refactor miscellaneous files
1 parent 865d1f6 commit 559fac9

14 files changed

Lines changed: 176 additions & 169 deletions

File tree

‎include/lbd/cmd.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
namespace cmd {
88
struct Options {
99
std::optional<std::string> filepath;
10-
bool show_help = false;
10+
bool showHelp = false;
1111
bool repl = false;
1212
bool debug = false;
1313
};
1414

15-
void print_help(std::ostream &os, const std::string &program_name);
15+
void printHelp(std::ostream &os, const std::string &programName);
1616

17-
Options parse_args(int argc, char **argv, const std::string &program_name);
17+
Options parseArguments(int argc, char **argv, const std::string &programName);
1818
}

‎include/lbd/error.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
#include <cassert>
44

55
template<typename>
6-
inline constexpr bool always_false_v = false;
6+
inline constexpr bool alwaysFalseValue = false;
77

88
#define STATIC_ASSERT(VAL, MSG) \
99
static_assert(VAL, __FILE__ ":" STRINGIFY(__LINE__) ": " MSG)
1010

11-
#define STATIC_ASSERT_UNREACHABLE_T(TYPE, MSG) STATIC_ASSERT((always_false_v<TYPE>), MSG)
11+
#define STATIC_ASSERT_UNREACHABLE_T(TYPE, MSG) STATIC_ASSERT((alwaysFalseValue<TYPE>), MSG)
1212
#define STATIC_ASSERT_UNREACHABLE(MSG) STATIC_ASSERT(false, MSG)
1313

1414
#define STRINGIFY2(x) #x
@@ -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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
23
#include <exception>
34

45
struct ControlledExit final : std::exception {

‎include/lbd/logs.h‎

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,59 @@
77

88
namespace logs {
99
struct Logger {
10-
bool exit_on_error = true;
11-
bool use_color = false;
12-
bool show_loc = true;
10+
bool exitOnError = true;
11+
bool useColor = false;
12+
bool showLocation = true;
1313

1414
Logger() = default;
1515

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();
16+
Logger(const bool exitOnError, const bool useColor, const bool showLocation) : exitOnError(exitOnError),
17+
useColor(useColor), showLocation(showLocation) {
18+
if (useColor) {
19+
enableVirtualTerminal();
2020
}
2121
}
2222

23-
template<typename... Args>
24-
[[noreturn]] void error(const std::optional<fe::loc::Loc> &loc, Args &&... args) const {
25-
if (use_color) {
23+
template<typename... Arguments>
24+
[[noreturn]] void error(const std::optional<fe::loc::Loc> &location, Arguments &&... arguments) const {
25+
if (useColor) {
2626
std::cerr << colors::RED;
2727
}
28-
if (show_loc && loc.has_value()) {
29-
std::cerr << loc.value() << ": ";
28+
if (showLocation && location.has_value()) {
29+
std::cerr << location.value() << ": ";
3030
}
31-
(std::cerr << ... << std::forward<Args>(args));
32-
if (use_color) {
31+
(std::cerr << ... << std::forward<Arguments>(arguments));
32+
if (useColor) {
3333
std::cerr << colors::RESET;
3434
}
3535
std::cerr << std::endl;
36-
if (exit_on_error) {
36+
if (exitOnError) {
3737
exit(EXIT_FAILURE);
3838
}
3939
throw ControlledExit{};
4040
}
4141

42-
template<typename... Args>
43-
void info(Args &&... args) const {
44-
log_impl(colors::BLUE, std::cout, std::forward<Args>(args)...);
42+
template<typename... Arguments>
43+
void info(Arguments &&... arguments) const {
44+
logImplementation(colors::BLUE, std::cout, std::forward<Arguments>(arguments)...);
4545
}
4646

47-
template<typename... Args>
48-
void debug(Args &&... args) const {
49-
log_impl(colors::YELLOW, std::cout, std::forward<Args>(args)...);
47+
template<typename... Arguments>
48+
void debug(Arguments &&... arguments) const {
49+
logImplementation(colors::YELLOW, std::cout, std::forward<Arguments>(arguments)...);
5050
}
5151

5252
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;
53+
template<typename... Arguments>
54+
void logImplementation(const std::string &color, std::ostream &stream, Arguments &&... arguments) const {
55+
if (useColor) {
56+
stream << color;
5757
}
58-
(os << ... << std::forward<Args>(args));
59-
if (use_color) {
60-
os << colors::RESET;
58+
(stream << ... << std::forward<Arguments>(arguments));
59+
if (useColor) {
60+
stream << colors::RESET;
6161
}
62-
os << std::endl;
62+
stream << std::endl;
6363
}
6464
};
6565
}

‎include/lbd/options.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
23
#include "logs.h"
34

45
namespace options {

‎include/lbd/utils/string_escape.h‎

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
#pragma once
22
#include <string>
33

4-
inline std::string escape(const std::string& str) {
4+
inline std::string escapeString(const std::string &data) {
55
std::string result;
6-
result.reserve(str.size());
7-
for (const char c : str) {
6+
result.reserve(data.size());
7+
for (const char c: data) {
88
switch (c) {
9-
case '\n': result += "\\n"; break;
10-
case '\t': result += "\\t"; break;
11-
case '\r': result += "\\r"; break;
12-
case '\\': result += "\\\\"; break;
13-
case '"': result += "\\\""; break;
14-
case '\'': result += "\\\'"; break;
9+
case '\n': result += "\\n";
10+
break;
11+
case '\t': result += "\\t";
12+
break;
13+
case '\r': result += "\\r";
14+
break;
15+
case '\\': result += "\\\\";
16+
break;
17+
case '"': result += "\\\"";
18+
break;
19+
case '\'': result += "\\\'";
20+
break;
1521
default:
16-
// Printable ASCII characters
22+
// Printable ASCII characters.
1723
if (c >= 32 && c <= 126) {
1824
result.push_back(c);
1925
} else {
20-
// Non-printable: use hex escape
21-
char buf[5];
22-
snprintf(buf, sizeof(buf), "\\x%02X", static_cast<unsigned char>(c));
23-
result += buf;
26+
// Non-printable: use hex escape.
27+
char buffer[5];
28+
snprintf(buffer, sizeof(buffer), "\\x%02X", static_cast<unsigned char>(c));
29+
result += buffer;
2430
}
2531
}
2632
}
2733
return result;
2834
}
2935

30-
inline std::string unescape_string(const std::string &str) {
36+
inline std::string unescapeString(const std::string &data) {
3137
std::string result;
32-
result.reserve(str.size());
33-
for (size_t i = 0; i < str.size(); ++i) {
34-
if (str[i] == '\\' && i + 1 < str.size()) {
35-
switch (str[i + 1]) {
38+
result.reserve(data.size());
39+
for (size_t index = 0; index < data.size(); ++index) {
40+
if (data[index] == '\\' && index + 1 < data.size()) {
41+
switch (data[index + 1]) {
3642
case 'n': result.push_back('\n');
3743
break;
3844
case 't': result.push_back('\t');
@@ -44,12 +50,12 @@ inline std::string unescape_string(const std::string &str) {
4450
case '"': result.push_back('"');
4551
break;
4652
default:
47-
result.push_back(str[i + 1]); // Unknown escape: take character
53+
result.push_back(data[index + 1]); // Unknown escape: take character.
4854
}
49-
++i; // Skip the escaped char
55+
++index; // Skip the escaped char.
5056
} else {
51-
result.push_back(str[i]);
57+
result.push_back(data[index]);
5258
}
5359
}
5460
return result;
55-
}
61+
}

‎include/lbd/utils/term.h‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <windows.h>
99
#endif
1010

11-
inline bool enable_virtual_terminal() {
11+
inline bool enableVirtualTerminal() {
1212
#if defined(_WIN32)
1313
const HANDLE output_handle = GetStdHandle(STD_OUTPUT_HANDLE); // NOLINT(*-misplaced-const)
1414
if (output_handle == INVALID_HANDLE_VALUE) {
@@ -37,17 +37,17 @@ namespace colors {
3737
constexpr auto GREY = "\033[38;5;200m";
3838
}
3939

40-
inline void print_table_row(const std::vector<std::string> &row, const std::vector<size_t> &widths,
41-
const std::string &color = "") {
40+
inline void printTableRow(const std::vector<std::string> &row, const std::vector<size_t> &widths,
41+
const std::string &color = "") {
4242
std::cout << color;
4343
for (size_t i = 0; i < row.size(); ++i) {
4444
std::cout << std::setw(static_cast<int>(widths[i])) << std::left << row[i] << " ";
4545
}
4646
std::cout << colors::RESET << std::endl;
4747
}
4848

49-
inline void print_table(const std::vector<std::string> &headers, const std::vector<std::vector<std::string> > &data,
50-
const std::string &color = colors::CYAN) {
49+
inline void printTable(const std::vector<std::string> &headers, const std::vector<std::vector<std::string> > &data,
50+
const std::string &color = colors::CYAN) {
5151
std::vector<size_t> widths(headers.size(), 0);
5252
for (size_t i = 0; i < headers.size(); ++i) {
5353
widths[i] = headers[i].size();
@@ -59,12 +59,12 @@ inline void print_table(const std::vector<std::string> &headers, const std::vect
5959
}
6060
}
6161
}
62-
print_table_row(headers, widths, color);
62+
printTableRow(headers, widths, color);
6363
for (size_t i = 0; i < headers.size(); ++i) {
6464
std::cout << std::string(widths[i], '-') << " ";
6565
}
6666
std::cout << std::endl;
6767
for (const auto &row: data) {
68-
print_table_row(row, widths);
68+
printTableRow(row, widths);
6969
}
7070
}

‎src/cmd.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
#include <lbd/cmd.h>
22

33
namespace cmd {
4-
void print_help(std::ostream &os, const std::string &program_name) {
5-
os << "usage: " << program_name << " [options]\n\n"
4+
void printHelp(std::ostream &os, const std::string &programName) {
5+
os << "usage: " << programName << " [options]\n\n"
66
<< "options:\n"
77
<< " -f, --file <filepath> Specify input source filepath to run\n"
88
<< " -h, --help Show this help message and exit\n"
99
<< " -d, --debug Enable debug mode\n"
1010
<< " -r, --repl Run in interactive REPL node" << std::endl;
1111
}
1212

13-
Options parse_args(const int argc, char **argv, const std::string &program_name) {
13+
Options parseArguments(const int argc, char **argv, const std::string &programName) {
1414
Options opts;
1515
for (int i = 1; i < argc; ++i) {
1616
if (std::string arg = argv[i]; arg == "-h" || arg == "--help") {
17-
opts.show_help = true;
17+
opts.showHelp = true;
1818
} else if (arg == "-f" || arg == "--file") {
1919
if (i + 1 < argc) {
2020
opts.filepath = argv[++i];
2121
} else {
2222
std::cerr << "error: missing filepath after " << arg << std::endl;
23-
print_help(std::cerr, program_name);
23+
printHelp(std::cerr, programName);
2424
std::exit(EXIT_FAILURE);
2525
}
2626
} else if (arg == "-d" || arg == "--debug") {
@@ -29,7 +29,7 @@ namespace cmd {
2929
opts.repl = true;
3030
} else {
3131
std::cerr << "unknown option: " << arg << "\n";
32-
print_help(std::cerr, program_name);
32+
printHelp(std::cerr, programName);
3333
std::exit(EXIT_FAILURE);
3434
}
3535
}

‎src/fe/ast.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace fe::ast {
1515
}
1616

1717
std::ostream &operator<<(std::ostream &stream, const StringAstNode &node) {\
18-
return stream << "\"" << escape(node.value) << "\"";
18+
return stream << "\"" << escapeString(node.value) << "\"";
1919
}
2020

2121
std::ostream &operator<<(std::ostream &stream, const FloatAstNode &node) {

‎src/fe/parser.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ namespace fe::parser {
8888
}
8989
if (std::is_same_v<T, token::String>) {
9090
++index;
91-
const auto value = unescape_string(std::get<token::String>(tok.tokenType).value);
91+
const auto value = unescapeString(std::get<token::String>(tok.tokenType).value);
9292
return ast::Expression(ast::StringAstNode{value, location});
9393
}
9494
if (std::is_same_v<T, token::Float>) {
@@ -175,7 +175,7 @@ namespace fe::parser {
175175
identifierValue == "use") {
176176
++index; // consume "use"
177177
assertToken<token::String>(tokens, index);
178-
const std::string filepath = unescape_string(
178+
const std::string filepath = unescapeString(
179179
std::get<token::String>(tokens[index].tokenType).value);
180180
++index; // consume <filepath>
181181
processUseFile(nodes, filepath);

0 commit comments

Comments
 (0)