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 using wstr = std::wstring;
49 using wchar = wstr::value_type;
50 using vdeci = double;
51 using indexT = uint64_t;
52 template<typename t>
53 using vec = std::vector<t>;
54 extern thread_local yoi::wstr __current_file_path;
55 extern std::mutex consoleMutex;
56
59 Warning,
60 Panic
61 };
62
63 extern std::map<std::string, ExceptionHandleType> exception_categories;
64
65 void parseString(std::wistream &input, wstr &value);
66
67 wstr escapeString(const wstr &value);
68
69 template<typename T>
70 std::basic_string<T> trim(const std::basic_string<T> &str) {
71 auto start = str.begin();
72 while (start != str.end() && std::isspace(*start))
73 ++start;
74
75 auto end = str.end();
76 do {
77 --end;
78 } while (end != start && std::isspace(*end));
79
80 return {start, end + 1};
81 }
82
83 void set_current_file_path(const std::wstring &path);
84
85 std::wstring get_line_hint_for_error(const std::wstring &file, yoi::indexT line, yoi::indexT col);
86
87 [[noreturn]] void panic(yoi::indexT line, yoi::indexT col, const std::string &msg);
88
89 void warning(yoi::indexT line, yoi::indexT col, const std::string &msg, const std::string &label);
90
91 void yoi_assert(bool cond, yoi::indexT line, yoi::indexT col, const std::string &msg);
92
93 std::wstring string2wstring(const std::string &v);
94
95 std::string wstring2string(const std::wstring &v);
96
97 std::wstring whereIsHoshiLang();
98
99 template<typename A, typename B>
102 public:
103
104 yoi::indexT put(const A &a, const B &b) {
105 for (yoi::indexT i = 0;i < indexes.size();i++)
106 if (indexes[i].first == a) {
107 indexes[i].second = b;
108 return i;
109 }
110 indexes.push_back({a, b});
111 return indexes.size() - 1;
112 }
113
114 B &operator[](const A &k) {
115 for (auto &i: indexes) {
116 if (i.first == k)
117 return i.second;
118 }
119 throw std::runtime_error("indexTable: invalid index");
120 }
121
123 if (k < indexes.size())
124 return indexes[k].second;
125 else
126 throw std::runtime_error("indexTable: invalid index");
127 }
128
129 yoi::indexT getIndex(const A& k) {
130 for (yoi::indexT i = 0;i < indexes.size();i++)
131 if (indexes[i].first == k) {
132 return i;
133 }
134 throw std::runtime_error("indexTable: invalid key");
135 }
136
137 class iterator {
138 typename vec<std::pair<A, B>>::iterator it;
139 public:
140 iterator(typename vec<std::pair<A, B>>::iterator it) : it(it) {}
141
142 bool operator!=(const iterator &other) const { return it!= other.it; }
143
144 iterator &operator++() { ++it; return *this; }
145
146 std::pair<A, B> &operator*() { return *it; }
147 };
148
149 iterator begin() { return iterator(indexes.begin()); }
150
151 iterator end() { return iterator(indexes.end()); }
152
153 yoi::indexT size() const { return indexes.size(); }
154 };
155
156 template<typename A, typename B>
158 std::map<A, yoi::indexT> indexes;
159 std::vector<std::pair<A, B>> values;
160 public:
161 yoi::indexT put(const A &a, const B &b) {
162 if (auto it = indexes.find(a); it == indexes.end()) {
163 indexes[a] = values.size();
164 values.push_back({a, b});
165 return values.size() - 1;
166 } else {
167 values[it->second].second = b;
168 return it->second;
169 }
170 }
171 yoi::indexT put_create(const A &a, const B &b) {
172 if (auto it = indexes.find(a); it == indexes.end()) {
173 indexes[a] = values.size();
174 values.push_back({a, b});
175 return values.size() - 1;
176 } else {
177 throw std::out_of_range("indexTable: key already exists");
178 }
179 }
180 B &operator[](const A &k) {
181 if (indexes.empty())
182 throw std::out_of_range("indexTable: empty table");
183 if (auto it = indexes.find(k); it == indexes.end()) {
184 throw std::out_of_range("indexTableRefactored: invalid key");
185 } else {
186 return values[it->second].second;
187 }
188 }
189
191 if (k < indexes.size()) {
192 return values[k].second;
193 } else {
194 throw std::out_of_range("indexTableRefactored: invalid index");
195 }
196 }
197
198 yoi::indexT getIndex(const A &k) {
199 if (auto it = indexes.find(k); it == indexes.end()) {
200 throw std::out_of_range("indexTableRefactored: invalid key");
201 } else {
202 return it->second;
203 }
204 }
205
206 yoi::indexT getIndex(const std::function<bool(const A&)> &pred) {
207 for (yoi::indexT i = 0; i < values.size(); i++) {
208 if (pred(values[i].first)) {
209 return i;
210 }
211 }
212 throw std::out_of_range("indexTableRefactored: no matching key found");
213 }
214
215 const A& getKey(yoi::indexT i) const {
216 if (i < indexes.size()) {
217 return values[i].first;
218 } else {
219 throw std::out_of_range("indexTableRefactored: invalid index");
220 }
221 }
223 return values.size();
224 }
225 // iterate over all values
226 class iterator {
227 typename std::vector<std::pair<A, B>>::iterator it;
228
229 public:
230 using value_type = std::pair<A, B>;
231 using difference_type = std::ptrdiff_t;
234 using iterator_category = std::random_access_iterator_tag;
235
236 iterator(typename std::vector<std::pair<A, B>>::iterator underlying_it) : it(underlying_it) {}
237
238 bool operator!=(const iterator &other) const { return it != other.it; }
239 bool operator==(const iterator &other) const { return it == other.it; }
240
241 reference operator*() const { return *it; }
242 pointer operator->() const { return &(*it); }
243
245 ++it;
246 return *this;
247 }
249 iterator temp = *this;
250 ++(*this);
251 return temp;
252 }
254 --it;
255 return *this;
256 }
258 iterator temp = *this;
259 --(*this);
260 return temp;
261 }
262
264 return iterator(it + n);
265 }
267 return iterator(it - n);
268 }
269 difference_type operator-(const iterator &other) const {
270 return it - other.it;
271 }
273 it += n;
274 return *this;
275 }
277 it -= n;
278 return *this;
279 }
280
281 bool operator<(const iterator &other) const { return it < other.it; }
282 bool operator<=(const iterator &other) const { return it <= other.it; }
283 bool operator>(const iterator &other) const { return it > other.it; }
284 bool operator>=(const iterator &other) const { return it >= other.it; }
285
287 return iter + n;
288 }
289 };
290
291 iterator begin() { return iterator(values.begin()); }
292
293 iterator end() { return iterator(values.end()); }
294
295 yoi::indexT size() const { return values.size(); }
296
297 bool contains(const A &k) const {
298 return indexes.find(k)!= indexes.end();
299 }
300 };
301
302 template<typename T>
303 class indexPool {
305 public:
306 yoi::indexT put(const T &t) {
307 yoi::indexT i = 0;
308 for (; i < pool.size(); i++)
309 if (pool[i] == t)
310 return i;
311 pool.push_back(t);
312 return i;
313 }
314 yoi::indexT size() const { return pool.size(); }
316 if (i < pool.size())
317 return pool[i];
318 else
319 throw std::runtime_error("indexPool: invalid index");
320 }
321 };
322
323 template<typename T>
324 std::shared_ptr<T> managedPtr(const T &v) {
325 return std::make_shared<T>(v);
326 }
327
328 yoi::wstr realpath(const std::wstring &path);
329
330 template <typename enumT, enumT TSize = enumT::FINAL>
331 class [[nodiscard]] enum_range final {
332 using type = std::underlying_type_t<enumT>;
333
334 public:
335 // The iterator that can be used to loop through all values
336 //
337 class [[nodiscard]] iterator final {
338 enumT value{static_cast<enumT>(0)};
339
340 public:
341 constexpr iterator() noexcept = default;
342 constexpr iterator(enumT e) noexcept : value{e} {}
343
344 constexpr auto operator*() const noexcept -> enumT { return value; }
345 constexpr auto operator-> () const & noexcept -> const enumT* {
346 return &value;
347 }
348 constexpr auto operator++() & noexcept -> iterator {
349 value = static_cast<enumT>(1 + static_cast<type>(value));
350 return *this;
351 }
352
353 [[nodiscard]] constexpr auto operator==(iterator i) -> bool { return i.value == value; }
354 [[nodiscard]] constexpr auto operator!=(iterator i) -> bool { return i.value != value; }
355 };
356
357 constexpr auto begin() const noexcept -> iterator { return iterator{}; }
358 constexpr auto cbegin() const noexcept -> iterator { return iterator{}; }
359
360 constexpr auto end() const noexcept -> iterator { return iterator{TSize}; }
361 constexpr auto cend() const noexcept -> iterator { return iterator{TSize}; }
362
363 [[nodiscard]] constexpr auto size() const noexcept -> type {
364 return static_cast<type>(TSize);
365 }
366 };
367
368 template <typename string_t> void replace_all(string_t &str, const string_t &from, const string_t &to) {
369 if (from.empty())
370 return;
371 size_t start_pos = 0;
372 while ((start_pos = str.find(from, start_pos))!= string_t::npos) {
373 str.replace(start_pos, from.length(), to);
374 start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
375 }
376 }
377}
378#endif
constexpr auto operator==(iterator i) -> bool
Definition def.hpp:353
constexpr auto operator++() &noexcept -> iterator
Definition def.hpp:348
constexpr auto operator!=(iterator i) -> bool
Definition def.hpp:354
constexpr iterator() noexcept=default
constexpr auto operator*() const noexcept -> enumT
Definition def.hpp:344
constexpr auto size() const noexcept -> type
Definition def.hpp:363
constexpr auto cend() const noexcept -> iterator
Definition def.hpp:361
constexpr auto cbegin() const noexcept -> iterator
Definition def.hpp:358
constexpr auto end() const noexcept -> iterator
Definition def.hpp:360
constexpr auto begin() const noexcept -> iterator
Definition def.hpp:357
std::underlying_type_t< enumT > type
Definition def.hpp:332
yoi::indexT put(const T &t)
Definition def.hpp:306
yoi::indexT size() const
Definition def.hpp:314
T & operator[](yoi::indexT i)
Definition def.hpp:315
vec< T > pool
Definition def.hpp:304
bool operator!=(const iterator &other) const
Definition def.hpp:142
std::pair< A, B > & operator*()
Definition def.hpp:146
iterator(typename vec< std::pair< A, B > >::iterator it)
Definition def.hpp:140
vec< std::pair< A, B > >::iterator it
Definition def.hpp:138
vec< std::pair< A, B > > indexes
Definition def.hpp:101
B & operator[](yoi::indexT k)
Definition def.hpp:122
B & operator[](const A &k)
Definition def.hpp:114
yoi::indexT size() const
Definition def.hpp:153
yoi::indexT getIndex(const A &k)
Definition def.hpp:129
yoi::indexT put(const A &a, const B &b)
Definition def.hpp:104
bool operator!=(const iterator &other) const
Definition def.hpp:238
iterator(typename std::vector< std::pair< A, B > >::iterator underlying_it)
Definition def.hpp:236
iterator operator--(int)
Definition def.hpp:257
iterator & operator-=(difference_type n)
Definition def.hpp:276
friend iterator operator+(difference_type n, const iterator &iter)
Definition def.hpp:286
value_type * pointer
Definition def.hpp:232
iterator & operator+=(difference_type n)
Definition def.hpp:272
iterator operator++(int)
Definition def.hpp:248
bool operator<(const iterator &other) const
Definition def.hpp:281
bool operator==(const iterator &other) const
Definition def.hpp:239
value_type & reference
Definition def.hpp:233
std::pair< A, B > value_type
Definition def.hpp:230
iterator & operator--()
Definition def.hpp:253
std::random_access_iterator_tag iterator_category
Definition def.hpp:234
bool operator>(const iterator &other) const
Definition def.hpp:283
iterator operator-(difference_type n) const
Definition def.hpp:266
reference operator*() const
Definition def.hpp:241
iterator & operator++()
Definition def.hpp:244
difference_type operator-(const iterator &other) const
Definition def.hpp:269
std::vector< std::pair< A, B > >::iterator it
Definition def.hpp:227
std::ptrdiff_t difference_type
Definition def.hpp:231
bool operator>=(const iterator &other) const
Definition def.hpp:284
bool operator<=(const iterator &other) const
Definition def.hpp:282
pointer operator->() const
Definition def.hpp:242
iterator operator+(difference_type n) const
Definition def.hpp:263
yoi::indexT put_create(const A &a, const B &b)
Definition def.hpp:171
B & operator[](yoi::indexT k)
Definition def.hpp:190
std::vector< std::pair< A, B > > values
Definition def.hpp:159
B & operator[](const A &k)
Definition def.hpp:180
yoi::indexT getIndex(const std::function< bool(const A &)> &pred)
Definition def.hpp:206
std::map< A, yoi::indexT > indexes
Definition def.hpp:158
yoi::indexT size() const
Definition def.hpp:295
yoi::indexT size()
Definition def.hpp:222
yoi::indexT getIndex(const A &k)
Definition def.hpp:198
yoi::indexT put(const A &a, const B &b)
Definition def.hpp:161
bool contains(const A &k) const
Definition def.hpp:297
iterator end()
Definition def.hpp:293
iterator begin()
Definition def.hpp:291
const A & getKey(yoi::indexT i) const
Definition def.hpp:215
std::string wstring2string(const std::wstring &v)
Definition def.cpp:184
std::shared_ptr< T > managedPtr(const T &v)
Definition def.hpp:324
wstr::value_type wchar
Definition def.hpp:49
void warning(yoi::indexT line, yoi::indexT col, const std::string &msg, const std::string &label)
Definition def.cpp:143
void parseString(std::wistream &input, wstr &value)
Definition def.cpp:25
void replace_all(string_t &str, const string_t &from, const string_t &to)
Definition def.hpp:368
std::mutex consoleMutex
Definition def.cpp:15
std::vector< t > vec
Definition def.hpp:53
std::wstring string2wstring(const std::string &v)
Definition def.cpp:178
yoi::wstr realpath(const std::wstring &path)
Definition def.cpp:190
double vdeci
Definition def.hpp:50
std::wstring whereIsHoshiLang()
Definition def.cpp:200
thread_local yoi::wstr __current_file_path
Definition def.cpp:14
ExceptionHandleType
Definition def.hpp:57
wstr escapeString(const wstr &value)
Definition def.cpp:84
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:171
std::wstring get_line_hint_for_error(const std::wstring &file, yoi::indexT line, yoi::indexT col)
Definition def.cpp:117
void set_current_file_path(const std::wstring &path)
Definition def.cpp:113
std::wstring wstr
Definition def.hpp:48
std::map< std::string, ExceptionHandleType > exception_categories
Definition def.cpp:17
uint64_t indexT
Definition def.hpp:51
std::basic_string< T > trim(const std::basic_string< T > &str)
Definition def.hpp:70
void panic(yoi::indexT line, yoi::indexT col, const std::string &msg)
Definition def.cpp:131