31 virtual void enter_new_scope()
override {
33 typename_to_type.push_context();
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());
40 virtual void exit_new_scope()
override {
42 typename_to_type.pop();
46 std::optional<Type> get_type_from_id(
const std::string &str) {
49 if (id_name_top.queryName(str) == nameNotFound) {
51 fmt::format(
"Name '{}' not found, this should not happen", str));
53 return id_name_top.get_from_name(str);
56 std::optional<Type> get_type_from_id_parent(
const std::string &str) {
58 auto &id_name_top = *
id_to_type.top().parent_scope;
59 if (id_name_top.queryName(str) == nameNotFound) {
61 fmt::format(
"Name '{}' not found, this should not happen", str));
63 return id_name_top.get_from_name(str);
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) {
71 return typename_top.get_from_name(str);
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;
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;
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;
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();
136 auto get_type_opt = this->get_typename_type(type_lexeme);
138 if (!get_type_opt.has_value()) {
139 this->add_error(location,
140 fmt::format(
"Type '{}' not found in the current scope.",
142 return Type::Poisoned();
144 return get_type_opt.value();
A simple scoping class, doesn't differentiate between different names, like variable name,...