-
Notifications
You must be signed in to change notification settings - Fork 5
/
comelang5.c
447 lines (341 loc) · 8.08 KB
/
comelang5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#include "comelang.h"
void char*::printf(char* self, const char* msg)
{
(void)printf(msg, self);
return string(self);
}
int int::printf(int self, const char* msg)
{
(void)printf(msg, self);
return self;
}
void fopen_block(const char* path, const char* mode, void* parent, void (*block)(void* parent, FILE* f))
{
FILE* f = fopen(path, mode);
if(f) {
block(parent, f);
fclose(f);
}
}
char char::putc(char self)
{
(void)putc(self, stdout);
return self;
}
unsigned int wchar_t*::get_hash_key(wchar_t* value)
{
using unsafe;
int result = 0;
wchar_t* p = value;
while(*p) {
result += (*p);
p++;
}
return result;
}
bool wchar_t*::equals(wchar_t* left, wchar_t* right)
{
return wcscmp(left, right) == 0;
}
unsigned int regex_struct*::get_hash_key(regex_struct* self)
{
return self.str.get_hash_key();
}
int int::compare(int left, int right)
{
if(left < right) {
return -1;
}
else if(left > right) {
return 1;
}
else {
return 0;
}
return 0;
}
string char*::printable(char* str)
{
int len = str.length();
string result = new char[len*2+1];
int n = 0;
for(int i=0; i<len; i++) {
char c = str[i];
if((c >= 0 && c < ' ')
|| c == 127)
{
result[n++] = '^';
result[n++] = c + 'A' - 1;
}
else {
result[n++] = c;
}
}
result[n] = '\0'
return result;
}
string wchar_t*::to_string(wchar_t* wstr)
{
int len = MB_LEN_MAX*(wcslen(wstr)+1);
string result = new char[len];
if(wcstombs(result, wstr, len) < 0)
{
strncpy(result, "", len);
}
return result;
}
wstring char*::to_wstring(char* str)
{
return wstring(str);
}
int wchar_t*::length(wchar_t* str)
{
return wcslen(str);
}
wstring wchar_t*::delete(wchar_t* str, int head, int tail)
{
using unsafe;
int len = wcslen(str);
if(len == 0) {
return str.to_string().to_wstring();
}
if(head < 0) {
head += len;
}
if(tail < 0) {
tail += len + 1;
}
if(head < 0) {
head = 0;
}
if(tail < 0) {
return str.to_string().to_wstring();
}
if(tail >= len) {
tail = len;
}
wstring sub_str = str.substring(tail, -1);
memcpy(str + head, sub_str, sizeof(wchar_t)*(sub_str.length()+1));
return str.to_string().to_wstring();
}
int wchar_t*::index(wchar_t* str, wchar_t* search_str, int default_value)
{
using unsafe;
wchar_t* head = wcsstr(str, search_str);
if(head == null) {
return default_value;
}
return head - str;
}
int wchar_t*::rindex(wchar_t* str, wchar_t* search_str, int default_value)
{
using unsafe;
int len = wcslen(search_str);
wchar_t* p = str + wcslen(str) - len;
while(p >= str) {
int len2 = wcslen(p);
bool result = true;
int i;
for(i=0; i<len && i < len2; i++) {
if(p[i] != search_str[i]) {
result = false;
}
}
if(result) {
return (p - str);
}
p--;
}
return default_value;
}
wstring wchar_t*::reverse(wchar_t* str)
{
int len = wcslen(str);
wstring result = new wchar_t[len + 1];
for(int i=0; i<len; i++) {
result[i] = str[len-i-1];
}
result[len] = '\0';
return result;
}
wstring wchar_t*::multiply(wchar_t* str, int n)
{
int len = wcslen(str) * n + 1;
wstring result = new wchar_t[len];
result[0] = '\0';
for(int i=0; i<n; i++) {
wcscat(result, str);
}
return result;
}
wstring wchar_t*::printable(wchar_t* str)
{
int len = str.length();
wstring result = new wchar_t[len*2+1];
int n = 0;
for(int i=0; i<len; i++) {
wchar_t c = str[i];
if((c >= 0 && c < ' ')
|| c == 127)
{
result[n++] = '^';
result[n++] = c + 'A' - 1;
}
else {
result[n++] = c;
}
}
result[n] = '\0'
return result;
}
bool char*::match_group_strings(char* self, regex_struct* reg, int count, list<string>* group_strings)
{
int offset = 0;
int ovec_max = 16;
int start[ovec_max];
int end[ovec_max];
int ovec_value[ovec_max * 3];
const char* err;
int erro_ofs;
int options = reg.options;
char* str = reg.str;
pcre* re = reg.re;
int n = 0;
while(true) {
int options = PCRE_NEWLINE_LF;
int len = strlen(self);
int regex_result = pcre_exec(re, (pcre_extra*)0, self, len, offset, options, ovec_value, ovec_max*3);
for(int i=0; i<ovec_max; i++) {
start[i] = ovec_value[i*2];
}
for(int i=0; i<ovec_max; i++) {
end[i] = ovec_value[i*2+1];
}
/// match and no group strings ///
if(regex_result == 1 || (group_strings == null && regex_result > 0))
{
n++;
if(n == count) {
return true;
}
if(offset == end[0]) {
offset++;
}
else {
offset = end[0];
}
}
/// group strings ///
else if(regex_result > 1) {
n++;
group_strings.reset();
for(int i = 1; i<regex_result; i++) {
auto match_string = self.substring(start[i], end[i]);
group_strings.push_back(match_string);
}
if(n == count) {
return true;
}
if(offset == end[0]) {
offset++;
}
else {
offset = end[0];
}
}
/// no match ///
else
{
return false;
}
}
return false;
}
bool wchar_t*::comapre(wchar_t* left, wchar_t* right)
{
return wcscmp(left, right) == 0;
}
unsigned int wchar_t::get_hash_key(wchar_t value)
{
return value;
}
bool wchar_t::equals(wchar_t left, wchar_t right)
{
return left == right;
}
string char*::operator_mult(char* str, int n)
{
return charp_multiply(str, n);
}
string string::operator_mult(char* str, int n)
{
return charp_multiply(str, n);
}
wstring wchar_t*::operator_mult(wchar_t* str, int n)
{
return wchar_tp_multiply(str, n);
}
wstring wstring::operator_mult(wchar_t* str, int n)
{
return wchar_tp_multiply(str, n);
}
bool char*::operator_equals(char* left, char* right)
{
return strcmp(left, right) == 0;
}
bool string::operator_equals(char* left, char* right)
{
return strcmp(left, right) == 0;
}
bool wchar_t*::operator_equals(wchar_t* left, wchar_t* right)
{
return wcscmp(left, right) == 0;
}
bool wstring::operator_equals(wchar_t* left, wchar_t* right)
{
return wcscmp(left, right) == 0;
}
bool char*::operator_not_equals(char* left, char* right)
{
return strcmp(left, right) != 0;
}
bool string::operator_not_equals(char* left, char* right)
{
return strcmp(left, right) != 0;
}
bool wchar_t*::operator_not_equals(wchar_t* left, wchar_t* right)
{
return wcscmp(left, right) != 0;
}
bool wstring::operator_not_equals(wchar_t* left, wchar_t* right)
{
return wcscmp(left, right) != 0;
}
string char*::operator_add(char* left, char* right)
{
buffer*% buf = new buffer.initialize();
buf.append_str(left);
buf.append_str(right);
return buf.to_string();
}
string string::operator_add(char* left, char* right)
{
buffer*% buf = new buffer.initialize();
buf.append_str(left);
buf.append_str(right);
return buf.to_string();
}
wstring wchar_t*::operator_add(wchar_t* left, wchar_t* right)
{
wchar_t*% result = new wchar_t[wcslen(left) + wcslen(right) + 1];
wcscpy(result, left);
wcscat(result, right);
return result;
}
wstring wstring::operator_add(wchar_t* left, wchar_t* right)
{
wchar_t*% result = new wchar_t[wcslen(left) + wcslen(right) + 1];
wcscpy(result, left);
wcscat(result, right);
return result;
}