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

Commit 5e844cd

Browse files
committed
chore: reformat REPL
1 parent 169d219 commit 5e844cd

1 file changed

Lines changed: 89 additions & 45 deletions

File tree

‎src/repl.cpp‎

Lines changed: 89 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,89 +12,111 @@
1212

1313
// TODO: Option :t for display type information of a symbol.
1414

15-
namespace repl {
15+
namespace repl
16+
{
1617
static context::Options optionsValue;
1718

18-
static void processLoadCommand(const std::string &argument,
19-
std::optional<std::shared_ptr<runtime::Environment> > &sharedEnvironment) {
20-
const std::string &filepath = argument;
19+
static void processLoadCommand(const std::string& argument,
20+
std::optional<std::shared_ptr<runtime::Environment>>& sharedEnvironment)
21+
{
22+
const std::string& filepath = argument;
2123
context::Options subOptions = optionsValue;
2224
subOptions.logger.showLocation = true;
2325

24-
if (!std::filesystem::exists(filepath)) {
26+
if (!std::filesystem::exists(filepath))
27+
{
2528
subOptions.logger.error({}, "IO error: filepath ", filepath, " does not exist");
2629
}
2730

2831
frontend::Lexer lexer = frontend::Lexer::fromFile(filepath, subOptions);
2932
const auto tokens = lexer.lex();
30-
if (subOptions.debug) {
31-
for (const auto &token: tokens) {
33+
if (subOptions.debug)
34+
{
35+
for (const auto& token : tokens)
36+
{
3237
subOptions.logger.debug(token);
3338
}
3439
}
3540

3641
frontend::Program program = frontend::Parser(tokens, subOptions).parse();
37-
if (subOptions.debug) {
38-
for (const auto &node: program.astNodes) {
42+
if (subOptions.debug)
43+
{
44+
for (const auto& node : program.astNodes)
45+
{
3946
subOptions.logger.debug(node);
4047
}
4148
}
4249

43-
const std::optional<std::shared_ptr<runtime::Environment> > temporaryEnvironment = sharedEnvironment;
50+
const std::optional<std::shared_ptr<runtime::Environment>> temporaryEnvironment = sharedEnvironment;
4451
// Merge loaded_env into shared_env
4552
if (const auto [loadedEnvironment, _, resultantOptions] = runtime::interpret(
46-
program, temporaryEnvironment, subOptions); loadedEnvironment) {
47-
if (!sharedEnvironment) {
53+
program, temporaryEnvironment, subOptions); loadedEnvironment)
54+
{
55+
if (!sharedEnvironment)
56+
{
4857
sharedEnvironment = loadedEnvironment;
49-
} else {
58+
}
59+
else
60+
{
5061
// Merge all Bindings from loaded_env into shared_env
51-
for (const auto &[first, second]: loadedEnvironment->table) {
62+
for (const auto& [first, second] : loadedEnvironment->table)
63+
{
5264
(*sharedEnvironment)->bind(first, second);
5365
}
5466
}
55-
if (resultantOptions.sideEffects) {
67+
if (resultantOptions.sideEffects)
68+
{
5669
std::cout << std::endl;
5770
}
5871
}
5972

6073
subOptions.logger.info("info: file loaded ", filepath);
6174
}
6275

63-
static int computeParenthesisDepth(const std::string &data) {
76+
static int computeParenthesisDepth(const std::string& data)
77+
{
6478
int depth = 0;
65-
for (const char c: data) {
79+
for (const char c : data)
80+
{
6681
if (c == '(') depth++;
6782
else if (c == ')') depth = std::max(0, depth - 1);
6883
}
6984
return depth;
7085
}
7186

72-
static std::string trim(const std::string &data) {
87+
static std::string trim(const std::string& data)
88+
{
7389
size_t start = 0;
7490
while (start < data.size() && std::isspace(static_cast<unsigned char>(data[start]))) start++;
7591
size_t end = data.size();
7692
while (end > start && std::isspace(static_cast<unsigned char>(data[end - 1]))) end--;
7793
return data.substr(start, end - start);
7894
}
7995

80-
void loop(const bool debug) {
96+
void loop(const bool debug)
97+
{
8198
enableVirtualTerminal();
8299

83100
static logs::Logger logger(false, true, false);
84101
optionsValue = {.ownExpression = true, .forceOnEnvironmentDump = false, .debug = debug, .logger = logger};
85102

86103
std::string line, buffer;
87104
size_t indentLevel = 0;
88-
std::optional<std::shared_ptr<runtime::Environment> > sharedGlobalEnvironment = std::nullopt;
105+
std::optional<std::shared_ptr<runtime::Environment>> sharedGlobalEnvironment = std::nullopt;
89106

90107
optionsValue.logger.info("Welcome to lambda-discipline REPL.\nType :quit to exit.");
91108

92-
while (true) {
93-
try {
109+
while (true)
110+
{
111+
try
112+
{
94113
// Prompt depends on whether we are continuing a buffer
95-
if (buffer.empty()) {
114+
if (buffer.empty())
115+
{
96116
std::cout << colors::CYAN << "\n>> " << colors::RESET;
97-
} else {
117+
}
118+
else
119+
{
98120
std::cout << colors::CYAN << ".. " << colors::RESET;
99121
}
100122
for (size_t i = 0; i < indentLevel; i++) std::cout << " ";
@@ -103,12 +125,14 @@ namespace repl {
103125

104126
// REPL arguments parsing
105127
{
106-
if (line == ":q" || line == ":quit" || line == ":exit") {
128+
if (line == ":q" || line == ":quit" || line == ":exit")
129+
{
107130
optionsValue.logger.info("\nexiting REPL.");
108131
break;
109132
}
110133

111-
if (line == ":c" || line == ":clear" || line == ":cls") {
134+
if (line == ":c" || line == ":clear" || line == ":cls")
135+
{
112136
#ifdef _WIN32
113137
std::system("cls");
114138
#else
@@ -117,7 +141,8 @@ namespace repl {
117141
continue;
118142
}
119143

120-
if (line == ":h" || line == ":help" || line == ":?") {
144+
if (line == ":h" || line == ":help" || line == ":?")
145+
{
121146
std::cout << std::endl;
122147
printTable({"General Commands", "Argument", "Description"}, {
123148
{":q, :quit, :exit", "", "Exit the REPL"},
@@ -143,39 +168,48 @@ namespace repl {
143168
continue;
144169
}
145170

146-
if (line == ":env" || line == ":e") {
171+
if (line == ":env" || line == ":e")
172+
{
147173
std::cout << std::endl;
148-
if (sharedGlobalEnvironment) {
174+
if (sharedGlobalEnvironment)
175+
{
149176
printTable({"Symbol", "Thunk"},
150177
(*sharedGlobalEnvironment)->toVector(optionsValue.forceOnEnvironmentDump),
151178
colors::GREEN);
152-
} else {
179+
}
180+
else
181+
{
153182
printTable({"Symbol", "Thunk"}, {{"Empty"}}, colors::GREEN);
154183
}
155184
continue;
156185
}
157186

158-
if (line == ":reset" || line == ":r") {
187+
if (line == ":reset" || line == ":r")
188+
{
159189
sharedGlobalEnvironment.reset();
160190
continue;
161191
}
162192

163-
if (line == ":debug" || line == ":d") {
193+
if (line == ":debug" || line == ":d")
194+
{
164195
optionsValue.debug = !optionsValue.debug;
165196
continue;
166197
}
167198

168-
if (line == ":force") {
199+
if (line == ":force")
200+
{
169201
optionsValue.forceOnEnvironmentDump = !optionsValue.forceOnEnvironmentDump;
170202
continue;
171203
}
172204

173-
if (line.rfind(":load ", 0) == 0) {
205+
if (line.rfind(":load ", 0) == 0)
206+
{
174207
processLoadCommand(line.substr(6), sharedGlobalEnvironment);
175208
continue;
176209
}
177210

178-
if (line.rfind(":l ", 0) == 0) {
211+
if (line.rfind(":l ", 0) == 0)
212+
{
179213
processLoadCommand(line.substr(3), sharedGlobalEnvironment);
180214
continue;
181215
}
@@ -186,14 +220,16 @@ namespace repl {
186220
std::string trimmed = trim(line);
187221
// Continue the line with trailing '`'.
188222
bool continuation = !trimmed.empty() && trimmed.back() == '`';
189-
if (continuation) {
223+
if (continuation)
224+
{
190225
trimmed.pop_back();
191226
}
192227
buffer += trimmed + "\n";
193228

194-
if (continuation) {
195-
// Indent level depends on whether the current line ends with '.' after trimming and removing trailing '`'.
196-
// Additionally, it also depends upon the current parenthesis depth.
229+
if (continuation)
230+
{
231+
// Indent level depends on whether the current line ends with '.' after trimming
232+
// and removing trailing '`'. Additionally, it also depends upon the current parenthesis depth.
197233
indentLevel = computeParenthesisDepth(buffer);
198234
trimmed = trim(trimmed);
199235
if (!trimmed.empty() && trimmed.back() == '.') indentLevel++;
@@ -206,28 +242,36 @@ namespace repl {
206242
// Lex
207243
frontend::Lexer lexerValue = frontend::Lexer::fromRepl(line, optionsValue);
208244
const std::vector<frontend::token::Token> tokens = lexerValue.lex();
209-
if (optionsValue.debug) {
210-
for (const auto &tok: tokens) {
245+
if (optionsValue.debug)
246+
{
247+
for (const auto& tok : tokens)
248+
{
211249
optionsValue.logger.debug(tok);
212250
}
213251
}
214252

215253
// Parse
216254
frontend::Program program = frontend::Parser(tokens, optionsValue).parse();
217-
if (optionsValue.debug) {
255+
if (optionsValue.debug)
256+
{
218257
optionsValue.logger.debug(program);
219258
}
220259

221260
// Interpret
222261
const auto [globalEnvironment, value, resultantOptions] = runtime::interpret(
223262
program, sharedGlobalEnvironment, optionsValue);
224-
if (resultantOptions.sideEffects) {
263+
if (resultantOptions.sideEffects)
264+
{
225265
std::cout << std::endl;
226266
}
227267
std::cout << colors::GREEN << "=> " << value << colors::RESET << std::endl;
228268
sharedGlobalEnvironment = globalEnvironment;
229-
} catch (const ControlledExit &) {
230-
} catch (const std::exception &ex) {
269+
}
270+
catch (const ControlledExit&)
271+
{
272+
}
273+
catch (const std::exception& ex)
274+
{
231275
optionsValue.logger.error({}, "error: ", ex.what());
232276
}
233277
}

0 commit comments

Comments
 (0)