Skip to content

Commit 256cd3a

Browse files
committed
refactor(core): introduce "pseu" namespaces and integrate JH-Toolkit
Introduce structured namespaces: * pseu::ast * pseu::lexer * pseu::ir * pseu::codegen Add namespace detail for main-related implementation Move most previously global-scope objects into appropriate namespaces Establish clearer module boundaries and encapsulation Remove obsolete TokenArray abstraction Prepare infrastructure for external toolkit usage: * Integrate JH-Toolkit via FetchContent in CMake * Configure build to use Release mode to disable toolkit tests/examples * Link compiler target against jh-toolkit This refactor continues the transition toward stricter compile-time structure and cleaner ownership boundaries, laying groundwork for subsequent behavior and performance optimizations using JH-Toolkit.
1 parent 1ce7b1b commit 256cd3a

10 files changed

Lines changed: 1017 additions & 979 deletions

File tree

CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
cmake_minimum_required(VERSION 3.16)
22
project(CompilerForNASMCpp LANGUAGES CXX)
33

4+
include(FetchContent)
5+
6+
# ---- JH-Toolkit configuration (must be BEFORE FetchContent_MakeAvailable) ----
7+
set(CMAKE_BUILD_TYPE Release CACHE STRING "" FORCE)
8+
# use Release to invalidate tests/examples in JH-Toolkit
9+
10+
FetchContent_Declare(
11+
JH_Toolkit
12+
GIT_REPOSITORY https://github.com/JeongHan-Bae/JH-Toolkit.git
13+
GIT_TAG 1.4.0-dev
14+
)
15+
16+
FetchContent_MakeAvailable(JH_Toolkit)
17+
418
set(CMAKE_CXX_STANDARD 20)
519
set(CMAKE_CXX_STANDARD_REQUIRED ON)
620

