forked from rebolsource/r3
/
s-mold.c
executable file
·1856 lines (1555 loc) · 51.3 KB
/
s-mold.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
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// File: %s-mold.c
// Summary: "value to string conversion"
// Section: strings
// Project: "Rebol 3 Interpreter and Run-time (Ren-C branch)"
// Homepage: https://github.com/metaeducation/ren-c/
//
//=////////////////////////////////////////////////////////////////////////=//
//
// Copyright 2012 REBOL Technologies
// Copyright 2012-2016 Rebol Open Source Contributors
// REBOL is a trademark of REBOL Technologies
//
// See README.md and CREDITS.md for more information.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//=////////////////////////////////////////////////////////////////////////=//
//
//#define INCLUDE_TYPE_NAMES // include the value names table
#include "sys-core.h"
#include <float.h>
#define MAX_QUOTED_STR 50 // max length of "string" before going to { }
//typedef REBSER *(*MOLD_FUNC)(REBVAL *, REBSER *, REBCNT);
typedef void (*MOLD_FUNC)(REBVAL *, REB_MOLD *);
//const REBYTE New_Line[4] = {LF, 0};
const REBYTE Punctuation[] = ".,-/";
enum REB_Punct {
PUNCT_DOT = 0, // Must be 0
PUNCT_COMMA, // Must be 1
PUNCT_DASH,
PUNCT_SLASH,
PUNCT_MAX
};
REBYTE *Char_Escapes;
#define MAX_ESC_CHAR (0x60-1) // size of escape table
#define IS_CHR_ESC(c) ((c) <= MAX_ESC_CHAR && Char_Escapes[c])
REBYTE *URL_Escapes;
#define MAX_URL_CHAR (0x80-1)
#define IS_URL_ESC(c) ((c) <= MAX_URL_CHAR && (URL_Escapes[c] & ESC_URL))
#define IS_FILE_ESC(c) ((c) <= MAX_URL_CHAR && (URL_Escapes[c] & ESC_FILE))
enum {
ESC_URL = 1,
ESC_FILE = 2,
ESC_EMAIL = 4
};
/***********************************************************************
************************************************************************
**
** SECTION: Global Mold Utilities
**
************************************************************************
***********************************************************************/
//
// Emit: C
//
REBSER *Emit(REB_MOLD *mold, const char *fmt, ...)
{
va_list va;
REBYTE ender = 0;
REBSER *series = mold->series;
assert(SER_WIDE(series) == 2);
va_start(va, fmt);
for (; *fmt; fmt++) {
switch (*fmt) {
case 'W': { // Word symbol
const REBVAL *any_word = va_arg(va, const REBVAL*);
REBSTR *spelling = VAL_WORD_SPELLING(any_word);
Append_UTF8_May_Fail(
series, STR_HEAD(spelling), STR_NUM_BYTES(spelling)
);
break;
}
case 'V': // Value
Mold_Value(mold, va_arg(va, const REBVAL*), TRUE);
break;
case 'S': // String of bytes
Append_Unencoded(series, va_arg(va, const char *));
break;
case 'C': // Char
Append_Codepoint_Raw(series, va_arg(va, REBCNT));
break;
case 'E': { // Series (byte or uni)
REBSER *src = va_arg(va, REBSER*);
Insert_String(
series, SER_LEN(series), src, 0, SER_LEN(src), FALSE
);
break;
}
case 'I': // Integer
Append_Int(series, va_arg(va, REBINT));
break;
case 'i':
Append_Int_Pad(series, va_arg(va, REBINT), -9);
Trim_Tail(mold->series, '0');
break;
case '2': // 2 digit int (for time)
Append_Int_Pad(series, va_arg(va, REBINT), 2);
break;
case 'T': { // Type name
const REBYTE *bytes = Get_Type_Name(va_arg(va, REBVAL*));
Append_UTF8_May_Fail(series, bytes, LEN_BYTES(bytes));
break; }
case 'N': { // Symbol name
REBSTR *spelling = va_arg(va, REBSTR*);
Append_UTF8_May_Fail(
series, STR_HEAD(spelling), STR_NUM_BYTES(spelling)
);
break; }
case '+': // Add #[ if mold/all
if (GET_MOPT(mold, MOPT_MOLD_ALL)) {
Append_Unencoded(series, "#[");
ender = ']';
}
break;
case 'D': // Datatype symbol: #[type
if (ender) {
REBSTR *canon = Canon(cast(REBSYM, va_arg(va, int)));
Append_UTF8_May_Fail(
series, STR_HEAD(canon), STR_NUM_BYTES(canon)
);
Append_Codepoint_Raw(series, ' ');
}
else
va_arg(va, REBCNT); // ignore it
break;
case 'B': // Boot string
Append_Boot_Str(series, va_arg(va, REBINT));
break;
default:
Append_Codepoint_Raw(series, *fmt);
}
}
va_end(va);
if (ender) Append_Codepoint_Raw(series, ender);
return series;
}
//
// Prep_String: C
//
// Helper function for the string related Mold functions below.
// Creates or expands the series and provides the location to
// copy text into.
//
REBSER *Prep_String(REBSER *series, REBYTE **str, REBCNT len)
{
REBCNT tail;
if (!series) {
series = Make_Binary(len);
SET_SERIES_LEN(series, len);
*str = BIN_HEAD(series);
}
else {
// This used "STR_AT" (obsolete) but didn't have an explicit case
// here that it was byte sized. Check it, because if you have
// unicode characters this would give the wrong pointer.
//
assert(BYTE_SIZE(series));
tail = SER_LEN(series);
EXPAND_SERIES_TAIL(series, len);
*str = BIN_AT(series, tail);
}
return series;
}
//
// Prep_Uni_Series: C
//
REBUNI *Prep_Uni_Series(REB_MOLD *mold, REBCNT len)
{
REBCNT tail = SER_LEN(mold->series);
EXPAND_SERIES_TAIL(mold->series, len);
return UNI_AT(mold->series, tail);
}
/***********************************************************************
************************************************************************
**
** SECTION: Local MOLD Utilities
**
************************************************************************
***********************************************************************/
//
// Pre_Mold: C
//
// Emit the initial datatype function, depending on /ALL option
//
void Pre_Mold(const RELVAL *value, REB_MOLD *mold)
{
Emit(mold, GET_MOPT(mold, MOPT_MOLD_ALL) ? "#[T " : "make T ", value);
}
//
// End_Mold: C
//
// Finish the mold, depending on /ALL with close block.
//
void End_Mold(REB_MOLD *mold)
{
if (GET_MOPT(mold, MOPT_MOLD_ALL)) Append_Codepoint_Raw(mold->series, ']');
}
//
// Post_Mold: C
//
// For series that has an index, add the index for mold/all.
// Add closing block.
//
void Post_Mold(const RELVAL *value, REB_MOLD *mold)
{
if (VAL_INDEX(value)) {
Append_Codepoint_Raw(mold->series, ' ');
Append_Int(mold->series, VAL_INDEX(value)+1);
}
if (GET_MOPT(mold, MOPT_MOLD_ALL)) Append_Codepoint_Raw(mold->series, ']');
}
//
// New_Indented_Line: C
//
// Create a newline with auto-indent on next line if needed.
//
void New_Indented_Line(REB_MOLD *mold)
{
REBINT n;
REBUNI *cp = 0;
// Check output string has content already but no terminator:
if (SER_LEN(mold->series)) {
cp = UNI_LAST(mold->series);
if (*cp == ' ' || *cp == '\t') *cp = '\n';
else cp = 0;
}
// Add terminator:
if (!cp) Append_Codepoint_Raw(mold->series, '\n');
// Add proper indentation:
if (!GET_MOPT(mold, MOPT_INDENT)) {
for (n = 0; n < mold->indent; n++)
Append_Unencoded(mold->series, " ");
}
}
/***********************************************************************
************************************************************************
**
** SECTION: Char/String Datatypes
**
************************************************************************
***********************************************************************/
typedef struct REB_Str_Flags {
REBCNT escape; // escaped chars
REBCNT brace_in; // {
REBCNT brace_out; // }
REBCNT newline; // lf
REBCNT quote; // "
REBCNT paren; // (1234)
REBCNT chr1e;
REBCNT malign;
} REB_STRF;
static void Sniff_String(REBSER *ser, REBCNT idx, REB_STRF *sf)
{
// Scan to find out what special chars the string contains?
REBYTE *bp = SER_DATA_RAW(ser);
REBUNI *up = cast(REBUNI*, bp);
REBUNI c;
REBCNT n;
for (n = idx; n < SER_LEN(ser); n++) {
c = BYTE_SIZE(ser) ? cast(REBUNI, bp[n]) : up[n];
switch (c) {
case '{':
sf->brace_in++;
break;
case '}':
sf->brace_out++;
if (sf->brace_out > sf->brace_in) sf->malign++;
break;
case '"':
sf->quote++;
break;
case '\n':
sf->newline++;
break;
default:
if (c == 0x1e) sf->chr1e += 4; // special case of ^(1e)
else if (IS_CHR_ESC(c)) sf->escape++;
else if (c >= 0x1000) sf->paren += 6; // ^(1234)
else if (c >= 0x100) sf->paren += 5; // ^(123)
else if (c >= 0x80) sf->paren += 4; // ^(12)
}
}
if (sf->brace_in != sf->brace_out) sf->malign++;
}
static REBUNI *Emit_Uni_Char(REBUNI *up, REBUNI chr, REBOOL parened)
{
if (chr >= 0x7f || chr == 0x1e) { // non ASCII or ^ must be (00) escaped
if (parened || chr == 0x1e) { // do not AND with above
*up++ = '^';
*up++ = '(';
up = Form_Uni_Hex(up, chr);
*up++ = ')';
return up;
}
}
else if (IS_CHR_ESC(chr)) {
*up++ = '^';
*up++ = Char_Escapes[chr];
return up;
}
*up++ = chr;
return up;
}
static void Mold_Uni_Char(REBSER *dst, REBUNI chr, REBOOL molded, REBOOL parened)
{
REBCNT tail = SER_LEN(dst);
REBUNI *up;
if (!molded) {
EXPAND_SERIES_TAIL(dst, 1);
*UNI_AT(dst, tail) = chr;
}
else {
EXPAND_SERIES_TAIL(dst, 10); // worst case: #"^(1234)"
up = UNI_AT(dst, tail);
*up++ = '#';
*up++ = '"';
up = Emit_Uni_Char(up, chr, parened);
*up++ = '"';
SET_SERIES_LEN(dst, up - UNI_HEAD(dst));
}
UNI_TERM(dst);
}
static void Mold_String_Series(const REBVAL *value, REB_MOLD *mold)
{
REBCNT len = VAL_LEN_AT(value);
REBSER *ser = VAL_SERIES(value);
REBCNT idx = VAL_INDEX(value);
REBYTE *bp;
REBUNI *up;
REBUNI *dp;
REBOOL unicode = NOT(BYTE_SIZE(ser));
REBCNT n;
REBUNI c;
REB_STRF sf;
CLEARS(&sf);
// Empty string:
if (idx >= VAL_LEN_HEAD(value)) {
// !!! Comment said `fail (Error(RE_PAST_END));`
Append_Unencoded(mold->series, "\"\"");
return;
}
Sniff_String(ser, idx, &sf);
if (!GET_MOPT(mold, MOPT_NON_ANSI_PARENED)) sf.paren = 0;
// Source can be 8 or 16 bits:
if (unicode) up = UNI_HEAD(ser);
else bp = BIN_HEAD(ser);
// If it is a short quoted string, emit it as "string":
if (len <= MAX_QUOTED_STR && sf.quote == 0 && sf.newline < 3) {
dp = Prep_Uni_Series(mold, len + sf.newline + sf.escape + sf.paren + sf.chr1e + 2);
*dp++ = '"';
for (n = idx; n < VAL_LEN_HEAD(value); n++) {
c = unicode ? up[n] : cast(REBUNI, bp[n]);
dp = Emit_Uni_Char(dp, c, GET_MOPT(mold, MOPT_NON_ANSI_PARENED));
}
*dp++ = '"';
*dp = 0;
return;
}
// It is a braced string, emit it as {string}:
if (!sf.malign) sf.brace_in = sf.brace_out = 0;
dp = Prep_Uni_Series(mold, len + sf.brace_in + sf.brace_out + sf.escape + sf.paren + sf.chr1e + 2);
*dp++ = '{';
for (n = idx; n < VAL_LEN_HEAD(value); n++) {
c = unicode ? up[n] : cast(REBUNI, bp[n]);
switch (c) {
case '{':
case '}':
if (sf.malign) {
*dp++ = '^';
*dp++ = c;
break;
}
case '\n':
case '"':
*dp++ = c;
break;
default:
dp = Emit_Uni_Char(dp, c, GET_MOPT(mold, MOPT_NON_ANSI_PARENED));
}
}
*dp++ = '}';
*dp = 0;
}
/*
http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
Only alphanumerics [0-9a-zA-Z], the special characters $-_.+!*'(),
and reserved characters used for their reserved purposes may be used
unencoded within a URL.
*/
static void Mold_Url(const REBVAL *value, REB_MOLD *mold)
{
REBUNI *dp;
REBCNT n;
REBUNI c;
REBCNT len = VAL_LEN_AT(value);
REBSER *ser = VAL_SERIES(value);
// Compute extra space needed for hex encoded characters:
for (n = VAL_INDEX(value); n < VAL_LEN_HEAD(value); n++) {
c = GET_ANY_CHAR(ser, n);
if (IS_URL_ESC(c)) len += 2;
}
dp = Prep_Uni_Series(mold, len);
for (n = VAL_INDEX(value); n < VAL_LEN_HEAD(value); n++) {
c = GET_ANY_CHAR(ser, n);
if (IS_URL_ESC(c)) dp = Form_Hex_Esc_Uni(dp, c); // c => %xx
else *dp++ = c;
}
*dp = 0;
}
static void Mold_File(const REBVAL *value, REB_MOLD *mold)
{
REBUNI *dp;
REBCNT n;
REBUNI c;
REBCNT len = VAL_LEN_AT(value);
REBSER *ser = VAL_SERIES(value);
// Compute extra space needed for hex encoded characters:
for (n = VAL_INDEX(value); n < VAL_LEN_HEAD(value); n++) {
c = GET_ANY_CHAR(ser, n);
if (IS_FILE_ESC(c)) len += 2;
}
len++; // room for % at start
dp = Prep_Uni_Series(mold, len);
*dp++ = '%';
for (n = VAL_INDEX(value); n < VAL_LEN_HEAD(value); n++) {
c = GET_ANY_CHAR(ser, n);
if (IS_FILE_ESC(c)) dp = Form_Hex_Esc_Uni(dp, c); // c => %xx
else *dp++ = c;
}
*dp = 0;
}
static void Mold_Tag(const REBVAL *value, REB_MOLD *mold)
{
Append_Codepoint_Raw(mold->series, '<');
Insert_String(
mold->series,
SER_LEN(mold->series), // "insert" at tail (append)
VAL_SERIES(value),
VAL_INDEX(value),
VAL_LEN_AT(value),
FALSE
);
Append_Codepoint_Raw(mold->series, '>');
}
//
// Mold_Binary: C
//
void Mold_Binary(const REBVAL *value, REB_MOLD *mold)
{
REBCNT len = VAL_LEN_AT(value);
REBSER *out;
switch (Get_System_Int(SYS_OPTIONS, OPTIONS_BINARY_BASE, 16)) {
default:
case 16:
out = Encode_Base16(value, 0, LOGICAL(len > 32));
break;
case 64:
Append_Unencoded(mold->series, "64");
out = Encode_Base64(value, 0, LOGICAL(len > 64));
break;
case 2:
Append_Codepoint_Raw(mold->series, '2');
out = Encode_Base2(value, 0, LOGICAL(len > 8));
break;
}
Emit(mold, "#{E}", out);
Free_Series(out);
}
static void Mold_All_String(const REBVAL *value, REB_MOLD *mold)
{
// The string that is molded for /all option:
REBVAL val;
//// ???? move to above Mold_String_Series function????
Pre_Mold(value, mold); // #[file! part
val = *value;
VAL_INDEX(&val) = 0;
if (IS_BINARY(value)) Mold_Binary(&val, mold);
else {
VAL_RESET_HEADER(&val, REB_STRING);
Mold_String_Series(&val, mold);
}
Post_Mold(value, mold);
}
/***********************************************************************
************************************************************************
**
** SECTION: Block Series Datatypes
**
************************************************************************
***********************************************************************/
//
// Mold_Array_At: C
//
void Mold_Array_At(
REB_MOLD *mold,
REBARR *array,
REBCNT index,
const char *sep
) {
REBSER *out = mold->series;
REBOOL line_flag = FALSE; // newline was part of block
REBOOL had_lines = FALSE;
RELVAL *value = ARR_AT(array, index);
if (!sep) sep = "[]";
if (IS_END(value)) {
Append_Unencoded(out, sep);
return;
}
// Recursion check:
if (Find_Same_Array(MOLD_STACK, value) != NOT_FOUND) {
Emit(mold, "C...C", sep[0], sep[1]);
return;
}
// We don't want to use Val_Init_Block because it will create an implicit
// managed value, and the incoming series may be from an unmanaged source
// !!! Review how to avoid needing to put the series into a value
{
REBVAL *temp = Alloc_Tail_Array(MOLD_STACK);
VAL_RESET_HEADER(temp, REB_BLOCK);
INIT_VAL_ARRAY(temp, array); // copies args
VAL_INDEX(temp) = 0;
}
if (sep[1]) {
Append_Codepoint_Raw(out, sep[0]);
mold->indent++;
}
// else out->tail--; // why?????
value = ARR_AT(array, index);
while (NOT_END(value)) {
if (GET_VAL_FLAG(value, VALUE_FLAG_LINE)) {
if (sep[1] || line_flag) New_Indented_Line(mold);
had_lines = TRUE;
}
line_flag = TRUE;
Mold_Value(mold, value, TRUE);
value++;
if (NOT_END(value))
Append_Codepoint_Raw(out, (sep[0] == '/') ? '/' : ' ');
}
if (sep[1]) {
mold->indent--;
if (GET_VAL_FLAG(value, VALUE_FLAG_LINE) || had_lines)
New_Indented_Line(mold);
Append_Codepoint_Raw(out, sep[1]);
}
TERM_ARRAY_LEN(MOLD_STACK, ARR_LEN(MOLD_STACK) - 1);
}
static void Mold_Block(const RELVAL *value, REB_MOLD *mold)
{
const char *sep;
REBOOL all = GET_MOPT(mold, MOPT_MOLD_ALL);
REBSER *series = mold->series;
REBOOL over = FALSE;
#if !defined(NDEBUG)
if (SER_WIDE(VAL_SERIES(value)) == 0) {
Debug_Fmt("** Mold_Block() zero series wide, t=%d", VAL_TYPE(value));
Panic_Series(VAL_SERIES(value));
}
#endif
// Optimize when no index needed:
if (VAL_INDEX(value) == 0 && !IS_MAP(value)) // && (VAL_TYPE(value) <= REB_LIT_PATH))
all = FALSE;
// If out of range, do not cause error to avoid error looping.
if (VAL_INDEX(value) >= VAL_LEN_HEAD(value)) over = TRUE; // Force it into []
if (all || (over && !IS_BLOCK(value) && !IS_GROUP(value))) {
SET_FLAG(mold->opts, MOPT_MOLD_ALL);
Pre_Mold(value, mold); // #[block! part
//if (over) Append_Unencoded(mold->series, "[]");
//else
Append_Codepoint_Raw(mold->series, '[');
Mold_Array_At(mold, VAL_ARRAY(value), 0, 0);
Post_Mold(value, mold);
Append_Codepoint_Raw(mold->series, ']');
}
else
{
switch(VAL_TYPE(value)) {
case REB_MAP:
Pre_Mold(value, mold);
sep = 0;
case REB_BLOCK:
if (GET_MOPT(mold, MOPT_ONLY)) {
CLR_FLAG(mold->opts, MOPT_ONLY); // only top level
sep = "\000\000";
}
else sep = 0;
break;
case REB_GROUP:
sep = "()";
break;
case REB_GET_PATH:
series = Append_Codepoint_Raw(series, ':');
sep = "/";
break;
case REB_LIT_PATH:
series = Append_Codepoint_Raw(series, '\'');
/* fall through */
case REB_PATH:
case REB_SET_PATH:
sep = "/";
break;
default:
sep = NULL;
}
if (over) Append_Unencoded(mold->series, sep ? sep : "[]");
else Mold_Array_At(mold, VAL_ARRAY(value), VAL_INDEX(value), sep);
if (VAL_TYPE(value) == REB_SET_PATH)
Append_Codepoint_Raw(series, ':');
}
}
static void Mold_Simple_Block(REB_MOLD *mold, RELVAL *block, REBCNT len)
{
// Simple molder for error locations. Series must be valid.
// Max length in chars must be provided.
REBCNT start = SER_LEN(mold->series);
while (NOT_END(block)) {
if ((SER_LEN(mold->series) - start) > len) break;
Mold_Value(mold, block, TRUE);
block++;
if (NOT_END(block)) Append_Codepoint_Raw(mold->series, ' ');
}
// If it's too large, truncate it:
if ((SER_LEN(mold->series) - start) > len) {
SET_SERIES_LEN(mold->series, start + len);
Append_Unencoded(mold->series, "...");
}
}
static void Form_Array_At(
REBARR *array,
REBCNT index,
REB_MOLD *mold,
REBCTX *context
) {
// Form a series (part_mold means mold non-string values):
REBINT n;
REBINT len = ARR_LEN(array) - index;
if (len < 0) len = 0;
for (n = 0; n < len;) {
RELVAL *val = ARR_AT(array, index + n);
REBVAL *wval = NULL;
if (context && (IS_WORD(val) || IS_GET_WORD(val))) {
wval = Select_Canon_In_Context(context, VAL_WORD_CANON(val));
if (wval) val = wval;
}
Mold_Value(mold, val, LOGICAL(wval != NULL));
n++;
if (GET_MOPT(mold, MOPT_LINES)) {
Append_Codepoint_Raw(mold->series, LF);
}
else {
// Add a space if needed:
if (n < len && SER_LEN(mold->series)
&& *UNI_LAST(mold->series) != LF
&& !GET_MOPT(mold, MOPT_TIGHT)
){
Append_Codepoint_Raw(mold->series, ' ');
}
}
}
}
/***********************************************************************
************************************************************************
**
** SECTION: Special Datatypes
**
************************************************************************
***********************************************************************/
static void Mold_Typeset(const REBVAL *value, REB_MOLD *mold, REBOOL molded)
{
REBINT n;
if (molded) {
Pre_Mold(value, mold); // #[typeset! or make typeset!
Append_Codepoint_Raw(mold->series, '[');
}
#if !defined(NDEBUG)
if (VAL_KEY_SPELLING(value) != NULL) {
//
// In debug builds we're probably more interested in the symbol than
// the typesets, if we are looking at a PARAMLIST or KEYLIST.
//
Append_Unencoded(mold->series, "(");
REBSTR *spelling = VAL_KEY_SPELLING(value);
Append_UTF8_May_Fail(
mold->series, STR_HEAD(spelling), STR_NUM_BYTES(spelling)
);
Append_Unencoded(mold->series, ") ");
// REVIEW: should detect when a lot of types are active and condense
// only if the number of types is unreasonable (often for keys/params)
//
if (TRUE) {
Append_Unencoded(mold->series, "...");
goto skip_types;
}
}
#endif
assert(!TYPE_CHECK(value, REB_0)); // REB_0 is used for internal purposes
// Note that although REB_MAX_VOID is used as an implementation detail for
// special typesets in function paramlists or context keys to indicate
// <opt>-style optionality, the "absence of a type" is not generally legal
// in user typesets. Only legal "key" typesets (that have symbols).
//
assert(
!TYPE_CHECK(value, REB_MAX_VOID) || VAL_KEY_SPELLING(value) != NULL
);
// Convert bits to types.
//
for (n = REB_0 + 1; n < REB_MAX; n++) {
if (TYPE_CHECK(value, cast(enum Reb_Kind, n))) {
Emit(mold, "+DN ", SYM_DATATYPE_X, Canon(cast(REBSYM, n)));
}
}
Trim_Tail(mold->series, ' ');
#if !defined(NDEBUG)
skip_types:
#endif
if (molded) {
//Form_Typeset(value, mold & ~(1<<MOPT_MOLD_ALL));
Append_Codepoint_Raw(mold->series, ']');
End_Mold(mold);
}
}
static void Mold_Function(const REBVAL *value, REB_MOLD *mold)
{
Pre_Mold(value, mold);
Append_Codepoint_Raw(mold->series, '[');
// !!! The system is no longer keeping the spec of functions, in order
// to focus on a generalized "meta info object" service. MOLD of
// functions temporarily uses the word list as a substitute (which
// drops types)
//
REBARR *words_list = List_Func_Words(value, TRUE); // show pure locals
Mold_Array_At(mold, words_list, 0, 0);
Free_Array(words_list);
if (IS_FUNCTION_INTERPRETED(value)) {
//
// MOLD is an example of user-facing code that needs to be complicit
// in the "lie" about the effective bodies of the functions made
// by the optimized generators FUNC and CLOS...
REBOOL is_fake;
REBARR *body = Get_Maybe_Fake_Func_Body(&is_fake, value);
Mold_Array_At(mold, body, 0, 0);
if (is_fake) Free_Array(body); // was shallow copy
}
else if (IS_FUNCTION_SPECIALIZER(value)) {
//
// !!! Interim form of looking at specialized functions... show
// the frame
//
// >> source first
// first: make function! [[aggregate index] [
// aggregate: $void
// index: 1
// ]]
//
REBVAL *exemplar = KNOWN(VAL_FUNC_BODY(value));
Mold_Value(mold, exemplar, TRUE);
}
Append_Codepoint_Raw(mold->series, ']');
End_Mold(mold);
}
static void Mold_Map(const REBVAL *value, REB_MOLD *mold, REBOOL molded)
{
REBARR *mapser = MAP_PAIRLIST(VAL_MAP(value));
RELVAL *val;
// Prevent endless mold loop:
if (Find_Same_Array(MOLD_STACK, value) != NOT_FOUND) {
Append_Unencoded(mold->series, "...]");
return;
}
Append_Value(MOLD_STACK, value);
if (molded) {
Pre_Mold(value, mold);
Append_Codepoint_Raw(mold->series, '[');
}
// Mold all entries that are set. As with contexts, void values are not
// valid entries but indicate the absence of a value.
//
mold->indent++;
for (val = ARR_HEAD(mapser); NOT_END(val) && NOT_END(val+1); val += 2) {
if (!IS_VOID(val + 1)) {
if (molded) New_Indented_Line(mold);
Emit(mold, "V V", val, val+1);
if (!molded) Append_Codepoint_Raw(mold->series, '\n');
}
}
mold->indent--;
if (molded) {
New_Indented_Line(mold);
Append_Codepoint_Raw(mold->series, ']');
}
End_Mold(mold);
TERM_ARRAY_LEN(MOLD_STACK, ARR_LEN(MOLD_STACK) - 1);
}
static void Form_Object(const REBVAL *value, REB_MOLD *mold)
{
REBVAL *key = CTX_KEYS_HEAD(VAL_CONTEXT(value));
REBVAL *var = CTX_VARS_HEAD(VAL_CONTEXT(value));
REBOOL had_output = FALSE;
// Prevent endless mold loop:
if (Find_Same_Array(MOLD_STACK, value) != NOT_FOUND) {
Append_Unencoded(mold->series, "...]");
return;
}
Append_Value(MOLD_STACK, value);
// Mold all words and their values:
for (; !IS_END(key); key++, var++) {
if (!GET_VAL_FLAG(key, TYPESET_FLAG_HIDDEN)) {
had_output = TRUE;
Emit(mold, "N: V\n", VAL_KEY_SPELLING(key), var);
}
}
// Remove the final newline...but only if WE added something to the buffer
if (had_output) {
SET_SERIES_LEN(mold->series, SER_LEN(mold->series) - 1);
TERM_SEQUENCE(mold->series);
}
TERM_ARRAY_LEN(MOLD_STACK, ARR_LEN(MOLD_STACK) - 1);
}
static void Mold_Object(const REBVAL *value, REB_MOLD *mold)
{
REBVAL *keys_head = CTX_KEYS_HEAD(VAL_CONTEXT(value));
REBVAL *vars_head;
REBVAL *key;
REBVAL *var;
if (
!GET_CTX_FLAG(VAL_CONTEXT(value), CONTEXT_FLAG_STACK) ||
GET_CTX_FLAG(VAL_CONTEXT(value), SERIES_FLAG_ACCESSIBLE)
) {
vars_head = CTX_VARS_HEAD(VAL_CONTEXT(value));
}
else {
// If something like a function call has gone of the stack, the data
// for the vars will no longer be available. The keys should still
// be good, however.