hoshi-lang dev
Yet another programming language
Loading...
Searching...
No Matches
def.hpp
Go to the documentation of this file.
1#ifndef HOSHI_DEF_HPP
2#define HOSHI_DEF_HPP
3
4#include <share/defines.h>
5#include <cstdint>
6#include <iterator>
7#include <map>
8#include <stdexcept>
9#include <string>
10#include <vector>
11#include <sstream>
12#include <stack>
13#include <memory>
14#include <mutex>
15#include "magic_enum.h"
16#include <share/utfutils.hpp>
17#include <share/whereami.h>
18#include <filesystem>
19
20#if defined(__linux__)
21#define YOI_PLATFORM "linux"
22#define YOI_DYLIB_SUFFIX "so"
23#elif defined(__APPLE__)
24#define YOI_PLATFORM "darwin"
25#define YOI_DYLIB_SUFFIX L"dylib"
26#elif defined(_WIN32)
27#pragma comment(lib, "ws2_32.lib")
28#define YOI_PLATFORM "win32"
29#define YOI_DYLIB_SUFFIX "dll"
30#else
31#define YOI_PLATFORM "unknown"
32#define YOI_DYLIB_SUFFIX "so"
33#endif
34
35#if defined(__aarch64__)
36#define YOI_ARCH "arm64"
37#elif defined(__x86_64__)
38#define YOI_ARCH "amd64"
39#elif defined(__i386__)
40#define YOI_ARCH "i386"
41#elif defined(__arm__)
42#define YOI_ARCH "arm"
43#else
44#define YOI_ARCH "unknown"
45#endif
46
47namespace yoi {
48
49 class DiagnosticEngine;
50
51 using wstr = std::wstring;
52 using wchar = wstr::value_type;
53 using vdeci = double;
54 using indexT = uint64_t;
55 template<typename t>
56 using vec = std::vector<t>;
57 extern thread_local yoi::wstr __current_file_path;
59 extern std::mutex consoleMutex;
60
63 Warning,
64 Panic
65 };
66
67 extern std::map<std::string, ExceptionHandleType> exception_categories;
68
71 void set_diagnostic_engine(DiagnosticEngine *engine);
72
74 DiagnosticEngine *get_diagnostic_engine();
75
76 void parseString(std::wistream &input, wstr &value);
77
78 wstr escapeString(const wstr &value);
79
80 template<typename T>
81 std::basic_string<T> trim(const std::basic_string<T> &str) {
82 auto start = str.begin();
83 while (start != str.end() && std::isspace(*start))
84 ++start;
85
86 auto end = str.end();
87 do {
88 --end;
89 } while (end != start && std::isspace(*end));
90
91 return {start, end + 1};
92 }
93
94 void set_current_file_path(const std::wstring &path);
95
96 std::wstring get_line_hint_for_error(const std::wstring &file, yoi::indexT line, yoi::indexT col);
97
98 [[noreturn]] void panic(yoi::indexT line, yoi::indexT col, const std::string &msg);
99
100 void warning(yoi::indexT line, yoi::indexT col, const std::string &msg, const std::string &label);
101
102 void yoi_assert(bool cond, yoi::indexT line, yoi::indexT col, const std::string &msg);
103
104 std::wstring string2wstring(const std::string &v);
105
106 std::string wstring2string(const std::wstring &v);
107
108 std::wstring whereIsHoshiLang();
109
110 template<typename A, typename B>
113 public:
114
115 yoi::indexT put(const A &a, const B &b) {
116 for (yoi::indexT i = 0;i < indexes.size();i++)
117 if (indexes[i].first == a) {
118 indexes[i].second = b;
119 return i;
120 }
121 indexes.push_back({a, b});
122 return indexes.size() - 1;
123 }
124
125 B &operator[](const A &k) {
126 for (auto &i: indexes) {
127 if (i.first == k)
128 return i.second;
129 }
130 throw std::runtime_error("indexTable: invalid index");
131 }
132
134 if (k < indexes.size())
135 return indexes[k].second;
136 else
137 throw std::runtime_error("indexTable: invalid index");
138 }
139
140 yoi::indexT getIndex(const A& k) {
141 for (yoi::indexT i = 0;i < indexes.size();i++)
142 if (indexes[i].first == k) {
143 return i;
144 }
145 throw std::runtime_error("indexTable: invalid key");
146 }
147
148 class iterator {
149 typename vec<std::pair<A, B>>::iterator it;
150 public:
151 iterator(typename vec<std::pair<A, B>>::iterator it) : it(it) {}
152
153 bool operator!=(const iterator &other) const { return it!= other.it; }
154
155 iterator &operator++() { ++it; return *this; }
156
157 std::pair<A, B> &operator*() { return *it; }
158 };
159
160 iterator begin() { return iterator(indexes.begin()); }
161
162 iterator end() { return iterator(indexes.end()); }
163
164 yoi::indexT size() const { return indexes.size(); }
165 };
166
167 template<typename A, typename B>
169 std::map<A, yoi::indexT> indexes;
170 std::vector<std::pair<A, B>> values;
171 public:
172 yoi::indexT put(const A &a, const B &b) {
173 if (auto it = indexes.find(a); it == indexes.end()) {
174 indexes[a] = values.size();
175 values.push_back({a, b});
176 return values.size() - 1;
177 } else {
178 values[it->second].second = b;
179 return it->second;
180 }
181 }
182 yoi::indexT put_create(const A &a, const B &b) {
183 if (auto it = indexes.find(a); it == indexes.end()) {
184 indexes[a] = values.size();
185 values.push_back({a, b});
186 return values.size() - 1;
187 } else {
188 throw std::out_of_range("indexTable: key already exists");
189 }
190 }
191 B &operator[](const A &k) {
192 if (indexes.empty())
193 throw std::out_of_range("indexTable: empty table");
194 if (auto it = indexes.find(k); it == indexes.end()) {
195 throw std::out_of_range("indexTableRefactored: invalid key");
196 } else {
197 return values[it->second].second;
198 }
199 }
200
202 if (k < indexes.size()) {
203 return values[k].second;
204 } else {
205 throw std::out_of_range("indexTableRefactored: invalid index");
206 }
207 }
208
209 yoi::indexT getIndex(const A &k) {
210 if (auto it = indexes.find(k); it == indexes.end()) {
211 throw std::out_of_range("indexTableRefactored: invalid key");
212 } else {
213 return it->second;
214 }
215 }
216
217 yoi::indexT getIndex(const std::function<bool(const A&)> &pred) {
218 for (yoi::indexT i = 0; i < values.size(); i++) {
219 if (pred(values[i].first)) {
220 return i;
221 }
222 }
223 throw std::out_of_range("indexTableRefactored: no matching key found");
224 }
225
226 const A& getKey(yoi::indexT i) const {
227 if (i < indexes.size()) {
228 return values[i].first;
229 } else {
230 throw std::out_of_range("indexTableRefactored: invalid index");
231 }
232 }
234 return values.size();
235 }
236 // iterate over all values
237 class iterator {
238 typename std::vector<std::pair<A, B>>::iterator it;
239
240 public:
241 using value_type = std::pair<A, B>;
242 using difference_type = std::ptrdiff_t;
245 using iterator_category = std::random_access_iterator_tag;
246
247 iterator(typename std::vector<std::pair<A, B>>::iterator underlying_it) : it(underlying_it) {}
248
249 bool operator!=(const iterator &other) const { return it != other.it; }
250 bool operator==(const iterator &other) const { return it == other.it; }
251
252 reference operator*() const { return *it; }
253 pointer operator->() const { return &(*it); }
254
256 ++it;
257 return *this;
258 }
260 iterator temp = *this;
261 ++(*this);
262 return temp;
263 }
265 --it;
266 return *this;
267 }
269 iterator temp = *this;
270 --(*this);
271 return temp;
272 }
273
275 return iterator(it + n);
276 }
278 return iterator(it - n);
279 }
280 difference_type operator-(const iterator &other) const {
281 return it - other.it;
282 }
284 it += n;
285 return *this;
286 }
288 it -= n;
289 return *this;
290 }
291
292 bool operator<(const iterator &other) const { return it < other.it; }
293 bool operator<=(const iterator &other) const { return it <= other.it; }
294 bool operator>(const iterator &other) const { return it > other.it; }
295 bool operator>=(const iterator &other) const { return it >= other.it; }
296
298 return iter + n;
299 }
300 };
301
302 iterator begin() { return iterator(values.begin()); }
303
304 iterator end() { return iterator(values.end()); }
305
306 yoi::indexT size() const { return values.size(); }
307
308 bool contains(const A &k) const {
309 return indexes.find(k)!= indexes.end();
310 }
311 };
312
313 template<typename T>
314 class indexPool {
316 public:
317 yoi::indexT put(const T &t) {
318 yoi::indexT i = 0;
319 for (; i < pool.size(); i++)
320 if (pool[i] == t)
321 return i;
322 pool.push_back(t);
323 return i;
324 }
325 yoi::indexT size() const { return pool.size(); }
327 if (i < pool.size())
328 return pool[i];
329 else
330 throw std::runtime_error("indexPool: invalid index");
331 }
332 };
333
334 template<typename T>
335 std::shared_ptr<T> managedPtr(const T &v) {
336 return std::make_shared<T>(v);
337 }
338
339 yoi::wstr realpath(const std::wstring &path);
340
341 template <typename enumT, enumT TSize = enumT::FINAL>
342 class [[nodiscard]] enum_range final {
343 using type = std::underlying_type_t<enumT>;
344
345 public:
346 // The iterator that can be used to loop through all values
347 //
348 class [[nodiscard]] iterator final {
349 enumT value{static_cast<enumT>(0)};
350
351 public:
352 constexpr iterator() noexcept = default;
353 constexpr iterator(enumT e) noexcept : value{e} {}
354
355 constexpr auto operator*() const noexcept -> enumT { return value; }
356 constexpr auto operator-> () const & noexcept -> const enumT* {
357 return &value;
358 }
359 constexpr auto operator++() & noexcept -> iterator {
360 value = static_cast<enumT>(1 + static_cast<type>(value));
361 return *this;
362 }
363
364 [[nodiscard]] constexpr auto operator==(iterator i) -> bool { return i.value == value; }
365 [[nodiscard]] constexpr auto operator!=(iterator i) -> bool { return i.value != value; }
366 };
367
368 constexpr auto begin() const noexcept -> iterator { return iterator{}; }
369 constexpr auto cbegin() const noexcept -> iterator { return iterator{}; }
370
371 constexpr auto end() const noexcept -> iterator { return iterator{TSize}; }
372 constexpr auto cend() const noexcept -> iterator { return iterator{TSize}; }
373
374 [[nodiscard]] constexpr auto size() const noexcept -> type {
375 return static_cast<type>(TSize);
376 }
377 };
378
379 template <typename string_t> void replace_all(string_t &str, const string_t &from, const string_t &to) {
380 if (from.empty())
381 return;
382 size_t start_pos = 0;
383 while ((start_pos = str.find(from, start_pos))!= string_t::npos) {
384 str.replace(start_pos, from.length(), to);
385 start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
386 }
387 }
388}
389#endif
constexpr auto operator==(iterator i) -> bool
Definition def.hpp:364
constexpr auto operator++() &noexcept -> iterator
Definition def.hpp:359
constexpr auto operator!=(iterator i) -> bool
Definition def.hpp:365
constexpr iterator() noexcept=default
constexpr auto operator*() const noexcept -> enumT
Definition def.hpp:355
constexpr auto size() const noexcept -> type
Definition def.hpp:374
constexpr auto cend() const noexcept -> iterator
Definition def.hpp:372
constexpr auto cbegin() const noexcept -> iterator
Definition def.hpp:369
constexpr auto end() const noexcept -> iterator
Definition def.hpp:371
constexpr auto begin() const noexcept -> iterator
Definition def.hpp:368
std::underlying_type_t< enumT > type
Definition def.hpp:343
yoi::indexT put(const T &t)
Definition def.hpp:317
yoi::indexT size() const
Definition def.hpp:325
T & operator[](yoi::indexT i)
Definition def.hpp:326
vec< T > pool
Definition def.hpp:315
bool operator!=(const iterator &other) const
Definition def.hpp:153
std::pair< A, B > & operator*()
Definition def.hpp:157
iterator(typename vec< std::pair< A, B > >::iterator it)
Definition def.hpp:151
vec< std::pair< A, B > >::iterator it
Definition def.hpp:149
vec< std::pair< A, B > > indexes
Definition def.hpp:112
B & operator[](yoi::indexT k)
Definition def.hpp:133
B & operator[](const A &k)
Definition def.hpp:125
yoi::indexT size() const
Definition def.hpp:164
yoi::indexT getIndex(const A &k)
Definition def.hpp:140
yoi::indexT put(const A &a, const B &b)
Definition def.hpp:115
bool operator!=(const iterator &other) const
Definition def.hpp:249
iterator(typename std::vector< std::pair< A, B > >::iterator underlying_it)
Definition def.hpp:247
iterator operator--(int)
Definition def.hpp:268
iterator & operator-=(difference_type n)
Definition def.hpp:287
friend iterator operator+(difference_type n, const iterator &iter)
Definition def.hpp:297
value_type * pointer
Definition def.hpp:243
iterator & operator+=(difference_type n)
Definition def.hpp:283
iterator operator++(int)
Definition def.hpp:259
bool operator<(const iterator &other) const
Definition def.hpp:292
bool operator==(const iterator &other) const
Definition def.hpp:250
value_type & reference
Definition def.hpp:244
std::pair< A, B > value_type
Definition def.hpp:241
iterator & operator--()
Definition def.hpp:264
std::random_access_iterator_tag iterator_category
Definition def.hpp:245
bool operator>(const iterator &other) const
Definition def.hpp:294
iterator operator-(difference_type n) const
Definition def.hpp:277
reference operator*() const
Definition def.hpp:252
iterator & operator++()
Definition def.hpp:255
difference_type operator-(const iterator &other) const
Definition def.hpp:280
std::vector< std::pair< A, B > >::iterator it
Definition def.hpp:238
std::ptrdiff_t difference_type
Definition def.hpp:242
bool operator>=(const iterator &other) const
Definition def.hpp:295
bool operator<=(const iterator &other) const
Definition def.hpp:293
pointer operator->() const
Definition def.hpp:253
iterator operator+(difference_type n) const
Definition def.hpp:274
yoi::indexT put_create(const A &a, const B &b)
Definition def.hpp:182
B & operator[](yoi::indexT k)
Definition def.hpp:201
std::vector< std::pair< A, B > > values
Definition def.hpp:170
B & operator[](const A &k)
Definition def.hpp:191
yoi::indexT getIndex(const std::function< bool(const A &)> &pred)
Definition def.hpp:217
std::map< A, yoi::indexT > indexes
Definition def.hpp:169
yoi::indexT size() const
Definition def.hpp:306
yoi::indexT size()
Definition def.hpp:233
yoi::indexT getIndex(const A &k)
Definition def.hpp:209
yoi::indexT put(const A &a, const B &b)
Definition def.hpp:172
bool contains(const A &k) const
Definition def.hpp:308
iterator end()
Definition def.hpp:304
iterator begin()
Definition def.hpp:302
const A & getKey(yoi::indexT i) const
Definition def.hpp:226
std::string wstring2string(const std::wstring &v)
Definition def.cpp:230
std::shared_ptr< T > managedPtr(const T &v)
Definition def.hpp:335
wstr::value_type wchar
Definition def.hpp:52
void warning(yoi::indexT line, yoi::indexT col, const std::string &msg, const std::string &label)
Definition def.cpp:164
void parseString(std::wistream &input, wstr &value)
Definition def.cpp:35
void replace_all(string_t &str, const string_t &from, const string_t &to)
Definition def.hpp:379
std::mutex consoleMutex
Definition def.cpp:17
std::vector< t > vec
Definition def.hpp:56
std::wstring string2wstring(const std::string &v)
Definition def.cpp:224
yoi::wstr realpath(const std::wstring &path)
Definition def.cpp:236
double vdeci
Definition def.hpp:53
thread_local DiagnosticEngine * __current_diagnostic_engine
Definition def.cpp:16
std::wstring whereIsHoshiLang()
Definition def.cpp:246
DiagnosticEngine * get_diagnostic_engine()
Get the current thread-local diagnostic engine pointer (may be nullptr).
Definition def.cpp:23
thread_local yoi::wstr __current_file_path
Definition def.cpp:15
ExceptionHandleType
Definition def.hpp:61
wstr escapeString(const wstr &value)
Definition def.cpp:94
void yoi_assert(bool condition, yoi::indexT line, yoi::indexT col, const std::string &msg)
Asserts a condition that would be true and throws a runtime_error if it is false.
Definition def.cpp:217
std::wstring get_line_hint_for_error(const std::wstring &file, yoi::indexT line, yoi::indexT col)
Definition def.cpp:127
void set_diagnostic_engine(DiagnosticEngine *engine)
Definition def.cpp:19
void set_current_file_path(const std::wstring &path)
Definition def.cpp:123
std::wstring wstr
Definition def.hpp:51
std::map< std::string, ExceptionHandleType > exception_categories
Definition def.cpp:27
uint64_t indexT
Definition def.hpp:54
std::basic_string< T > trim(const std::basic_string< T > &str)
Definition def.hpp:81
void panic(yoi::indexT line, yoi::indexT col, const std::string &msg)
Definition def.cpp:141