@@ -33,4 +47,6 @@ add_executable(compiler
3347

3448
target_include_directories(compiler PRIVATE ${INC_DIR} ${CMAKE_CURRENT_BINARY_DIR})
3549

50+
target_link_libraries(compiler PRIVATE jh-toolkit)
51+
3652
target_compile_options(compiler PRIVATE -fno-rtti)

include/ast.hpp

Lines changed: 98 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,87 @@
11
#pragma once
2+
23
#include <memory>
34
#include <string>
45
#include <vector>
56
#include <variant>
67
#include "tokens.hpp"
78

9+
namespace pseu::ast {
810

911
/* ======================
1012
* Literal Nodes
1113
* ====================== */
12-
struct NumberNode;
13-
struct StringLiteralNode;
14-
struct IdentifierNode;
15-
struct BinOpNode;
16-
struct Statement;
17-
struct Condition;
18-
struct IfStatement;
19-
struct WhileStatement;
20-
struct PrintStatement;
21-
struct Assignment;
22-
struct Declaration;
23-
24-
using ASTNode = std::variant<
25-
NumberNode,
26-
StringLiteralNode,
27-
IdentifierNode,
28-
BinOpNode,
29-
Statement,
30-
Condition,
31-
IfStatement,
32-
WhileStatement,
33-
PrintStatement,
34-
Assignment,
35-
Declaration
36-
>;
37-
38-
struct NumberNode final
39-
{
40-
Token tok;
41-
explicit NumberNode(Token t) : tok(std::move(t)) {}
42-
[[nodiscard]] std::string getValue() const { return tok.value; }
43-
};
44-
45-
struct IdentifierNode final
46-
{
47-
Token tok;
48-
explicit IdentifierNode(Token t) : tok(std::move(t)) {}
49-
[[nodiscard]] std::string getValue() const { return tok.value; }
50-
};
51-
52-
53-
struct StringLiteralNode final
54-
{
55-
Token tok;
56-
explicit StringLiteralNode(Token t) : tok(std::move(t)) {}
57-
[[nodiscard]] std::string getValue() const { return tok.value; }
58-
};
14+
struct NumberNode;
15+
struct StringLiteralNode;
16+
struct IdentifierNode;
17+
struct BinOpNode;
18+
struct Statement;
19+
struct Condition;
20+
struct IfStatement;
21+
struct WhileStatement;
22+
struct PrintStatement;
23+
struct Assignment;
24+
struct Declaration;
25+
26+
using ASTNode = std::variant<
27+
NumberNode,
28+
StringLiteralNode,
29+
IdentifierNode,
30+
BinOpNode,
31+
Statement,
32+
Condition,
33+
IfStatement,
34+
WhileStatement,
35+
PrintStatement,
36+
Assignment,
37+
Declaration
38+
>;
39+
40+
struct NumberNode final {
41+
lexer::Token tok;
42+
43+
explicit NumberNode(lexer::Token t) : tok(std::move(t)) {}
44+
45+
[[nodiscard]] std::string getValue() const { return tok.value; }
46+
};
47+
48+
struct IdentifierNode final {
49+
lexer::Token tok;
50+
51+
explicit IdentifierNode(lexer::Token t) : tok(std::move(t)) {}
52+
53+
[[nodiscard]] std::string getValue() const { return tok.value; }
54+
};
55+
56+
57+
struct StringLiteralNode final {
58+
lexer::Token tok;
59+
60+
explicit StringLiteralNode(lexer::Token t) : tok(std::move(t)) {}
61+
62+
[[nodiscard]] std::string getValue() const { return tok.value; }
63+
};
5964

6065

6166
/* ======================
6267
* Binary Operation
6368
* ====================== */
6469

65-
struct BinOpNode final
66-
{
67-
std::shared_ptr<ASTNode> left;
68-
Token op_tok;
69-
std::shared_ptr<ASTNode> right;
70-
};
70+
struct BinOpNode final {
71+
std::shared_ptr<ASTNode> left;
72+
lexer::Token op_tok;
73+
std::shared_ptr<ASTNode> right;
74+
};
7175

7276

7377
/* ======================
7478
* Statements List (linked tree)
7579
* ====================== */
7680

77-
struct Statement final
78-
{
79-
std::shared_ptr<ASTNode> left;
80-
std::shared_ptr<ASTNode> right; // may be null
81-
};
81+
struct Statement final {
82+
std::shared_ptr<ASTNode> left;
83+
std::shared_ptr<ASTNode> right; // may be null
84+
};
8285

8386

8487
/* ======================
@@ -87,19 +90,18 @@ struct Statement final
8790
* std::make_shared<Condition>(expr, token, expr)
8891
* ====================== */
8992

90-
struct Condition final
91-
{
92-
std::shared_ptr<ASTNode> left_expression;
93-
Token comparison;
94-
std::shared_ptr<ASTNode> right_expression;
93+
struct Condition final {
94+
std::shared_ptr<ASTNode> left_expression;
95+
lexer::Token comparison;
96+
std::shared_ptr<ASTNode> right_expression;
9597

96-
Condition(std::shared_ptr<ASTNode> left,
97-
Token op,
98-
std::shared_ptr<ASTNode> right)
99-
: left_expression(std::move(left)),
100-
comparison(std::move(op)),
101-
right_expression(std::move(right)) {}
102-
};
98+
Condition(std::shared_ptr<ASTNode> left,
99+
lexer::Token op,
100+
std::shared_ptr<ASTNode> right)
101+
: left_expression(std::move(left)),
102+
comparison(std::move(op)),
103+
right_expression(std::move(right)) {}
104+
};
103105

104106

105107
/* ======================
@@ -109,23 +111,21 @@ struct Condition final
109111
* else_body = Node* or null
110112
* ====================== */
111113

112-
struct IfStatement final
113-
{
114-
std::shared_ptr<ASTNode> if_condition;
115-
std::shared_ptr<ASTNode> if_body;
116-
std::shared_ptr<ASTNode> else_body; // may be null
117-
};
114+
struct IfStatement final {
115+
std::shared_ptr<ASTNode> if_condition;
116+
std::shared_ptr<ASTNode> if_body;
117+
std::shared_ptr<ASTNode> else_body; // may be null
118+
};
118119

119120

120121
/* ======================
121122
* While
122123
* ====================== */
123124

124-
struct WhileStatement final
125-
{
126-
std::shared_ptr<ASTNode> condition;
127-
std::shared_ptr<ASTNode> body;
128-
};
125+
struct WhileStatement final {
126+
std::shared_ptr<ASTNode> condition;
127+
std::shared_ptr<ASTNode> body;
128+
};
129129

130130

131131
/* ======================
@@ -135,23 +135,21 @@ struct WhileStatement final
135135
* - type = "string" => strValue
136136
* ====================== */
137137

138-
struct PrintStatement final
139-
{
140-
std::string type;
141-
std::shared_ptr<ASTNode> intExpr; // if type == "int"
142-
std::string strValue; // if type == "string"
143-
};
138+
struct PrintStatement final {
139+
std::string type;
140+
std::shared_ptr<ASTNode> intExpr; // if type == "int"
141+
std::string strValue; // if type == "string"
142+
};
144143

145144

146145
/* ======================
147146
* Assignment
148147
* ====================== */
149148

150-
struct Assignment final
151-
{
152-
Token identifier; // e.g., T_VAR
153-
std::shared_ptr<ASTNode> expression; // e.g., BinOpNode / NumberNode
154-
};
149+
struct Assignment final {
150+
lexer::Token identifier; // e.g., T_VAR
151+
std::shared_ptr<ASTNode> expression; // e.g., BinOpNode / NumberNode
152+
};
155153

156154

157155
/* ======================
@@ -165,10 +163,11 @@ struct Assignment final
165163
* - init_expr (optional)
166164
* ====================== */
167165

168-
struct Declaration final
169-
{
170-
Token declaration_type; // "int" or "string"
171-
std::vector<Token> identifiers; // list of var IDs
172-
std::shared_ptr<ASTNode> init_expr; // only for int x = expr;
173-
Declaration() : init_expr(nullptr) {}
174-
};
166+
struct Declaration final {
167+
lexer::Token declaration_type; // "int" or "string"
168+
std::vector<lexer::Token> identifiers; // list of var IDs
169+
std::shared_ptr<ASTNode> init_expr; // only for int x = expr;
170+
Declaration() : init_expr(nullptr) {}
171+
};
172+
173+
} // namespace pseu::ast

include/codegen.hpp

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,58 @@
11
#pragma once
2+
23
#include "ir.hpp"
34
#include <vector>
45
#include <string>
56
#include <string_view>
67
#include <unordered_map>
78

8-
class CodeGenerator final
9-
{
10-
public:
11-
CodeGenerator(const InterCodeArray &arr,
12-
const std::unordered_map<std::string, std::string> &identifiers,
13-
const std::unordered_map<std::string, std::string> &constants,
14-
const std::unordered_map<std::string, std::string> &tempmap = {});
15-
void writeAsm(const std::string &path);
16-
17-
private:
18-
void pr(std::string_view s);
19-
void gen_variables();
20-
void gen_start();
21-
void gen_end();
22-
void gen_code();
23-
void gen_assignment(const AssignmentCode &a);
24-
void gen_jump(const JumpCode &j);
25-
void gen_label(const LabelCode &l);
26-
void gen_compare(const CompareCodeIR &c);
27-
void gen_print(const PrintCodeIR &p);
28-
void gen_print_num_function();
29-
void gen_print_string_function();
30-
static std::string handleVar(const std::string &a, const std::unordered_map<std::string, std::string> &tempmap);
31-
32-
private:
33-
const InterCodeArray &arr;
34-
std::unordered_map<std::string, std::string> ids;
35-
std::unordered_map<std::string, std::string> consts;
36-
std::unordered_map<std::string, std::string> tempmap;
37-
std::vector<char> out;
38-
bool need_print_num = false;
39-
bool need_print_string = false;
40-
};
9+
namespace pseu::codegen {
10+
11+
class CodeGenerator final {
12+
public:
13+
CodeGenerator(const ir::InterCodeArray &arr,
14+
const std::unordered_map<std::string, std::string> &identifiers,
15+
const std::unordered_map<std::string, std::string> &constants,
16+
const std::unordered_map<std::string, std::string> &tempmap = {});
17+
18+
void writeAsm(const std::string &path);
19+
20+
private:
21+
void pr(std::string_view s);
22+
23+
void gen_variables();
24+
25+
void gen_start();
26+
27+
void gen_end();
28+
29+
void gen_code();
30+
31+
void gen_assignment(const ir::AssignmentCode &a);
32+
33+
void gen_jump(const ir::JumpCode &j);
34+
35+
void gen_label(const ir::LabelCode &l);
36+
37+
void gen_compare(const ir::CompareCodeIR &c);
38+
39+
void gen_print(const ir::PrintCodeIR &p);
40+
41+
void gen_print_num_function();
42+
43+
void gen_print_string_function();
44+
45+
static std::string handleVar(const std::string &a, const std::unordered_map<std::string, std::string> &tempmap);
46+
47+
private:
48+
const ir::InterCodeArray &arr;
49+
std::unordered_map<std::string, std::string> ids;
50+
std::unordered_map<std::string, std::string> consts;
51+
std::unordered_map<std::string, std::string> tempmap;
52+
std::vector<char> out;
53+
bool need_print_num = false;
54+
bool need_print_string = false;
55+
};
56+
57+
58+
} // namespace pseu::codegen

0 commit comments

Comments
 (0)