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