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

Commit cde832d

Browse files
committed
chore: refactor intp module
1 parent b339b3c commit cde832d

15 files changed

Lines changed: 581 additions & 559 deletions

‎include/lbd/intp/builtin-modules/builtin_module_core.h‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
#include <lbd/intp/builtins.h>
44

55
namespace intp::interp::builtins {
6-
NativeFunction make_print();
6+
NativeFunction makePrint();
77

8-
NativeFunction make_add();
8+
NativeFunction makeAdd();
99

10-
NativeFunction make_sub();
10+
NativeFunction makeSub();
1111

12-
NativeFunction make_mul();
12+
NativeFunction makeMul();
1313

14-
NativeFunction make_cmp();
14+
NativeFunction makeCmp();
1515

16-
NativeFunction make_if_zero();
16+
NativeFunction makeIfZero();
1717

18-
NativeFunction make_parse_float();
18+
NativeFunction makeParseFloat();
1919
}

‎include/lbd/intp/builtin-modules/builtin_module_io.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#include <lbd/intp/builtins.h>
44

55
namespace intp::interp::builtins {
6-
NativeFunction make_slurp_file();
6+
NativeFunction makeSlurpFile();
77

8-
NativeFunction make_lines();
8+
NativeFunction makeLines();
99

10-
NativeFunction make_split();
10+
NativeFunction makeSplit();
1111
}

‎include/lbd/intp/builtin-modules/builtin_module_list.h‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
#include <lbd/intp/builtins.h>
44

55
namespace intp::interp::builtins {
6-
NativeFunction make_list();
6+
NativeFunction makeList();
77

8-
NativeFunction make_list_size();
8+
NativeFunction makeListSize();
99

10-
NativeFunction make_list_get();
10+
NativeFunction makeListGet();
1111

12-
NativeFunction make_list_remove();
12+
NativeFunction makeListRemove();
1313

14-
NativeFunction make_list_append();
14+
NativeFunction makeListAppend();
1515

16-
NativeFunction make_map();
16+
NativeFunction makeMap();
1717

18-
NativeFunction make_transpose();
18+
NativeFunction makeTranspose();
1919

20-
NativeFunction make_sort();
20+
NativeFunction makeSort();
2121

22-
NativeFunction make_zip();
22+
NativeFunction makeZip();
2323

24-
NativeFunction make_foldr();
24+
NativeFunction makeFoldRight();
2525
}

‎include/lbd/intp/builtins.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <lbd/options.h>
55

66
namespace intp::interp::builtins {
7-
extern options::Options options_v;
7+
extern options::Options optionsValue;
88

9-
std::vector<NativeFunction> get_builtins(options::Options options_ = {});
9+
std::vector<NativeFunction> getBuiltins(options::Options options_ = {});
1010
}

