sammine-lang
Loading...
Searching...
No Matches
AstBase.h
Go to the documentation of this file.
1//
2// Created by jjasmine on 3/9/24.
3//
4
5#pragma once
6#include "ast/AstDecl.h"
7#include "lex/Lexer.h"
8#include "lex/Token.h"
9#include "typecheck/Types.h"
10#include "util/LexicalContext.h"
11#include "util/Utilities.h"
12#include <stack>
13
16namespace llvm {
17class Value;
18
19class Function;
20} // namespace llvm
21
22namespace sammine_lang {
23namespace AST {
24class Visitable;
25class AstBase;
26class ASTVisitor;
27
28struct ASTPrinter {
29 static void print(AstBase *t);
30 static void print(ProgramAST *t);
31};
33 ProgramAST *top_level_ast;
34
35public:
36 [[noreturn]] virtual void
37 abort(const std::string &msg = "<NO MESSAGE>") override final;
38
39 virtual void visit(ProgramAST *ast);
40 virtual void preorder_walk(ProgramAST *ast) = 0;
41 virtual void postorder_walk(ProgramAST *ast) = 0;
42
43 virtual void visit(VarDefAST *ast);
44 virtual void preorder_walk(VarDefAST *ast) = 0;
45 virtual void postorder_walk(VarDefAST *ast) = 0;
46
47 virtual void visit(ExternAST *ast);
48 virtual void preorder_walk(ExternAST *ast) = 0;
49 virtual void postorder_walk(ExternAST *ast) = 0;
50
51 virtual void visit(FuncDefAST *ast);
52 virtual void preorder_walk(FuncDefAST *ast) = 0;
53 virtual void postorder_walk(FuncDefAST *ast) = 0;
54
55 virtual void visit(RecordDefAST *ast);
56 virtual void preorder_walk(RecordDefAST *ast) = 0;
57 virtual void postorder_walk(RecordDefAST *ast) = 0;
58
59 virtual void visit(PrototypeAST *ast);
60 virtual void preorder_walk(PrototypeAST *ast) = 0;
61 virtual void postorder_walk(PrototypeAST *ast) = 0;
62
63 virtual void visit(CallExprAST *ast);
64 virtual void preorder_walk(CallExprAST *ast) = 0;
65 virtual void postorder_walk(CallExprAST *ast) = 0;
66
67 virtual void visit(ReturnExprAST *ast);
68 virtual void preorder_walk(ReturnExprAST *ast) = 0;
69 virtual void postorder_walk(ReturnExprAST *ast) = 0;
70
71 virtual void visit(BinaryExprAST *ast);
72 virtual void preorder_walk(BinaryExprAST *ast) = 0;
73 virtual void postorder_walk(BinaryExprAST *ast) = 0;
74
75 virtual void visit(NumberExprAST *ast);
76 virtual void preorder_walk(NumberExprAST *ast) = 0;
77 virtual void postorder_walk(NumberExprAST *ast) = 0;
78
79 virtual void visit(StringExprAST *ast);
80 virtual void preorder_walk(StringExprAST *ast) = 0;
81 virtual void postorder_walk(StringExprAST *ast) = 0;
82
83 virtual void visit(BoolExprAST *ast);
84 virtual void preorder_walk(BoolExprAST *ast) = 0;
85 virtual void postorder_walk(BoolExprAST *ast) = 0;
86
87 virtual void visit(UnitExprAST *ast);
88 virtual void preorder_walk(UnitExprAST *ast) = 0;
89 virtual void postorder_walk(UnitExprAST *ast) = 0;
90
91 virtual void visit(VariableExprAST *ast);
92 virtual void preorder_walk(VariableExprAST *ast) = 0;
93 virtual void postorder_walk(VariableExprAST *ast) = 0;
94
95 virtual void visit(BlockAST *ast);
96 virtual void preorder_walk(BlockAST *ast) = 0;
97 virtual void postorder_walk(BlockAST *ast) = 0;
98
99 virtual void visit(IfExprAST *ast);
100 virtual void preorder_walk(IfExprAST *ast) = 0;
101 virtual void postorder_walk(IfExprAST *ast) = 0;
102
103 virtual void visit(TypedVarAST *ast);
104 virtual void preorder_walk(TypedVarAST *ast) = 0;
105 virtual void postorder_walk(TypedVarAST *ast) = 0;
106
107 virtual ~ASTVisitor() = 0;
108};
109
110template <class T> class LexicalStack : public std::stack<LexicalContext<T>> {
111public:
112 void push_context() {
113 if (this->empty())
114 this->push(LexicalContext<T>());
115 else
116 this->push(LexicalContext<T>(&this->top()));
117 }
118 void pop_context() {
119 if (this->empty())
120 sammine_util::abort("ICE: You are popping an empty lexical stack");
121 this->pop();
122 }
123
124 void registerNameT(const std::string &name, T l) {
125 return this->top().registerNameT(name, l);
126 }
127 NameQueryResult recursiveQueryName(const std::string &name) const {
128 return this->top().recursiveQueryName(name);
129 }
130
131 T get_from_name(const std::string &name) {
132 return this->top().get_from_name(name);
133 }
134 NameQueryResult queryName(const std::string &name) const {
135 return this->top().queryName(name);
136 }
137
138 T recursive_get_from_name(const std::string &name) {
139 return this->top().recursive_get_from_name(name);
140 }
141
142 LexicalContext<T> *parent_scope() { return this->top().parent_scope; }
143};
144
146public:
147 virtual void enter_new_scope() = 0;
148 virtual void exit_new_scope() = 0;
149
153 virtual void visit(FuncDefAST *ast);
154
155 virtual ~ScopedASTVisitor() = 0;
156};
157
159public:
160 virtual Type synthesize(ProgramAST *ast) = 0;
161 virtual Type synthesize(VarDefAST *ast) = 0;
162 virtual Type synthesize(ExternAST *ast) = 0;
163 virtual Type synthesize(FuncDefAST *ast) = 0;
164 virtual Type synthesize(RecordDefAST *ast) = 0;
165 virtual Type synthesize(PrototypeAST *ast) = 0;
166 virtual Type synthesize(CallExprAST *ast) = 0;
167 virtual Type synthesize(ReturnExprAST *ast) = 0;
168 virtual Type synthesize(BinaryExprAST *ast) = 0;
169 virtual Type synthesize(NumberExprAST *ast) = 0;
170 virtual Type synthesize(StringExprAST *ast) = 0;
171 virtual Type synthesize(UnitExprAST *ast) = 0;
172 virtual Type synthesize(BoolExprAST *ast) = 0;
173 virtual Type synthesize(VariableExprAST *ast) = 0;
174 virtual Type synthesize(BlockAST *ast) = 0;
175 virtual Type synthesize(IfExprAST *ast) = 0;
176 virtual Type synthesize(TypedVarAST *ast) = 0;
177
178 virtual ~TypeCheckerVisitor() = 0;
179};
180
182public:
183 virtual ~Visitable() = default;
184 virtual void accept_vis(ASTVisitor *visitor) = 0;
185 virtual void walk_with_preorder(ASTVisitor *visitor) = 0;
186 virtual void walk_with_postorder(ASTVisitor *visitor) = 0;
187 virtual Type accept_synthesis(TypeCheckerVisitor *visitor) = 0;
188 virtual std::string getTreeName() = 0;
189};
190
191class AstBase : public Visitable {
192 void change_location(sammine_util::Location loc) {
193 if (first_location) {
194 this->location = loc;
195 first_location = false;
196 } else
197 this->get_location() |= loc;
198 }
199
200 bool first_location = true;
201
202protected:
203 sammine_util::Location location;
204
205public:
206 // INFO: Parser error
207 bool pe = false;
208 llvm::Value *val;
209 Type type = Type::NonExistent();
210 AstBase *join_location(AstBase *ast) {
211 if (!ast)
212 pe = true;
213 else
214 change_location(ast->get_location());
215
216 return this;
217 }
218
219 AstBase *join_location(std::shared_ptr<Token> tok) {
220
221 if (!tok)
222 pe = true;
223 else
224 change_location(tok->get_location());
225
226 return this;
227 }
228
229 AstBase *join_location(sammine_util::Location location) {
230 if (location.source_start <= 0 && location.source_end <= 0)
231 return this;
232
233 change_location(location);
234 return this;
235 }
236 sammine_util::Location get_location() { return this->location; }
237 bool synthesized() const { return this->type.synthesized(); }
238
239 bool checked() const { return this->type.checked(); }
240 void set_checked() { this->type.is_checked = true; }
241};
242} // namespace AST
243} // namespace sammine_lang
Holds declaration for all the AST Nodes.
Houses the lexer and the declaration of its method.
A simple scoping class, doesn't differentiate between different names, like variable name,...
NameQueryResult
NameQueryResult enum.
Definition LexicalContext.h:13
Defines the token structure (TokenType, TokStream, TokenMap)
Defines the core Type system for Sammine.
Holds classes and functionalities for dealing with Error handling, source locations caching & indexin...
LexicalContext class.
Definition LexicalContext.h:19
Definition AstBase.h:32
Definition AstBase.h:191
An AST to simulate a { } code block.
Definition Ast.h:158
Definition Ast.h:301
Definition Ast.h:375
A Function Definition that has the prototype and definition in terms of a block.
Definition Ast.h:129
Definition Ast.h:175
Definition Ast.h:432
Definition AstBase.h:110
A prototype to present "func func_name(...) -> type;".
Definition Ast.h:76
Definition AstBase.h:145
virtual void visit(FuncDefAST *ast)
Definition Ast.cpp:122
Definition Ast.h:406
A variable definition: "var x = expression;".
Definition Ast.h:231
Definition AstBase.h:181
Definition Utilities.h:56
Definition Utilities.h:107
Definition Types.h:46
Definition AstBase.h:28