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

Commit 375637b

Browse files
committed
chore: moved program structure from ast to standalone file
1 parent aa6c29e commit 375637b

14 files changed

Lines changed: 36 additions & 22 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_library(frontend STATIC
1212
${CMAKE_SOURCE_DIR}/src/frontend/token.cpp
1313
${CMAKE_SOURCE_DIR}/src/frontend/lexer.cpp
1414
${CMAKE_SOURCE_DIR}/src/frontend/ast.cpp
15+
${CMAKE_SOURCE_DIR}/src/frontend/program.cpp
1516
${CMAKE_SOURCE_DIR}/src/frontend/parser.cpp
1617
)
1718

‎include/lbd/frontend/ast.h‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ namespace frontend {
9898
friend std::ostream &operator<<(std::ostream &outputStream, const DefinitionAstNode &node);
9999
};
100100

101+
// NOTE: Owning and move-only, not copyable.
101102
struct AstNode {
102103
using NodeVariant = std::variant<Expression, DefinitionAstNode>;
103104

@@ -107,10 +108,4 @@ namespace frontend {
107108

108109
friend std::ostream &operator<<(std::ostream &outputStream, const AstNode &node);
109110
};
110-
111-
struct Program {
112-
std::vector<AstNode> nodes;
113-
114-
friend std::ostream &operator<<(std::ostream &outputStream, const Program &program);
115-
};
116111
}

‎include/lbd/frontend/location.h‎

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

33
#include <string>
4+
#include <iostream>
45

56
namespace frontend {
67
struct Location {

‎include/lbd/frontend/parser.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
#include <lbd/options.h>
44
#include <lbd/frontend/ast.h>
55
#include <lbd/frontend/token.h>
6+
#include <lbd/frontend/program.h>
67
#include <lbd/interpreter/type.h>
78
#include <vector>
89

910
namespace frontend {
1011
struct Parser {
12+
// TODO: Make this private
1113
Program program;
1214

1315
explicit Parser(const std::vector<token::Token> &tokens, context::Options options_ = {});
@@ -22,7 +24,8 @@ namespace frontend {
2224

2325
static IdentifierAstNode consumeIdentifier(const std::vector<token::Token> &tokens, size_t &index);
2426

25-
static interpreter::type::PrimitiveType consumePrimitiveTypeName(const std::vector<token::Token> &tokens, size_t &index);
27+
static interpreter::type::PrimitiveType consumePrimitiveTypeName(
28+
const std::vector<token::Token> &tokens, size_t &index);
2629

2730
static interpreter::type::Type parseType(const std::vector<token::Token> &tokens, size_t &index);
2831

‎include/lbd/frontend/program.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <lbd/frontend/ast.h>
4+
#include <vector>
5+
6+
namespace frontend {
7+
struct Program {
8+
std::vector<AstNode> astNodes;
9+
10+
friend std::ostream &operator<<(std::ostream &outputStream, const Program &program);
11+
};
12+
}

‎include/lbd/logs.h‎

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

33
#include <optional>
44
#include <lbd/frontend/location.h>
5-
#include <lbd/utils/term.h>
5+
#include <lbd/utils/terminal.h>
66
#include <lbd/exceptions.h>
77

88
namespace logs {

‎include/lbd/options.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "logs.h"
3+
#include <lbd/logs.h>
44

55
namespace context {
66
struct Options {

‎src/frontend/ast.cpp‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,4 @@ namespace frontend {
109109
node.print(outputStream, 0);
110110
return outputStream;
111111
}
112-
113-
std::ostream &operator<<(std::ostream &outputStream, const Program &program) {
114-
for (const AstNode &node: program.nodes) {
115-
outputStream << node;
116-
}
117-
return outputStream;
118-
}
119112
}

‎src/frontend/parser.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ namespace frontend {
159159
optionsValue.logger.debug(token);
160160
}
161161
}
162-
for (Parser parser(tokens, optionsValue); auto &node: parser.program.nodes) {
163-
nodes.emplace_back(std::move(node));
162+
for (Parser parser(tokens, optionsValue); auto &astNode: parser.program.astNodes) {
163+
nodes.emplace_back(std::move(astNode));
164164
}
165165
}
166166

0 commit comments

Comments
 (0)