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

Commit b339b3c

Browse files
committed
chore: refactor fe module
1 parent 80ac54f commit b339b3c

14 files changed

Lines changed: 334 additions & 324 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea/
22
cmake-build-debug/
3-
venv/
3+
venv/
4+
**__pycache__/

‎include/lbd/fe/ast.h‎

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,56 @@
77
#include <vector>
88

99
namespace fe::ast {
10-
struct IdenAstNode {
10+
struct IdentifierAstNode {
1111
std::string value;
12-
loc::Loc loc;
12+
loc::Loc location;
1313

14-
friend std::ostream &operator<<(std::ostream &os, const IdenAstNode &node);
14+
friend std::ostream &operator<<(std::ostream &stream, const IdentifierAstNode &node);
1515
};
1616

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

21-
friend std::ostream &operator<<(std::ostream &os, const StringAstNode &node);
21+
friend std::ostream &operator<<(std::ostream &stream, const StringAstNode &node);
2222
};
2323

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

28-
friend std::ostream &operator<<(std::ostream &os, const FloatAstNode &node);
28+
friend std::ostream &operator<<(std::ostream &stream, const FloatAstNode &node);
2929
};
3030

3131
struct Expression;
3232

3333
struct LambdaExpression {
34-
IdenAstNode arg;
35-
intp::types::Type arg_type;
36-
std::unique_ptr<Expression> expr;
37-
loc::Loc loc;
34+
IdentifierAstNode arg;
35+
intp::types::Type argumentType;
36+
std::unique_ptr<Expression> expression;
37+
loc::Loc location;
3838
/// Maybe, this is redundant
3939
/// TODO: Remove after checking
40-
intp::types::CompoundType lmd_expr_type; // Lambda Expression Type
40+
intp::types::CompoundType lambdaExpressionType;
4141

42-
void print(std::ostream &os, size_t indent) const;
42+
void print(std::ostream &stream, size_t indent) const;
4343

44-
friend std::ostream &operator<<(std::ostream &os, const LambdaExpression &l_expr);
44+
friend std::ostream &operator<<(std::ostream &stream, const LambdaExpression &lambdaExpression);
4545
};
4646

4747
struct FunctionApplication {
48-
IdenAstNode fn_name;
49-
std::vector<std::unique_ptr<Expression> > args;
50-
loc::Loc loc;
48+
IdentifierAstNode functionName;
49+
std::vector<std::unique_ptr<Expression> > arguments;
50+
loc::Loc location;
5151

52-
void print(std::ostream &os, size_t indent) const;
52+
void print(std::ostream &stream, size_t indent) const;
5353

54-
friend std::ostream &operator<<(std::ostream &os, const FunctionApplication &fn_apl);
54+
friend std::ostream &operator<<(std::ostream &stream, const FunctionApplication &functionApplication);
5555
};
5656

