-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast_demo.cpp
More file actions
254 lines (214 loc) · 8.77 KB
/
ast_demo.cpp
File metadata and controls
254 lines (214 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <iostream>
#include "photon/memory/arena.hpp"
#include "photon/parser/ast.hpp"
#include "photon/diagnostics/source_location.hpp"
using namespace photon;
using namespace photon::memory;
using namespace photon::parser;
using namespace photon::diagnostics;
class PrettyPrintVisitor : public ASTVisitor {
public:
explicit PrettyPrintVisitor(int indent = 0) : indent_(indent) {}
void visit_program(Program& node) override {
std::cout << "Program {\n";
for (auto& decl : node.declarations_mut()) {
PrettyPrintVisitor visitor(indent_ + 2);
visitor.print_indent();
decl->accept(visitor);
}
std::cout << "}\n";
}
void visit_program(const Program& node) override {
std::cout << "Program {\n";
for (const auto& decl : node.declarations()) {
PrettyPrintVisitor visitor(indent_ + 2);
visitor.print_indent();
decl->accept(visitor);
}
std::cout << "}\n";
}
void visit_function_decl(FunctionDecl& node) override {
std::cout << "Function '" << node.name() << "' (";
for (usize i = 0; i < node.parameters().size(); ++i) {
if (i > 0) std::cout << ", ";
std::cout << node.parameters()[i].name << ": " << node.parameters()[i].type->to_string();
}
std::cout << ")";
if (node.return_type()) {
std::cout << " -> " << node.return_type()->to_string();
}
std::cout << " ";
PrettyPrintVisitor visitor(indent_);
node.body().accept(visitor);
}
void visit_function_decl(const FunctionDecl& node) override {
std::cout << "Function '" << node.name() << "' (";
for (usize i = 0; i < node.parameters().size(); ++i) {
if (i > 0) std::cout << ", ";
std::cout << node.parameters()[i].name << ": " << node.parameters()[i].type->to_string();
}
std::cout << ")";
if (node.return_type()) {
std::cout << " -> " << node.return_type()->to_string();
}
std::cout << " ";
PrettyPrintVisitor visitor(indent_);
node.body().accept(visitor);
}
void visit_block(Block& node) override {
std::cout << "{\n";
for (auto& stmt : node.statements_mut()) {
PrettyPrintVisitor visitor(indent_ + 2);
visitor.print_indent();
stmt->accept(visitor);
std::cout << "\n";
}
print_indent();
std::cout << "}";
}
void visit_block(const Block& node) override {
std::cout << "{\n";
for (const auto& stmt : node.statements()) {
PrettyPrintVisitor visitor(indent_ + 2);
visitor.print_indent();
stmt->accept(visitor);
std::cout << "\n";
}
print_indent();
std::cout << "}";
}
void visit_var_decl(VarDecl& node) override {
std::cout << "VarDecl: " << (node.is_mutable() ? "mut " : "") << node.name();
if (node.type()) {
std::cout << ": " << node.type()->to_string();
}
if (node.init()) {
std::cout << " = " << node.init()->to_string();
}
}
void visit_var_decl(const VarDecl& node) override {
std::cout << "VarDecl: " << (node.is_mutable() ? "mut " : "") << node.name();
if (node.type()) {
std::cout << ": " << node.type()->to_string();
}
if (node.init()) {
std::cout << " = " << node.init()->to_string();
}
}
void visit_binary_expr(BinaryExpr& node) override {
std::cout << node.to_string();
}
void visit_binary_expr(const BinaryExpr& node) override {
std::cout << node.to_string();
}
// Required stubs
void visit_integer_literal(IntegerLiteral&) override {}
void visit_integer_literal(const IntegerLiteral&) override {}
void visit_float_literal(FloatLiteral&) override {}
void visit_float_literal(const FloatLiteral&) override {}
void visit_string_literal(StringLiteral&) override {}
void visit_string_literal(const StringLiteral&) override {}
void visit_bool_literal(BoolLiteral&) override {}
void visit_bool_literal(const BoolLiteral&) override {}
void visit_identifier(Identifier&) override {}
void visit_identifier(const Identifier&) override {}
void visit_unary_expr(UnaryExpr&) override {}
void visit_unary_expr(const UnaryExpr&) override {}
void visit_call_expr(CallExpr&) override {}
void visit_call_expr(const CallExpr&) override {}
private:
int indent_;
void print_indent() {
for (int i = 0; i < indent_; ++i) {
std::cout << " ";
}
}
};
int main() {
std::cout << "🌟 Photon AST Manual Demo\n";
std::cout << "=========================\n\n";
try {
// Setup memory arena and factory
MemoryArena<> arena;
ASTFactory factory(arena);
std::cout << "Creating AST manually to demonstrate the parser's capabilities...\n\n";
// Create a simple function: fn add(x: i32, y: i32) -> i32 { let result = x + y }
// 1. Create parameter types
auto x_type = factory.create<Identifier>("i32");
auto y_type = factory.create<Identifier>("i32");
auto return_type = factory.create<Identifier>("i32");
// 2. Create parameters
Vec<FunctionDecl::Parameter> params;
params.push_back({"x", std::move(x_type), {}});
params.push_back({"y", std::move(y_type), {}});
// 3. Create function body
auto x_ref = factory.create<Identifier>("x");
auto y_ref = factory.create<Identifier>("y");
auto add_expr = factory.create<BinaryExpr>(
std::move(x_ref),
BinaryExpr::Operator::Add,
std::move(y_ref)
);
auto var_decl = factory.create<VarDecl>(
"result",
nullptr, // no type annotation
std::move(add_expr),
false // immutable
);
auto statements = factory.create_list<Statement>();
statements.push_back(std::move(var_decl));
auto body = factory.create<Block>(std::move(statements));
// 4. Create function declaration
auto func_decl = factory.create<FunctionDecl>(
"add",
std::move(params),
std::move(return_type),
std::move(body)
);
// 5. Create program
auto declarations = factory.create_list<Declaration>();
declarations.push_back(std::move(func_decl));
auto program = factory.create<Program>(std::move(declarations));
// 6. Print the AST
std::cout << "Generated AST for: fn add(x: i32, y: i32) -> i32 { let result = x + y }\n\n";
PrettyPrintVisitor visitor;
program->accept(visitor);
std::cout << "\n\n✅ AST creation and traversal successful!\n";
// Demonstrate expression parsing capabilities
std::cout << "\n" << std::string(50, '=') << "\n";
std::cout << "Expression AST Demo:\n";
std::cout << std::string(50, '=') << "\n\n";
// Create: (5 + 3) * 2
auto five = factory.create<IntegerLiteral>(5);
auto three = factory.create<IntegerLiteral>(3);
auto two = factory.create<IntegerLiteral>(2);
auto add_5_3 = factory.create<BinaryExpr>(
std::move(five),
BinaryExpr::Operator::Add,
std::move(three)
);
auto multiply_result = factory.create<BinaryExpr>(
std::move(add_5_3),
BinaryExpr::Operator::Mul,
std::move(two)
);
std::cout << "Expression: (5 + 3) * 2\n";
std::cout << "AST: " << multiply_result->to_string() << "\n";
std::cout << "\n📊 AST Node Types Supported:\n";
std::cout << " • Literals: Integer, Float, String, Boolean\n";
std::cout << " • Expressions: Binary, Unary, Call, Identifier\n";
std::cout << " • Statements: Variable Declaration, Block\n";
std::cout << " • Declarations: Function Declaration\n";
std::cout << " • Program: Top-level container\n";
std::cout << "\n🎯 Parser Features Demonstrated:\n";
std::cout << " • Type-safe AST construction\n";
std::cout << " • Memory arena allocation\n";
std::cout << " • Visitor pattern traversal\n";
std::cout << " • Proper operator precedence handling\n";
std::cout << " • Complex expression trees\n";
} catch (const std::exception& e) {
std::cerr << "❌ Error: " << e.what() << std::endl;
return 1;
}
return 0;
}