sammine-lang
Loading...
Searching...
No Matches
Ast.h
Go to the documentation of this file.
1#pragma once
2#include "ast/AstBase.h"
3#include "ast/AstDecl.h"
4#include "util/QualifiedName.h"
5#include "util/Utilities.h"
6#include <cassert>
7#include <cstddef>
8#include <memory>
9#include <optional>
10#include <string>
11#include <vector>
12
16
17// clang-format off
18#define AST_NODE_METHODS(tree_name, kind_val) \
19 std::string getTreeName() const override { return tree_name; } \
20 static bool classof(const AstBase *node) { \
21 return node->getKind() == kind_val; \
22 } \
23 void accept_vis(ASTVisitor *visitor) override { visitor->visit(this); } \
24 void walk_with_preorder(ASTVisitor *visitor) override { \
25 visitor->preorder_walk(this); \
26 } \
27 void walk_with_postorder(ASTVisitor *visitor) override { \
28 visitor->postorder_walk(this); \
29 } \
30 Type accept_synthesis(TypeCheckerVisitor *visitor) override { \
31 return visitor->synthesize(this); \
32 }
33// clang-format on
34
35namespace sammine_lang {
36
37namespace AST {
38
39class Printable {};
40
41enum class ParseKind {
42 Simple,
43 Pointer,
44 Array,
45 Function,
46 Generic,
47 Tuple,
48};
49
50class TypeExprAST {
51 ParseKind kind;
52public:
53 ParseKind getKind() const { return kind; }
55 TypeExprAST(ParseKind kind) : kind(kind) {}
56 virtual ~TypeExprAST() = default;
57 virtual std::string to_string() const = 0;
58};
59
60class SimpleTypeExprAST : public TypeExprAST {
61public:
63 explicit SimpleTypeExprAST(std::shared_ptr<Token> tok)
64 : TypeExprAST(ParseKind::Simple),
65 name(sammine_util::QualifiedName::local(tok ? tok->lexeme : "")) {
66 if (tok)
67 location = tok->get_location();
68 }
69 explicit SimpleTypeExprAST(sammine_util::QualifiedName qn,
71 : TypeExprAST(ParseKind::Simple), name(std::move(qn)) {
72 location = loc;
73 }
74 static bool classof(const TypeExprAST *node) {
75 return node->getKind() == ParseKind::Simple;
76 }
77 std::string to_string() const override { return name.mangled(); }
78};
79
80class PointerTypeExprAST : public TypeExprAST {
81public:
82 std::unique_ptr<TypeExprAST> pointee;
83 bool is_linear = false;
84 explicit PointerTypeExprAST(std::unique_ptr<TypeExprAST> pointee,
85 bool is_linear = false)
86 : TypeExprAST(ParseKind::Pointer), pointee(std::move(pointee)),
87 is_linear(is_linear) {}
88 static bool classof(const TypeExprAST *node) {
89 return node->getKind() == ParseKind::Pointer;
90 }
91 std::string to_string() const override {
92 return (is_linear ? "'" : "") + std::string("ptr<") + pointee->to_string() + ">";
93 }
94};
95
96class ArrayTypeExprAST : public TypeExprAST {
97public:
98 std::unique_ptr<TypeExprAST> element;
99 size_t size;
100 ArrayTypeExprAST(std::unique_ptr<TypeExprAST> element, size_t size)
101 : TypeExprAST(ParseKind::Array), element(std::move(element)),
102 size(size) {}
103 static bool classof(const TypeExprAST *node) {
104 return node->getKind() == ParseKind::Array;
105 }
106 std::string to_string() const override {
107 return "[" + element->to_string() + ";" + std::to_string(size) + "]";
108 }
109};
110
111class FunctionTypeExprAST : public TypeExprAST {
112public:
113 std::vector<std::unique_ptr<TypeExprAST>> paramTypes;
114 std::unique_ptr<TypeExprAST> returnType;
115 FunctionTypeExprAST(std::vector<std::unique_ptr<TypeExprAST>> paramTypes,
116 std::unique_ptr<TypeExprAST> returnType)
117 : TypeExprAST(ParseKind::Function),
118 paramTypes(std::move(paramTypes)), returnType(std::move(returnType)) {}
119 static bool classof(const TypeExprAST *node) {
120 return node->getKind() == ParseKind::Function;
121 }
122 std::string to_string() const override {
123 std::string res = "(";
124 for (size_t i = 0; i < paramTypes.size(); i++) {
125 res += paramTypes[i]->to_string();
126 if (i != paramTypes.size() - 1)
127 res += ", ";
128 }
129 res += ") -> " + returnType->to_string();
130 return res;
131 }
132};
133
134// Generic type expression: Option<i32>, Result<i32, String>
135class GenericTypeExprAST : public TypeExprAST {
136public:
138 std::vector<std::unique_ptr<TypeExprAST>> type_args;
139 GenericTypeExprAST(sammine_util::QualifiedName base_name,
140 std::vector<std::unique_ptr<TypeExprAST>> type_args,
142 : TypeExprAST(ParseKind::Generic), base_name(std::move(base_name)),
143 type_args(std::move(type_args)) {
144 location = loc;
145 }
146 static bool classof(const TypeExprAST *node) {
147 return node->getKind() == ParseKind::Generic;
148 }
149 std::string to_string() const override {
150 std::string res = base_name.mangled() + "<";
151 for (size_t i = 0; i < type_args.size(); i++) {
152 res += type_args[i]->to_string();
153 if (i != type_args.size() - 1)
154 res += ", ";
155 }
156 res += ">";
157 return res;
158 }
159};
160
161class TupleTypeExprAST : public TypeExprAST {
162public:
163 std::vector<std::unique_ptr<TypeExprAST>> element_types;
164 TupleTypeExprAST(std::vector<std::unique_ptr<TypeExprAST>> element_types)
165 : TypeExprAST(ParseKind::Tuple),
166 element_types(std::move(element_types)) {}
167 static bool classof(const TypeExprAST *node) {
168 return node->getKind() == ParseKind::Tuple;
169 }
170 std::string to_string() const override {
171 std::string res = "(";
172 for (size_t i = 0; i < element_types.size(); i++) {
173 res += element_types[i]->to_string();
174 if (i != element_types.size() - 1)
175 res += ", ";
176 }
177 res += ")";
178 return res;
179 }
180};
181
182class DefinitionAST : public AstBase, public Printable {
183public:
184 DefinitionAST(NodeKind kind) : AstBase(kind) {}
185 static bool classof(const AstBase *node) {
186 return node->getKind() >= NodeKind::FirstDef &&
187 node->getKind() <= NodeKind::LastDef;
188 }
189};
190
192 std::string module_name;
193 std::string alias;
194 sammine_util::Location location;
195};
196
197class ProgramAST : public AstBase, public Printable {
198public:
199 ProgramAST() : AstBase(NodeKind::ProgramAST) {}
200 std::vector<ImportDecl> imports;
201 std::vector<std::unique_ptr<DefinitionAST>> DefinitionVec;
202 AST_NODE_METHODS("ProgramAST", NodeKind::ProgramAST)
203};
204
205class TypedVarAST : public AstBase, public Printable {
206public:
207 std::string name;
208 bool is_mutable = false;
209 std::unique_ptr<TypeExprAST> type_expr;
210
211 explicit TypedVarAST(std::shared_ptr<Token> name,
212 std::unique_ptr<TypeExprAST> type_expr,
213 bool is_mutable = false)
214 : AstBase(NodeKind::TypedVarAST), is_mutable(is_mutable),
215 type_expr(std::move(type_expr)) {
216 this->join_location(name);
217 if (name)
218 this->name = name->lexeme;
219 if (this->type_expr)
220 this->join_location(this->type_expr->location);
221 }
222 explicit TypedVarAST(std::shared_ptr<Token> name, bool is_mutable = false)
223 : AstBase(NodeKind::TypedVarAST), is_mutable(is_mutable) {
224 this->join_location(name);
225 if (name)
226 this->name = name->lexeme;
227 }
228 std::string to_string() const;
229 AST_NODE_METHODS("TypedVarAST", NodeKind::TypedVarAST)
230};
231
233
236class PrototypeAST : public AstBase, public Printable {
237public:
238 llvm::Function *function;
239 sammine_util::QualifiedName functionName;
240 std::unique_ptr<TypeExprAST> return_type_expr;
241 std::vector<std::unique_ptr<AST::TypedVarAST>> parameterVectors;
242 std::vector<std::string> type_params;
243 bool is_var_arg = false;
244 bool is_generic() const { return !type_params.empty(); }
245
246 explicit PrototypeAST(
247 sammine_util::QualifiedName functionName,
248 sammine_util::Location name_loc,
249 std::unique_ptr<TypeExprAST> return_type_expr,
250 std::vector<std::unique_ptr<AST::TypedVarAST>> parameterVectors)
251 : AstBase(NodeKind::PrototypeAST),
252 return_type_expr(std::move(return_type_expr)) {
253 this->functionName = std::move(functionName);
254 this->join_location(name_loc);
255 if (this->return_type_expr)
256 this->join_location(this->return_type_expr->location);
257
258 this->parameterVectors = std::move(parameterVectors);
259
260 for (size_t i = 0; i < this->parameterVectors.size(); i++)
261 assert(this->parameterVectors[i]);
262 for (size_t i = 0; i < this->parameterVectors.size(); i++)
263 this->join_location(this->parameterVectors[i]->get_location());
264 }
265
266 explicit PrototypeAST(
267 sammine_util::QualifiedName functionName,
268 sammine_util::Location name_loc,
269 std::vector<std::unique_ptr<AST::TypedVarAST>> parameterVectors)
270 : AstBase(NodeKind::PrototypeAST) {
271 this->functionName = std::move(functionName);
272 this->join_location(name_loc);
273
274 this->parameterVectors = std::move(parameterVectors);
275
276 for (size_t i = 0; i < this->parameterVectors.size(); i++)
277 assert(this->parameterVectors[i]);
278 for (size_t i = 0; i < this->parameterVectors.size(); i++)
279 this->join_location(this->parameterVectors[i]->get_location());
280 }
281
282 bool returnsUnit() const { return return_type_expr == nullptr; }
283
284 std::string to_string() const;
285
286 AST_NODE_METHODS("PrototypeAST", NodeKind::PrototypeAST)
287};
288
291class ExternAST : public DefinitionAST {
292public:
293 std::unique_ptr<PrototypeAST> Prototype;
294 bool is_exposed = false;
295
296 ExternAST(std::unique_ptr<PrototypeAST> Prototype)
297 : DefinitionAST(NodeKind::ExternAST), Prototype(std::move(Prototype)) {
298 this->join_location(this->Prototype.get());
299 }
300 AST_NODE_METHODS("ExternAST", NodeKind::ExternAST)
301};
302class ExprAST : public AstBase, public Printable {
303public:
304 bool is_statement = true;
305 ExprAST(NodeKind kind) : AstBase(kind) {}
306 ~ExprAST() = default;
307 static bool classof(const AstBase *node) {
308 return node->getKind() >= NodeKind::FirstExpr &&
309 node->getKind() <= NodeKind::LastExpr;
310 }
311 virtual std::string to_string() const = 0;
312};
313
317class BlockAST : public AstBase, public Printable {
318
319public:
320 BlockAST() : AstBase(NodeKind::BlockAST) {}
321 std::vector<std::unique_ptr<ExprAST>> Statements;
322 std::string to_string() const;
323 AST_NODE_METHODS("BlockAST", NodeKind::BlockAST)
324};
325
326class FuncDefAST : public DefinitionAST {
327public:
328 ~FuncDefAST() = default;
329 std::unique_ptr<PrototypeAST> Prototype;
330 std::unique_ptr<BlockAST> Block;
331 bool is_exported = false;
332
333 FuncDefAST(std::unique_ptr<PrototypeAST> Prototype,
334 std::unique_ptr<BlockAST> Block)
335 : DefinitionAST(NodeKind::FuncDefAST), Prototype(std::move(Prototype)),
336 Block(std::move(Block)) {
337 this->join_location(this->Prototype.get())
338 ->join_location(this->Block.get());
339 }
340
341 std::string getFunctionName() const { return Prototype->functionName.mangled(); }
342
343 bool returnsUnit() const { return Prototype->returnsUnit(); }
344
345 std::string to_string() const;
346
347 AST_NODE_METHODS("FuncDefAST", NodeKind::FuncDefAST)
348};
349
350// struct id { typed_var }
351class StructDefAST : public DefinitionAST {
352public:
353 sammine_util::QualifiedName struct_name;
354 std::vector<std::unique_ptr<TypedVarAST>> struct_members;
355 bool is_exported = false;
356
357 explicit StructDefAST(sammine_util::QualifiedName name,
358 sammine_util::Location name_loc,
359 decltype(struct_members) struct_members)
360 : DefinitionAST(NodeKind::StructDefAST),
361 struct_name(std::move(name)),
362 struct_members(std::move(struct_members)) {
363 this->join_location(name_loc);
364 for (auto &m : struct_members)
365 this->join_location(m.get());
366 }
367 AST_NODE_METHODS("StructDefAST", NodeKind::StructDefAST)
368};
369
370// enum Name = Variant1(Type) | Variant2 | Variant3(Type, Type);
372 std::string name;
373 std::vector<std::unique_ptr<TypeExprAST>> payload_types;
374 std::optional<int64_t> discriminant_value;
375 sammine_util::Location location;
376};
377
378class EnumDefAST : public DefinitionAST {
379public:
381 std::vector<EnumVariantDef> variants;
382 std::vector<std::string> type_params;
383 bool is_integer_backed = false;
384 bool is_exported = false;
385 std::optional<std::string> backing_type_name;
386
387 explicit EnumDefAST(sammine_util::QualifiedName name,
388 sammine_util::Location name_loc,
389 std::vector<EnumVariantDef> variants)
390 : DefinitionAST(NodeKind::EnumDefAST),
391 enum_name(std::move(name)), variants(std::move(variants)) {
392 this->join_location(name_loc);
393 for (auto &v : this->variants)
394 this->join_location(v.location);
395 }
396 AST_NODE_METHODS("EnumDefAST", NodeKind::EnumDefAST)
397};
398
399// type Name = ExistingType;
400class TypeAliasDefAST : public DefinitionAST {
401public:
403 std::unique_ptr<TypeExprAST> type_expr;
404 bool is_exported = false;
405
406 explicit TypeAliasDefAST(std::shared_ptr<Token> name_tok,
407 std::unique_ptr<TypeExprAST> type_expr)
408 : DefinitionAST(NodeKind::TypeAliasDefAST),
409 type_expr(std::move(type_expr)) {
410 if (name_tok)
411 alias_name = sammine_util::QualifiedName::local(name_tok->lexeme);
412 this->join_location(name_tok);
413 if (this->type_expr)
414 this->join_location(this->type_expr->location);
415 }
416 AST_NODE_METHODS("TypeAliasDefAST", NodeKind::TypeAliasDefAST)
417};
418
420class VarDefAST : public ExprAST {
421public:
422 bool is_mutable = false;
423 bool is_tuple_destructure = false;
424 std::unique_ptr<TypedVarAST> TypedVar; // single var (existing)
425 std::vector<std::unique_ptr<TypedVarAST>> destructure_vars; // tuple (new)
426 std::unique_ptr<ExprAST> Expression;
427
428 explicit VarDefAST(std::shared_ptr<Token> let,
429 std::unique_ptr<TypedVarAST> TypedVar,
430 std::unique_ptr<ExprAST> Expression,
431 bool is_mutable = false)
432 : ExprAST(NodeKind::VarDefAST), is_mutable(is_mutable),
433 TypedVar(std::move(TypedVar)), Expression(std::move(Expression)) {
434
435 this->join_location(let)
436 ->join_location(this->TypedVar.get())
437 ->join_location(this->Expression.get());
438 };
439
440 // Destructuring constructor: let (a, b) = expr;
441 explicit VarDefAST(std::shared_ptr<Token> let,
442 std::vector<std::unique_ptr<TypedVarAST>> destructure_vars,
443 std::unique_ptr<ExprAST> Expression,
444 bool is_mutable = false)
445 : ExprAST(NodeKind::VarDefAST), is_mutable(is_mutable),
446 is_tuple_destructure(true),
447 destructure_vars(std::move(destructure_vars)),
448 Expression(std::move(Expression)) {
449 this->join_location(let);
450 for (auto &v : this->destructure_vars)
451 this->join_location(v.get());
452 this->join_location(this->Expression.get());
453 };
454
455 std::string to_string() const override;
456 AST_NODE_METHODS("VarDefAST", NodeKind::VarDefAST)
457};
458class NumberExprAST : public ExprAST {
459public:
460 std::string number;
461
462 explicit NumberExprAST(std::shared_ptr<Token> t)
463 : ExprAST(NodeKind::NumberExprAST) {
464 assert(t);
465 join_location(t);
466 number = t->lexeme;
467 }
468 std::string to_string() const override;
469 AST_NODE_METHODS("NumberExprAST", NodeKind::NumberExprAST)
470};
471class StringExprAST : public ExprAST {
472public:
473 std::string string_content;
474
475 explicit StringExprAST(std::shared_ptr<Token> t)
476 : ExprAST(NodeKind::StringExprAST) {
477 assert(t);
478 join_location(t);
479 string_content = t->lexeme;
480 }
481 std::string to_string() const override;
482 AST_NODE_METHODS("StringExprAST", NodeKind::StringExprAST)
483};
484
485class BoolExprAST : public ExprAST {
486public:
487 bool b;
488 BoolExprAST(bool b, sammine_util::Location loc)
489 : ExprAST(NodeKind::BoolExprAST), b(b) {
490 this->location = loc;
491 }
492 std::string to_string() const override;
493 AST_NODE_METHODS("BoolExprAST", NodeKind::BoolExprAST)
494};
495class CharExprAST : public ExprAST {
496public:
497 char value;
498 CharExprAST(char value, sammine_util::Location loc)
499 : ExprAST(NodeKind::CharExprAST), value(value) {
500 this->location = loc;
501 }
502 std::string to_string() const override;
503 AST_NODE_METHODS("CharExprAST", NodeKind::CharExprAST)
504};
505class BinaryExprAST : public ExprAST {
506public:
507 std::shared_ptr<Token> Op;
508 std::unique_ptr<ExprAST> LHS, RHS;
509 BinaryExprAST(std::shared_ptr<Token> op, std::unique_ptr<ExprAST> LHS,
510 std::unique_ptr<ExprAST> RHS)
511 : ExprAST(NodeKind::BinaryExprAST), Op(op), LHS(std::move(LHS)),
512 RHS(std::move(RHS)) {
513 this->join_location(this->Op)
514 ->join_location(this->LHS.get())
515 ->join_location(this->RHS.get());
516 }
517
518 std::string to_string() const override;
519 AST_NODE_METHODS("BinaryExprAST", NodeKind::BinaryExprAST)
520};
521class ReturnExprAST : public ExprAST {
522
523public:
524 bool is_implicit;
525 std::unique_ptr<ExprAST> return_expr;
526 ReturnExprAST(std::shared_ptr<Token> return_tok,
527 std::unique_ptr<ExprAST> return_expr)
528 : ExprAST(NodeKind::ReturnExprAST), is_implicit(false),
529 return_expr(std::move(return_expr)) {
530 if (this->return_expr == nullptr) {
531 this->join_location(return_tok);
532 } else if (return_tok == nullptr) {
533 this->join_location(this->return_expr.get());
534 } else if (return_tok && this->return_expr) {
535 this->join_location(return_tok)->join_location(this->return_expr.get());
536 }
537 }
538
539 ReturnExprAST(std::unique_ptr<ExprAST> return_expr)
540 : ExprAST(NodeKind::ReturnExprAST), is_implicit(true),
541 return_expr(std::move(return_expr)) {
542 this->join_location(this->return_expr.get());
543 }
544
545 std::string to_string() const override;
546 AST_NODE_METHODS("ReturnExprAST", NodeKind::ReturnExprAST)
547};
548class CallExprAST : public ExprAST {
549
550public:
551 sammine_util::QualifiedName functionName;
552 std::vector<std::unique_ptr<AST::ExprAST>> arguments;
553 std::vector<std::unique_ptr<TypeExprAST>> explicit_type_args;
554 explicit CallExprAST(
555 std::shared_ptr<Token> tok,
556 std::vector<std::unique_ptr<AST::ExprAST>> arguments = {})
557 : ExprAST(NodeKind::CallExprAST),
558 functionName(sammine_util::QualifiedName::local(
559 tok ? tok->lexeme : "")) {
560 join_location(tok);
561 for (auto &arg : arguments)
562 if (arg)
563 this->join_location(arg.get());
564 this->arguments = std::move(arguments);
565 }
566 explicit CallExprAST(
568 std::vector<std::unique_ptr<AST::ExprAST>> arguments = {})
569 : ExprAST(NodeKind::CallExprAST), functionName(std::move(qn)) {
570 this->location = loc;
571 for (auto &arg : arguments)
572 if (arg)
573 this->join_location(arg.get());
574 this->arguments = std::move(arguments);
575 }
576
577 std::string to_string() const override;
578 AST_NODE_METHODS("CallExprAST", NodeKind::CallExprAST)
579};
580
581class UnitExprAST : public ExprAST {
582
583public:
584 bool is_implicit;
585
586 explicit UnitExprAST(std::shared_ptr<Token> left_paren,
587 std::shared_ptr<Token> right_paren)
588 : ExprAST(NodeKind::UnitExprAST), is_implicit(false) {
589 assert(left_paren);
590 assert(right_paren);
591 this->join_location(left_paren)->join_location(right_paren);
592 };
593 explicit UnitExprAST() : ExprAST(NodeKind::UnitExprAST), is_implicit(true) {}
594
595 std::string to_string() const override;
596 AST_NODE_METHODS("UnitExprAST", NodeKind::UnitExprAST)
597};
598class IfExprAST : public ExprAST {
599public:
600 std::unique_ptr<ExprAST> bool_expr;
601 std::unique_ptr<BlockAST> thenBlockAST, elseBlockAST;
602 explicit IfExprAST(std::unique_ptr<ExprAST> bool_expr,
603 std::unique_ptr<BlockAST> thenBlockAST,
604 std::unique_ptr<BlockAST> elseBlockAST)
605 : ExprAST(NodeKind::IfExprAST), bool_expr(std::move(bool_expr)),
606 thenBlockAST(std::move(thenBlockAST)),
607 elseBlockAST(std::move(elseBlockAST)) {
608 this->join_location(this->bool_expr.get())
609 ->join_location(this->thenBlockAST.get())
610 ->join_location(this->elseBlockAST.get());
611 }
612
613 std::string to_string() const override;
614 AST_NODE_METHODS("IfExprAST", NodeKind::IfExprAST)
615};
616class VariableExprAST : public ExprAST {
617public:
618 std::string variableName;
619 VariableExprAST(std::shared_ptr<Token> var)
620 : ExprAST(NodeKind::VariableExprAST) {
621 join_location(var);
622 if (var)
623 variableName = var->lexeme;
624 };
625
626 std::string to_string() const override;
627 AST_NODE_METHODS("VariableExprAST", NodeKind::VariableExprAST)
628};
629class DerefExprAST : public ExprAST {
630public:
631 std::unique_ptr<ExprAST> operand;
632 explicit DerefExprAST(std::shared_ptr<Token> star_tok,
633 std::unique_ptr<ExprAST> operand)
634 : ExprAST(NodeKind::DerefExprAST), operand(std::move(operand)) {
635 this->join_location(star_tok)->join_location(this->operand.get());
636 }
637 std::string to_string() const override;
638 AST_NODE_METHODS("DerefExprAST", NodeKind::DerefExprAST)
639};
640
641class AddrOfExprAST : public ExprAST {
642public:
643 std::unique_ptr<ExprAST> operand;
644 explicit AddrOfExprAST(std::shared_ptr<Token> amp_tok,
645 std::unique_ptr<ExprAST> operand)
646 : ExprAST(NodeKind::AddrOfExprAST), operand(std::move(operand)) {
647 this->join_location(amp_tok)->join_location(this->operand.get());
648 }
649 std::string to_string() const override;
650 AST_NODE_METHODS("AddrOfExprAST", NodeKind::AddrOfExprAST)
651};
652class AllocExprAST : public ExprAST {
653public:
654 std::unique_ptr<TypeExprAST> type_arg; // the T in alloc<T>(count)
655 std::unique_ptr<ExprAST> operand; // the count expression
656 explicit AllocExprAST(std::shared_ptr<Token> tok,
657 std::unique_ptr<TypeExprAST> type_arg,
658 std::unique_ptr<ExprAST> operand)
659 : ExprAST(NodeKind::AllocExprAST), type_arg(std::move(type_arg)),
660 operand(std::move(operand)) {
661 this->join_location(tok)->join_location(this->operand.get());
662 }
663 std::string to_string() const override;
664 AST_NODE_METHODS("AllocExprAST", NodeKind::AllocExprAST)
665};
666
667class FreeExprAST : public ExprAST {
668public:
669 std::unique_ptr<ExprAST> operand;
670 explicit FreeExprAST(std::shared_ptr<Token> tok,
671 std::unique_ptr<ExprAST> operand)
672 : ExprAST(NodeKind::FreeExprAST), operand(std::move(operand)) {
673 this->join_location(tok)->join_location(this->operand.get());
674 }
675 std::string to_string() const override;
676 AST_NODE_METHODS("FreeExprAST", NodeKind::FreeExprAST)
677};
678class ArrayLiteralExprAST : public ExprAST {
679public:
680 std::vector<std::unique_ptr<ExprAST>> elements;
681 explicit ArrayLiteralExprAST(std::vector<std::unique_ptr<ExprAST>> elements)
682 : ExprAST(NodeKind::ArrayLiteralExprAST),
683 elements(std::move(elements)) {
684 for (auto &e : this->elements)
685 if (e)
686 this->join_location(e.get());
687 }
688 std::string to_string() const override;
689 AST_NODE_METHODS("ArrayLiteralExprAST", NodeKind::ArrayLiteralExprAST)
690};
691
692class IndexExprAST : public ExprAST {
693public:
694 std::unique_ptr<ExprAST> array_expr;
695 std::unique_ptr<ExprAST> index_expr;
696 explicit IndexExprAST(std::unique_ptr<ExprAST> array_expr,
697 std::unique_ptr<ExprAST> index_expr)
698 : ExprAST(NodeKind::IndexExprAST), array_expr(std::move(array_expr)),
699 index_expr(std::move(index_expr)) {
700 this->join_location(this->array_expr.get())
701 ->join_location(this->index_expr.get());
702 }
703 std::string to_string() const override;
704 AST_NODE_METHODS("IndexExprAST", NodeKind::IndexExprAST)
705};
706
707class LenExprAST : public ExprAST {
708public:
709 std::unique_ptr<ExprAST> operand;
710 explicit LenExprAST(std::shared_ptr<Token> tok,
711 std::unique_ptr<ExprAST> operand)
712 : ExprAST(NodeKind::LenExprAST), operand(std::move(operand)) {
713 this->join_location(tok)->join_location(this->operand.get());
714 }
715 std::string to_string() const override;
716 AST_NODE_METHODS("LenExprAST", NodeKind::LenExprAST)
717};
718
719class UnaryNegExprAST : public ExprAST {
720public:
721 std::unique_ptr<ExprAST> operand;
722 explicit UnaryNegExprAST(std::shared_ptr<Token> op_tok,
723 std::unique_ptr<ExprAST> operand)
724 : ExprAST(NodeKind::UnaryNegExprAST), operand(std::move(operand)) {
725 this->join_location(op_tok)->join_location(this->operand.get());
726 }
727 std::string to_string() const override;
728 AST_NODE_METHODS("UnaryNegExprAST", NodeKind::UnaryNegExprAST)
729};
730
731class StructLiteralExprAST : public ExprAST {
732public:
733 sammine_util::QualifiedName struct_name;
734 std::vector<std::string> field_names;
735 std::vector<std::unique_ptr<ExprAST>> field_values;
736 explicit StructLiteralExprAST(
737 std::shared_ptr<Token> name_tok,
738 std::vector<std::string> field_names,
739 std::vector<std::unique_ptr<ExprAST>> field_values)
740 : ExprAST(NodeKind::StructLiteralExprAST),
741 struct_name(sammine_util::QualifiedName::local(
742 name_tok ? name_tok->lexeme : "")),
743 field_names(std::move(field_names)),
744 field_values(std::move(field_values)) {
745 this->join_location(name_tok);
746 for (auto &v : this->field_values)
747 if (v)
748 this->join_location(v.get());
749 }
750 explicit StructLiteralExprAST(
752 std::vector<std::string> field_names,
753 std::vector<std::unique_ptr<ExprAST>> field_values)
754 : ExprAST(NodeKind::StructLiteralExprAST),
755 struct_name(std::move(qn)),
756 field_names(std::move(field_names)),
757 field_values(std::move(field_values)) {
758 this->location = loc;
759 for (auto &v : this->field_values)
760 if (v)
761 this->join_location(v.get());
762 }
763 std::string to_string() const override;
764 AST_NODE_METHODS("StructLiteralExprAST", NodeKind::StructLiteralExprAST)
765};
766
767class FieldAccessExprAST : public ExprAST {
768public:
769 std::unique_ptr<ExprAST> object_expr;
770 std::string field_name;
771 explicit FieldAccessExprAST(std::unique_ptr<ExprAST> object_expr,
772 std::shared_ptr<Token> field_tok)
773 : ExprAST(NodeKind::FieldAccessExprAST),
774 object_expr(std::move(object_expr)) {
775 this->join_location(this->object_expr.get());
776 this->join_location(field_tok);
777 if (field_tok)
778 this->field_name = field_tok->lexeme;
779 }
780 std::string to_string() const override;
781 AST_NODE_METHODS("FieldAccessExprAST", NodeKind::FieldAccessExprAST)
782};
783
785 sammine_util::QualifiedName variant_name =
786 sammine_util::QualifiedName::local("");
787 std::vector<std::string> bindings;
788 bool is_wildcard = false;
789 sammine_util::Location location;
790 size_t variant_index = 0;
791};
792
793struct CaseArm {
794 CasePattern pattern;
795 std::unique_ptr<BlockAST> body;
796};
797
798class CaseExprAST : public ExprAST {
799public:
800 std::unique_ptr<ExprAST> scrutinee;
801 std::vector<CaseArm> arms;
802
803 explicit CaseExprAST(std::shared_ptr<Token> case_tok,
804 std::unique_ptr<ExprAST> scrutinee,
805 std::vector<CaseArm> arms)
806 : ExprAST(NodeKind::CaseExprAST), scrutinee(std::move(scrutinee)),
807 arms(std::move(arms)) {
808 this->join_location(case_tok)->join_location(this->scrutinee.get());
809 for (auto &arm : this->arms) {
810 if (arm.body)
811 this->join_location(arm.body.get());
812 }
813 }
814
815 std::string to_string() const override;
816 AST_NODE_METHODS("CaseExprAST", NodeKind::CaseExprAST)
817};
818
819class WhileExprAST : public ExprAST {
820public:
821 std::unique_ptr<ExprAST> condition;
822 std::unique_ptr<BlockAST> body;
823 explicit WhileExprAST(std::unique_ptr<ExprAST> condition,
824 std::unique_ptr<BlockAST> body)
825 : ExprAST(NodeKind::WhileExprAST), condition(std::move(condition)),
826 body(std::move(body)) {
827 this->join_location(this->condition.get())
828 ->join_location(this->body.get());
829 }
830 std::string to_string() const override;
831 AST_NODE_METHODS("WhileExprAST", NodeKind::WhileExprAST)
832};
833
834class TupleLiteralExprAST : public ExprAST {
835public:
836 std::vector<std::unique_ptr<ExprAST>> elements;
837 explicit TupleLiteralExprAST(std::vector<std::unique_ptr<ExprAST>> elements)
838 : ExprAST(NodeKind::TupleLiteralExprAST),
839 elements(std::move(elements)) {
840 for (auto &e : this->elements)
841 if (e)
842 this->join_location(e.get());
843 }
844 std::string to_string() const override;
845 AST_NODE_METHODS("TupleLiteralExprAST", NodeKind::TupleLiteralExprAST)
846};
847
848class TypeClassDeclAST : public DefinitionAST {
849public:
850 std::string class_name;
851 std::string type_param;
852 std::vector<std::unique_ptr<PrototypeAST>> methods;
853 explicit TypeClassDeclAST(std::shared_ptr<Token> tok, std::string name,
854 std::string param,
855 std::vector<std::unique_ptr<PrototypeAST>> methods)
856 : DefinitionAST(NodeKind::TypeClassDeclAST),
857 class_name(std::move(name)), type_param(std::move(param)),
858 methods(std::move(methods)) {
859 this->join_location(tok);
860 }
861 AST_NODE_METHODS("TypeClassDeclAST", NodeKind::TypeClassDeclAST)
862};
863
864class TypeClassInstanceAST : public DefinitionAST {
865public:
866 std::string class_name;
867 std::unique_ptr<TypeExprAST> concrete_type_expr;
868 std::vector<std::unique_ptr<FuncDefAST>> methods;
869 explicit TypeClassInstanceAST(
870 std::shared_ptr<Token> tok, std::string class_name,
871 std::unique_ptr<TypeExprAST> type_expr,
872 std::vector<std::unique_ptr<FuncDefAST>> methods)
873 : DefinitionAST(NodeKind::TypeClassInstanceAST),
874 class_name(std::move(class_name)),
875 concrete_type_expr(std::move(type_expr)),
876 methods(std::move(methods)) {
877 this->join_location(tok);
878 }
879 AST_NODE_METHODS("TypeClassInstanceAST", NodeKind::TypeClassInstanceAST)
880};
881
882} // namespace AST
883} // namespace sammine_lang
884
885#undef AST_NODE_METHODS
Defines the AST Abstract class for printing out AST Nodes.
Holds declaration for all the AST Nodes.
Holds classes and functionalities for dealing with Error handling, source locations caching & indexin...
Definition Ast.h:39
Definition Utilities.h:70
Definition Ast.h:793
Definition Ast.h:784
Definition Ast.h:191
Definition QualifiedName.h:23