forked from Matway/mpl-sl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.mpl
executable file
·629 lines (545 loc) · 14.2 KB
/
String.mpl
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
"String" module
"Array" includeModule
{arg: 0nx;} 0nx {convention: cdecl;} "strlen" importFunction
getCodePointAndSize: [
copy endSize:;
copy buffer:;
endSize 0 > not [
0n32 0
] [
cu0: buffer Nat8 addressToReference copy;
cu0 0x80n8 < cu0 0xc0n8 < not cu0 0xf8n8 < and or not [
0n32 0
] [
cu0 0x80n8 < [
cu0 0n32 cast
1
] [
endSize 1 > not [
0n32 0
] [
cu1: buffer 1nx + Nat8 addressToReference copy;
cu1 0xc0n8 and 0x80n8 = not [
0n32 0
] [
cu0 0xe0n8 < [
cu0 0n32 cast 0x1fn32 and 6n32 lshift
cu1 0n32 cast 0x3fn32 and or
2
] [
endSize 2 > not [
0n32 0
] [
cu2: buffer 2nx + Nat8 addressToReference copy;
cu2 0xc0n8 and 0x80n8 = not [
0n32 0
] [
cu0 0xf0n8 < [
cu0 0n32 cast 0x0fn32 and 12n32 lshift
cu1 0n32 cast 0x3fn32 and 6n32 lshift or
cu2 0n32 cast 0x3fn32 and or
3
] [
endSize 3 > not [
0n32 0
] [
cu3: buffer 3nx + Nat8 addressToReference copy;
cu3 0xc0n8 and 0x80n8 = not [
0n32 0
] [
cu0 0n32 cast 0x07n32 and 18n32 lshift
cu1 0n32 cast 0x3fn32 and 12n32 lshift or
cu2 0n32 cast 0x3fn32 and 6n32 lshift or
cu3 0n32 cast 0x3fn32 and or
4
] if
] if
] if
] if
] if
] if
] if
] if
] if
] if
] if
];
getCodePointSize: [
copy endSize:;
copy buffer:;
endSize 0 > not [0] [
cu0: buffer Nat8 addressToReference copy;
cu0 0x80n8 < cu0 0xc0n8 < not cu0 0xf8n8 < and or not [0] [
cu0 0x80n8 < [
1
] [
endSize 1 > not [0] [
cu1: buffer 1nx + Nat8 addressToReference copy;
cu1 0xc0n8 and 0x80n8 = not [0] [
cu0 0xe0n8 < [
2
] [
endSize 2 > not [0] [
cu2: buffer 2nx + Nat8 addressToReference copy;
cu2 0xc0n8 and 0x80n8 = not [0] [
cu0 0xf0n8 < [
3
] [
endSize 3 > not [0] [
cu3: buffer 3nx + Nat8 addressToReference copy;
cu3 0xc0n8 and 0x80n8 = not [0] [
4
] if
] if
] if
] if
] if
] if
] if
] if
] if
] if
] if
];
previousCodePointSize: [
copy begSize:;
copy buffer:;
begSize 0 > not [0] [
cu0: buffer 1nx - Nat8 addressToReference copy;
cu0 0xc0n8 and 0x80n8 = not [
cu0 0x80n8 < [1][0] if
] [
begSize 1 > not [0] [
cu1: buffer 2nx - Nat8 addressToReference copy;
cu1 0xc0n8 and 0x80n8 = not [
cu0 0xe0n8 and 0xc0n8 = [2][0] if
] [
begSize 2 > not [0] [
cu2: buffer 3nx - Nat8 addressToReference copy;
cu2 0xc0n8 and 0x80n8 = not [
cu0 0xf0n8 and 0xe0n8 = [3][0] if
] [
begSize 3 > not [0] [
cu3: buffer 4nx - Nat8 addressToReference copy;
cu3 0xf8n8 and 0xf0n8 = cu3 0n32 cast 0x07n32 and 0x6n32 lshift cu0 0n32 cast 0x3fn32 and or 0x110n32 < and [4][0] if
] if
] if
] if
] if
] if
] if
] if
];
fromCodePoint: [
copy codePoint:;
copy bufferAddr:;
buffer: bufferAddr (0n8 0n8 0n8 0n8) addressToReference;
codePoint 0x110000n32 < [
codePoint 0x80n32 < [
codePoint 0n8 cast 0 @buffer @ set
1
] [
codePoint 0x800n32 < [
codePoint 6n32 rshift 0xc0n32 or 0n8 cast 0 @buffer @ set
codePoint 0x3fn32 and 0x80n32 or 0n8 cast 1 @buffer @ set
2
] [
codePoint 0x10000n32 < [
codePoint 12n32 rshift 0xe0n32 or 0n8 cast 0 @buffer @ set
codePoint 6n32 rshift 0x3fn32 and 0x80n32 or 0n8 cast 1 @buffer @ set
codePoint 0x3fn32 and 0x80n32 or 0n8 cast 2 @buffer @ set
3
] [
codePoint 18n32 rshift 0xf0n32 or 0n8 cast 0 @buffer @ set
codePoint 12n32 rshift 0x3fn32 and 0x80n32 or 0n8 cast 1 @buffer @ set
codePoint 6n32 rshift 0x3fn32 and 0x80n32 or 0n8 cast 2 @buffer @ set
codePoint 0x3fn32 and 0x80n32 or 0n8 cast 3 @buffer @ set
4
] if
] if
] if
] [
0
] if
];
intPow: [
copy up:;
copy down:;
up 0 = [
1 down cast
] [
up 0 < [
up neg @up set
1 down cast down / @down set
] when
acc: 1 down cast;
[
up 4 mod 0 = [
sqrDown: down down *;
sqrDown sqrDown * @down set
up 4 / @up set
] [
up 2 mod 0 = [
down down * @down set
up 2 / @up set
] [
down acc * @acc set
up 1 - @up set
] if
] if
up 0 = not
] loop
acc
] if
];
makeStringViewRaw: [{
virtual STRING_VIEW: ();
dataBegin: storageAddress Nat8 addressToReference; #dynamize
dataSize: copy dynamic;
getTextSize: [
dataSize copy
];
split: [
result: {
success: TRUE dynamic;
errorOffset: -1 dynamic;
chars: StringView Array;
};
buffer: dataBegin storageAddress;
endSize: dataSize copy;
[endSize 0 > [result.success copy] &&] [
nextSize: buffer endSize getCodePointSize;
nextSize 0 = [
buffer dataBegin storageAddress - Int32 cast @result.@errorOffset set
FALSE @result.@success set
] [
nextSize buffer Nat8 addressToReference makeStringViewRaw @[email protected]
buffer nextSize Natx cast + @buffer set
endSize nextSize - @endSize set
] if
] while
@result
];
}];
StringView: [0 Nat8 Ref makeStringViewRaw];
String: [{
virtual STRING: ();
chars: Nat8 Array;
getTextSize: [
chars.getSize 0 = [
0
] [
chars.getSize 1 -
] if
];
getStringMemory: [chars.getBufferBegin];
getStringView: [
chars.getSize 0 = [
StringView
] [
chars.getSize 1 - 0 chars.at storageAddress Nat8 addressToReference makeStringViewRaw
] if
];
makeNZ: [
chars.getSize 0 > [@chars.popBack] when #end zero
];
makeZ: [
chars.getSize 0 > [0n8 @chars.pushBack] when #end zero
];
catAsciiSymbolCodeNZ: [
copy codePoint:;
[codePoint 128n32 <] "Is not ascii symbol code!" assert
codePoint 0n8 cast @chars.pushBack #end zero
];
catSymbolCodeNZ: [
copy codePoint:;
buf: (0n8 0n8 0n8 0n8);
length: @buf storageAddress codePoint fromCodePoint;
i: 0 dynamic;
[i length <] [
i buf @ @chars.pushBack
i 1 + @i set
] while
];
catStringNZ: [
stringView: makeStringView;
stringView.dataSize 0 > [
i: 0 dynamic;
[
i stringView.dataSize < [
char: stringView.dataBegin storageAddress i Natx cast + Nat8 addressToReference;
char @chars.pushBack
i 1 + @i set TRUE
] &&
] loop
] when
];
catIntNZ: [
copy number:;
number number number - < [
"-" catStringNZ
number Int64 cast neg Nat64 cast catUintNZ
] [
number Nat64 cast catUintNZ
] if
];
catUintNZ: [
copy number:;
nc: [number cast];
shifted: number 0n64 cast;
rounded: 1n64 dynamic;
shifted 10n64 < not [
[
rounded 10n64 * @rounded set
rounded shifted 10n64 / > not
] loop
] when
[
digit: shifted rounded /;
digit 0n32 cast 48n32 + catAsciiSymbolCodeNZ
shifted digit rounded * - @shifted set
rounded 10n64 / @rounded set
rounded 0n64 >
] loop
];
catHexNZ: [
copy number:;
nc: [number cast];
shifted: number 0n64 cast;
rounded: 1n64 dynamic;
shifted 16n64 < not [
[
rounded 16n64 * @rounded set
rounded shifted 16n64 / > not
] loop
] when
[
digit: shifted rounded /;
digit 10n64 < [
digit 0n32 cast 48n32 + catAsciiSymbolCodeNZ # '0'
] [
digit 0n32 cast 55n32 + catAsciiSymbolCodeNZ # 'A'-10
] if
shifted digit rounded * - @shifted set
rounded 16n64 / @rounded set
rounded 0n64 >
] loop
];
catFloatNZ: [
copy number:;
nc: [number cast];
number 0 nc = [
"0" catStringNZ
] [
number number number + = [
number 0 nc > [
"inf" catStringNZ
] [
"-inf" catStringNZ
] if
] [
number number = not [
"nan" catStringNZ
] [
number 0 nc < [
"-" catStringNZ
number neg @number set
] when
ten: 10 nc;
nlog10: number log10 floor 0 cast;
maxDigits: 6 dynamic;
maxOrder: 8 dynamic;
number 0.0r32 same [
5 dynamic @maxDigits set
7 dynamic @maxOrder set
] when
digits: maxDigits copy;
shift: nlog10 copy;
nlog10 maxOrder > nlog10 maxOrder 2 / neg < or not [
0 @shift set
newDigits: maxOrder nlog10 -;
newDigits digits < [
newDigits @digits set
] when
] when
rp: nlog10 shift -;
rp 0 < [0 @rp set] when
rounded: ten rp digits + intPow 0n32 cast;
shifted: number ten digits shift - intPow * 0.5 nc + 0n32 cast;
digitToCodePoint: [48n32 +];
shifted rounded / 9n32 > [
rounded 10n32 * @rounded set
rp 1 + @rp set
] when
rp 1 + @rp set
[
rp 0 = [
"." catStringNZ
] when
digit: shifted rounded /;
digit digitToCodePoint catAsciiSymbolCodeNZ
shifted digit rounded * - @shifted set
rounded 10n32 / @rounded set
rounded 0n32 = [1n32 @rounded set] when
rp 1 - @rp set
shifted 0n32 > [rp 0 < not] ||
] loop
shift 0 = not [
"e" catStringNZ
shift catIntNZ
] when
] if
] if
] if
];
catCondNZ: [
[
"TRUE" catStringNZ
] [
"FALSE" catStringNZ
] if
];
catNZ: [
arg:;
@arg "" same [@arg "STRING_VIEW" has] || [@arg "STRING" has] || [
@arg catStringNZ
] [
@arg 0.0r64 same [@arg 0.0r32 same] || [
@arg catFloatNZ
] [
@arg 0i8 same [@arg 0i16 same] || [@arg 0i32 same] || [@arg 0i64 same] || [@arg 0ix same] || [
@arg catIntNZ
] [
@arg 0n8 same [@arg 0n16 same] || [@arg 0n32 same] || [@arg 0n64 same] || [@arg 0nx same] || [
@arg catUintNZ
] [
@arg TRUE same [
@arg catCondNZ
] [
@arg printStack
0 .CANNOT_CAT_THIS_TYPE
] if
] if
] if
] if
] if
];
catAsciiSymbolCode: [makeNZ catAsciiSymbolCodeNZ makeZ];
catSymbolCode: [makeNZ catSymbolCodeNZ makeZ];
catString: [makeNZ catStringNZ makeZ];
catInt: [makeNZ catIntNZ makeZ];
catUint: [makeNZ catUintNZ makeZ];
catHex: [makeNZ catHexNZ makeZ];
catFloat: [makeNZ catFloatNZ makeZ];
cat: [makeNZ catNZ makeZ];
catMany: [
list:;
makeNZ
i: 0;
[i list fieldCount <] [
i 1 + list fieldCount < [i 1 + list @ "HEX" has] && [
i list @ catHexNZ
i 1 + @i set
] [
i list @ catNZ
] if
i 1 + @i set
] while
makeZ
];
}];
Hex: [{
virtual HEX: ();
}];
textSize: ["STRING_VIEW" has] [.getTextSize Natx cast] pfunc;
textSize: ["STRING" has] [.getTextSize Natx cast] pfunc;
makeStringView: [0 .CANNOT_MAKE_STRING_VIEW];
makeStringView: ["" same] [
arg:;
arg textSize Int32 cast dynamic arg storageAddress Nat8 addressToReference makeStringViewRaw
] pfunc;
makeStringView: ["STRING_VIEW" has] [
copy
] pfunc;
makeStringView: ["STRING" has] [
.getStringView
] pfunc;
makeStringViewByAddress: [
copy arg:;
arg strlen Int32 cast arg Nat8 addressToReference makeStringViewRaw
];
stringMemory: [0 .CANNOT_GET_STRING_MEMORY];
stringMemory: ["" same] [storageAddress] pfunc;
stringMemory: ["STRING_VIEW" has] [.dataBegin storageAddress] pfunc;
stringMemory: ["STRING" has] [.getStringMemory] pfunc;
toString: [
arg:;
result: String;
@arg @result.cat
@result
];
codepointToString: [
arg:;
result: String;
arg @result.catSymbolCode
@result
];
assembleString: [
list:;
result: String;
list @result.catMany
@result
];
stringness: [
arg:;
arg "" same [1][arg "STRING_VIEW" has [2][arg "STRING" has [2][0] if ] if] if
];
=: [
p1: stringness;
p2: stringness;
p1 p2 + 2 >
] [
arg1: makeStringView;
arg2: makeStringView;
arg1.dataSize arg2.dataSize = [
result: TRUE dynamic;
i: 0 dynamic;
[result copy [i arg1.dataSize <] &&] [
addr1: arg1.dataBegin storageAddress i Natx cast + Nat8 addressToReference;
addr2: arg2.dataBegin storageAddress i Natx cast + Nat8 addressToReference;
addr1 addr2 = @result set
i 1 + @i set
] while
result copy
] &&
] pfunc;
print: [
toString stringMemory printAddr
];
print: ["" same] [
stringMemory printAddr
] pfunc;
print: ["STRING" has] [
stringMemory printAddr
] pfunc;
addLog: [
HAS_LOGS [
printList LF print
] [
list:;
] if
];
printList: [
assembleString print
];
hash: [makeStringView TRUE] [
stringView: makeStringView;
result: 33n32;
i: 0;
[i stringView.dataSize <] [
curByte: stringView.dataBegin storageAddress i Natx cast + Nat8 addressToReference r:; @r Nat32 cast;
result 47n32 * curByte + @result set
i 1 + @i set
] while
result
] pfunc;