-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredbol.reb
1245 lines (1086 loc) · 37.6 KB
/
redbol.reb
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 [
System: "Rebol 3 (Ren-C Branch)"
Title: "Rebol2 and Red Compatibility Shim"
Homepage: https://trello.com/b/l385BE7a/porting-guide
Rights: {
Copyright 2012-2024 Ren-C Open Source Contributors
REBOL is a trademark of REBOL Technologies
}
Type: module
Name: Redbol
License: {
Licensed under the Lesser GPL, Version 3.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/lgpl-3.0.html
}
Description: {
This module attempts to adapt Ren-C so that basic functionality will
respond similarly to the compatible subset of Rebol2 and Red.
The current lack of a GUI in Ren-C means that this will only be
useful for command-line scripts and utilities. However, it serves as
a test of the system's flexibility, as well as a kind of "living
documentation" of the nuances of what has been changed.
}
Notes: {
* Ren-C does not allow the mutation of PATH!. You can JOIN a path to
make a new one, and FOREACH a path to enumerate one, but you can't
APPEND or INSERT into them. Calling code that expects to do these
kinds of mutations needs to be changed to do them on BLOCK! and
convert to PATH! when done.
}
]
export ren: lib ; save the Ren-C library
export lib: does [print "Use REN or REDBOL in Redbol implementation, not LIB"]
redbol: ~ ; emerge variable so $redbol finds it in module
redbol: binding of $redbol ; not exported--local for disambiguating this file
=== WRAPPER FOR "EMULATION-DEFINITIONS-ARE-IN-PROGRESS" ===
; This module is redefining the workings of the system fundamentally. While
; doing those definitions it's preferable to not have to say `ren.switch`
; or otherwise prefix each call in the implementation so it doesn't use the
; new definitions that have been made so far.
;
; Until a module feature to facilitate this kind of thing becomes standard,
; this binds the bodies of EMULATE or HELPER things into lib for you.
helpers: module [
Exports: []
] compose [
redbol: (redbol)
import <function2.r>
import <parse2.r>
import <io2.r>
]
helper: enfix ren.lambda [
{NON-EXPORTED definition relying on words in REN (e.g. baseline APPEND)}
:set-word [set-word!]
code [block!] ; will remove REDBOL module binding and rebind to REN.
][
sys.util.export* helpers (set-word): ren.do ren.overbind helpers ren.inside ren ren.bindable code
]
emulate: enfix ren.lambda [
{EXPORTED definition that relying on words in REN (e.g. baseline APPEND)}
:set-word [set-word!]
code [block!] ; will remove REDBOL module binding and rebind to REN.
<local> temp
][
export (set-word): ren.do ren.overbind helpers ren.inside ren ren.bindable code
]
=== DATATYPES ===
string!: emulate [text!]
string?: emulate [:text?]
to-string: emulate [specialize :to [type: text!]]
; There is no CHAR! "datatype" in Ren-C (ISSUE! and char are unified)
; The belief is that TO a char-like thing of 1 should be #"1"
; Currently AS is serving as the numeric converter.
;
char!: emulate [&char?] ; type contraint on issue
to-char: emulate [
lambda [value] [
either integer? value [
as issue! value
][
to issue! value
]
]
]
lit-word!: emulate [&lit-word?] ; quoted word, not its own fundamental type
lit-path: emulate [&lit-path?] ; quoted path, not its own fundamental type
refinement!: emulate [&refinement?] ; blank-headed PATH! with word
paren!: emulate [group!]
paren?: emulate [:group?]
to-paren: emulate [specialize :to [type: group!]]
number!: emulate [&any-number?]
number?: emulate [:any-number?]
scalar!: emulate [&any-scalar?]
scalar?: emulate [:any-scalar?]
series!: emulate [&any-series?]
series?: emulate [:any-series?]
any-type!: emulate [&any-value?]
any-block!: emulate [&any-array?]
any-block?: emulate [:any-array?]
any-object!: emulate [&any-context?]
any-object?: emulate [:any-context?]
; Redbol wants something reified that does not reduce that is falsey.
;
; Ren-C has no such concept in reified space. We use the-word! for now because
; it's a datatype that didn't exist in historical Rebol, so it should not
; conflate with issues/etc...and it is non-reducing. Functions like IF and
; CASE have to be hooked with new predicates for the revised definition of
; truthiness and falseyness that includes @none
;
none: emulate [@none]
none!: emulate [the-word!]
none?: emulate [x -> [:x = @none]]
type?: emulate [
lambda [
value [~null~ any-value?]
/word {Note: SWITCH evaluates https://trello.com/c/fjJb3eR2}
][
case [
not word [either @none = :value [none!] [type of :value]]
unset? 'value ['unset!] ; https://trello.com/c/rmsTJueg
:value = @none ['none!] ; https://trello.com/c/vJTaG3w5
group? :value ['paren!] ; https://trello.com/c/ANlT44nH
(match [lit-word?] :value) ['lit-word!]
(match [lit-path?] :value) ['lit-path!]
] else [
to-word type of :value
]
]
]
true?: helper [x -> [not any [not :x, :x = @none]]]
false?: helper [x -> [did any [not :x, :x = @none]]]
true?: emulate [:true?]
false?: emulate [:false?]
export not: :false?
=== FUNCTIONS ===
any-function!: emulate [action?!]
any-function?: emulate [:action?]
function!: emulate [action?!]
function?: emulate [:action?]
native!: emulate [action?!]
native?: emulate [:action?]
closure!: emulate [:action?!]
closure?: emulate [:action?]
; If a Ren-C function suspects it is running code that may happen more than
; once (e.g. a loop or function body) it marks that parameter `<const>`.
; That prevents casual mutations.
;
; !!! This depended on the RESKINNED function, which was removed to make way
; for more coherent granular parameter tweaking in the spec and function
; creation with AS FRAME!. In order to begin implementing that approach
; correctly, the bad implementation of RESKINNED had to be pulled out. For
; the moment, the const parameter is not tweaked in Redbol...but the feature
; is aiming to come back shortly in much better form.
func: emulate [:func2]
function: emulate [:function2]
apply: emulate [:apply2]
has: emulate [:has2]
does: emulate [:does2]
; Some of CLOSURE's functionality was subsumed into all FUNCTIONs, but
; the indefinite lifetime of all locals and arguments was not.
; https://forum.rebol.info/t/234
;
closure: emulate [:function2]
clos: emulate [:func2]
for-each-nonconst: emulate [
; reskinned [
; body [block!] ; no <const> annotation
; ] adapt :for-each [] ; see RESKINNED for why this is an ADAPT for now
:for-each
]
?: emulate [:help]
to-local-file: emulate [
if undefined? $file-to-local [
does [fail "TO-LOCAL-FILE not available in web build"]
] else [
get $file-to-local
]
]
to-rebol-file: emulate [
if undefined? $local-to-file [
does [fail "LOCAL-TO-FILE not available in web build"]
] else [
get $local-to-file
]
]
why?: emulate [does [ren.why]] ; not exported yet, :why not bound
null: emulate [
make issue! 0 ; NUL in Ren-C https://en.wikipedia.org/wiki/Null_character
]
comment: emulate [
func [
return: [~] {Not invisible: https://trello.com/c/dWQnsspG}
:discarded [block! any-string? binary! any-scalar?]
][
]
]
found?: emulate [
func [
{See DID and NOT: https://trello.com/c/Cz0qs5d7}
return: [logic?]
value
][
return not blank? :value
]
]
=== SETTING AND GETTING ===
unset!: emulate [antiform!]
unset?: emulate [:unset?] ; checks *value* is unset, not var
unset: emulate [:unset]
; Note: R3-Alpha had a /PAD option, which was the inverse of /SOME.
; If someone needs it, they can adapt this routine as needed.
;
set: emulate [
func [
return: [any-value?]
target [any-word? any-path? block! object!]
value [~null~ any-value?]
/any "Allow UNSET as a value rather than causing an error"
/only "Block or object value argument is set as a single value"
/some "None values in a block or object value argument, are not set"
][
let set_ANY: any
any: :lib.any
all [ ; !!! is it necessary to impose this historical restriction?
not set_ANY
unset? 'value
fail "Can't SET a value to UNSET! unless SET/ANY is used"
]
if not block? target [ ; handle simple WORD!/PATH! case
return set target :value
]
if object? target [ ; turn OBJECT! case into BLOCK! case
target: words of target
]
if only or (not block? :value) [ ; don't set itemwise, all get same
for-each t target [set t :value]
return :value
]
let block: value ; save so we can return at same position
for-each t target [
if blank? try :block.1 [ ; may be at end of block, block.1 = null
if not some [set t blank]
] else [
set t :block.1
]
block: try next block
]
return value
]
]
get: emulate [
func [
{Now no OBJECT! support, unset vars always null}
return: [~null~ any-value?]
source {Legacy handles Rebol2 types, not *any* type like R3-Alpha}
[blank! any-word? any-path? any-context? block!]
/any
][
let any_GET: any
any: :lib.any
if block? :source [
return source ; this is what it did :-/
]
if any-context? source [
return apply :get [words of source /any any_GET]
]
return apply :get [source /any any_GET]
]
]
value?: emulate [
func [
{See SET? in Ren-C: https://trello.com/c/BlktEl2M}
return: [logic?]
value
][
return either any-word? :value [set? value] [true] ; bizarre. :-/
]
]
; R3-Alpha and Rebol2's DO was effectively variadic. If you gave it an
; action, it could "reach out" to grab arguments from after the call. Ren-C
; replaced this functionality with EVAL:
;
; https://forum.rebol.info/t/meet-the-eval-native/311
;
; !!! This code contains an early and awkward attempt at emulating the old
; DO behavior for functions in userspace, through an early version of
; variadics. Ren-C is aiming to have functions that make "writing your own
; EVAL-like-thing" easier.
;
do: emulate [
func [
return: [~null~ any-value?]
source [~null~ blank! block! group! text! binary! url! file! tag!
error! action?
]
normals [any-value? <variadic>]
'softs [any-value? <variadic>]
:hards [any-value? <variadic>]
/args [any-value?]
/next [word!]
][
let var: next
next: :lib.next
if var [ ; DO/NEXT
if args [fail "Can't use DO/NEXT with ARGS"]
let [result 'source]: evaluate :source
set var source ; DO/NEXT put the *position* in the var
return :result ; DO/NEXT returned the *evaluative result*
]
if action? :source [
code: reduce [:source]
params: parameters of :source
iterate params [
append code switch/type params.1 [
word! [take normals]
lit-word?! [take softs]
get-word! [take hards]
set-word! [[]] ; empty block appends nothing
refinement! [break]
fail ["bad param type" params.1]
]
]
return do code
]
return do/args :source :args ; if args is null, refinement is "revoked"
]
]
to: emulate [
enclose :to func [f] [
all [
:f.value = group!
find any-word? f.type
return as type! 'paren!
]
all [
f.type = char!
integer? :f.value
return make issue! :f.value
]
all [
find any-array? f.type
binary? :f.value
return as f.type transcode f.value
]
return do f
]
]
try: emulate [
lambda [
{See TRAP for Ren-C equivalent: https://trello.com/c/IbnfBaLI}
block [block!]
/except "Note TRAP doesn't take a handler...use THEN instead"
[<unrun> block! frame!]
<local>
error result
][
if ([error result]: trap [do block else '_]) [
case [
not except [error]
block? except [do except else '_]
frame? except [apply except [error] else '_]
]
] else [
result else '_ ; Note: may be an ERROR! that was evaluated to
]
]
]
default: emulate [
lambda [
{See the new enfix DEFAULT: https://trello.com/c/cTCwc5vX}
'word [word! set-word! lit-word?]
value
][
any [
unset? word
blank? get word
] then [
set word :value
] else [
:value
]
]
]
also: emulate [
lambda [
{Supplanted by ELIDE: https://trello.com/c/pGhk9EbV}
returned [any-value?]
discarded [any-value?]
][
:returned
]
]
=== PARSE ===
; PARSE in Ren-C is vastly redesigned, but the goal is that it act as a
; framework (codename "UPARSE") that can be easily twistable for compatibility:
;
; https://forum.rebol.info/t/introducing-uparse-the-hackable-usermode-parse/1529
;
; UPARSE is in early development at time of writing and is very slow. But the
; goal is to speed it up over time. But Redbol uses it today anyway.
parse: emulate [
func [
{Non-block rules replaced by SPLIT: https://trello.com/c/EiA56IMR}
return: [logic? block!]
input [any-series?]
rules [block! text! blank!]
/case
/all "Ignored refinement in <r3-legacy>"
][
let case_PARSE: case
case: :lib.case
comment [all_PARSE: all] ; Not used
all: :lib.all
return switch type of rules [
blank! [split input charset reduce [tab space CR LF]]
text! [split input to-bitset rules]
] else [
ok? apply :uparse2 [
input rules
/case case_PARSE
]
]
]
]
=== EVALUATING ===
reduce: emulate [
lambda [
value "Not just BLOCK!s evaluated: https://trello.com/c/evTPswH3"
/into "https://forum.rebol.info/t/stopping-the-into-virus/705"
[any-array?]
][
case [
not block? :value [:value]
into [insert into reduce :value]
] else [
reduce :value
]
]
]
compose: emulate [
lambda [
value "Ren-C does not splice by default (needs SPREAD)" [any-value?]
/deep "Ren-C recurses into PATH!s: https://trello.com/c/8WMgdtMp"
/only
/into "https://forum.rebol.info/t/stopping-the-into-virus/705"
[any-array? any-string? binary!]
][
if not block? value [return value] ; `compose 1` is `1` in Rebol2
let composed: apply :compose [
;
; !!! Note: COMPOSE has a LABEL argument that is <skip>-able.
; Skippable arguments are entwined with quoting and detection, and
; as such have more in common with refinements than ordinary
; arguments. If you want a skippable argument in an APPLY you
; must specify it explicitly by name...APPLY always <skip>s.
value
/deep deep
; The predicate is a function that runs on whatever is generated
; in the COMPOSE'd slot. If you put it in a block, that will
; splice but protect its contents from splicing (the default).
; We add the twist that `~` antiforms subvert errors in Rebol2.
;
; rebol2> type? either true [] []
; == unset!
;
; rebol2> compose [(either true [] [])]
; == []
;
/predicate if not only [
lambda [group <local> product] [
product: eval group else [@none]
(non &any-array? :product) else array -> [spread array]
]
]
]
either into [insert into composed] [composed]
]
]
collect: emulate [
lambda [
body [block!]
/into "https://forum.rebol.info/t/stopping-the-into-virus/705"
[any-series?]
][
let out: any [into, make block! 16]
let keeper: specialize* (
enclose* :insert func* [
f [frame!]
<with> out
][
f.series: out ; want new series position capture each time
:f.value ; evalutate input before the DO to be return result
elide out: do f ; update position on each insertion
; original f.value will be returned due to ELIDE
]
)[
series: <remove-unused-series-parameter>
]
reeval func* compose [(name) [action?] <with> return] body :keeper
either into [out] [head of out]
]
]
repend: emulate [
lambda [
series [any-series? port! map! object! bitset!]
value
/part [any-number? any-series? pair!]
/only
/dup [any-number? pair!]
][
apply :redbol.append [ ; Want overridden APPEND semantics (vs Ren-C)
series
either block? value [reduce value] [value]
/part part
/only only
/dup dup
]
]
]
; REJOIN in R3-Alpha meant "reduce and join" and was arity-1. It was used
; in many places, such as producing strings out of blocks of string parts and
; expressions. But it also had some really wonky properties:
;
; https://forum.rebol.info/t/rejoin-ugliness-and-the-usefulness-of-tests/248/
;
; Ren-C eliminates it, and pushes on the definition of JOIN as arity-2...where
; if there was a REJOIN operation, it would be JOIN/REDUCE. But there is no
; arity-1 REJOIN parallel in Ren-C at time of writing...you can ask JOIN to
; have its first argument as a datatype! and it will produce that type, which
; substitutes for the intent.
;
rejoin: emulate [
func [
{Reduces and joins a block of values}
return: "Same type as first non-null item produced by evaluation"
[issue! any-series? any-sequence?]
block "Values to reduce and join together"
[block!]
<local> base
][
cycle [ ; Keep evaluating until a usable BASE is found
if not (base: evaluate/next block $block, block) [
return copy [] ; exhausted block without finding a base value
]
any [
null? :base ; consider to have dissolved
blank? :base ; treat same as NULL
] then [
continue ; do another evaluation step
]
; !!! Historical Rebol would default to a TEXT! if the first thing
; found wasn't JOIN-able. This is questionable.
;
if not match [issue! any-sequence? any-series?] :base [
base: to text! :base
]
return join base spread reduce block ; JOIN what's left of block
]
]
]
join: emulate [
lambda [value rest] [
print "WE ARE THE CHAMPIONS!"
apply :append [
if series? value [copy value] else [form value]
if block? rest [spread reduce rest] else [rest]
]
]
]
ajoin: emulate [:unspaced]
reform: emulate [:spaced]
form: emulate [
lambda [
value [any-value?]
/unspaced "Outer level, append {} [1 2 [3 4]] => {123 4}"
][
case [
issue? :value [
as text! value ; e.g. Rebol2 said `form #<<` was `<<`
]
word? :value [
as text! value
]
decimal? :value [
;
; Regarding IEEE `double` values, Wikipedia says:
;
; "The 53-bit significand precision gives from 15 to 17
; significant decimal digits precision"
;
; Rebol2 printed 15 digits after the decimal point. R3-Alpha gave
; 16 digits...as does Red and seemingly JavaScript.
;
; rebol2>> 1 / 3
; == 0.333333333333333
;
; r3-alpha>> 1 / 3
; == 0.3333333333333333
;
; red>> 1 / 3
; == 0.3333333333333333
;
; JavaScript> 1 / 3
; -> 0.3333333333333333 ; Chrome
; -> 0.3333333333333333 ; Firefox
;
; While this may seem a minor issue, generated output in diff
; gets thrown off, making it hard to see what has changed.
; It can't be addressed via rounding, because rounding
; floating point numbers can't guarantee a digit count when
; printing--since some numbers aren't evenly representible.
;
; This truncates the number to the right length but doesn't
; round it. That would be more complicated, and is probably
; best done via C code once Redbol is an extension.
;
value: form value
if not find value "E" [
use [pos] [
all [
pos: try skip (find value ".") 15
clear pos
]
]
]
value
]
block? value [
delimit: either unspaced [:lib.unspaced] [:lib.spaced]
delimit map-each item value [
redbol.form :item
]
]
] else [
form value
]
]
]
quit: emulate [
lambda [
/return "Ren-C is variadic, 0 or 1 arg: https://trello.com/c/3hCNux3z"
[any-value?]
][
apply :quit [/with :return]
]
]
; OBJECT is a noun-ish word; Ren-C tried HAS for a while and did not like it.
; A more generalized version of CONSTRUCT is being considered:
;
; https://forum.rebol.info/t/has-hasnt-worked-rethink-construct/1058
;
object: emulate [
specialize :make [type: object!]
]
construct: emulate [
lambda [
spec [block!]
/with [object!]
/only
][
if only [
fail [
{/ONLY not yet supported in emulation layer for CONSTRUCT}
{see %redbol.reb if you're interested in adding support}
]
]
to any [with object!] spec
]
]
break: emulate [
lambda [
/return "/RETURN is deprecated: https://trello.com/c/cOgdiOAD"
[any-value?]
][
if return [
fail [
"BREAK/RETURN not implemented in Redbol emulation, use THROW"
"and CATCH. See https://trello.com/c/uPiz2jLL/"
]
]
break
]
]
++: emulate [
func [] [
fail @return [
{++ and -- are not in the Redbol layer by default, as they were}
{not terribly popular to begin with...but also because `--` is}
{a very useful and easy-to-type dumping construct in Ren-C, that}
{comes in very handy when debugging Redbol. Implementations of}
{++ and -- are available in %redbol.reb if you need them.}
{See also ME and MY: https://trello.com/c/8Bmwvwya}
]
]
]
comment [ ; ^-- see remark above
++: emulate [
lambda [
{Deprecated, use ME and MY: https://trello.com/c/8Bmwvwya}
'word [word!]
][
value: get word ; returned value
elide (set word case [
any-series? :value [next value]
integer? :value [value + 1]
] else [
fail "++ only works on SERIES! or INTEGER!"
])
]
]
--: emulate [
lambda [
{Deprecated, use ME and MY: https://trello.com/c/8Bmwvwya}
'word [word!]
][
value: get word ; returned value
elide (set word case [
any-series? :value [next value]
integer? :value [value + 1]
] else [
fail "-- only works on SERIES! or INTEGER!"
])
]
]
]
compress: emulate [
func [
{Deprecated, use DEFLATE or GZIP: https://trello.com/c/Bl6Znz0T}
return: [binary!]
data [binary! text!]
/part [any-value?]
/gzip
/only
][
any [gzip, only] else [ ; assume caller wants "Rebol compression"
data: to-binary copy/part data part
let deflated: zdeflate data
length-32bit: modulo (length of data) (to-integer power 2 32)
repeat 4 [
append deflated modulo (to-integer length-32bit) 256
length-32bit: me / 256
]
return deflated ; ^-- plus size mod 2^32 in big endian
]
return deflate/part/envelope data :lim [
gzip [assert [not only] 'gzip]
not only ['zlib]
]
]
]
decompress: emulate [
func [
{Deprecated, use DEFLATE or GUNZIP: https://trello.com/c/Bl6Znz0T}
return: [binary!]
data [binary!] "Red assumes GZIP, Rebol assumed 'Rebol compressed'"
/part [binary!] "R3-Alpha refinement, must match end of compression"
/gzip "R3-Alpha refinement (no size argument, envelope stores)"
/limit [integer!] "R3-Alpha refinement, error if larger"
/zlib [integer!] "Red refinement (RFC 1951), uncompressed size"
/deflate [integer!] "Red refinement (RFC 1950), uncompressed size"
][
any [gzip, zlib, deflate] else [
;
; Assume data is "Rebol compressed". Could get more compatibility
; by testing for gzip header or otherwise having a fallback, as
; Red went with a Gzip default.
;
part: default [tail of data]
return zinflate/part/max data (skip part -4) limit
]
return inflate/part/max/envelope data part limit case [
gzip [assert [not zlib not deflate] 'gzip]
zlib [assert [not deflate] 'zlib]
deflate [_]
fail
]
]
]
and: emulate [enfix :intersect]
or: emulate [enfix :union]
xor: emulate [enfix :difference]
mod: emulate [:modulo] ; MOD is enfix in Ren-C, MODULO still prefix
; Ren-C NULL means no branch ran, Rebol2 this is communicated by #[none]. We
; use the inert WORD! form @none for this.
;
denuller: helper [
action -> [
chain [
:action
lambda [^x [~null~ ~void~ pack? any-value?]] [
(unmeta x) else [@none]
]
]
]
]
=== CONDITIONALS ===
; Concept of "truthy" and "falsey" are different in Ren-C, where NULL is falsey
; and all ANY-ELEMENT? are true. We want to make exceptions for reified ideas
; of LOGIC! and NONE! for Redbol. Currently the only way to do that is to
; leverage the predicates of the conditionals.
if: emulate [denuller adapt :if [condition: true? :condition]]
either: emulate [denuller adapt :either [condition: true? :condition]]
unless: emulate [denuller adapt :if [condition: false? :condition]]
case: emulate [denuller specialize :case [predicate: :true?]]
any: emulate [denuller specialize :all [predicate: :true?]]
all: emulate [denuller adapt :any [predicate: :true?]]
switch: emulate [ ; Ren-C evaluates cases: https://trello.com/c/9ChhSWC4/
enclose (augment :switch [
/default "Default case if no others are found"
[block!]
]) lambda [f [frame!]] [
f.cases: map-each c f.cases [
match block! c else [quote c] ; suppress eval on non-blocks
]
let def: f.default ; the DO expires frame right now (for safety)
(do f else (def)) else [@none]
]
]
for: emulate [denuller :cfor]
while: emulate [denuller :while]
foreach: emulate [
func [
{No SET-WORD! capture, see https://trello.com/c/AXkiWE5Z}
return: [~null~ any-value?]
'vars [word! block!]
data [any-series? any-context? map! blank!]
body [block!]
][
any [
not block? vars
for-each-nonconst item vars [if set-word? item [break] true]
] then [
return (for-each-nonconst :vars data body else [@none])
]
; Weird FOREACH, transform to WHILE: https://trello.com/c/AXkiWE5Z
;
use :vars [
let position: data
return while [not tail? position] compose [
(spread collect [
for-each item vars [
case [
set-word? item [
keep compose [(item) position]
]
word? item [
keep compose [
(to-set-word :item) position.1
position: next position
]
]
fail "non SET-WORD?/WORD? in FOREACH vars"
]
]
])
(as group! body)
]
]
]
]
loop: emulate [denuller :repeat]
; REPEAT in Rebol2 with an SERIES! argument acted like a FOR-EACH on that
; series. This is redundant with FOR-EACH.
;
; R3-Alpha changed the semantics to be like a FOR-NEXT (e.g. FORALL) where you
; could specify the loop variable instead of insisting your loop variable be
; the data you are iterating.
;
; Red forbids SERIES! as the argument of what to iterate over.
;
; https://trello.com/c/CjEfA0ef
;
; The common denominator here is to act like COUNT-UP.
;
repeat: emulate [denuller :count-up]