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

Commit 2c5b953

Browse files
committed
chore: refactor frontend module
1 parent 559fac9 commit 2c5b953

19 files changed

Lines changed: 201 additions & 201 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ include_directories(
77
${CMAKE_SOURCE_DIR}/include
88
)
99

10-
add_library(fe STATIC
11-
${CMAKE_SOURCE_DIR}/src/fe/loc.cpp
12-
${CMAKE_SOURCE_DIR}/src/fe/token.cpp
13-
${CMAKE_SOURCE_DIR}/src/fe/lexer.cpp
14-
${CMAKE_SOURCE_DIR}/src/fe/ast.cpp
15-
${CMAKE_SOURCE_DIR}/src/fe/parser.cpp
10+
add_library(frontend STATIC
11+
${CMAKE_SOURCE_DIR}/src/frontend/location.cpp
12+
${CMAKE_SOURCE_DIR}/src/frontend/token.cpp
13+
${CMAKE_SOURCE_DIR}/src/frontend/lexer.cpp
14+
${CMAKE_SOURCE_DIR}/src/frontend/ast.cpp
15+
${CMAKE_SOURCE_DIR}/src/frontend/parser.cpp
1616
)
1717

1818
add_library(intp STATIC
@@ -30,8 +30,8 @@ add_executable(lbd
3030
${CMAKE_SOURCE_DIR}/src/repl.cpp
3131
)
3232

33-
target_link_libraries(lbd PRIVATE fe intp -static)
33+
target_link_libraries(lbd PRIVATE frontend intp -static)
3434

3535
# TODO: Add build tests
3636
# EXAMPLE: add_executable(lexer_test ../tests/lexer_test.cc)
37-
# target_link_libraries(lexer_test PRIVATE fe)
37+
# target_link_libraries(lexer_test PRIVATE frontend)

‎include/lbd/fe/parser.h‎

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
#pragma once
22

3-
#include <lbd/fe/loc.h>
3+
#include <lbd/frontend/location.h>
44
#include <lbd/intp/types.h>
55
#include <string>
66
#include <variant>
77
#include <vector>
88

9-
namespace fe::ast {
9+
namespace frontend {
1010
struct IdentifierAstNode {
1111
std::string value;
12-
loc::Loc location;
12+
Location location;
1313

1414
friend std::ostream &operator<<(std::ostream &stream, const IdentifierAstNode &node);
1515
};
1616

1717
struct StringAstNode {
1818
std::string value;
19-
loc::Loc location;
19+
Location location;
2020

2121
friend std::ostream &operator<<(std::ostream &stream, const StringAstNode &node);
2222
};
2323

2424
struct FloatAstNode {
2525
double value;
26-
loc::Loc location;
26+
Location location;
2727

2828
friend std::ostream &operator<<(std::ostream &stream, const FloatAstNode &node);
2929
};
3030

3131
struct Expression;
3232

3333
struct LambdaExpression {
34-
IdentifierAstNode arg;
34+
IdentifierAstNode argument;
3535
intp::types::Type argumentType;
3636
std::unique_ptr<Expression> expression;
37-
loc::Loc location;
38-
/// Maybe, this is redundant
39-
/// TODO: Remove after checking
37+
Location location;
38+
/// Maybe, this is redundant.
39+
// TODO: Remove after checking.
4040
intp::types::CompoundType lambdaExpressionType;
4141

4242
void print(std::ostream &stream, size_t indent) const;
@@ -47,7 +47,7 @@ namespace fe::ast {
4747
struct FunctionApplication {
4848
IdentifierAstNode functionName;
4949
std::vector<std::unique_ptr<Expression> > arguments;
50-
loc::Loc location;
50+
Location location;
5151

5252
void print(std::ostream &stream, size_t indent) const;
5353

@@ -79,7 +79,7 @@ namespace fe::ast {
7979

8080
friend std::ostream &operator<<(std::ostream &stream, const Expression &expression);
8181

82-
[[nodiscard]] loc::Loc getLocation() const;
82+
[[nodiscard]] Location getLocation() const;
8383

8484
Expression(const Expression &) = delete;
8585

@@ -94,7 +94,7 @@ namespace fe::ast {
9494
IdentifierAstNode definitionName;
9595
intp::types::Type definitionType;
9696
Expression expression;
97-
loc::Loc location;
97+
Location location;
9898

9999
void print(std::ostream &stream, size_t indent) const;
100100

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#pragma once
22

3-
#include <lbd/fe/loc.h>
4-
#include <lbd/fe/token.h>
3+
#include <lbd/frontend/location.h>
4+
#include <lbd/frontend/token.h>
55
#include <lbd/options.h>
66
#include <string>
77
#include <vector>
88

9-
namespace fe::lexer {
9+
namespace frontend {
1010
struct FromFile {
1111
};
1212

1313
struct FromRepl {
1414
};
1515

1616
struct Lexer {
17+
// TODO: Make factory instead of these constructors.
1718
Lexer(const std::string &filepath, FromFile, options::Options options_ = {});
1819

1920
Lexer(std::string str, FromRepl, options::Options options_ = {});
@@ -36,6 +37,6 @@ namespace fe::lexer {
3637

3738
[[nodiscard]] bool isEof() const;
3839

39-
[[nodiscard]] loc::Loc getCurrentLocation() const;
40+
[[nodiscard]] Location getCurrentLocation() const;
4041
};
4142
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
#include <string>
44

5-
namespace fe::loc {
6-
struct Loc {
5+
namespace frontend {
6+
struct Location {
77
// TODO: Maybe make these members private.
88
size_t row;
99
size_t col;
1010
std::string filepath;
1111

12-
Loc(size_t row, size_t col, std::string filepath);
12+
Location(size_t row, size_t col, std::string filepath);
1313

14-
friend std::ostream &operator<<(std::ostream &stream, const Loc &location);
14+
friend std::ostream &operator<<(std::ostream &stream, const Location &location);
1515
};
1616
}

‎include/lbd/frontend/parser.h‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
3+
#include <lbd/options.h>
4+
#include <lbd/frontend/ast.h>
5+
#include <lbd/frontend/token.h>
6+
#include <lbd/intp/types.h>
7+
#include <vector>
8+
9+
namespace frontend {
10+
struct Parser {
11+
Program program;
12+
13+
explicit Parser(const std::vector<token::Token> &tokens, options::Options options_ = {});
14+
15+
private:
16+
// TODO: Add checks for T to be a variant of frontend::TokenType
17+
template<typename T>
18+
static void assertToken(const std::vector<token::Token> &tokens, size_t &index);
19+
20+
template<typename T>
21+
static void assertAndConsume(const std::vector<token::Token> &tokens, size_t &index);
22+
23+
static IdentifierAstNode consumeIdentifier(const std::vector<token::Token> &tokens, size_t &index);
24+
25+
static intp::types::PrimitiveType consumePrimitiveTypeName(const std::vector<token::Token> &tokens, size_t &index);
26+
27+
static intp::types::Type parseType(const std::vector<token::Token> &tokens, size_t &index);
28+
29+
static Expression parseExpression(const std::vector<token::Token> &tokens, size_t &index);
30+
31+
static LambdaExpression parseLambdaExpression(const std::vector<token::Token> &tokens, size_t &index);
32+
33+
static FunctionApplication parseFunctionApplication(const std::vector<token::Token> &tokens, size_t &index);
34+
35+
static DefinitionAstNode parseDefinitionAstNode(const std::vector<token::Token> &tokens, size_t &index);
36+
37+
static std::vector<AstNode> build(const std::vector<token::Token> &tokens, size_t &index);
38+
};
39+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#pragma once
22

3-
#include <lbd/fe/loc.h>
3+
#include <lbd/frontend/location.h>
44
#include <string>
55
#include <variant>
66

7-
namespace fe::token {
7+
namespace frontend::token {
88
struct Identifier {
99
std::string value;
1010
};
@@ -57,10 +57,10 @@ namespace fe::token {
5757
>;
5858

5959
struct Token {
60-
loc::Loc location;
60+
Location location;
6161
TokenType tokenType;
6262

63-
Token(TokenType tokenType, loc::Loc location);
63+
Token(TokenType tokenType, Location location);
6464

6565
friend std::ostream &operator<<(std::ostream &stream, const Token &token);
6666

‎include/lbd/intp/interpreter.h‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <optional>
55
#include <string>
66
#include <variant>
7-
#include <lbd/fe/ast.h>
8-
#include <lbd/fe/parser.h>
7+
#include <lbd/frontend/ast.h>
8+
#include <lbd/frontend/parser.h>
99
#include <lbd/options.h>
1010

1111
namespace intp::interp {
@@ -18,7 +18,7 @@ namespace intp::interp {
1818
/// Runtime representation of Lambda-Expression.
1919
struct Closure {
2020
std::string parameter;
21-
const fe::ast::Expression *body; /// Non-owning, read-only AST pointer.
21+
const frontend::Expression *body; /// Non-owning, read-only AST pointer.
2222
std::shared_ptr<Environment> environment; /// Environment at the time of Lambda-Expression creation.
2323

2424
[[nodiscard]] std::string toString() const;
@@ -73,26 +73,26 @@ namespace intp::interp {
7373
/// 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 *expression = nullptr; /// Non-owning, read-only AST pointer.
77-
std::unique_ptr<fe::ast::Expression> owned; /// Owning storage (when needed) (primarily in REPL).
76+
const frontend::Expression *expression = nullptr; /// Non-owning, read-only AST pointer.
77+
std::unique_ptr<frontend::Expression> owned; /// Owning storage (when needed) (primarily in REPL).
7878
std::shared_ptr<Environment> environment; /// Environment for evaluating Expression.
79-
std::optional<fe::loc::Loc> origin = std::nullopt;
79+
std::optional<frontend::Location> origin = std::nullopt;
8080

8181
Thunk() = default;
8282

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

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

8989
/// Sets Thunk's fields after construction.
9090
/// Allows for recursive reference.
91-
void set(const fe::ast::Expression *expression_, std::shared_ptr<Environment> environment_,
92-
std::optional<fe::loc::Loc> origin_ = std::nullopt);
91+
void set(const frontend::Expression *expression_, std::shared_ptr<Environment> environment_,
92+
std::optional<frontend::Location> origin_ = std::nullopt);
9393

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

9898
struct Environment : std::enable_shared_from_this<Environment> {
@@ -108,11 +108,11 @@ namespace intp::interp {
108108
std::vector<std::vector<std::string> > toVector(bool force) const;
109109
};
110110

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

113113
Value applyFunctionApplication(Value functionName, const std::vector<std::shared_ptr<Thunk> > &arguments,
114114
const std::shared_ptr<Environment> &callSiteEnvironment,
115-
const std::optional<fe::loc::Loc> &callLocation = std::nullopt);
115+
const std::optional<frontend::Location> &callLocation = std::nullopt);
116116

117117
/// Program Driver.
118118
struct Result {
@@ -121,7 +121,7 @@ namespace intp::interp {
121121
ResultOptions options = {};
122122
};
123123

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

127127
/// Add builtins Native Functions into Environment.

‎include/lbd/logs.h‎

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

33
#include <optional>
4-
#include <lbd/fe/loc.h>
4+
#include <lbd/frontend/location.h>
55
#include <lbd/utils/term.h>
66
#include <lbd/exceptions.h>
77

@@ -21,7 +21,7 @@ namespace logs {
2121
}
2222

2323
template<typename... Arguments>
24-
[[noreturn]] void error(const std::optional<fe::loc::Loc> &location, Arguments &&... arguments) const {
24+
[[noreturn]] void error(const std::optional<frontend::Location> &location, Arguments &&... arguments) const {
2525
if (useColor) {
2626
std::cerr << colors::RED;
2727
}

‎include/lbd/options.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "logs.h"
44

5+
// TODO: Choose better namespace for this. Maybe global, context.
56
namespace options {
67
struct Options {
78
bool ownExpression = false; /// Owning Expression inside Thunk. Turned on for REPL

0 commit comments

Comments
 (0)