5757
struct Expression {
5858
using ExpressionVariant = std::variant<
59-
IdenAstNode,
59+
IdentifierAstNode,
6060
StringAstNode,
6161
FloatAstNode,
6262
LambdaExpression,
@@ -65,7 +65,7 @@ namespace fe::ast {
6565

6666
ExpressionVariant value;
6767

68-
explicit Expression(IdenAstNode value);
68+
explicit Expression(IdentifierAstNode value);
6969

7070
explicit Expression(StringAstNode value);
7171

@@ -75,11 +75,11 @@ namespace fe::ast {
7575

7676
explicit Expression(FunctionApplication value);
7777

78-
void print(std::ostream &os, size_t indent) const;
78+
void print(std::ostream &stream, size_t indent) const;
7979

80-
friend std::ostream &operator<<(std::ostream &os, const Expression &expr);
80+
friend std::ostream &operator<<(std::ostream &stream, const Expression &expression);
8181

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

8484
Expression(const Expression &) = delete;
8585

@@ -90,30 +90,30 @@ namespace fe::ast {
9090
Expression &operator=(Expression &&) noexcept = default;
9191
};
9292

93-
struct DefAstNode {
94-
IdenAstNode def_name;
95-
intp::types::Type typ;
96-
Expression expr;
97-
loc::Loc loc;
93+
struct DefinitionAstNode {
94+
IdentifierAstNode definitionName;
95+
intp::types::Type definitionType;
96+
Expression expression;
97+
loc::Loc location;
9898

99-
void print(std::ostream &os, size_t indent) const;
99+
void print(std::ostream &stream, size_t indent) const;
100100

101-
friend std::ostream &operator<<(std::ostream &os, const DefAstNode &node);
101+
friend std::ostream &operator<<(std::ostream &stream, const DefinitionAstNode &node);
102102
};
103103

104104
struct AstNode {
105-
using NodeVariant = std::variant<Expression, DefAstNode>;
105+
using NodeVariant = std::variant<Expression, DefinitionAstNode>;
106106

107107
NodeVariant value;
108108

109-
void print(std::ostream &os, size_t indent) const;
109+
void print(std::ostream &stream, size_t indent) const;
110110

111-
friend std::ostream &operator<<(std::ostream &os, const AstNode &node);
111+
friend std::ostream &operator<<(std::ostream &stream, const AstNode &node);
112112
};
113113

114114
struct Program {
115115
std::vector<AstNode> nodes;
116116

117-
friend std::ostream &operator<<(std::ostream &os, const Program &p);
117+
friend std::ostream &operator<<(std::ostream &stream, const Program &program);
118118
};
119119
}

‎include/lbd/fe/lexer.h‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,28 @@ namespace fe::lexer {
1414
};
1515

1616
struct Lexer {
17-
std::string source;
18-
size_t pos = 0;
19-
size_t row = 1;
20-
size_t col = 1;
21-
std::string filepath;
22-
2317
Lexer(const std::string &filepath, FromFile, options::Options options_ = {});
2418

2519
Lexer(std::string str, FromRepl, options::Options options_ = {});
2620

27-
token::Token next_token();
21+
token::Token nextToken();
2822

29-
std::vector<token::Token> lex_all();
23+
// TODO: Find a better name for this.
24+
std::vector<token::Token> lexAll();
3025

3126
private:
27+
std::string source;
28+
size_t position = 0;
29+
size_t row = 1;
30+
size_t col = 1;
31+
std::string filepath;
32+
3233
[[nodiscard]] char peek() const;
3334

3435
char get();
3536

36-
[[nodiscard]] bool is_eof() const;
37+
[[nodiscard]] bool isEof() const;
3738

38-
[[nodiscard]] loc::Loc get_cur_loc() const;
39+
[[nodiscard]] loc::Loc getCurrentLocation() const;
3940
};
4041
}

‎include/lbd/fe/loc.h‎

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

3-
#include <iostream>
43
#include <string>
54

65
namespace fe::loc {
76
struct Loc {
7+
// TODO: Maybe make these members private.
88
size_t row;
99
size_t col;
1010
std::string filepath;
1111

1212
Loc(size_t row, size_t col, std::string filepath);
1313

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

‎include/lbd/fe/parser.h‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ namespace fe::parser {
1515
private:
1616
// TODO: Add checks for T to be a variant of fe::token::TokenType
1717
template<typename T>
18-
static void assert_token(const std::vector<token::Token> &tokens, size_t &i);
18+
static void assertToken(const std::vector<token::Token> &tokens, size_t &index);
1919

2020
template<typename T>
21-
static void assert_n_eat(const std::vector<token::Token> &tokens, size_t &i);
21+
static void assertAndConsume(const std::vector<token::Token> &tokens, size_t &index);
2222

23-
static ast::IdenAstNode eat_iden(const std::vector<token::Token> &tokens, size_t &i);
23+
static ast::IdentifierAstNode consumeIdentifier(const std::vector<token::Token> &tokens, size_t &index);
2424

25-
static intp::types::PrimitiveType eat_primitive_type_name(const std::vector<token::Token> &tokens, size_t &i);
25+
static intp::types::PrimitiveType consumePrimitiveTypeName(const std::vector<token::Token> &tokens, size_t &index);
2626

27-
static intp::types::Type parse_type(const std::vector<token::Token> &tokens, size_t &i);
27+
static intp::types::Type parseType(const std::vector<token::Token> &tokens, size_t &index);
2828

29-
static ast::Expression parse_expression(const std::vector<token::Token> &tokens, size_t &i);
29+
static ast::Expression parseExpression(const std::vector<token::Token> &tokens, size_t &index);
3030

31-
static ast::LambdaExpression parse_lambda_expression(const std::vector<token::Token> &tokens, size_t &i);
31+
static ast::LambdaExpression parseLambdaExpression(const std::vector<token::Token> &tokens, size_t &index);
3232

33-
static ast::FunctionApplication parse_function_application(const std::vector<token::Token> &tokens, size_t &i);
33+
static ast::FunctionApplication parseFunctionApplication(const std::vector<token::Token> &tokens, size_t &index);
3434

35-
static ast::DefAstNode parse_def_ast_node(const std::vector<token::Token> &tokens, size_t &i);
35+
static ast::DefinitionAstNode parseDefinitionAstNode(const std::vector<token::Token> &tokens, size_t &index);
3636

37-
static std::vector<ast::AstNode> build_ast(const std::vector<token::Token> &tokens, size_t &i);
37+
static std::vector<ast::AstNode> build(const std::vector<token::Token> &tokens, size_t &index);
3838
};
3939
}

‎include/lbd/fe/token.h‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <variant>
66

77
namespace fe::token {
8-
struct Iden {
8+
struct Identifier {
99
std::string value;
1010
};
1111

@@ -32,41 +32,41 @@ namespace fe::token {
3232
struct Dot {
3333
};
3434

35-
struct OpenParen {
35+
struct OpenParenthesis {
3636
};
3737

38-
struct CloseParen {
38+
struct CloseParenthesis {
3939
};
4040

4141
struct Eof {
4242
};
4343

4444
using TokenType = std::variant<
4545
std::monostate, // analogous to NULL
46-
Iden,
46+
Identifier,
4747
String,
4848
Colon,
4949
Equal,
5050
Float,
5151
Arrow,
5252
BackwardSlash,
5353
Dot,
54-
OpenParen,
55-
CloseParen,
54+
OpenParenthesis,
55+
CloseParenthesis,
5656
Eof
5757
>;
5858

5959
struct Token {
60-
loc::Loc loc;
61-
TokenType typ;
60+
loc::Loc location;
61+
TokenType tokenType;
6262

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

65-
friend std::ostream &operator<<(std::ostream &os, const Token &token);
65+
friend std::ostream &operator<<(std::ostream &stream, const Token &token);
6666

67-
[[nodiscard]] std::string to_string() const;
67+
[[nodiscard]] std::string toString() const;
6868
};
6969

7070
template<typename T>
71-
std::string to_string();
71+
std::string toString();
7272
}

0 commit comments

Comments
 (0)