sammine-lang
Loading...
Searching...
No Matches
Types.h
Go to the documentation of this file.
1#pragma once
2#include "util/Utilities.h"
3#include <cmath>
4#include <execution>
5#include <map>
6#include <memory>
7#include <optional>
8#include <span>
9#include <string>
10#include <unordered_map>
11#include <variant>
12#include <vector>
15enum class TypeKind {
16 I64_t,
17 F64_t,
18 Unit,
19 Bool,
20 String,
21 Function,
22 NonExistent,
23 Poisoned
24};
25
26struct Type;
27class FunctionType;
28
29using TypePtr = std::shared_ptr<Type>;
30class FunctionType {
31 std::vector<Type> total_types;
32
33public:
34 bool operator==(const FunctionType &t) const;
35
36 bool operator<(const FunctionType &t) const;
37
38 std::span<const Type> get_params_types() const;
39 Type get_return_type() const;
40
41 FunctionType(const std::vector<Type> &total_types);
42};
43using TypeData = std::variant<FunctionType, std::string, std::monostate>;
44
45struct Type {
46 TypeKind type_kind;
47 TypeData type_data;
48 bool is_checked = false;
49 // Constructors
50 Type() : type_kind(TypeKind::NonExistent), type_data(std::monostate()) {}
51 static Type I64_t() { return Type{TypeKind::I64_t, std::monostate()}; }
52 static Type F64_t() { return Type{TypeKind::F64_t, std::monostate()}; }
53 static Type Bool() { return Type{TypeKind::Bool, std::monostate()}; }
54 static Type Poisoned() { return Type{TypeKind::Poisoned, std::monostate()}; }
55 static Type Unit() { return Type{TypeKind::Unit, std::monostate()}; }
56 static Type String(const std::string &str) {
57 return Type{TypeKind::String, str};
58 }
59 static Type NonExistent() {
60 return Type{TypeKind::NonExistent, std::monostate()};
61 }
62 static Type Function(std::vector<Type> params);
63 explicit operator bool() const {
64 return this->type_kind != TypeKind::Poisoned;
65 }
66 bool synthesized() const {
67 return this->type_kind != TypeKind::NonExistent &&
68 this->type_kind == TypeKind::Poisoned;
69 }
70 bool checked() const {
71 return this->is_checked || this->type_kind == TypeKind::Poisoned;
72 }
73 Type(TypeKind type_kind, TypeData type_data)
74 : type_kind(type_kind), type_data(type_data) {}
75
76 bool operator==(const Type &other) const;
77
78 bool operator!=(const Type &other) const;
79 bool operator<(const Type &t) const;
80 bool operator>(const Type &t) const;
81
82 std::string to_string() const {
83 switch (type_kind) {
84 case TypeKind::I64_t:
85 return "i64";
86 case TypeKind::F64_t:
87 return "f64";
88 case TypeKind::Unit:
89 return "()";
90 case TypeKind::Bool:
91 return "bool";
92 case TypeKind::Function: {
93 std::string res = "(";
94 auto fn_type = std::get<FunctionType>(type_data);
95 auto param = fn_type.get_params_types();
96 for (size_t i = 0; i < param.size(); i++) {
97 res += param[i].to_string();
98 if (i != param.size() - 1)
99 res += ", ";
100 }
101 res += ") -> ";
102 res += fn_type.get_return_type().to_string();
103
104 return res;
105 }
106 case TypeKind::NonExistent:
107 return "??";
108 case TypeKind::Poisoned:
109 return "Poisoned";
110 case TypeKind::String:
111 return fmt::format("\"{}\"", std::get<std::string>(type_data));
112 }
113 sammine_util::abort("Reaching the end of switch case and still cant "
114 "convert to string, blame Jasmine (badumbatish)!!!!!");
115 return "";
116 }
117
118 operator std::string() { return to_string(); }
119};
120
122 std::map<Type, Type> type_map;
123 std::vector<Type> visit_ancestor(const Type &t);
124 std::optional<Type> lowest_common_type(const Type &a, const Type &b);
125
126 bool compatible_to_from(const Type &a, const Type &b);
127};
Holds classes and functionalities for dealing with Error handling, source locations caching & indexin...
Definition Types.h:30
Definition Types.h:121
Definition Types.h:45