topazc
ast.hpp
Go to the documentation of this file.
1
6
7#pragma once
8#include "../lexer/token.hpp"
9#include <uchar.h>
10#include <variant>
11#include <memory>
12#include <vector>
13#include <cmath>
14
15namespace AST {
33
37 struct Type {
39 std::string name;
40 bool is_const;
41 bool is_ptr;
43
44 Type(TypeValue t, std::string n, bool ic = false, bool ip = false, bool in = false) : type(t), name(n), is_const(ic), is_ptr(ip), is_nullable(in) {}
45
46 bool operator ==(Type& other) {
47 return type == other.type && name == other.name && is_const == other.is_const && is_ptr == other.is_ptr && is_nullable == other.is_nullable;
48 }
49
50 bool operator !=(Type& other) {
51 return !(*this == other);
52 }
53
61 std::string to_str() {
62 std::stringstream ss;
63 ss << (is_const ? "const " : "") << name << (is_ptr ? "*" : "") << (is_nullable ? "? " : "");
64 return ss.str();
65 }
66 };
67
71 struct Value {
72 std::variant<bool, char8_t, int16_t, int32_t, int64_t, float_t, double_t, std::string> value;
73
74 Value(bool v) : value(v) {}
75 Value(char8_t v) : value(v) {}
76 Value(int16_t v) : value(v) {}
77 Value(int32_t v) : value(v) {}
78 Value(int64_t v) : value(v) {}
79 Value(float_t v) : value(v) {}
80 Value(double_t v) : value(v) {}
81 Value(std::string v) : value(v) {}
82 };
83
87 struct Argument {
88 std::string name;
90
91 Argument(std::string n, Type t) : name(n), type(t) {}
92 };
93
102
106 class Stmt {
107 public:
108 uint32_t line;
109
110 Stmt(uint32_t l) : line(l) {}
111 virtual ~Stmt() = default;
112 };
113
117 class Expr {
118 public:
119 uint32_t line;
120
121 Expr(uint32_t l) : line(l) {}
122 virtual ~Expr() = default;
123 };
124
125 using StmtPtr = std::unique_ptr<Stmt>;
126 using ExprPtr = std::unique_ptr<Expr>;
127
128 // EXPRESSIONS
129
133 class Literal : public Expr {
134 public:
137
138 Literal(Type t, Value v, uint32_t l) : type(t), value(v), Expr(l) {}
139 ~Literal() override = default;
140 };
141
145 class BoolLiteral : public Literal {
146 public:
147 BoolLiteral(bool v, uint32_t l) : Literal(Type(TYPE_BOOL, "bool"), Value(v), l) {}
148 ~BoolLiteral() override = default;
149 };
150
154 class CharacterLiteral : public Literal {
155 public:
156 CharacterLiteral(char8_t v, uint32_t l) : Literal(Type(TYPE_CHAR, "char"), Value(v), l) {}
157 ~CharacterLiteral() override = default;
158 };
159
163 class ShortLiteral : public Literal {
164 public:
165 ShortLiteral(int16_t v, uint32_t l) : Literal(Type(TYPE_SHORT, "short"), Value(v), l) {}
166 ~ShortLiteral() override = default;
167 };
168
172 class IntLiteral : public Literal {
173 public:
174 IntLiteral(int32_t v, uint32_t l) : Literal(Type(TYPE_INT, "int"), Value(v), l) {}
175 ~IntLiteral() override = default;
176 };
177
181 class LongLiteral : public Literal {
182 public:
183 LongLiteral(int64_t v, uint32_t l) : Literal(Type(TYPE_LONG, "long"), Value(v), l) {}
184 ~LongLiteral() override = default;
185 };
186
190 class FloatLiteral : public Literal {
191 public:
192 FloatLiteral(float_t v, uint32_t l) : Literal(Type(TYPE_FLOAT, "float"), Value(v), l) {}
193 ~FloatLiteral() override = default;
194 };
195
199 class DoubleLiteral : public Literal {
200 public:
201 DoubleLiteral(double_t v, uint32_t l) : Literal(Type(TYPE_DOUBLE, "double"), Value(v), l) {}
202 ~DoubleLiteral() override = default;
203 };
204
208 class StringLiteral : public Literal {
209 public:
210 StringLiteral(std::string v, uint32_t l) : Literal(Type(TYPE_STRING_LIT, "string"), Value(v), l) {}
211 ~StringLiteral() override = default;
212 };
213
219 class BinaryExpr : public Expr {
220 public:
224
225 BinaryExpr(Token o, ExprPtr le, ExprPtr re, uint32_t l) : op(o), left_expr(std::move(le)), right_expr(std::move(re)), Expr(l) {}
226 ~BinaryExpr() override = default;
227 };
228
234 class UnaryExpr : public Expr {
235 public:
238
239 UnaryExpr(Token o, ExprPtr e, uint32_t l) : op(o), expr(std::move(e)), Expr(l) {}
240 ~UnaryExpr() override = default;
241 };
242
248 class VarExpr : public Expr {
249 public:
250 std::string name;
251
252 VarExpr(std::string n, uint32_t l) : name(n), Expr(l) {}
253 ~VarExpr() override = default;
254 };
255
261 class FuncCallExpr : public Expr {
262 public:
263 std::string name;
264 std::vector<ExprPtr> args;
265
266 FuncCallExpr(std::string n, std::vector<ExprPtr> a, uint32_t l) : name(n), args(std::move(a)), Expr(l) {}
267 ~FuncCallExpr() override = default;
268 };
269
273 class ChainObjects : public Expr {
274 public:
275 std::vector<ExprPtr> chain;
276
277 ChainObjects(std::vector<ExprPtr> c, uint32_t l) : chain(std::move(c)), Expr(l) {}
278 ~ChainObjects() override = default;
279 };
280
281 // STATEMENTS
282
286 class VarDeclStmt : public Stmt {
287 public:
291 std::string name;
292
293 VarDeclStmt(AccessModifier am, Type t, ExprPtr e, std::string n, uint32_t l) : access(am), type(t), expr(std::move(e)), name(n), Stmt(l) {}
294 ~VarDeclStmt() override = default;
295 };
296
300 class VarAsgnStmt : public Stmt {
301 public:
302 std::string name;
304 bool is_deref;
305
306 VarAsgnStmt(std::string n, ExprPtr e, bool id, uint32_t l) : name(n), expr(std::move(e)), is_deref(id), Stmt(l) {}
307 ~VarAsgnStmt() override = default;
308 };
309
313 class FuncDeclStmt : public Stmt {
314 public:
316 std::string name;
317 std::vector<Argument> args;
319 std::vector<StmtPtr> block;
320
321 FuncDeclStmt(AccessModifier am, std::string n, std::vector<Argument> a, Type rt, std::vector<StmtPtr> b, uint32_t l) : access(am), name(n), args(std::move(a)), ret_type(rt), block(std::move(b)), Stmt(l) {}
322 ~FuncDeclStmt() override = default;
323 };
324
328 class FuncCallStmt : public Stmt {
329 public:
330 std::string name;
331 std::vector<ExprPtr> args;
332
333 FuncCallStmt(std::string n, std::vector<ExprPtr> a, uint32_t l) : name(n), args(std::move(a)), Stmt(l) {}
334 ~FuncCallStmt() override = default;
335 };
336
340 class ReturnStmt : public Stmt {
341 public:
343
344 ReturnStmt(ExprPtr e, uint32_t l) : expr(std::move(e)), Stmt(l) {}
345 ~ReturnStmt() override = default;
346 };
347
351 class IfElseStmt : public Stmt {
352 public:
354 std::vector<StmtPtr> then_block;
355 std::vector<StmtPtr> else_block;
356
357 IfElseStmt(ExprPtr c, std::vector<StmtPtr> tb, std::vector<StmtPtr> eb, uint32_t l) : cond(std::move(c)), then_block(std::move(tb)), else_block(std::move(eb)), Stmt(l) {}
358 };
359
363 class WhileCycleStmt : public Stmt {
364 public:
366 std::vector<StmtPtr> block;
367
368 WhileCycleStmt(ExprPtr c, std::vector<StmtPtr> b, uint32_t l) : cond(std::move(c)), block(std::move(b)), Stmt(l) {}
369 ~WhileCycleStmt() override = default;
370 };
371
375 class DoWhileCycleStmt : public Stmt {
376 public:
378 std::vector<StmtPtr> block;
379
380 DoWhileCycleStmt(ExprPtr c, std::vector<StmtPtr> b, uint32_t l) : cond(std::move(c)), block(std::move(b)), Stmt(l) {}
381 ~DoWhileCycleStmt() override = default;
382 };
383
387 class ForCycleStmt : public Stmt {
388 public:
392 std::vector<StmtPtr> block;
393
394 ForCycleStmt(StmtPtr ix, ExprPtr c, StmtPtr it, std::vector<StmtPtr> b, uint32_t l) : indexator(std::move(ix)), cond(std::move(c)), iteration(std::move(it)), block(std::move(b)), Stmt(l) {}
395 ~ForCycleStmt() override = default;
396 };
397
401 class BreakStmt : public Stmt {
402 public:
403 BreakStmt(uint32_t l) : Stmt(l) {}
404 ~BreakStmt() override = default;
405 };
406
410 class ContinueStmt : public Stmt {
411 public:
412 ContinueStmt(uint32_t l) : Stmt(l) {}
413 ~ContinueStmt() override = default;
414 };
415
419 class ModuleStmt : public Stmt {
420 public:
422 std::string file_name;
423 std::string name;
424 std::vector<StmtPtr> block;
425
426 ModuleStmt(AccessModifier am, std::string fn, std::string n, std::vector<StmtPtr> b, uint32_t l) : access(am), file_name(fn), name(n), block(std::move(b)), Stmt(l) {}
427 ~ModuleStmt() override = default;
428 };
429
433 class UseModuleStmt : public Stmt {
434 public:
435 std::vector<std::string> path;
436
437 UseModuleStmt(std::vector<std::string> p, uint32_t l) : path(std::move(p)), Stmt(l) {}
438 ~UseModuleStmt() override = default;
439 };
440
444 class ExternStmt : public Stmt {
445 public:
446 std::string lang_name_lit;
447 std::vector<StmtPtr> block;
448
449 ExternStmt(std::string lnl, std::vector<StmtPtr> b, uint32_t l) : lang_name_lit(lnl), block(std::move(b)), Stmt(l) {}
450 ~ExternStmt() override = default;
451 };
452}
BinaryExpr(Token o, ExprPtr le, ExprPtr re, uint32_t l)
Definition ast.hpp:225
ExprPtr right_expr
Definition ast.hpp:223
ExprPtr left_expr
Definition ast.hpp:222
~BinaryExpr() override=default
BoolLiteral(bool v, uint32_t l)
Definition ast.hpp:147
~BoolLiteral() override=default
~BreakStmt() override=default
BreakStmt(uint32_t l)
Definition ast.hpp:403
std::vector< ExprPtr > chain
Definition ast.hpp:275
ChainObjects(std::vector< ExprPtr > c, uint32_t l)
Definition ast.hpp:277
~ChainObjects() override=default
CharacterLiteral(char8_t v, uint32_t l)
Definition ast.hpp:156
~CharacterLiteral() override=default
~ContinueStmt() override=default
ContinueStmt(uint32_t l)
Definition ast.hpp:412
~DoWhileCycleStmt() override=default
std::vector< StmtPtr > block
Definition ast.hpp:378
DoWhileCycleStmt(ExprPtr c, std::vector< StmtPtr > b, uint32_t l)
Definition ast.hpp:380
DoubleLiteral(double_t v, uint32_t l)
Definition ast.hpp:201
~DoubleLiteral() override=default
virtual ~Expr()=default
uint32_t line
Definition ast.hpp:119
Expr(uint32_t l)
Definition ast.hpp:121
~ExternStmt() override=default
std::vector< StmtPtr > block
Definition ast.hpp:447
std::string lang_name_lit
Definition ast.hpp:446
ExternStmt(std::string lnl, std::vector< StmtPtr > b, uint32_t l)
Definition ast.hpp:449
~FloatLiteral() override=default
FloatLiteral(float_t v, uint32_t l)
Definition ast.hpp:192
StmtPtr indexator
Definition ast.hpp:389
~ForCycleStmt() override=default
StmtPtr iteration
Definition ast.hpp:391
ForCycleStmt(StmtPtr ix, ExprPtr c, StmtPtr it, std::vector< StmtPtr > b, uint32_t l)
Definition ast.hpp:394
ExprPtr cond
Definition ast.hpp:390
std::vector< StmtPtr > block
Definition ast.hpp:392
std::string name
Definition ast.hpp:263
~FuncCallExpr() override=default
std::vector< ExprPtr > args
Definition ast.hpp:264
FuncCallExpr(std::string n, std::vector< ExprPtr > a, uint32_t l)
Definition ast.hpp:266
std::vector< ExprPtr > args
Definition ast.hpp:331
FuncCallStmt(std::string n, std::vector< ExprPtr > a, uint32_t l)
Definition ast.hpp:333
std::string name
Definition ast.hpp:330
~FuncCallStmt() override=default
std::vector< StmtPtr > block
Definition ast.hpp:319
std::vector< Argument > args
Definition ast.hpp:317
AccessModifier access
Definition ast.hpp:315
~FuncDeclStmt() override=default
FuncDeclStmt(AccessModifier am, std::string n, std::vector< Argument > a, Type rt, std::vector< StmtPtr > b, uint32_t l)
Definition ast.hpp:321
std::string name
Definition ast.hpp:316
IfElseStmt(ExprPtr c, std::vector< StmtPtr > tb, std::vector< StmtPtr > eb, uint32_t l)
Definition ast.hpp:357
ExprPtr cond
Definition ast.hpp:353
std::vector< StmtPtr > else_block
Definition ast.hpp:355
std::vector< StmtPtr > then_block
Definition ast.hpp:354
~IntLiteral() override=default
IntLiteral(int32_t v, uint32_t l)
Definition ast.hpp:174
Type type
Definition ast.hpp:135
Literal(Type t, Value v, uint32_t l)
Definition ast.hpp:138
Value value
Definition ast.hpp:136
~Literal() override=default
~LongLiteral() override=default
LongLiteral(int64_t v, uint32_t l)
Definition ast.hpp:183
AccessModifier access
Definition ast.hpp:421
std::string name
Definition ast.hpp:423
~ModuleStmt() override=default
std::vector< StmtPtr > block
Definition ast.hpp:424
std::string file_name
Definition ast.hpp:422
ModuleStmt(AccessModifier am, std::string fn, std::string n, std::vector< StmtPtr > b, uint32_t l)
Definition ast.hpp:426
~ReturnStmt() override=default
ReturnStmt(ExprPtr e, uint32_t l)
Definition ast.hpp:344
ExprPtr expr
Definition ast.hpp:342
ShortLiteral(int16_t v, uint32_t l)
Definition ast.hpp:165
~ShortLiteral() override=default
virtual ~Stmt()=default
uint32_t line
Definition ast.hpp:108
Stmt(uint32_t l)
Definition ast.hpp:110
StringLiteral(std::string v, uint32_t l)
Definition ast.hpp:210
~StringLiteral() override=default
UnaryExpr(Token o, ExprPtr e, uint32_t l)
Definition ast.hpp:239
~UnaryExpr() override=default
ExprPtr expr
Definition ast.hpp:237
UseModuleStmt(std::vector< std::string > p, uint32_t l)
Definition ast.hpp:437
~UseModuleStmt() override=default
std::vector< std::string > path
Definition ast.hpp:435
VarAsgnStmt(std::string n, ExprPtr e, bool id, uint32_t l)
Definition ast.hpp:306
std::string name
Definition ast.hpp:302
~VarAsgnStmt() override=default
ExprPtr expr
Definition ast.hpp:303
ExprPtr expr
Definition ast.hpp:290
~VarDeclStmt() override=default
std::string name
Definition ast.hpp:291
VarDeclStmt(AccessModifier am, Type t, ExprPtr e, std::string n, uint32_t l)
Definition ast.hpp:293
AccessModifier access
Definition ast.hpp:288
VarExpr(std::string n, uint32_t l)
Definition ast.hpp:252
~VarExpr() override=default
std::string name
Definition ast.hpp:250
std::vector< StmtPtr > block
Definition ast.hpp:366
~WhileCycleStmt() override=default
WhileCycleStmt(ExprPtr c, std::vector< StmtPtr > b, uint32_t l)
Definition ast.hpp:368
Definition ast.hpp:15
AccessModifier
Access modifier for statements.
Definition ast.hpp:97
@ ACCESS_PRIVATE
Definition ast.hpp:99
@ ACCESS_PUBLIC
Definition ast.hpp:100
@ ACCESS_NONE
Definition ast.hpp:98
std::unique_ptr< Stmt > StmtPtr
Definition ast.hpp:125
TypeValue
Type values enum.
Definition ast.hpp:19
@ TYPE_STRING_LIT
Definition ast.hpp:28
@ TYPE_INT
Definition ast.hpp:23
@ TYPE_MODULE
Definition ast.hpp:31
@ TYPE_CHAR
Definition ast.hpp:21
@ TYPE_FLOAT
Definition ast.hpp:25
@ TYPE_BOOL
Definition ast.hpp:20
@ TYPE_LONG
Definition ast.hpp:24
@ TYPE_DOUBLE
Definition ast.hpp:26
@ TYPE_TRAIT
Definition ast.hpp:29
@ TYPE_CLASS
Definition ast.hpp:30
@ TYPE_NOTH
Definition ast.hpp:27
@ TYPE_SHORT
Definition ast.hpp:22
std::unique_ptr< Expr > ExprPtr
Definition ast.hpp:126
Type type
Definition ast.hpp:89
Argument(std::string n, Type t)
Definition ast.hpp:91
std::string name
Definition ast.hpp:88
Structure for describing the type.
Definition ast.hpp:37
std::string name
Definition ast.hpp:39
bool is_nullable
Definition ast.hpp:42
bool is_ptr
Definition ast.hpp:41
Type(TypeValue t, std::string n, bool ic=false, bool ip=false, bool in=false)
Definition ast.hpp:44
bool operator==(Type &other)
Definition ast.hpp:46
bool is_const
Definition ast.hpp:40
bool operator!=(Type &other)
Definition ast.hpp:50
TypeValue type
Definition ast.hpp:38
std::string to_str()
Method for convert type to string.
Definition ast.hpp:61
Structure for describing the value.
Definition ast.hpp:71
Value(bool v)
Definition ast.hpp:74
Value(char8_t v)
Definition ast.hpp:75
Value(int16_t v)
Definition ast.hpp:76
Value(float_t v)
Definition ast.hpp:79
std::variant< bool, char8_t, int16_t, int32_t, int64_t, float_t, double_t, std::string > value
Definition ast.hpp:72
Value(int32_t v)
Definition ast.hpp:77
Value(double_t v)
Definition ast.hpp:80
Value(std::string v)
Definition ast.hpp:81
Value(int64_t v)
Definition ast.hpp:78
Token structure.
Definition token.hpp:92
Header file for defining the token.