forked from rebolsource/r3
-
Notifications
You must be signed in to change notification settings - Fork 27
/
n-data.c
executable file
·1457 lines (1234 loc) · 35.9 KB
/
n-data.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: %n-data.c
// Summary: "native functions for data and context"
// Section: natives
// Project: "Rebol 3 Interpreter and Run-time (Ren-C branch)"
// Homepage: https://github.com/metaeducation/ren-c/
//
//=////////////////////////////////////////////////////////////////////////=//
//
// Copyright 2012 REBOL Technologies
// Copyright 2012-2017 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.
//
//=////////////////////////////////////////////////////////////////////////=//
//
#include "sys-core.h"
static REBOOL Check_Char_Range(REBVAL *val, REBINT limit)
{
if (IS_CHAR(val)) {
if (VAL_CHAR(val) > limit) return FALSE;
return TRUE;
}
if (IS_INTEGER(val)) {
if (VAL_INT64(val) > limit) return FALSE;
return TRUE;
}
REBCNT len = VAL_LEN_AT(val);
if (VAL_BYTE_SIZE(val)) {
REBYTE *bp = VAL_BIN_AT(val);
if (limit == 0xff) return TRUE; // by definition
for (; len > 0; len--, bp++)
if (*bp > limit) return FALSE;
}
else {
REBUNI *up = VAL_UNI_AT(val);
for (; len > 0; len--, up++)
if (*up > limit) return FALSE;
}
return TRUE;
}
//
// ascii?: native [
//
// {Returns TRUE if value or string is in ASCII character range (below 128).}
//
// value [any-string! char! integer!]
// ]
//
REBNATIVE(ascii_q)
{
INCLUDE_PARAMS_OF_ASCII_Q;
return R_FROM_BOOL(Check_Char_Range(ARG(value), 0x7f));
}
//
// latin1?: native [
//
// {Returns TRUE if value or string is in Latin-1 character range (below 256).}
//
// value [any-string! char! integer!]
// ]
//
REBNATIVE(latin1_q)
{
INCLUDE_PARAMS_OF_LATIN1_Q;
return R_FROM_BOOL(Check_Char_Range(ARG(value), 0xff));
}
//
// verify: native [
//
// {Ensure conditions are TRUE?, even when not debugging (see also: ASSERT)}
//
// return: [<opt>]
// conditions [logic! block!]
// {Block of conditions to evaluate, void and FALSE? trigger alerts}
// ]
//
REBNATIVE(verify)
{
INCLUDE_PARAMS_OF_VERIFY;
if (IS_LOGIC(ARG(conditions))) {
if (VAL_LOGIC(ARG(conditions)))
return R_VOID;
fail (Error_Verify_Failed_Raw(FALSE_VALUE));
}
DECLARE_FRAME (f);
Push_Frame(f, ARG(conditions));
DECLARE_LOCAL (temp);
while (NOT_END(f->value)) {
const RELVAL *start = f->value;
if (Do_Next_In_Frame_Throws(D_OUT, f)) {
Drop_Frame(f);
return R_OUT_IS_THROWN;
}
if (!IS_VOID(D_OUT) && IS_CONDITIONAL_TRUE(D_OUT))
continue;
Init_Block(
temp,
Copy_Values_Len_Shallow(start, f->specifier, f->value - start)
);
if (IS_VOID(D_OUT))
fail (Error_Verify_Void_Raw(temp));
fail (Error_Verify_Failed_Raw(temp));
}
Drop_Frame(f);
return R_VOID;
}
// Test used iteratively by MAYBE native. Returns R_BLANK if the test fails,
// R_OUT if success, or R_OUT_IS_THROWN if a test throws.
//
inline static REB_R Do_Test_For_Maybe(
REBVAL *out,
const REBVAL *value,
const RELVAL *test
) {
if (IS_DATATYPE(test)) {
if (VAL_TYPE_KIND(test) != VAL_TYPE(value))
return R_BLANK;
Move_Value(out, value);
return R_OUT;
}
if (IS_TYPESET(test)) {
if (!TYPE_CHECK(test, VAL_TYPE(value)))
return R_BLANK;
Move_Value(out, value);
return R_OUT;
}
if (IS_FUNCTION(test)) {
if (Apply_Only_Throws(out, TRUE, const_KNOWN(test), value, END))
return R_OUT_IS_THROWN;
if (IS_VOID(out))
fail (Error_No_Return_Raw());
if (IS_CONDITIONAL_FALSE(out))
return R_BLANK;
Move_Value(out, value);
return R_OUT;
}
fail (Error_Invalid_Type(VAL_TYPE(test)));
}
//
// maybe: native [
//
// {Check value using tests (match types, TRUE? or FALSE?, filter function)}
//
// return: [<opt> any-value!]
// {The input value or BLANK! if no match, void if FALSE? and matched}
// test [function! datatype! typeset! block! logic!]
// value [<opt> any-value!]
// /?
// "Return LOGIC! of match vs. pass-through of value or blank"
// ]
//
REBNATIVE(maybe)
{
INCLUDE_PARAMS_OF_MAYBE; // ? is renamed as "q"
REBVAL *test = ARG(test);
REBVAL *value = ARG(value);
if (IS_LOGIC(test)) {
if (!IS_VOID(value) && VAL_LOGIC(test) == IS_CONDITIONAL_TRUE(value))
goto type_matched;
return REF(q) ? R_FALSE : R_BLANK;
}
REB_R r;
if (IS_BLOCK(test)) {
//
// !!! What should the behavior for `MAYBE [] ...` be? Should that be
// an error? People wouldn't write it literally, but could wind up
// with an empty array as the product of a COMPOSE or something.
// Consider it ambiguous for now and give back void...
//
r = R_VOID;
const RELVAL *item;
for (item = VAL_ARRAY_AT(test); NOT_END(item); ++item) {
r = Do_Test_For_Maybe(
D_OUT,
value,
IS_WORD(item)
? Get_Opt_Var_May_Fail(item, VAL_SPECIFIER(test))
: item
);
if (r != R_BLANK)
goto type_matched;
}
}
else
r = Do_Test_For_Maybe(D_OUT, value, test);
if (r == R_OUT_IS_THROWN)
return r;
if (REF(q))
return r == R_BLANK ? R_FALSE : R_TRUE;
if (r == R_BLANK)
return r;
assert(r == R_OUT); // must have matched!
type_matched:
if (REF(q))
return R_TRUE;
// Because there may be usages like `if maybe logic! x [print "logic!"]`,
// it would be bad to take in a FALSE and pass back a FALSE. This is
// why /? (and its specialization MAYBE?) exist, but to help avoid
// likely mistakes this returns a void.
//
if (IS_CONDITIONAL_FALSE(value))
return R_VOID;
return R_OUT;
}
//
// as-pair: native [
//
// "Combine X and Y values into a pair."
//
// x [any-number!]
// y [any-number!]
// ]
//
REBNATIVE(as_pair)
{
INCLUDE_PARAMS_OF_AS_PAIR;
REBVAL *x = ARG(x);
REBVAL *y = ARG(y);
SET_PAIR(
D_OUT,
IS_INTEGER(x) ? VAL_INT64(x) : VAL_DECIMAL(x),
IS_INTEGER(y) ? VAL_INT64(y) : VAL_DECIMAL(y)
);
return R_OUT;
}
//
// bind: native [
//
// "Binds words or words in arrays to the specified context."
//
// value [any-array! any-word!]
// "A word or array (modified) (returned)"
// target [any-word! any-context!]
// "The target context or a word whose binding should be the target"
// /copy
// "Bind and return a deep copy of a block, don't modify original"
// /only
// "Bind only first block (not deep)"
// /new
// "Add to context any new words found"
// /set
// "Add to context any new set-words found"
// ]
//
REBNATIVE(bind)
{
INCLUDE_PARAMS_OF_BIND;
REBVAL *value = ARG(value);
REBVAL *target = ARG(target);
REBCTX *context;
REBARR *array;
REBCNT flags = REF(only) ? BIND_0 : BIND_DEEP;
REBU64 bind_types = TS_ANY_WORD;
REBU64 add_midstream_types;
if (REF(new)) {
add_midstream_types = TS_ANY_WORD;
}
else if (REF(set)) {
add_midstream_types = FLAGIT_KIND(REB_SET_WORD);
}
else
add_midstream_types = 0;
if (ANY_CONTEXT(target)) {
//
// Get target from an OBJECT!, ERROR!, PORT!, MODULE!, FRAME!
//
context = VAL_CONTEXT(target);
}
else {
//
// Extract target from whatever word we were given
//
assert(ANY_WORD(target));
if (IS_WORD_UNBOUND(target))
fail (Error_Not_Bound_Raw(target));
// The word in hand may be a relatively bound one. To return a
// specific frame, this needs to ensure that the Reb_Frame's data
// is a real context, not just a chunk of data.
//
context = VAL_WORD_CONTEXT(target);
}
if (ANY_WORD(value)) {
//
// Bind a single word
if (Try_Bind_Word(context, value)) {
Move_Value(D_OUT, value);
return R_OUT;
}
// not in context, bind/new means add it if it's not.
//
if (REF(new) || (IS_SET_WORD(value) && REF(set))) {
Append_Context(context, value, NULL);
Move_Value(D_OUT, value);
return R_OUT;
}
fail (Error_Not_In_Context_Raw(ARG(value)));
}
// Copy block if necessary (/copy)
//
// !!! NOTE THIS IS IGNORING THE INDEX! If you ask to bind, it should
// bind forward only from the index you specified, leaving anything
// ahead of that point alone. Not changing it now when finding it
// because there could be code that depends on the existing (mis)behavior
// but it should be followed up on.
//
Move_Value(D_OUT, value);
if (REF(copy)) {
array = Copy_Array_At_Deep_Managed(
VAL_ARRAY(value), VAL_INDEX(value), VAL_SPECIFIER(value)
);
INIT_VAL_ARRAY(D_OUT, array); // warning: macro copies args
}
else
array = VAL_ARRAY(value);
Bind_Values_Core(
ARR_HEAD(array),
context,
bind_types,
add_midstream_types,
flags
);
return R_OUT;
}
//
// context-of: native [
//
// "Returns the context in which a word is bound."
//
// word [any-word!]
// ]
//
REBNATIVE(context_of)
{
INCLUDE_PARAMS_OF_CONTEXT_OF;
if (IS_WORD_UNBOUND(ARG(word))) return R_BLANK;
// Requesting the context of a word that is relatively bound may result
// in that word having a FRAME! incarnated as a REBSER node (if it
// was not already reified.)
//
// !!! Mechanically it is likely that in the future, all FRAME!s for
// user functions will be reified from the moment of invocation.
//
Move_Value(D_OUT, CTX_VALUE(VAL_WORD_CONTEXT(ARG(word))));
return R_OUT;
}
//
// any-value?: native [
//
// "Returns whether a data cell contains a value."
//
// cell [<opt> any-value!]
// ]
//
REBNATIVE(any_value_q)
{
INCLUDE_PARAMS_OF_ANY_VALUE_Q;
if (IS_VOID(ARG(cell)))
return R_FALSE;
return R_TRUE;
}
//
// unbind: native [
//
// "Unbinds words from context."
//
// word [block! any-word!]
// "A word or block (modified) (returned)"
// /deep
// "Process nested blocks"
// ]
//
REBNATIVE(unbind)
{
INCLUDE_PARAMS_OF_UNBIND;
REBVAL *word = ARG(word);
if (ANY_WORD(word))
Unbind_Any_Word(word);
else
Unbind_Values_Core(VAL_ARRAY_AT(word), NULL, REF(deep));
Move_Value(D_OUT, word);
return R_OUT;
}
//
// collect-words: native [
//
// {Collect unique words used in a block (used for context construction).}
//
// block [block!]
// /deep
// "Include nested blocks"
// /set
// "Only include set-words"
// /ignore
// "Ignore prior words"
// hidden [any-context! block!]
// "Words to ignore"
// ]
//
REBNATIVE(collect_words)
{
INCLUDE_PARAMS_OF_COLLECT_WORDS;
REBARR *words;
REBCNT modes;
RELVAL *values = VAL_ARRAY_AT(ARG(block));
RELVAL *prior_values;
if (REF(set))
modes = COLLECT_ONLY_SET_WORDS;
else
modes = COLLECT_ANY_WORD;
if (REF(deep)) modes |= COLLECT_DEEP;
// If ignore, then setup for it:
if (REF(ignore)) {
if (ANY_CONTEXT(ARG(hidden))) {
//
// !!! These are typesets and not words. Is Collect_Words able
// to handle that?
//
prior_values = CTX_KEYS_HEAD(VAL_CONTEXT(ARG(hidden)));
}
else {
assert(IS_BLOCK(ARG(hidden)));
prior_values = VAL_ARRAY_AT(ARG(hidden));
}
}
else
prior_values = NULL;
words = Collect_Words(values, prior_values, modes);
Init_Block(D_OUT, words);
return R_OUT;
}
//
// get: native [
//
// {Gets the value of a word or path, or values of a context.}
//
// return: [<opt> any-value!]
// {If the source looks up to a value, that value--else void}
// source [blank! any-word! any-path! block!]
// {Word or path to get, or block of words or paths (blank is no-op)}
// /opt
// {Return void if no value instead of blank}
// ]
//
REBNATIVE(get)
//
// Note: GET* cannot be the fundamental operation, because GET could not be
// written for blocks (since voids can't be put in blocks, so they couldn't
// be "blankified")
{
INCLUDE_PARAMS_OF_GET;
RELVAL *source;
REBVAL *dest;
REBSPC *specifier;
// Move the argument into the single cell in the frame if it's not a
// block, so the same enumeration-up-to-an-END marker can work on it
// as for handling a block of items.
//
REBARR *results;
if (IS_BLOCK(ARG(source))) {
//
// If a BLOCK! of gets are performed, voids cannot be put into the
// resulting BLOCK!. Hence for /OPT to be legal, it would have to
// give back a BLANK! or other placeholder. However, since GET-VALUE
// is built on GET/OPT, we defer the error until we actually encounter
// an unset variable...which produces that error case that could not
// be done by "checking the block for voids"
source = VAL_ARRAY_AT(ARG(source));
specifier = VAL_SPECIFIER(ARG(source));
results = Make_Array(VAL_LEN_AT(ARG(source)));
TERM_ARRAY_LEN(results, VAL_LEN_AT(ARG(source)));
dest = SINK(ARR_HEAD(results));
}
else {
Move_Value(D_CELL, ARG(source));
source = D_CELL;
specifier = SPECIFIED;
dest = D_OUT;
results = NULL; // wasteful but avoids maybe-used-uninitalized warning
}
DECLARE_LOCAL (get_path_hack); // runs prep code, don't put inside loop
for (; NOT_END(source); ++source, ++dest) {
if (IS_BAR(source)) {
//
// `a: 10 | b: 20 | get [a | b]` will give back `[10 | 20]`.
// While seemingly not a very useful feature standalone, this
// compatibility with SET could come in useful so that blocks
// don't have to be rearranged to filter out BAR!s.
//
SET_BAR(dest);
}
else if (IS_BLANK(source)) {
SET_VOID(dest); // may be turned to blank after loop, or error
}
else if (ANY_WORD(source)) {
Copy_Opt_Var_May_Fail(dest, source, specifier);
}
else if (ANY_PATH(source)) {
//
// Make sure the path does not contain any GROUP!s, because that
// would trigger evaluations. GET does not sound like something
// that should have such a side-effect, the user should go with
// a REDUCE operation if that's what they want.
//
RELVAL *temp = VAL_ARRAY_AT(source);
for (; NOT_END(temp); ++temp)
if (IS_GROUP(temp))
fail ("GROUP! can't be in paths with GET, use REDUCE");
// Piggy-back on the GET-PATH! mechanic by copying to a temp
// value and changing its type bits.
//
// !!! Review making a more efficient method of doing this.
//
Derelativize(get_path_hack, source, specifier);
VAL_SET_TYPE_BITS(get_path_hack, REB_GET_PATH);
// Here we DO it, which means that `get 'foo/bar` will act the
// same as `:foo/bar` for all types.
//
if (Do_Path_Throws_Core(
dest,
NULL,
get_path_hack,
SPECIFIED,
NULL
)){
// Should not be possible if there's no GROUP!
//
fail (Error_No_Catch_For_Throw(dest));
}
}
if (IS_VOID(dest)) {
if (REF(opt)) {
if (IS_BLOCK(ARG(source))) // can't put voids in blocks
fail (Error_No_Value_Core(source, specifier));
}
else
SET_BLANK(dest);
}
}
if (IS_BLOCK(ARG(source)))
Init_Block(D_OUT, results);
return R_OUT;
}
//
// to-value: native [
//
// {Turns unset to NONE, with ANY-VALUE! passing through. (See: OPT)}
//
// return: [any-value!]
// value [<opt> any-value!]
// ]
//
REBNATIVE(to_value)
{
INCLUDE_PARAMS_OF_TO_VALUE;
if (IS_VOID(ARG(value)))
return R_BLANK;
Move_Value(D_OUT, ARG(value));
return R_OUT;
}
//
// opt: native [
//
// {Convert blanks to optionals. (See Also: TO-VALUE)}
//
// return: [<opt> any-value!]
// {void if input was a BLANK!, or original value otherwise}
// value [<opt> any-value!]
// ]
//
REBNATIVE(opt)
{
INCLUDE_PARAMS_OF_OPT;
if (IS_BLANK(ARG(value)))
return R_VOID;
Move_Value(D_OUT, ARG(value));
return R_OUT;
}
//
// in: native [
//
// "Returns the word or block bound into the given context."
//
// context [any-context! block!]
// word [any-word! block! group!] "(modified if series)"
// ]
//
REBNATIVE(in)
//
// !!! The argument names here are bad... not necessarily a context and not
// necessarily a word. `code` or `source` to be bound in a `target`, perhaps?
{
INCLUDE_PARAMS_OF_IN;
REBVAL *val = ARG(context); // object, error, port, block
REBVAL *word = ARG(word);
DECLARE_LOCAL (safe);
if (IS_BLOCK(val) || IS_GROUP(val)) {
if (IS_WORD(word)) {
const REBVAL *v;
REBCNT i;
for (i = VAL_INDEX(val); i < VAL_LEN_HEAD(val); i++) {
Get_Simple_Value_Into(
safe,
VAL_ARRAY_AT_HEAD(val, i),
VAL_SPECIFIER(val)
);
v = safe;
if (IS_OBJECT(v)) {
REBCTX *context = VAL_CONTEXT(v);
REBCNT index = Find_Canon_In_Context(
context, VAL_WORD_CANON(word), FALSE
);
if (index != 0) {
CLEAR_VAL_FLAG(word, VALUE_FLAG_RELATIVE);
SET_VAL_FLAG(word, WORD_FLAG_BOUND);
INIT_WORD_CONTEXT(word, context);
INIT_WORD_INDEX(word, index);
Move_Value(D_OUT, word);
return R_OUT;
}
}
}
return R_BLANK;
}
fail (word);
}
REBCTX *context = VAL_CONTEXT(val);
// Special form: IN object block
if (IS_BLOCK(word) || IS_GROUP(word)) {
Bind_Values_Deep(VAL_ARRAY_HEAD(word), context);
Move_Value(D_OUT, word);
return R_OUT;
}
REBCNT index = Find_Canon_In_Context(context, VAL_WORD_CANON(word), FALSE);
if (index == 0)
return R_BLANK;
Init_Any_Word_Bound(
D_OUT,
VAL_TYPE(word),
VAL_WORD_SPELLING(word),
context,
index
);
return R_OUT;
}
//
// resolve: native [
//
// {Copy context by setting values in the target from those in the source.}
//
// target [any-context!] "(modified)"
// source [any-context!]
// /only
// "Only specific words (exports) or new words in target"
// from [block! integer!]
// "(index to tail)"
// /all
// "Set all words, even those in the target that already have a value"
// /extend
// "Add source words to the target if necessary"
// ]
//
REBNATIVE(resolve)
{
INCLUDE_PARAMS_OF_RESOLVE;
if (IS_INTEGER(ARG(from))) {
// check range and sign
Int32s(ARG(from), 1);
}
UNUSED(REF(only)); // handled by noticing if ARG(from) is void
Resolve_Context(
VAL_CONTEXT(ARG(target)),
VAL_CONTEXT(ARG(source)),
ARG(from),
REF(all),
REF(extend)
);
Move_Value(D_OUT, ARG(target));
return R_OUT;
}
//
// set: native [
//
// {Sets a word, path, block of words, or context to specified value(s).}
//
// return: [<opt> any-value!]
// {Will be the values set to, or void if any set values are void}
// target [blank! any-word! any-path! block!]
// {Word or path, or block of words and paths (blanks are no-ops)}
// value [<opt> any-value!]
// "Value or block of values"
// /opt
// {Treat void values as unsetting the target instead of an error}
// /pad
// {Set remaining words to BLANK! if block is too short}
// /lookback
// {Function uses evaluator lookahead to "look back" (see SET-INFIX)}
// ]
//
REBNATIVE(set)
//
// Blocks are supported as:
//
// >> set [a b] [1 2]
// >> print a
// 1
// >> print b
// 2
//
// !!! Should the /LOOKBACK refinement be called /ENFIX?
{
INCLUDE_PARAMS_OF_SET;
const RELVAL *value;
REBSPC *value_specifier;
const RELVAL *target;
REBSPC *target_specifier;
if (IS_BLOCK(ARG(target))) {
//
// R3-Alpha and Red let you write `set [a b] 10`, since the thing
// you were setting to was not a block, would assume you meant to set
// all the values to that. BUT since you can set things to blocks,
// this has a bad characteristic of `set [a b] [10]` being treated
// differently, which can bite you if you `set [a b] value` for some
// generic value.
//
// Ren-C does not carry forward the behavior, rather it allows only
// one case...to pass in a blank value with /PAD and handle that blank
// as if it were an empty block, return blanks. This permits things
// like `if set [a b] find data 'whatever [...]` to do a blank
// propagation.
//
// !!! Should there be a SET/ONLY to allow for `set/only [a b] [10]`
// such that a and b would be set to 10? Seems to add complexity for
// a case that is not that compelling.
//
if (IS_BLANK(ARG(value)) && REF(pad)) {
value = ARR_HEAD(EMPTY_ARRAY); // don't change ARG(value)
value_specifier = SPECIFIED;
}
else if (IS_BLOCK(ARG(value))) {
//
// There is no need to check values for voidness in this case,
// since arrays cannot contain voids.
//
value = VAL_ARRAY_AT(ARG(value));
value_specifier = VAL_SPECIFIER(ARG(value));
}
else
fail (ARG(value)); // value must be a block if setting a block
target = VAL_ARRAY_AT(ARG(target));
target_specifier = VAL_SPECIFIER(ARG(target));
}
else {
// Use the fact that D_CELL is implicitly terminated so that the
// loop below can share code between `set [a b] x` and `set a x`, by
// incrementing the target pointer and hitting an END marker
//
assert(
ANY_WORD(ARG(target))
|| ANY_PATH(ARG(target))
|| IS_BLANK(ARG(target))
);
Move_Value(D_CELL, ARG(target));
target = D_CELL;
target_specifier = SPECIFIED;
// The target is used in the incrementation, so there's no need to
// terminate the value for iteration.
//
if (IS_VOID(ARG(value)) && NOT(REF(opt)))
fail (Error_No_Value(ARG(value)));
value = ARG(value);
value_specifier = SPECIFIED;
}
DECLARE_LOCAL (get_path_hack); // runs prep code, don't put inside loop
for (; NOT_END(target); ++target) {
if (IS_END(value) && NOT(REF(pad)))
break;
if (IS_BAR(target)) {
if (NOT_END(value) || NOT(IS_BAR(value)))
fail ("BAR! can only line up with other BAR! in SET");
}
else if (IS_BLANK(target)) {
//
// Just skip it
}
else if (ANY_WORD(target)) {
if (REF(lookback) && NOT(IS_FUNCTION(ARG(value))))
fail ("Attempt to SET/LOOKBACK on a non-function");
REBVAL *var = Sink_Var_May_Fail(target, target_specifier);
Derelativize(var, IS_END(value) ? BLANK_VALUE : value, value_specifier);
if (REF(lookback))
SET_VAL_FLAG(var, VALUE_FLAG_ENFIXED);
}
else if (ANY_PATH(target)) {
//
// Make sure the path does not contain any GROUP!s, because that
// would trigger evaluations. SET does sound like it has a
// side effect (unlike GET), but you don't expect the side effect
// to do things like PRINT, which arbitrary code can do.
//
RELVAL *temp = VAL_ARRAY_AT(target);
for (; NOT_END(temp); ++temp)
if (IS_GROUP(temp))
fail ("GROUP! can't be in paths with SET");
// !!! For starters, just the word form is supported for lookback.
// Though you can't dispatch a lookback from a path, you should be
// able to set a word in a context to one.
//
if (REF(lookback))
fail ("Cannot currently SET/LOOKBACK on a PATH!");
DECLARE_LOCAL (specific);
if (IS_END(value))
SET_BLANK(specific);
else
Derelativize(specific, value, value_specifier);
// Currently we have to tweak the bits of the path so that it's a
// GET-PATH!, since Do_Path is sensitive to the path type, and we
// want all to act the same.
//
Derelativize(get_path_hack, target, target_specifier);
VAL_SET_TYPE_BITS(get_path_hack, REB_GET_PATH);
if (
Do_Path_Throws_Core(
D_OUT,
NULL,
get_path_hack,
SPECIFIED,
specific
)
){
fail (Error_No_Catch_For_Throw(D_OUT));
}
// If not a throw, then there is no result out of a setting a path
}
else
fail (Error_Invalid_Arg_Core(target, target_specifier));
if (NOT_END(value))
++value; // may not have terminator, incrementation unused then
}
Move_Value(D_OUT, ARG(value));
return R_OUT;
}