sammine-lang
Loading...
Searching...
No Matches
BiTypeChecker.h
Go to the documentation of this file.
1#pragma once
2
3#include "ast/AstBase.h"
4#include "typecheck/Types.h"
6
11namespace sammine_lang {
12
13namespace AST {
14
15class TypingContext : public LexicalContext<Type> {};
16class BiTypeCheckerVisitor : public ScopedASTVisitor,
17 public TypeCheckerVisitor {
21
22 // We're gonna provide look up in different
23
24public:
25 // INFO: x, y, z
27
28 // INFO: i64, f64 bla bla bla
29 LexicalStack<Type> typename_to_type;
30 TypeMapOrdering type_map_ordering;
31 virtual void enter_new_scope() override {
32 id_to_type.push_context();
33 typename_to_type.push_context();
34
35 typename_to_type.registerNameT("i64", Type::I64_t());
36 typename_to_type.registerNameT("f64", Type::F64_t());
37 typename_to_type.registerNameT("bool", Type::Bool());
38 typename_to_type.registerNameT("unit", Type::Unit());
39 }
40 virtual void exit_new_scope() override {
41 id_to_type.pop();
42 typename_to_type.pop();
43 }
44 BiTypeCheckerVisitor() { this->enter_new_scope(); }
45
46 std::optional<Type> get_type_from_id(const std::string &str) {
47
48 auto &id_name_top = id_to_type.top();
49 if (id_name_top.queryName(str) == nameNotFound) {
50 this->abort(
51 fmt::format("Name '{}' not found, this should not happen", str));
52 }
53 return id_name_top.get_from_name(str);
54 }
55
56 std::optional<Type> get_type_from_id_parent(const std::string &str) {
57
58 auto &id_name_top = *id_to_type.top().parent_scope;
59 if (id_name_top.queryName(str) == nameNotFound) {
60 this->abort(
61 fmt::format("Name '{}' not found, this should not happen", str));
62 }
63 return id_name_top.get_from_name(str);
64 }
65
66 std::optional<Type> get_typename_type(const std::string &str) {
67 auto &typename_top = typename_to_type.top();
68 if (typename_top.queryName(str) == nameNotFound) {
69 return std::nullopt;
70 }
71 return typename_top.get_from_name(str);
72 }
73
74 // pre order
75
76 virtual void preorder_walk(ProgramAST *ast) override;
77 virtual void preorder_walk(VarDefAST *ast) override;
78 virtual void preorder_walk(ExternAST *ast) override;
79 virtual void preorder_walk(FuncDefAST *ast) override;
80 virtual void preorder_walk(RecordDefAST *ast) override;
81 virtual void preorder_walk(PrototypeAST *ast) override;
82 virtual void preorder_walk(CallExprAST *ast) override;
83 virtual void preorder_walk(ReturnExprAST *ast) override;
84 virtual void preorder_walk(BinaryExprAST *ast) override;
85 virtual void preorder_walk(NumberExprAST *ast) override;
86 virtual void preorder_walk(StringExprAST *ast) override;
87 virtual void preorder_walk(BoolExprAST *ast) override;
88 virtual void preorder_walk(UnitExprAST *ast) override;
89 virtual void preorder_walk(VariableExprAST *ast) override;
90 virtual void preorder_walk(BlockAST *ast) override;
91 virtual void preorder_walk(IfExprAST *ast) override;
92 virtual void preorder_walk(TypedVarAST *ast) override;
93
94 // post order
95 virtual void postorder_walk(ProgramAST *ast) override;
96 virtual void postorder_walk(VarDefAST *ast) override;
97 virtual void postorder_walk(ExternAST *ast) override;
98 virtual void postorder_walk(FuncDefAST *ast) override;
99 virtual void postorder_walk(RecordDefAST *ast) override;
100 virtual void postorder_walk(PrototypeAST *ast) override;
101 virtual void postorder_walk(CallExprAST *ast) override;
102 virtual void postorder_walk(ReturnExprAST *ast) override;
103 virtual void postorder_walk(BinaryExprAST *ast) override;
104 virtual void postorder_walk(NumberExprAST *ast) override;
105 virtual void postorder_walk(StringExprAST *ast) override;
106 virtual void postorder_walk(BoolExprAST *ast) override;
107 virtual void postorder_walk(UnitExprAST *ast) override;
108 virtual void postorder_walk(VariableExprAST *ast) override;
109 virtual void postorder_walk(BlockAST *ast) override;
110 virtual void postorder_walk(IfExprAST *ast) override;
111 virtual void postorder_walk(TypedVarAST *ast) override;
112
113 virtual Type synthesize(ProgramAST *ast) override;
114 virtual Type synthesize(VarDefAST *ast) override;
115 virtual Type synthesize(ExternAST *ast) override;
116 virtual Type synthesize(FuncDefAST *ast) override;
117 virtual Type synthesize(RecordDefAST *ast) override;
118 virtual Type synthesize(PrototypeAST *ast) override;
119 virtual Type synthesize(CallExprAST *ast) override;
120 virtual Type synthesize(ReturnExprAST *ast) override;
121 virtual Type synthesize(BinaryExprAST *ast) override;
122 virtual Type synthesize(NumberExprAST *ast) override;
123 virtual Type synthesize(UnitExprAST *ast) override;
124 virtual Type synthesize(StringExprAST *ast) override;
125 virtual Type synthesize(BoolExprAST *ast) override;
126 virtual Type synthesize(VariableExprAST *ast) override;
127 virtual Type synthesize(BlockAST *ast) override;
128 virtual Type synthesize(IfExprAST *ast) override;
129 virtual Type synthesize(TypedVarAST *ast) override;
130
131 Type get_type_from_type_lexeme(const std::string &type_lexeme,
132 sammine_util::Location location) {
133 if (type_lexeme.empty()) {
134 return Type::NonExistent();
135 }
136 auto get_type_opt = this->get_typename_type(type_lexeme);
137
138 if (!get_type_opt.has_value()) {
139 this->add_error(location,
140 fmt::format("Type '{}' not found in the current scope.",
141 type_lexeme));
142 return Type::Poisoned();
143 } else
144 return get_type_opt.value();
145 }
146};
147} // namespace AST
148} // namespace sammine_lang
Defines the AST Abstract class for printing out AST Nodes.
A simple scoping class, doesn't differentiate between different names, like variable name,...
Defines the core Type system for Sammine.
Definition BiTypeChecker.h:17
LexicalStack< Type > id_to_type
Definition BiTypeChecker.h:26
Definition AstBase.h:110
Definition AstBase.h:145
Definition BiTypeChecker.h:15
Definition Types.h:122