hoshi-lang dev
Yet another programming language
Loading...
Searching...
No Matches
def.cpp
Go to the documentation of this file.
1//
2// Created by XIaokang00010 on 2023/2/10.
3//
4
5#include <cstdio>
6#include <cstdlib>
7#include <filesystem>
8#include <fstream>
9#include <iostream>
10#include <share/def.hpp>
11#include <stdlib.h>
12
13namespace yoi {
14 thread_local yoi::wstr __current_file_path = L"";
15 std::mutex consoleMutex;
16
17 std::map<std::string, ExceptionHandleType> exception_categories = {
18 {"NULLABLE_VALUE_SUPPLY_TO_RAW", ExceptionHandleType::Suppress},
19 {"INTERNAL", ExceptionHandleType::Panic},
20 {"UCRT_NOT_FOUND", ExceptionHandleType::Warning},
21 {"ELYSIA_RUNTIME_NOT_FOUND", ExceptionHandleType::Warning},
22 {"MODULE_NOT_MODIFIED", ExceptionHandleType::Suppress},
23 };
24
25 void parseString(std::wistream &input, wstr &value) {
26 wchar ch = '\0';
27 while (input) {
28 if (!input.get(ch)) break;
29 if (ch == '\\') {
30 if (!input.get(ch)) break;
31 switch (ch) {
32 case '\\':
33 case '"':
34 case '\'':
35 case '/':
36 value.push_back(ch);
37 break;
38 case 'b':
39 value.push_back('\b');
40 break;
41 case 'f':
42 value.push_back('\f');
43 break;
44 case 'n':
45 value.push_back('\n');
46 break;
47 case 'r':
48 value.push_back('\r');
49 break;
50 case 't':
51 value.push_back('\t');
52 break;
53 case 'e':
54 value.push_back('\033');
55 break;
56 case 'u': {
57 wchar fuckutf{};
58 for (int64_t i = 3; input && i >= 0; i--) {
59 if (!input.get(ch)) break;
60 if ('a' <= ch and ch <= 'z')
61 fuckutf += ((ch - 'a' + 10) * (1 << 4 * i));
62 else if ('A' <= ch and ch <= 'Z')
63 fuckutf += ((ch - 'A' + 10) * (1 << 4 * i));
64 else
65 fuckutf += ((ch - '0') * (1 << 4 * i));
66 }
67 value.push_back(fuckutf);
68 break;
69 }
70 case '0':
71 value.push_back('\0');
72 break;
73 default:
74 value.push_back(ch);
75 break;
76 }
77 } else {
78 value.push_back(ch);
79 }
80 }
81
82 }
83
84 wstr escapeString(const wstr &value) {
85 wstr result;
86 for (auto ch : value) {
87 switch (ch) {
88 case L'\\': result += L"\\\\"; break;
89 case L'\"': result += L"\\\""; break;
90 case L'\'': result += L"\\\'"; break;
91 case L'\b': result += L"\\b"; break;
92 case L'\f': result += L"\\f"; break;
93 case L'\n': result += L"\\n"; break;
94 case L'\r': result += L"\\r"; break;
95 case L'\t': result += L"\\t"; break;
96 case L'\033': result += L"\\e"; break;
97 case L'\0': result += L"\\0"; break;
98 default:
99 if (ch < 32 || ch > 126) {
100 wchar_t buf[7];
101 swprintf(buf, 7, L"\\u%04x", (unsigned int)ch);
102 result += buf;
103 } else {
104 result += ch;
105 }
106 break;
107 }
108 }
109 return result;
110 }
111
112
113 void set_current_file_path(const std::wstring &path) {
114 __current_file_path = path;
115 }
116
117 std::wstring get_line_hint_for_error(const std::wstring &file, yoi::indexT line, yoi::indexT col) {
118 std::fstream fileStream(yoi::wstring2string(file), std::ios::in);
119 if (!fileStream.is_open()) {
120 return L"";
121 }
122 std::string lineStr;
123 for (yoi::indexT i = 0; i < line; i++) {
124 std::getline(fileStream, lineStr);
125 }
126 std::wstring result = yoi::string2wstring(lineStr) + L"\n";
127 result += std::wstring(col - 1, ' ') + L"^";
128 return result;
129 }
130
131 void panic(yoi::indexT line, yoi::indexT col, const std::string &msg) {
132 auto message = msg;
133 if (!__current_file_path.empty()) {
134 message += " near " + yoi::wstring2string(__current_file_path) + ":" + std::to_string(line + 1) + ":" + std::to_string(col + 1);
135 message += "\n" + yoi::wstring2string(get_line_hint_for_error(__current_file_path, line + 1, col + 1));
136 } else {
137 message += " near line " + std::to_string(line) + " col " + std::to_string(col);
138 }
139
140 throw std::runtime_error(message);
141 }
142
143 void warning(yoi::indexT line, yoi::indexT col, const std::string& msg, const std::string& label) {
145 return;
146 }
148 panic(line, col, msg);
149 }
150
151 auto message = msg;
152 if (!__current_file_path.empty()) {
153 message += " near " + yoi::wstring2string(__current_file_path) + ":" + std::to_string(line + 1) + ":" + std::to_string(col + 1);
154 message += "\n" + yoi::wstring2string(get_line_hint_for_error(__current_file_path, line + 1, col + 1));
155 } else {
156 message += " near line " + std::to_string(line) + " col " + std::to_string(col);
157 }
158
159 std::lock_guard<std::mutex> lock(consoleMutex);
160 std::cerr << "[hoshi-lang warning] " << message << std::endl;
161 }
162
171 void yoi_assert(bool condition, yoi::indexT line, yoi::indexT col, const std::string &msg) {
172 if (not condition) {
173 // throw std::runtime_error("At line " + std::to_string(line) + " col " + std::to_string(col) + ": " + msg);
174 panic(line, col, msg);
175 }
176 }
177
178 std::wstring string2wstring(const std::string &v) {
179 std::wstring result;
181 return result;
182 }
183
184 std::string wstring2string(const std::wstring &v) {
185 std::string result;
187 return result;
188 }
189
190 yoi::wstr realpath(const std::wstring &path) {
191 std::wstring result;
192 std::filesystem::path p(wstring2string(path));
193 std::error_code ec;
194 if (auto res = std::filesystem::absolute(p, ec); ec)
195 throw std::runtime_error("Unable to resolve real path: [Errno " + std::to_string(ec.value()) + "]" +
196 ec.message());
197 else
198 return string2wstring(res.string());
199 }
200 std::wstring whereIsHoshiLang() {
201 std::string path;
202 int length, dirnameLength;
203
204 if (auto e = getenv("HOSHI_HOME"); e != nullptr) {
205 return string2wstring(e) + L"/bin";
206 } else {
207 length = wai_getExecutablePath(nullptr, 0, &dirnameLength);
208 path.resize(length + 1);
209 wai_getExecutablePath(path.data(), length, &dirnameLength);
210 path[length] = '\0';
211 return yoi::string2wstring(
212 path.substr(0, path.rfind(std::filesystem::path::preferred_separator)));
213 }
214 }
215} // namespace yoi
static bool utf8ToUnicode(const std::string &utf8, std::wstring &unicode)
Definition utfutils.hpp:13
static void unicodeToUtf8(const std::wstring &unicode, std::string &utf8)
Definition utfutils.hpp:77
std::string wstring2string(const std::wstring &v)
Definition def.cpp:184
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
std::mutex consoleMutex
Definition def.cpp:15
std::wstring string2wstring(const std::string &v)
Definition def.cpp:178
yoi::wstr realpath(const std::wstring &path)
Definition def.cpp:190
std::wstring whereIsHoshiLang()
Definition def.cpp:200
thread_local yoi::wstr __current_file_path
Definition def.cpp:14
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
void panic(yoi::indexT line, yoi::indexT col, const std::string &msg)
Definition def.cpp:131