-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.h
More file actions
306 lines (266 loc) · 8.78 KB
/
ast.h
File metadata and controls
306 lines (266 loc) · 8.78 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#ifndef AST_H
#define AST_H
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <memory>
#include <llvm/IR/Value.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Type.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/BasicBlock.h>
#include <llvm/IR/Verifier.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/Support/TargetSelect.h>
// Forward declarations
class CodeGenContext;
// Base class for all expressions
class Expr {
public:
virtual ~Expr() {}
virtual llvm::Value* codegen(CodeGenContext& context) = 0;
};
// Integer literal expression
class IntLiteral : public Expr {
public:
int value;
IntLiteral(int value) : value(value) {}
virtual llvm::Value* codegen(CodeGenContext& context);
};
// Boolean literal expression
class BoolLiteral : public Expr {
public:
bool value;
BoolLiteral(bool value) : value(value) {}
virtual llvm::Value* codegen(CodeGenContext& context);
};
// String literal expression
class StringLiteral : public Expr {
public:
std::string value;
StringLiteral(const std::string& value) : value(value) {}
virtual llvm::Value* codegen(CodeGenContext& context);
};
// Variable expression
class VarExpr : public Expr {
public:
std::string name;
VarExpr(const std::string& name) : name(name) {}
virtual llvm::Value* codegen(CodeGenContext& context);
};
// Binary operation types
enum class BinaryOp {
ADD, SUB, MUL, DIV, EQ, NEQ, LT, GT, AND, OR
};
// Binary expression
class BinaryExpr : public Expr {
public:
Expr* lhs;
BinaryOp op;
Expr* rhs;
BinaryExpr(Expr* lhs, BinaryOp op, Expr* rhs) : lhs(lhs), op(op), rhs(rhs) {}
virtual ~BinaryExpr() {
delete lhs;
delete rhs;
}
virtual llvm::Value* codegen(CodeGenContext& context);
};
// Base class for all statements
class Stmt {
public:
virtual ~Stmt() {}
virtual llvm::Value* codegen(CodeGenContext& context) = 0;
};
// Declaration statement
class DeclStmt : public Stmt {
public:
std::string name;
Expr* initializer;
DeclStmt(const std::string& name, Expr* initializer) : name(name), initializer(initializer) {}
virtual ~DeclStmt() {
delete initializer;
}
virtual llvm::Value* codegen(CodeGenContext& context);
};
// Assignment statement
class AssignStmt : public Stmt {
public:
std::string name;
Expr* value;
AssignStmt(const std::string& name, Expr* value) : name(name), value(value) {}
virtual ~AssignStmt() {
delete value;
}
virtual llvm::Value* codegen(CodeGenContext& context);
};
// If statement
class IfStmt : public Stmt {
public:
Expr* condition;
std::vector<Stmt*>* thenBlock;
std::vector<Stmt*>* elseBlock;
IfStmt(Expr* condition, std::vector<Stmt*>* thenBlock, std::vector<Stmt*>* elseBlock)
: condition(condition), thenBlock(thenBlock), elseBlock(elseBlock) {}
virtual ~IfStmt() {
delete condition;
if (thenBlock) {
for (auto stmt : *thenBlock) {
delete stmt;
}
delete thenBlock;
}
if (elseBlock) {
for (auto stmt : *elseBlock) {
delete stmt;
}
delete elseBlock;
}
}
virtual llvm::Value* codegen(CodeGenContext& context);
};
// Loop statement
class LoopStmt : public Stmt {
public:
Expr* count;
std::vector<Stmt*>* body;
LoopStmt(Expr* count, std::vector<Stmt*>* body) : count(count), body(body) {}
virtual ~LoopStmt() {
delete count;
if (body) {
for (auto stmt : *body) {
delete stmt;
}
delete body;
}
}
virtual llvm::Value* codegen(CodeGenContext& context);
};
// Print statement
class PrintStmt : public Stmt {
public:
Expr* value;
PrintStmt(Expr* value) : value(value) {}
virtual ~PrintStmt() {
delete value;
}
virtual llvm::Value* codegen(CodeGenContext& context);
};
// Return statement
class ReturnStmt : public Stmt {
public:
Expr* value;
ReturnStmt(Expr* value) : value(value) {}
virtual ~ReturnStmt() {
delete value;
}
virtual llvm::Value* codegen(CodeGenContext& context);
};
// Function definition
class FunctionDef : public Stmt {
public:
std::string name;
std::vector<std::string> params; // New: list of parameter names
std::vector<Stmt*>* body;
// Updated constructor with parameters
FunctionDef(const std::string& name, const std::vector<std::string>& params, std::vector<Stmt*>* body)
: name(name), params(params), body(body) {}
virtual ~FunctionDef() {
if (body) {
for (auto stmt : *body) {
delete stmt;
}
delete body;
}
}
virtual llvm::Value* codegen(CodeGenContext& context);
};
// Function call
class FunctionCall : public Stmt, public Expr {
public:
std::string name;
std::vector<Expr*>* args;
FunctionCall(const std::string& name, std::vector<Expr*>* args) : name(name), args(args) {}
virtual ~FunctionCall() {
if (args) {
for (auto arg : *args) {
delete arg;
}
delete args;
}
}
virtual llvm::Value* codegen(CodeGenContext& context);
};
// Program - the root of our AST
class Program {
public:
std::vector<Stmt*>* statements;
std::vector<FunctionDef*>* functions;
Program(std::vector<Stmt*>* statements) : statements(statements) {}
virtual ~Program() {
if (statements) {
for (auto stmt : *statements) {
delete stmt;
}
delete statements;
}
}
virtual llvm::Value* codegen(CodeGenContext& context);
void printTokens() const {
if (!statements || statements->empty()) {
std::cout << "Program is empty" << std::endl;
return;
}
std::cout << "Program with " << statements->size() << " statements:" << std::endl;
for (size_t i = 0; i < statements->size(); i++) {
std::cout << " Statement " << (i+1) << ": ";
// Print statement type information and content
if (auto decl = dynamic_cast<DeclStmt*>(statements->at(i)))
std::cout << "Declaration of '" << decl->name << "'" << std::endl;
else if (auto assign = dynamic_cast<AssignStmt*>(statements->at(i)))
std::cout << "Assignment to '" << assign->name << "'" << std::endl;
else if (auto ifStmt = dynamic_cast<IfStmt*>(statements->at(i)))
std::cout << "If statement with " << ifStmt->thenBlock->size() << " statements in then block"
<< (ifStmt->elseBlock ? " and " + std::to_string(ifStmt->elseBlock->size()) + " in else block" : "") << std::endl;
else if (auto loop = dynamic_cast<LoopStmt*>(statements->at(i)))
std::cout << "Loop statement with " << loop->body->size() << " statements in body" << std::endl;
else if (dynamic_cast<PrintStmt*>(statements->at(i)))
std::cout << "Print statement" << std::endl;
else if (dynamic_cast<ReturnStmt*>(statements->at(i)))
std::cout << "Return statement" << std::endl;
else if (auto func = dynamic_cast<FunctionDef*>(statements->at(i)))
std::cout << "Function definition '" << func->name << "' with "
<< (func->params.size() > 0 ? std::to_string(func->params.size()) + " parameters and " : "")
<< func->body->size() << " statements" << std::endl;
else if (auto call = dynamic_cast<FunctionCall*>(statements->at(i)))
std::cout << "Function call to '" << call->name << "' with "
<< (call->args ? call->args->size() : 0) << " arguments" << std::endl;
else
std::cout << "Unknown statement type" << std::endl;
}
}
};
// Code generation context
class CodeGenContext {
public:
llvm::LLVMContext context;
std::unique_ptr<llvm::Module> module;
llvm::IRBuilder<> builder;
std::map<std::string, llvm::AllocaInst*> namedValues;
std::map<std::string, llvm::Function*> functions;
CodeGenContext() :
module(std::make_unique<llvm::Module>("linguo", context)),
builder(context) {}
llvm::AllocaInst* createEntryBlockAlloca(llvm::Function* function,
const std::string& varName,
llvm::Type* type);
void generateCode(Program* program);
void printIR();
void writeIR(const std::string& filename);
void optimize();
void runJIT();
};
#endif