-
Notifications
You must be signed in to change notification settings - Fork 2
/
r2-forward.r
1881 lines (1770 loc) · 62.4 KB
/
r2-forward.r
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
REBOL [
Title: "REBOL 3 Forward Compatibility Functions"
Name: 'r2-forward
Type: 'module
Version: 2.100.80.4
Date: 23-Feb-2011
File: %r2-forward.r
Author: "Brian Hawley" ; BrianH
Purpose: "Make REBOL 2 more compatible with REBOL 3."
Exports: [
; Function creators
funco
func
function
funct
does
has
closure
; Error management
cause-error
throw-error ; R2 only, not needed in R3
attempt
assert
; Datatype spoofing
map! map? to-map
get-path! get-path? to-get-path
to-function
closure! closure? to-closure
;module! module? to-module module import
typeset! typeset? to-typeset
any-path! any-path?
any-object! any-object?
scalar! scalar?
immediate!
internal!
; Control functions
!
++
--
also
quote
true?
default
apply
eval
;delect
; Object functions
object
extend
resolve
;unbind
; Series functions
ajoin
first+
last?
past?
remold
append
swap
take
move
array
extract
replace
alter
map-each
find-all
collect
collect-words
; Character/string encoding functions
ascii?
latin1?
utf?
invalid-utf?
deline
enline
; File functions
what-dir
info?
dir?
exists?
undirize
list-dir
ls
pwd
rm
mkdir
cd
more
in-dir
to-relative-file
; Reflection functions
reflect
spec-of
body-of
words-of
values-of
types-of
title-of
; Profiling functions
dt delta-time
;dp delta-profile
speed?
] ; No Globals to limit any potential damage.
License: {
Copyright (c) 2008-2009 Brian Hawley
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
} ; MIT
]
; Note: The functions in this file are backports from R3, and will stay as
; compatible with R3 as possible. Don't add functions that existed in 2.7.5
; unless they are not compatible with their behavior in 2.7.5.
;
; R3's current behavior will be tracked as best as it is feasible, and any
; missing functions or changed behavior will be added eventually. In the short
; term some proposed features/changes will be added if they seem likely to be
; accepted (the acceptance process can be a little slow sometimes). The version
; number reflects the corresponding R3 alpha version number, plus revisions.
;
; Intentionally not supported (in some cases impossible in R2):
; - Unicode codepoints over 255 in string! and char!.
; - Conversion from UTF-8 binary! to string! of Unicode codepoints over 127.
; - Conversion from string! to UTF-8 binary! of Unicode codepoints over 127.
; - Functions related to the new port model.
; - Functions related to the new graphics model.
; - Functions related to codecs (decode, encode, encoding?, ...).
; - Functions related to other new types I can't spoof (task!, utype!, ...).
; - Functions or types for guru or internal use (evoke, stack, native, ...).
; - Functions that extend objects, or other unsupported datatype tricks.
; - Functions that call chat or the new docs.
; - Changes or fixes to datatypes already in R2 (string!, port!, error!, ...).
; - Changed or fixed R2 natives (there will be another file for those).
; - Compatibility patches to R2 GUI or port code (there will be other files
; for those, maybe, as possible or needed).
; - Pre-2.6.2 changes (there will be another file for those).
; - Syntax changes (percent!, get-word meaning get/any, ...).
; - Changes to the system object or command line parameters.
; - Deliberately removed features (ordinal reflection, ...).
;
; R3-compatibility notes about lit-word! and get-word! parameters:
; - In R2, lit-word! parameters evaluate get-word! arguments. In R3, param!
; arguments are also evaluated. This can be emulated in R2: see CD below.
; - In R2, get-word! parameters retrieve the value from word! arguments. In R3
; they don't evaluate or retrieve anything. There is no way to do this in R2,
; which is why QUOTE is impossible in R2 for word! arguments (use lit-words).
; Also, op!s in R2 operate in prefix mode when assigned to another word, while
; they still operate in infix mode in R3 - we take advantage of that here.
;
; Unless otherwise marked, all functions are based on the behavior of their
; R3 counterparts (written by Carl if native, by BrianH if not), and are
; all new code written by BrianH. Some were suggested or proposed by others,
; and will be marked as such. The doc strings are copied from R3.
; History:
; 27-Jan-2009: Initial version, plus initial changes.
; - Moved the post-2.7.5 new R2 functions I wrote in 2008-2009 here.
; - Added MAP/into, SUBSTITUTE and DEFAULT to the future additions.
; 3-Feb-2009: Tweaking future additions based on community input.
; - Renamed SUBSTITUTE to REWORD on Carl's request.
; - Added FILEIZE, FILE-EXISTS? and DIR-EXISTS? to future additions.
; 6-Feb-2009: Tweaking future additions based on community input.
; - Renamed FILEIZE to UNDIRIZE.
; - Replaced FILE-EXISTS? and DIR-EXISTS? with new EXISTS? function.
; 12-Feb-2009: Community requests and backports.
; - QUOTE, ACCUMULATE, COLLECT, and GATHER added to future additions.
; - FUNCT fixed for 'a and :a parameters, and a: directives.
; - Backported CAUSE-ERROR and the whole R3 reflection model.
; 13-Feb-2009: Friday the 13th, prep for release.
; - Made the header compatible with Gabriele's R2 modules.
; - Clarified licensing, authorship and attribution.
; - Tweaked the comments and ported the history from DevBase.
; - Created a detab-and-copy script so DevBase and Qtask sync.
; 14-Feb-2009: Backported APPLY. Compatibility comment for QUOTE.
; 15-Feb-2009: Implemented MAP with full ideal R3 behavior. Exported APPLY.
; 16-Feb-2009: The rest of the practical, for now.
; - Spoofing of MAP!, GET-PATH!, TYPESET!, ANY-PATH! and SCALAR!.
; - Backported the remaining function creators, !, ++, --, TRUE?,
; OBJECT, EXTEND, ASCII?, LATIN1? (partial), UTF?, DELINE, ENLINE,
; WHAT-DIR, LIST-DIR, LS, PWD, RM, MKDIR, CD, MORE, DT & DELTA-TIME.
; - Enhanced ARRAY, EXTRACT, REPLACE, ALTER (some long ago).
; - Formatting and documentation tweaks.
; 17-Feb-2009: Fix to the platform numbers in ENLINE (silly bug), comments.
; 20-Feb-2009: Tweaks.
; - Fixed ASCII? and LATIN1? (bug-for-bug compatible with R3 now).
; - Backported EVAL (don't know what it's used for, but it's in the docs).
; - Changed 'script 'invalid-arg error to 'invalid (R2 compatible, need docs).
; - Removed error! from default parameter types in TYPES-OF (R2 compatible).
; - Added R3 lit-word! parameter compatiblity paren! trick to FIRST+.
; 21-Feb-2009: More coverage of the newly semi-documented R3 function set.
; - Backported SWAP, INFO?, DIR? (partial), ALTER/case, and promoted COLLECT.
; - Replaced the PRINTF call in LIST-DIR since R2 doesn't have PRINTF yet.
; - Minor rearrangement in the order of functions for better grouping.
; - Filled in the rest of my todo list (commented exports above).
; - Added a section to the notes about what changes won't be done here.
; 22-Feb-2009: Added proposed /with option to ENLINE, modeled after WRITE/with.
; 25-Feb-2009: Fixes...
; - Changed 'script 'invalid error back to 'invalid-arg (found docs).
; - Nicer and more specific errors for TO-TYPESET, ++, --, APPLY, TAKE, ARRAY,
; EXTRACT, MAP, LIST-DIR, CD, MORE and REFLECT.
; - Backported new APPEND. Put RESOLVE on todo list.
; 25-Feb-2009: New R3 acceptances.
; - Promoted QUOTE, DEFAULT, EXISTS? and UNDIRIZE. EXISTS? now uses new INFO?.
; - Reworked the error handling of the *-OF reflectors to better match R3.
; 26-Feb-2009: Backported RESOLVE.
; 28-Feb-2009: Tweaks.
; - R3's WORDS-OF binds to an object, so changed WORDS-OF to match.
; - Tweaked APPLY and RESOLVE for new WORDS-OF behavior.
; 10-Mar-2009: Removed catch from APPLY function spec to aid debugging.
; 11-Mar-2009: EXTRACT tweaked to work around a bug in PARSE.
; 28-Apr-2009: 2.100.37.1 (tracking R3 2.100.37, revision 1)
; - Rearranged the header to better reflect R3 module headers, with a version.
; - Removed limit in APPLY on number of function parameter workarounds.
; - Tweaked many functions after a conversation with Ladislav about error!.
; - Added THROW-ERROR for use in R2 functions with the [catch] attribute.
; 4-May-2009: Fixed bugs in APPLY and MAP, lowered MAP memory overhead.
; 5-May-2009: Implemented RESOLVE/only.
; 9-Jun-2009: Fixed ATTEMPT.
; 10-Jun-2009: Made CAUSE-ERROR and THROW-ERROR more compatible with 2.100.53.
; 16-Jun-2009: Added FUNCT/with, removed FUNCTOR, loose backport of ASSERT.
; 17-Jun-2009: Catching up with R3 alpha 56.
; - Added SPEED? and ANY-OBJECT!, fixed SCALAR!, tweaked APPLY and MOVE.
; - Added modules, UNBIND and COLLECT-WORDS to to-do list.
; - Added codecs, system object and command line changes to not-to-do list.
; 22-Jun-2009: Added closures, TO-FUNCTION. Improved FUNCT.
; 12-Jan-2010: Minor fixes to TO-TYPESET, TYPES-OF and UNDIRIZE.
; 22-Jan-2010: Updated to 2.100.80 semantics
; - Added COLLECT-WORDS, REMOLD, SINGLE?, IMMEDIATE!, INTERNAL!, INVALID-UTF?.
; - Removed buggy binary! support from ASCII? and LATIN1?, as done in 2.100.60.
; - Removed buggy commented-out Unicode checking charsets.
; - Rewrote RESOLVE, adding the /all option, as done in 2.100.73.
; - Renamed MAP to MAP-EACH, as done in 2.100.79.
; - Note: TAKE doesn't have a /deep option, as in 2.100.80.
; 26-Mar-2010: 2.100.80.1 (tracking R3 2.100.80, revision 1)
; - Fixed EXTRACT of FALSE, APPLY with word! values.
; 3-Jul-2010: 2.100.80.2 (tracking R3 2.100.80, revision 2)
; - Fixed index math of MOVE/to/skip.
; - Backported PAST?.
; 30-Dec-2010: 2.100.80.3 (tracking R3 2.100.80, revision 3, and then some)
; - Renamed SINGLE? to LAST?, in keeping with bug#1636.
; - Added FUNCT /extern words option from 2.100.108.
; - Added FIND-ALL based on the bug#1811 changes.
; - INVALID-UTF? was missing a [catch] clause in its function spec.
; 23-Feb-2011: 2.100.80.4 (tracking R3 2.100.80, revision 3, and then some)
; - Fixed bug in MAP-EACH related to the [throw] function spec clause.
; - Exported FIND-ALL, now that bug#1811 is accepted in R3.
; Function creation functions
funco: make function! [
"Defines a function, but does not copy spec or body."
spec [block!] "Help string (opt) followed by arg words (and opt type and string)"
body [block!] "The body block of the function"
][ ; For functions known to have no syntax errors or recursive issues.
make function! spec body
]
; Carl wrote the R3 version, partly copied here.
func: funco [
"Defines a user function with given spec and body."
[catch]
spec [block!] "Help string (opt) followed by arg words (and opt type and string)"
body [block!] "The body block of the function"
][
throw-on-error [make function! copy/deep spec copy/deep body]
]
; Carl wrote the R3 version, partly copied here.
function: funco [
"Defines a user function with local words."
[catch]
spec [block!] "Optional help info followed by arg words (and optional type and string)"
vars [block!] "List of words that are local to the function"
body [block!] "The body block of the function"
][
throw-on-error [make function! copy/deep compose [(spec) /local (vars)] copy/deep body]
]
; Carl wrote the R3 version, partly copied here.
funct: funco [
"Defines a function with all set-words as locals."
[catch]
spec [block!] "Help string (opt) followed by arg words (and opt type and string)"
body [block!] "The body block of the function"
/with "Define or use a persistent object (self)"
object [object! block!] "The object or spec"
/extern words [block!] "These words are not local"
/local r ws wb a
][
spec: copy/deep spec
body: copy/deep body
; Get the words in the spec (ws) as word! so we can screen them out later
ws: make block! length? spec
parse spec [any [
set-word! | set a any-word! (insert tail ws to-word a) | skip
]]
; Build the object if need be and screen out its words too
if with [
unless object? object [object: make object! object]
bind body object ; Bind any object words found in the body
insert tail ws first object ; first used since 'self should be included
]
; Screen out the external words too
insert tail ws words
; Get any set-words in the code block as words (wb)
wb: make block! 12 ; This should be a reasonable default
parse body r: [any [
set a set-word! (insert tail wb to-word a) |
hash! | into r | skip
]]
; Remove the ws words from wb and add the rest to the spec as locals
unless empty? wb: exclude wb ws [
remove find wb 'local
unless find spec /local [insert tail spec /local]
insert tail spec wb
]
throw-on-error [make function! spec body]
]
; Note: The set-word! collection and spec word screening is native in R3.
; All new code based on an initial R3 version from Carl.
does: funco [
"A shortcut to define a function that has no arguments or locals."
[catch]
body [block!] "The body block of the function"
][
throw-on-error [make function! copy [] copy/deep body]
]
; Carl wrote the R3 version, partly copied here.
has: funco [
"A shortcut to define a function that has local variables but no arguments."
[catch]
vars [block!] "List of words that are local to the function"
body [block!] "The body block of the function"
][
throw-on-error [make function! head insert copy/deep vars /local copy/deep body]
]
; Carl wrote the R3 version, partly copied here.
closure: funco [
"Defines a closure function."
[catch]
spec [block!] "Help string (opt) followed by arg words (and opt type and string)"
body [block!] "The body block of the function"
/local spc bdy word
] [
spc: make block! 1 + (2 * length? spec) ; 2 * in case arguments not typed
insert/only spc [throw]
bdy: make block! 5 + length? spec
insert bdy reduce [:do :make :function! spc body]
parse spec [any [
set-word! | set word any-word! (
insert tail bdy to word! :word
insert tail spc to get-word! :word
insert/only tail spc [any-type!]
) | skip
]]
throw-on-error [make function! spec bdy]
]
; Note: Adapted from code by Ladislav Mecir, used with permission.
; This uses the R2-style get-word! parameter word! retrieval.
; The :do :make :function! signiature is checked in CLOSURE? below.
; Error management
cause-error: funco [
"Causes an immediate error with the provided information."
err-type [word!]
err-id [word!]
args
][
parse args: compose [(:args)] [
0 3 [args: any-function! (change/only args spec-of first args) | skip]
]
args: head args
make error! reduce [err-type err-id pick args 1 pick args 2 pick args 3]
]
; Note: Some of the errors have changed names between R2 and R3.
; All new code based on an initial R3 version from Carl.
throw-error: funco [
"Causes an immediate error throw with the provided information."
err-type [word!]
err-id [word!]
args
][
parse args: compose [(:args)] [
0 3 [args: any-function! (change/only args spec-of first args) | skip]
]
args: head args
throw make error! reduce [err-type err-id pick args 1 pick args 2 pick args 3]
]
; Note: Version of CAUSE-ERROR for R2 functions with the [catch] attribute.
; There is no reason for R3 to have this function - R3 has stack traces.
attempt: funco [
"Tries to evaluate and returns result or NONE on error."
[throw]
value ;[block!] ; Unnecessary overhead - TRY tests for this type
][
unless error? set/any 'value try :value [get/any 'value]
]
assert: funco [
"Assert that condition is true, else throw an assertion error."
[catch throw]
conditions [block!]
/type "Safely check datatypes of variables (words)" ; not paths
/local w t
][throw-on-error [
either type [
parse conditions [any [
[set w word! | set w skip (
cause-error 'script 'invalid-arg type? get/any 'w
)]
[set t [block! | word!] (
unless find to-typeset t type? get/any w [
make error! join "datatype assertion failed for: " w
]
) | set t skip (
cause-error 'script 'invalid-arg type? get/any 't
)]
]]
][
any [
all conditions
make error! join "assertion failed for: " mold conditions
]
]
]]
; This is a relatively slow, loose approximation of a native in R3.
; ASSERT/type of paths doesn't work yet, since GET doesn't either.
; Datatype spoofing (be careful)
; Fake map! with hash!
map!: :hash! ; Doesn't work in function specs, TYPE?
map?: :hash?
to-map: :to-hash
; Note: Not exactly the same thing, so use /skip and be careful.
; Fake get-path! with path!
get-path!: :path! ; Doesn't work in function specs, TYPE?
get-path?: funco ["Returns TRUE if it is this type." value [any-type!]] [
found? all [path? get/any 'value get-word? pick value 1]
] ; Note: PATH? will also succeed in R2
to-get-path: funco ["Converts to get-path! value." value] [
value: to-path :value
if word? pick value 1 [poke value 1 to-get-word pick value 1]
value ; get-path? fails if first element not get-word!
]
; R3 has TO-FUNCTION
to-function: funco ["Converts to function! value." value [block!]] [
make function! pick value 1 pick value 2
] ; Should be close enough to the R3 version.
; Fake closure! with function!
closure!: :function! ; Doesn't work in function specs, TYPE?
closure?: funco ["Returns TRUE if it is this type." value [any-type!]] [
all [
function? get/any 'value
value: second :value ; Not doing BODY-OF here to lower overhead
same? pick value 1 :do
same? pick value 2 :make
same? pick value 3 :function!
] ; This should be good enough...
] ; Note: FUNCTION? will also succeed in R2
to-closure: funco ["Converts to closure! value." value [block!]] [
closure pick value 1 pick value 2
] ; Should be close enough to the R3 version.
; Fake typeset! with block! of datatype!
typeset!: :block!
typeset?: funco ["Returns TRUE if it is this type." value [any-type!]] [
found? all [block? get/any 'value parse value [any datatype!]]
] ; Note: BLOCK? will also succeed in R2
to-typeset: funct [
"Converts to typeset! value." [catch] value
] [
anytype: [
none! logic! integer! decimal! money! char! pair! tuple! time! date!
string! binary! file! email! url! tag! issue! bitset! image! block!
paren! path! set-path! lit-path! datatype! word! set-word! get-word!
lit-word! refinement! native! action! routine! op! function! object!
error! port! event! struct! library! hash! list! symbol! unset!
]
anyblock: [
block! paren! path! set-path! lit-path! hash! list!
]
anyfunction: [
native! action! routine! op! function! ;rebcode!
]
anystring: [
string! binary! file! email! url! tag! issue!
]
anyword: [
word! set-word! get-word! lit-word! refinement!
]
series: [
string! binary! file! email! url! tag! issue! image!
block! paren! path! set-path! lit-path! hash! list!
]
number: [integer! decimal!]
switch/default either datatype? :value [to-word value] [:value] [
any-type! [reduce anytype]
any-block! [reduce anyblock]
any-function! [reduce anyfunction]
any-string! [reduce anystring]
any-word! [reduce anyword]
series! [reduce series]
number! [reduce number]
] [
switch/default type?/word :value [
datatype! [reduce [value]]
block! [
parse value: copy value [any [
datatype! | value: word! (
change value throw-on-error [to-datatype first value]
) | value: block! (
value: change/part value to-typeset first value 1
) :value |
value: skip (
throw-error 'script 'invalid-arg reduce [first value]
)
]]
head value
]
] [throw-error 'script 'invalid-arg reduce [:value]] ; 'bad-make-arg in R3
]
] ; MAKE or TO typeset! anything-else doesn't work - use TO-TYPESET.
; These blocks of datatypes can be used with FIND like R3 typesets.
; The logical operations AND, OR and XOR don't work with fake typesets.
; Note: You need to use FOUND? with FIND typeset in R2 to get the R3 result.
; R3 new types not included: percent! vector! get-path! map! typeset! rebcode!
; command! closure! frame! module! task! gob! handle! utype!
; R2 pseudotypes special-cased:
; any-block! any-function! any-string! any-type! any-word! series! number!
; R2 obsolete types included: hash! list! symbol!
; Fake any-path! typeset
any-path!: reduce [path! lit-path! set-path!]
any-path?: funco [
"Return TRUE if value is any type of path."
value [any-type!]
][
found? find any-path! type? get/any 'value
]
; Fake any-object! typeset
any-object!: reduce [object! error! port!] ; module! task!
any-object?: funco [
"Return TRUE if value is any type of object."
value [any-type!]
][
found? find any-object! type? get/any 'value
]
; Fake scalar! typeset
scalar!: reduce
[integer! decimal! money! char! pair! tuple! time!]
scalar?: funco [
"Return TRUE if value is any type of scalar."
value [any-type!]
][
found? find scalar! type? get/any 'value
]
; Fake immediate! and internal! typesets, for documentation
immediate!: reduce [
none! logic! integer! decimal! money! char! pair! tuple! time! date!
datatype! word! set-word! get-word! lit-word! refinement! event!
] ; percent! typeset!
internal!: reduce [end! unset! symbol!] ; frame! handle!
; The module! and vector! types are not done yet in R3, no others practical...
; Control functions
!: :not
++: funco [
"Increment an integer or series index. Return its prior value."
[catch]
'word [word! paren!] "Integer or series variable."
][
if all [paren? word not word? set/any 'word do word] [
throw-error 'script 'expect-arg reduce ['++ 'word type? get/any 'word]
] ; Workaround for R3 change in lit-word! parameters with paren! arguments
case [
number? get/any word [also get word set word add get word 1]
series? get/any word [also get word set word next get word]
'else [throw-error 'script 'expect-arg reduce ['++ 'word type? get/any 'word]]
]
]
; Note: Native in R3.
--: funco [
"Decrement an integer or series index. Return its prior value."
[catch]
'word [word! paren!] "Integer or series variable."
][
if all [paren? word not word? set/any 'word do word] [
throw-error 'script 'expect-arg reduce ['-- 'word type? get/any 'word]
] ; Workaround for R3 change in lit-word! parameters with paren! arguments
case [
number? get/any word [also get word set word subtract get word 1]
series? get/any word [also get word set word back get word]
'else [throw-error 'script 'expect-arg reduce ['-- 'word type? get/any 'word]]
]
]
; Note: Native in R3.
also: funco [
"Returns the first value, but also evaluates the second."
value1 [any-type!]
value2 [any-type!]
][
return get/any 'value1
]
; Note: Native in R3.
quote: funco [
"Returns the value passed to it without evaluation."
:value [any-type!]
][ ; Broken for word! arguments - use lit-word! instead.
return get/any 'value
]
; Based on a proposal by Peta.
true?: funco [
"Returns true if an expression can be used as true."
val
][not not :val]
; Code written by Carl, spec by BrianH.
default: funco [ ; Needs consensus
"Set a word to a default value if it hasn't been set yet."
'word [word! set-word! lit-word!] "The word (use :var for word! values)"
value "The value" ; unset! not allowed on purpose
][
unless all [value? word not none? get word] [set word :value] :value
]
; Suggested by a discussion in R3 chat in Jan-2009.
apply: funco [
"Apply a function to a reduced block of arguments."
[throw]
func [any-function!] "Function value to apply"
block [block!] "Block of args, reduced first (unless /only)"
/only "Use arg values as-is, do not reduce the block"
/local words path todo noref value vars var
v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20
][
unless only [block: reduce block]
words: words-of :func
vars: [ ; Used to special-case 'a and :a parameters (the quick way)
v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20
]
path: to-path [func] ; Even a one-element path works.
todo: head insert/only make block! 1 + length? block path ; Should be OK
noref: false ; True if the refinement isn't used
while [not tail? words] [
set/any 'value pick block 1 ; Safe for unset! and error!
switch type?/word first words [
word! [ ; Regular param, all values need evaluation blocked.
unless noref [
either word? get/any 'value [ ; Work around QUOTE bug
insert tail todo to-lit-word get/any 'value
] [
insert/only insert tail todo 'quote get/any 'value
]
]
]
lit-word! [ ; Lit-word param, need to special-case get-words.
unless noref [
either get-word? get/any 'value [
; Needs to be a get-word! assigned to a get-word! to get in
either var: pick vars 1 [vars: next vars] [
var: use [a] copy ['a] ; else do it the slow way
]
set var value
insert tail todo to-get-word var
] [
insert/only tail todo get/any 'value
]
]
]
get-word! [ ; Get-word param, need to special-case words.
unless noref [
either word? get/any 'value [
; Needs to be a word! assigned to a word! to get in
either var: pick vars 1 [vars: next vars] [
var: use [a] copy ['a] ; else do it the slow way
]
set var value
insert tail todo var
] [
insert/only tail todo get/any 'value
]
]
]
refinement! [ ; Refinement, skip its associated params if not TRUE?
unless noref: not get/any 'value [
insert tail path to-word first words
]
]
]
words: next words
block: next block
]
also do todo ( ; DO the built code, then cleanup memory references
set [func words block path todo value vars var] set head vars none
)
]
; Note: APPLY evaluates the arguments block (or not with /only), regardless of
; how parameters are specified for the function itself. This means that the
; normal evalation needs to be shortcircuited, so to speak - the REBOL code
; above does this. However, the R3 version is a native that is about as quick
; as a regular function call, while the R2 version is a bit slower.
; Even so, there are good reasons to call a function with APPLY:
; - APPLY does positional evaluation of refinements, so it is easier to write
; wrapper functions that pass along refinements.
; - If you are calling function values that are passed as arguments to your
; function, any lit-word! or get-word! parameters can be used to inject
; code into your function, which can be a security hole. APPLY can make
; functional-style REBOL code safer to write.
; Gotcha: Returning an error from the applied function triggers the error. It
; is not yet known how to fix this in R2 without modifying APPLY at runtime.
eval: funco [
"Evaluates a block, file, URL, function, word, or any other value."
value "Normally a file name, URL, or block"
][
either block? :value [do value] [:value]
]
; Note: Don't know what this native! is for in R3, but there's a doc page...
; Object functions
object: funco [
"Defines a unique object."
blk [block!] "Object words and values."
][ ; Build an object! safely even if there is trailing set-words
make object! head insert tail blk none
]
; Note: CONSTRUCT already does this, but doesn't evaluate expressions.
; Carl wrote the R3 version, mostly copied here.
extend: funco [
"Extend a block type with word and value pair." ; Not objects in R2 :(
obj [block! paren! hash! list!] ; No point in including paths
word [any-word!]
val ; No unset!
][
if :val [insert tail obj reduce [to-set-word word :val]]
:val
]
; Note: This also works on object! and map! in R3.
; Carl wrote the R3 code, spec by BrianH, mostly copied here.
resolve: funco [
"Copy context by setting values in the target from those in the source."
[catch]
target [object! port!]
source [object! port!]
/only from [block! integer!] "Only specific words (exports) or new words in target (index to tail)"
/all "Set all words, even those in the target that already have a value"
][
either only [
from: either integer? from [
; Only set words in the target positioned at the number from or later
unless positive? from [throw-error 'script 'out-of-range from]
intersect words-of source at words-of target from
] [
; Only set the words in the target that are also in the from block
intersect words-of source intersect words-of target from
]
foreach word from pick [
[unless value? in target word [error? set/any in target word get/any word]]
[error? set/any in target word get/any word]
] not all ; See below for what this means
] [
either all [ ; Override all target words even if they have values
error? set/any bind words-of source target get/any source
] [ ; Only set target words if they aren't yet set
foreach word intersect words-of source words-of target [
unless value? in target word [error? set/any in target word get/any word]
]
]
]
also target set [source target from] none
]
; Note: This is native in R3 and supports module! too.
; Implementation note: INTERSECT returns the values from its first argument,
; and WORDS-OF returns a block of words that are bound to its argument, so
; intersect words-of source words-of target
; returns a block of words bound to source.
; Series functions
ajoin: funco [
"Reduces and joins a block of values into a new string."
[throw]
block [block!]
][
make string! reduce block
]
; Note: Native in R3 without the reduce overhead.
first+: funco [
{Return FIRST of series, and increment the series index.}
[catch]
'word [word! paren!] "Word must be a series." ; paren! added for R2
][
; Workaround for R3 change in lit-word! parameters with paren! arguments
if paren? :word [set/any 'word do :word]
throw-on-error [also pick get word 1 set word next get word]
]
; Note: Native in R3.
last?: funco [
"Returns TRUE if the series length is 1."
series [series! port! tuple! bitset! struct!] ; map! object! gob! any-word!
][1 = length? :series]
; Note: Type spec same as LENGTH?, which also supports the extra types in R3
past?: funco [
"Returns TRUE if a series index is past its tail."
series [series!] ; gob! port!
][
not same? :series skip :series 0
]
; Note: Native in R3. Not tested on ports, and problem may not exist for them.
; INDEX? doesn't stay consistent with past-tail references in R2.
; Spec from R3, code from Ladislav, used with permission.
remold: funco [
"Reduces and converts a value to a REBOL-readable string."
value [any-type!] "The value to reduce and mold"
/only "For a block value, mold only its contents, no outer []"
/all "Mold in serialized format"
/flat "No indentation"
][ ; Nasty, but the best you can do without native APPLY
do pick pick pick [[[
[mold reduce :value]
[mold/flat reduce :value]
] [
[mold/all reduce :value]
[mold/all/flat reduce :value]
]] [[
[mold/only reduce :value]
[mold/only/flat reduce :value]
] [
[mold/only/all reduce :value]
[mold/only/all/flat reduce :value]
]]] not only not all not flat
]
; Note: Uses APPLY in R3.
append: funco [
"Inserts a value at tail of series and returns the series at head. (Modifies)"
series [series! port! bitset!] "Series at point to insert"
value [any-type!] "The value to insert"
/part "Limits to a given length or position"
length [number! series! port! pair!]
/only "Inserts a series as a series"
/dup "Duplicates the insert a specified number of times"
count [number! pair!]
][ ; Nasty, but the best you can do without native APPLY
head do pick pick pick [[[
[insert tail series get/any 'value]
[insert/part tail series get/any 'value length]
] [
[insert/only tail series get/any 'value]
[insert/part/only tail series get/any 'value length]
]] [[
[insert/dup tail series get/any 'value count]
[insert/part/dup tail series get/any 'value length count]
] [
[insert/dup/only tail series get/any 'value count]
[insert/part/dup/only tail series get/any 'value length count]
]]] not dup not only not part
]
; Note: Native in R3.
swap: funco [
"Swaps elements of a series. (Modifies)"
series1 [series!]
series2 [series!]
][
unless any [empty? series1 empty? series2] [
poke series1 1 also pick series2 1 poke series2 1 pick series1 1
]
series1
]
; Note: Native (action!) in R3.
alter: func [
"If a value is not found in a series, append it; otherwise, remove it. Returns true if added. (Modifies)"
series [series! port!]
value
/case "Case-sensitive comparison"
][
found? unless remove (
either case [find/case series :value] [find series :value]
) [append series :value]
]
; Note: Useful change to previously useless return value.
take: funco [
"Copies and removes from series. (Modifies)"
[catch]
value [series! port! none!]
/part "Limits to a given length or position"
length [number! series! port! pair!]
/last "Take it from the tail end"
][
if value [
either part [
case [
pair? length [
unless image? value [
throw-error 'script 'invalid-part length
]
last: none
]
any [series? length port? length] [
either same? head value head length [
length: subtract index? length index? value
][
throw-error 'script 'invalid-part reduce [length]
]
]
]
if last [
length: negate length
value: tail value
]
also copy/part value length remove/part value length
][
also pick either last [
value: back tail value
] [value] 1 remove value
]
]
]
; Note: Native (action!) in R3.
move: funco [
"Move a value or span of values in a series."
[catch]
source [series!] "Source series"
offset [integer!] "Offset to move by, or index to move to"
/part "Move part of a series"
length [integer!] "The length of the part to move"
/skip "Treat the series as records of fixed size" ;; SKIP redefined
size [integer!] "Size of each record"
/to "Move to an index relative to the head of the series" ;; TO redefined
][
unless length [length: 1]
if skip [
if 1 > size [throw-error 'script 'out-of-range size]
offset: either to [offset - 1 * size + 1] [offset * size]
length: length * size
]
part: copy/part source length
remove/part source length
insert either to [at head source offset] [
system/words/skip source offset
] part
]
; Note: This is the best you can do without overlap and aliasing issues.
; The R3 version is included to prevent it from being reinvented, badly.
; Suggested by a discussion in AltME in Nov-2007.
array: func [