Skip to content

Commit a235fc8

Browse files
authored
Merge pull request #1 from IlyasKhatipov/debug
Debug
2 parents 361c0d2 + 200010c commit a235fc8

33 files changed

Lines changed: 774 additions & 8390 deletions

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CXX := g++
2+
CXXFLAGS := -std=c++17 -Wall -Wextra -O2
3+
4+
all: mycompiler
5+
6+
parser.cpp parser.hpp: parser.y
7+
bison -d -o parser.cpp parser.y
8+
9+
lexer.cpp: lexer.l parser.hpp
10+
flex -o lexer.cpp lexer.l
11+
12+
mycompiler: parser.cpp lexer.cpp main.cpp
13+
$(CXX) $(CXXFLAGS) -o $@ parser.cpp lexer.cpp main.cpp -lfl
14+
15+
clean:
16+
rm -f parser.cpp parser.hpp lexer.cpp mycompiler

README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

ast.h

Lines changed: 0 additions & 84 deletions
This file was deleted.

ast.hpp

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#pragma once
2+
#include <string>
3+
#include <vector>
4+
#include <iostream>
5+
#include <cstdint>
6+
7+
namespace AST {
8+
9+
struct Node {
10+
virtual ~Node() = default;
11+
virtual void print(std::ostream& os, int indent = 0) const = 0;
12+
};
13+
14+
inline void doIndent(std::ostream& os, int n) {
15+
for (int i = 0; i < n; ++i) os << " ";
16+
}
17+
18+
struct Expr : Node { };
19+
20+
struct IntLiteral : Expr {
21+
std::int64_t value;
22+
explicit IntLiteral(std::int64_t v) : value(v) {}
23+
void print(std::ostream& os, int indent) const override {
24+
doIndent(os, indent); os << "Int(" << value << ")\n";
25+
}
26+
};
27+
28+
struct BoolLiteral : Expr {
29+
bool value;
30+
explicit BoolLiteral(bool v) : value(v) {}
31+
void print(std::ostream& os, int indent) const override {
32+
doIndent(os, indent); os << "Bool(" << (value ? "true" : "false") << ")\n";
33+
}
34+
};
35+
36+
struct Identifier : Expr {
37+
std::string name;
38+
explicit Identifier(std::string n) : name(std::move(n)) {}
39+
void print(std::ostream& os, int indent) const override {
40+
doIndent(os, indent); os << "Id(" << name << ")\n";
41+
}
42+
};
43+
44+
enum class BinOp { Add, Sub, Mul, Div, Assign };
45+
46+
struct Binary : Expr {
47+
BinOp op;
48+
Expr* lhs;
49+
Expr* rhs;
50+
Binary(BinOp o, Expr* l, Expr* r) : op(o), lhs(l), rhs(r) {}
51+
~Binary() { delete lhs; delete rhs; }
52+
static const char* opToStr(BinOp o) {
53+
switch (o) {
54+
case BinOp::Add: return "+";
55+
case BinOp::Sub: return "-";
56+
case BinOp::Mul: return "*";
57+
case BinOp::Div: return "/";
58+
case BinOp::Assign: return "=";
59+
}
60+
return "?";
61+
}
62+
void print(std::ostream& os, int indent) const override {
63+
doIndent(os, indent);
64+
os << "BinOp(" << opToStr(op) << ")\n";
65+
lhs->print(os, indent + 1);
66+
rhs->print(os, indent + 1);
67+
}
68+
};
69+
70+
struct Unary : Expr {
71+
enum class Op { Neg };
72+
Op op;
73+
Expr* rhs;
74+
Unary(Op o, Expr* e) : op(o), rhs(e) {}
75+
~Unary() { delete rhs; }
76+
void print(std::ostream& os, int indent) const override {
77+
doIndent(os, indent);
78+
os << "Unary(-)\n";
79+
rhs->print(os, indent + 1);
80+
}
81+
};
82+
83+
struct Stmt : Node { };
84+
85+
struct ReturnStmt : Stmt {
86+
Expr* value;
87+
explicit ReturnStmt(Expr* v) : value(v) {}
88+
~ReturnStmt() { delete value; }
89+
void print(std::ostream& os, int indent) const override {
90+
doIndent(os, indent); os << "return\n";
91+
value->print(os, indent + 1);
92+
}
93+
};
94+
95+
struct IfStmt : Stmt {
96+
Expr* cond;
97+
Stmt* thenS;
98+
Stmt* elseS;
99+
IfStmt(Expr* c, Stmt* t, Stmt* e) : cond(c), thenS(t), elseS(e) {}
100+
~IfStmt() { delete cond; delete thenS; delete elseS; }
101+
void print(std::ostream& os, int indent) const override {
102+
doIndent(os, indent); os << "if\n";
103+
cond->print(os, indent + 1);
104+
doIndent(os, indent); os << "then\n";
105+
thenS->print(os, indent + 1);
106+
doIndent(os, indent); os << "else\n";
107+
elseS->print(os, indent + 1);
108+
}
109+
};
110+
111+
struct VarDecl : Node {
112+
std::string name;
113+
std::string typeName;
114+
Expr* init;
115+
VarDecl(std::string n, std::string t, Expr* i)
116+
: name(std::move(n)), typeName(std::move(t)), init(i) {}
117+
~VarDecl() { delete init; }
118+
void print(std::ostream& os, int indent) const override {
119+
doIndent(os, indent);
120+
os << "var " << name << " : " << typeName;
121+
if (init) {
122+
os << " =\n";
123+
init->print(os, indent + 1);
124+
} else {
125+
os << "\n";
126+
}
127+
}
128+
};
129+
130+
struct Param {
131+
std::string name;
132+
std::string typeName;
133+
Param(std::string n, std::string t) : name(std::move(n)), typeName(std::move(t)) {}
134+
};
135+
136+
struct MethodDecl : Node {
137+
std::string name;
138+
std::vector<Param*> params;
139+
std::string returnType;
140+
Stmt* body;
141+
MethodDecl(std::string n, std::string rt, Stmt* b)
142+
: name(std::move(n)), returnType(std::move(rt)), body(b) {}
143+
~MethodDecl() { for (auto* p : params) delete p; delete body; }
144+
void print(std::ostream& os, int indent) const override {
145+
doIndent(os, indent);
146+
os << "method " << name << "(";
147+
for (size_t i = 0; i < params.size(); ++i) {
148+
os << params[i]->name << " : " << params[i]->typeName;
149+
if (i + 1 < params.size()) os << ", ";
150+
}
151+
os << ") : " << returnType << "\n";
152+
if (body) body->print(os, indent + 1);
153+
}
154+
};
155+
156+
struct ClassDecl : Node {
157+
std::string name;
158+
std::vector<VarDecl*> fields;
159+
std::vector<MethodDecl*> methods;
160+
explicit ClassDecl(std::string n) : name(std::move(n)) {}
161+
~ClassDecl() { for (auto* v : fields) delete v; for (auto* m : methods) delete m; }
162+
void print(std::ostream& os, int indent) const override {
163+
doIndent(os, indent);
164+
os << "Class: " << name << "\n";
165+
for (auto* v : fields) v->print(os, indent + 1);
166+
for (auto* m : methods) m->print(os, indent + 1);
167+
}
168+
};
169+
170+
struct Program : Node {
171+
std::vector<ClassDecl*> classes;
172+
~Program() { for (auto* c : classes) delete c; }
173+
void print(std::ostream& os, int indent = 0) const override {
174+
for (auto* c : classes) c->print(os, indent);
175+
}
176+
};
177+
178+
}

0 commit comments

Comments
 (0)