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

Commit 4c11ec0

Browse files
committed
chore: refactor interpreter module
1 parent 7851236 commit 4c11ec0

2 files changed

Lines changed: 38 additions & 33 deletions

File tree

‎include/lbd/interpreter/interpreter.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ namespace interpreter {
2323

2424
[[nodiscard]] std::string toString() const;
2525

26-
friend std::ostream &operator<<(std::ostream &stream, const Closure &closure);
26+
friend std::ostream &operator<<(std::ostream &outputStream, const Closure &closure);
2727
};
2828

2929
struct List {
3030
std::vector<Value> elements;
3131

3232
[[nodiscard]] std::string toString() const;
3333

34-
friend std::ostream &operator<<(std::ostream &stream, const List &list);
34+
friend std::ostream &operator<<(std::ostream &outputStream, const List &list);
3535
};
3636

3737
using ValueVariant = std::variant<
@@ -48,14 +48,14 @@ namespace interpreter {
4848
/// Pretty print a runtime value for REPL/diagnostics.
4949
[[nodiscard]] std::string toString() const;
5050

51-
friend std::ostream &operator<<(std::ostream &stream, const Value &value);
51+
friend std::ostream &operator<<(std::ostream &outputStream, const Value &value);
5252
};
5353

5454
struct ResultOptions {
5555
bool sideEffects = false;
5656

5757
// TODO: _ may not be needed here
58-
void interpolate(const ResultOptions &resultOptions_);
58+
void interpolate(const ResultOptions &resultOptions);
5959
};
6060

6161
struct NativeFunction {
@@ -68,7 +68,7 @@ namespace interpreter {
6868

6969
[[nodiscard]] std::string toString() const;
7070

71-
friend std::ostream &operator<<(std::ostream &stream, const NativeFunction &nativeFunction);
71+
friend std::ostream &operator<<(std::ostream &outputStream, const NativeFunction &nativeFunction);
7272
};
7373

7474
/// Lazy-Thunk (call-by-need).

‎src/interpreter/interpreter.cpp‎

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ namespace interpreter {
1111
static ResultOptions globalResultOptions;
1212

1313
[[nodiscard]] std::string Closure::toString() const {
14-
std::ostringstream oss;
15-
oss << "<closure: " << parameter << ">";
16-
return oss.str();
14+
std::ostringstream outputStringStream;
15+
outputStringStream << "<Closure: " << parameter << ">";
16+
return outputStringStream.str();
1717
}
1818

19-
std::ostream &operator<<(std::ostream &stream, const Closure &closure) {
20-
return stream << closure.toString();
19+
std::ostream &operator<<(std::ostream &outputStream, const Closure &closure) {
20+
return outputStream << closure.toString();
2121
}
2222

2323
[[nodiscard]] std::string List::toString() const {
24-
std::ostringstream stringStream;
25-
stringStream << "[";
24+
std::ostringstream outputStringStream;
25+
outputStringStream << "[";
2626
for (size_t i = 0; i < elements.size(); ++i) {
27-
stringStream << escapeString(elements[i].toString());
27+
outputStringStream << escapeString(elements[i].toString());
2828
if (i + 1 != elements.size()) {
29-
stringStream << ", ";
29+
outputStringStream << ", ";
3030
}
3131
}
32-
stringStream << "]";
33-
return stringStream.str();
32+
outputStringStream << "]";
33+
return outputStringStream.str();
3434
}
3535

3636
[[nodiscard]] std::string Value::toString() const {
@@ -51,27 +51,33 @@ namespace interpreter {
5151
}, *this);
5252
}
5353

54-
std::ostream &operator<<(std::ostream &stream, const Value &value) {
55-
return stream << value.toString();
54+
std::ostream &operator<<(std::ostream &outputStream, const Value &value) {
55+
return outputStream << value.toString();
5656
}
5757

58-
void ResultOptions::interpolate(const ResultOptions &resultOptions_) {
59-
sideEffects |= resultOptions_.sideEffects;
58+
void ResultOptions::interpolate(const ResultOptions &resultOptions) {
59+
sideEffects |= resultOptions.sideEffects;
6060
}
6161

62-
std::ostream &operator<<(std::ostream &stream, const List &list) {
63-
return stream << list.toString();
62+
std::ostream &operator<<(std::ostream &outputStream, const List &list) {
63+
return outputStream << list.toString();
6464
}
6565

6666
[[nodiscard]] std::string NativeFunction::toString() const {
67-
std::ostringstream stringStream;
68-
// TODO: Update the type name.
69-
stringStream << "<native_fn: " << name << " " << arity << ">";
70-
return stringStream.str();
67+
std::ostringstream outputStringStream;
68+
outputStringStream << "<NativeFunction: " << name << "(";
69+
if (arity == -1) {
70+
// Variadic-Function.
71+
outputStringStream << "...";
72+
} else {
73+
outputStringStream << arity;
74+
}
75+
outputStringStream << ")>";
76+
return outputStringStream.str();
7177
}
7278

73-
std::ostream &operator<<(std::ostream &stream, const NativeFunction &nativeFunction) {
74-
return stream << nativeFunction.toString();
79+
std::ostream &operator<<(std::ostream &outputStream, const NativeFunction &nativeFunction) {
80+
return outputStream << nativeFunction.toString();
7581
}
7682

7783
Thunk::Thunk(const frontend::Expression *expression, std::shared_ptr<Environment> environment,
@@ -136,7 +142,7 @@ namespace interpreter {
136142
std::vector<std::vector<std::string> > result;
137143
result.reserve(table.size());
138144
for (const auto &[name, thunk]: table) {
139-
std::string valueString = "<thunk: unevaluated>";
145+
std::string valueString = "<Thunk: unevaluated>";
140146
try {
141147
if (force) {
142148
const Value &value = thunk->force();
@@ -304,7 +310,7 @@ namespace interpreter {
304310
frames.back() = std::move(*resultantValue);
305311
continue;
306312
}
307-
// Resultant is a concrete Value (different from Function Value i.e. double, string).
313+
// Result is a concrete Value (different from Function Value i.e. double, string).
308314
// Pop the frame that produced it, and insert this Value as a thunk at the current index
309315
// so the previous Frame (if any) will consume it.
310316
Value concrete = std::move(*resultantValue);
@@ -322,9 +328,8 @@ namespace interpreter {
322328
}
323329
// There is a previous Frame; insert the concrete Value as a Thunk at the
324330
// current index so the previous Frame will consume it on the next iteration.
325-
// TODO: Why is it named injThunk
326-
auto injThunk = valueToThunk(concrete);
327-
workArguments.insert(workArguments.begin() + static_cast<int>(index), injThunk);
331+
auto thunk = valueToThunk(concrete);
332+
workArguments.insert(workArguments.begin() + static_cast<int>(index), thunk);
328333
// Do NOT advance index: the inserted thunk is at position index and will be consumed
329334
// by the previous frame on the next iteration.
330335
// Loop will continue with frames.back() being the previous frame.

0 commit comments

Comments
 (0)