‎include/lbd/intp/interpreter.h‎

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@
1111
namespace intp::interp {
1212
struct NativeFunction;
1313
struct Thunk;
14-
struct Env;
14+
struct Environment;
1515
struct List;
1616
struct Value;
1717

18-
/// Runtime representation of Lambda Expression
18+
/// Runtime representation of Lambda-Expression.
1919
struct Closure {
20-
std::string param;
21-
const fe::ast::Expression *body; /// Non-owning, read-only AST pointer
22-
std::shared_ptr<Env> env; /// Environment at the time of Lambda Expression creation
20+
std::string parameter;
21+
const fe::ast::Expression *body; /// Non-owning, read-only AST pointer.
22+
std::shared_ptr<Environment> environment; /// Environment at the time of Lambda-Expression creation.
2323

24-
[[nodiscard]] std::string to_string() const;
24+
[[nodiscard]] std::string toString() const;
2525

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

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

32-
[[nodiscard]] std::string to_string() const;
32+
[[nodiscard]] std::string toString() const;
3333

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

3737
using ValueVariant = std::variant<
@@ -43,87 +43,87 @@ namespace intp::interp {
4343
>;
4444

4545
struct Value : ValueVariant {
46-
using ValueVariant::ValueVariant; // inherit constructors
46+
using ValueVariant::ValueVariant; // Inherit constructors.
4747

48-
/// Pretty print a runtime value for REPL/diagnostics
49-
[[nodiscard]] std::string to_string() const;
48+
/// Pretty print a runtime value for REPL/diagnostics.
49+
[[nodiscard]] std::string toString() const;
5050

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

5454
struct ResultOptions {
55-
bool side_effects = false;
55+
bool sideEffects = false;
5656

57-
void interpolate(const ResultOptions &result_options_);
57+
void interpolate(const ResultOptions &resultOptions_);
5858
};
5959

6060
struct NativeFunction {
61-
using Impl = std::function<std::pair<Value, ResultOptions>(
62-
const std::vector<std::shared_ptr<Thunk> > &, const std::shared_ptr<Env> &)>;
61+
using Implementation = std::function<std::pair<Value, ResultOptions>(
62+
const std::vector<std::shared_ptr<Thunk> > &, const std::shared_ptr<Environment> &)>;
6363

6464
int arity;
6565
std::string name;
66-
Impl impl;
66+
Implementation implementation;
6767

68-
[[nodiscard]] std::string to_string() const;
68+
[[nodiscard]] std::string toString() const;
6969

70-
friend std::ostream &operator<<(std::ostream &os, const NativeFunction &native_fn);
70+
friend std::ostream &operator<<(std::ostream &stream, const NativeFunction &nativeFunction);
7171
};
7272

73-
/// Lazy Thunk (call-by-need)
73+
/// Lazy-Thunk (call-by-need).
7474
struct Thunk : std::enable_shared_from_this<Thunk> {
7575
mutable std::optional<Value> cached;
76-
const fe::ast::Expression *expr = nullptr; /// Non-owning, read-only AST pointer
77-
std::unique_ptr<fe::ast::Expression> owned; /// Owning storage (when needed) (primarily in REPL)
78-
std::shared_ptr<Env> env; /// Environment for evaluating Expression
76+
const fe::ast::Expression *expression = nullptr; /// Non-owning, read-only AST pointer.
77+
std::unique_ptr<fe::ast::Expression> owned; /// Owning storage (when needed) (primarily in REPL).
78+
std::shared_ptr<Environment> environment; /// Environment for evaluating Expression.
7979
std::optional<fe::loc::Loc> origin = std::nullopt;
8080

8181
Thunk() = default;
8282

83-
Thunk(const fe::ast::Expression *expr, std::shared_ptr<Env> env,
83+
Thunk(const fe::ast::Expression *expression, std::shared_ptr<Environment> environment,
8484
std::optional<fe::loc::Loc> origin = std::nullopt);
8585

86-
/// Force computation on Thunk and return a const reference to Value
87-
const Value &force() const; /// Marked const as cached is mutable
86+
/// Force computation on Thunk and return a const reference to Value.
87+
const Value &force() const; /// Marked const as cached is mutable.
8888

89-
/// Sets Thunk's fields after construction
90-
/// Allows for recursive reference
91-
void set(const fe::ast::Expression *expr_, std::shared_ptr<Env> env_,
89+
/// Sets Thunk's fields after construction.
90+
/// Allows for recursive reference.
91+
void set(const fe::ast::Expression *expression_, std::shared_ptr<Environment> environment_,
9292
std::optional<fe::loc::Loc> origin_ = std::nullopt);
9393

94-
void set_owned(fe::ast::Expression expr_, std::shared_ptr<Env> env_,
94+
void set_owned(fe::ast::Expression expression_, std::shared_ptr<Environment> environment_,
9595
std::optional<fe::loc::Loc> origin_ = std::nullopt);
9696
};
9797

98-
struct Env : std::enable_shared_from_this<Env> {
98+
struct Environment : std::enable_shared_from_this<Environment> {
9999
std::unordered_map<std::string, std::shared_ptr<Thunk> > table;
100-
std::shared_ptr<Env> parent;
100+
std::shared_ptr<Environment> parent;
101101

102-
explicit Env(std::shared_ptr<Env> parent = nullptr);
102+
explicit Environment(std::shared_ptr<Environment> parent = nullptr);
103103

104104
std::shared_ptr<Thunk> lookup(const std::string &name) const;
105105

106106
void bind(const std::string &name, std::shared_ptr<Thunk> thunk);
107107

108-
std::vector<std::vector<std::string> > to_vector(bool force) const;
108+
std::vector<std::vector<std::string> > toVector(bool force) const;
109109
};
110110

111-
Value eval_expr(const fe::ast::Expression &expr, std::shared_ptr<Env> env);
111+
Value evalExpression(const fe::ast::Expression &expression, std::shared_ptr<Environment> environment);
112112

113-
Value apply_fn_apl(Value fn_value, const std::vector<std::shared_ptr<Thunk> > &args,
114-
const std::shared_ptr<Env> &call_site_env,
115-
const std::optional<fe::loc::Loc> &call_loc = std::nullopt);
113+
Value applyFunctionApplication(Value functionName, const std::vector<std::shared_ptr<Thunk> > &arguments,
114+
const std::shared_ptr<Environment> &callSiteEnvironment,
115+
const std::optional<fe::loc::Loc> &callLocation = std::nullopt);
116116

117-
/// Program Driver
117+
/// Program Driver.
118118
struct Result {
119-
std::shared_ptr<Env> global_env;
119+
std::shared_ptr<Environment> globalEnvironment;
120120
Value value;
121121
ResultOptions options = {};
122122
};
123123

124-
Result interpret(fe::ast::Program &program, std::optional<std::shared_ptr<Env> > global_env = std::nullopt,
124+
Result interpret(fe::ast::Program &program, std::optional<std::shared_ptr<Environment> > globalEnvironment = std::nullopt,
125125
options::Options options_ = {});
126126

127-
/// Add builtins Native Functions into Environment
128-
void install_builtins(const std::shared_ptr<Env> &env);
127+
/// Add builtins Native Functions into Environment.
128+
void installBuiltins(const std::shared_ptr<Environment> &environment);
129129
}

‎include/lbd/intp/types.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@ namespace intp::types {
88
struct PrimitiveType {
99
enum class Type {
1010
Float,
11-
Str,
11+
String,
1212
Custom,
1313
Any,
1414
};
1515

1616
Type type;
1717
std::string custom;
1818

19-
friend std::ostream &operator<<(std::ostream &os, const PrimitiveType &typ);
19+
friend std::ostream &operator<<(std::ostream &stream, const PrimitiveType &type);
2020
};
2121

2222
struct CompoundType;
2323

2424
using Type = std::variant<PrimitiveType, std::shared_ptr<CompoundType> >;
2525

2626
struct CompoundType {
27-
PrimitiveType l_type;
28-
Type r_type;
27+
PrimitiveType leftType;
28+
Type rightType;
2929

30-
friend std::ostream &operator<<(std::ostream &os, const CompoundType &typ);
30+
friend std::ostream &operator<<(std::ostream &stream, const CompoundType &type);
3131
};
3232

33-
std::ostream &operator<<(std::ostream &os, const Type &typ);
33+
std::ostream &operator<<(std::ostream &stream, const Type &type);
3434
}

‎include/lbd/options.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
namespace options {
55
struct Options {
6-
bool own_expr = false; /// Owning Expression inside Thunk. Turned on for REPL
7-
bool force_on_env_dump = false;
6+
bool ownExpression = false; /// Owning Expression inside Thunk. Turned on for REPL
7+
bool forceOnEnvironmentDump = false;
88
bool debug = false;
99
logs::Logger logger;
1010
};

‎src/fe/parser.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace fe::parser {
4747
return intp::types::PrimitiveType{intp::types::PrimitiveType::Type::Float};
4848
}
4949
if (value == "Str") {
50-
return intp::types::PrimitiveType{intp::types::PrimitiveType::Type::Str};
50+
return intp::types::PrimitiveType{intp::types::PrimitiveType::Type::String};
5151
}
5252
if (value == "Any") {
5353
return intp::types::PrimitiveType{intp::types::PrimitiveType::Type::Any};
@@ -69,8 +69,8 @@ namespace fe::parser {
6969
intp::types::Type currentType = types.back();
7070
for (int typeIndex = static_cast<int>(types.size()) - 2; typeIndex >= 0; --typeIndex) {
7171
auto nextCompoundType = std::make_shared<intp::types::CompoundType>();
72-
nextCompoundType->l_type = types[typeIndex];
73-
nextCompoundType->r_type = currentType;
72+
nextCompoundType->leftType = types[typeIndex];
73+
nextCompoundType->rightType = currentType;
7474
currentType = nextCompoundType;
7575
}
7676
return currentType;

0 commit comments

Comments
 (0)