-
Notifications
You must be signed in to change notification settings - Fork 5
/
rebmu.reb
937 lines (771 loc) · 28.3 KB
/
rebmu.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
Rebol [
Title: "Rebmu Dialect"
Purpose: {
Rebol dialect designed for participating in "Code Golf"
challenges
}
Author: {"Dr. Rebmu"}
Home: http://rebmu.hostilefork.com
License: 'mit
Date: 15-Sep-2015
Version: 0.7.0
; Header conventions: http://www.rebol.org/one-click-submission-help.r
File: %rebmu.reb
Type: module
Name: Rebmu
Level: 'genius
Notes: {
### SINGLE CHARACTER DEFINITIONS
Originally Rebmu tried to define single characters as having values
so you could have "a value of that type around" (x, y, z as 0.0 to
have a float around, s as {} to have an empty string, etc.)
Thought to be helpful for golfing, it turned out to not be THAT
helpful. The trivial puzzles in which that count wasn't lost in the
noise were usually solvable in fewer characters by another language
that was a precise match for the domain of the question. It was
difficult to remember and taught nothing that would be relevant to
Rebol or Red.
So the single character definitions were scaled back drastically.
They are tracked here as an index and to-do list, while the actual
definitions are in the functional group in the code.
The . character was once available but was taken for TUPLE!.
The & character was also once available, but reserved for future use.
~ ; is hardwired as "quasi null", it evaluates to VOID
?
|
a ; usually a program argument or a-function variable
b
c => copy
d
e => else
f => for ; now more generic
g
h
i => if
j
k
l => let
m => match
n
o
p => print
q => quote
r => return ; only applies in functions, if not in function will QUIT
s
t => to ; variants like TSW for TO-SET-WORD trumps T for THEN
u => until
v
w => while
x
y
z
### EXISTING TWO-CHARACTER SPACE
Because there are only so many single characters (unless you start
using Unicode...) the majority of Rebmu function definitions live
in the two-character space. However, refinements follow a system...
so even if it would be *possible* to do APPEND/DUP => AD, such
compression tricks are seen as less consistent than if you have
APPEND => AP and APPEND/DUP => APD. So the two-character space
is the baseline for growing further in a systemic way.
Yet Rebol itself does define a few things already in the two character
space that should not be overridden, to reach Rebmu's goal of being
able to compatibly run any all-lowercase Rebol code in midstream.
Here's a short study of the space used.
Very Reasonable Use of English Words
TO to conversion
AS aliasing operator
OR or operator (infix)
IN word or block in the object's context
IF conditional if
DO evaluates a block, file, url, function word
AT returns the series at the specified index
NO logic false
ON logic true
SO postfix assert (2 = 1 + 1 so print "math works")
AN english pluralize (an "axe" -> "an axe", an "cat" -> "a cat")
ME self-reference after set word (variable: me + 1)
MY variant of ME for non-enfix (block: my append 10)
BE (unused)
BY (unused)
Reasonably non-controversial use of Symbolic Operators
>= true if the first value is greater than the second (infix)
<= true if the first value is less than the second (infix)
!= true if the values are not equal (infix)
<> same function as != despite looking like empty tag
-> lambda function (`x -> [print x]` is `func [x] [print x]`)
<- pointfree function (`<- append b` is `func [x] [append b x]`)
Debug use of "drawing-looking" operators that pop off the page (their
non-English appearance makes them preferable for this purpose instead
of quirkier things like "-- decrements variables")
-- debug dump following variable name and its value
** comment line out
!! breakpoint
++ (unused)
== section header (TBD, currently strict equality)
?? Debug probe a word, path, block or such
Unapplied in Rebol but used in Red for questionable benefit; these are
likely to take on some kind of larger systemic purpose, possibly as
console DSL operations:
<< infix version of prefix shift left
>> infix version of prefix shift right
Deprecated shorthands for terms defined elsewhere, which have been
reclaimed for Rebmu as "free terms" by the rebol-proposals (and should
be removed from the language, existing only in console modes or
user-preferences)
RM alias for DELETE
DP alias for DELTA-PROFILE
DT alias for DELTA-TIME
LS print contents of a directory
CD change directory
DS temporary stack debug
Several more options were opened up in the two character space by the
so-called "arrow words". They are things like |> and >< etc. These
have experiments but nothing settled down completely.
There are tricks for `/` acting like a WORD! even though it is actually
a path. That opens up some potential space for `//`, `/.`, `./` and
`..` as being operations, but this concept has not been explored.
### SINGLE CHARACTER PLUS QUESTION MARK
Several of these freed up with the requirement that ending in a ?
actually return a LOGIC!. The useful function empty? doesn't fit
if E? is EQUAL? and EM? is EMAIL?
A? => and?
B? ; could be... block?
C? ; could be... char?
D? => distinct?
E? => equal?
F?
G? => greater?
H? => head?
I? ; could be... integer?
J?
K?
L? => lesser?
M? => match? ; MM? is mismatch.
N? => negative?
O? => or?
P? => positive?
Q?
R?
S? => same?
T? => tail? ; can't be TRUE?, TAIL? is more important
U? => unequal?
V? => value?
W?
X? => xor?
Y? => true? ; (a.k.a. yes?)
Z? => zero?
}
]
; Load the modules implementing mush/unmush
import %mush.reb
import %unmush.reb
; Load the library of xxx-mu functions; tricks that are specific to Rebmu
; and would not seriously find their way into Rebol/Red mainline
;
; NOTE: While originally there was a tendency to be liberal with these,
; they are being excised as they can sort of be seen as interfering with
; Rebmu's main mission, which is to teach/evangelize Rebol dialecting.
; A trick just for the sake of helping win code golf that does not really
; assist with that (or worse, inhibits learning the languages proper)
; should be included sparingly--if at all
import %mulibrary.reb
; returns a block of definitions to include in the context
remap-datatype: function [type [type-word!] shorter [text!] /noconvert] [
stem: to-text to-word type
result: reduce [
load-value unspaced [shorter "!" ":"] load-value unspaced [":" stem "!"]
load-value unspaced [shorter "?" ":"] load-value unspaced [":" stem "?"]
]
if not noconvert [
append result spread reduce [
load-value unspaced [shorter "-" ":"] load-value unspaced [":" "to-" stem]
]
]
return bind result system.contexts.user
]
rebmu-base-context: make object! compose [
;----------------------------------------------------------------------
; DATATYPE SHORTHANDS (3 CHARS)
; Though I considered giving the datatypes 2-character names, I decided
; on 3 (so IN! for INTEGER! instead of I!, in order that the test will
; be IN? with I? available for other purposes). This is a decision
; which may be worth revisiting for some types, as INDEX? has become
; INDEX-OF in the language, so I? is free (for instance). Not all
; types will fit in that space, however.
;----------------------------------------------------------------------
; Shorcuts for datatypes. Establishes both the type and the query functions.
; (so remapping "em" for EMAIL! makes EM! => EMAIL! and EM? => EMAIL?)
; ACTION! is a "type constraint" in Ren-C, review
(remap-datatype block! "bl")
; CHAR! is a "type constraint" in Ren-C, review
(remap-datatype decimal! "dc")
(remap-datatype email! "em")
(remap-datatype error! "er")
(remap-datatype frame! "fr")
(remap-datatype get-word! "gw")
(remap-datatype group! "gr")
(remap-datatype integer! "in")
(remap-datatype pair! "pr")
(remap-datatype percent! "pc")
(remap-datatype map! "mp")
(remap-datatype object! "ob")
(remap-datatype path! "pa")
; LIT-WORD! is a "type constraint" in Ren-C, review
; REFINEMENT! is a "type constraint" in Ren-C, review
(remap-datatype time! "tm")
(remap-datatype tuple! "tu")
(remap-datatype text! "tx")
(remap-datatype file! "fi")
(remap-datatype word! "wd")
(remap-datatype tag! "tg")
(remap-datatype money! "mn")
(remap-datatype binary! "bi")
; there is no "to-blank" operation in Rebol, all other datatypes have it...
(remap-datatype/noconvert blank! "bn")
;----------------------------------------------------------------------
; TYPE CONVERSION SHORTHANDS
; These are particularly common and there aren't many commands starting
; with T so aliasing them is useful. May reconsider this later. Also,
; these are special variations that add behaviors for types unsupported
; by Rebol's operators.
;----------------------------------------------------------------------
t: :to
tw: :to-word-mu
tsw: :to-set-word
tt: :to-text-mu
tc: :to-char-mu
tb: :to-block
ti: :to-integer
;----------------------------------------------------------------------
; DEFINING FUNCTIONS
;
; Note: This is done first because its used in later definitions.
;
; The behavior of FUNCTION and FUNC vs. CLOSURE and CLOS has to do with
; performance optimization, and ideally only the closure and clos
; semantics would exist. Since performance is not the axis of concern
; for Rebmu, it goes with the more expressive construct (and so may
; Rebol3 at some point)
;----------------------------------------------------------------------
fn: :function-mu
ds: :does
; DX is a variadic branch builder, delegated to by most constructs that
; want to be able to "do more" than just a branch.
;
dx: func [:args [<opt> any-value! <variadic>]] [
a: take args
if block? a [return does a]
if word? a [
; !!! TBD
]
]
d: :dx
; Function dialects in Rebmu employ a method of quoting their arguments
; literally. If not blocks, they are read as *instructions* for what spec
; or body to build. It is a compact dialect for function construction.
;
fq: :funqtion-mu
; FX is a particularly tweaked version of FQ that pulls from a list of
; helpful memoizations of names for args and locals. It's designed to
; not stomp on common abbreviations like I for IF.
;
; !!! To be written...
;
fx: :fq
f: :fx
;----------------------------------------------------------------------
; CONDITIONALS
;----------------------------------------------------------------------
; Conditionals in Rebmu are less conservative than in Rebol. They are
; willing to handle non-BLOCK! and non-FUNCTION! branches, returning the
; values as-is. For why this is not done in general, see:
;
; https://forum.rebol.info/t/backpedaling-on-non-block-branches/476
;
; !!! Should they also take isotope forms and treat them as false or pass
; them through without running any brances?
if: :if-mu
i: :if
inz: adapt :if [condition: :condition != 0]
either: :either-mu
ei: :either
un: adapt :if [condition: not to-value :condition]
es: :else
e: :es
th: :then
ao: :also ; AS is a valid language keyword, AL is ALL
sw: :switch
cs: :case
csa: :case/all
;----------------------------------------------------------------------
; LOOPING CONSTRUCTS
;----------------------------------------------------------------------
lp: :loop ; L is LET
fe: :for-each
fr: :for ; !!! FOR-MU to tolerate more options, so `f x 10 [...]` works
f: :fr
ev: :every
me: :map-each
rme: :remove-each-mu
cy: :cycle
rp: :repeat ; R is RETURN inside a function (globally R acts as QUIT)
; More valuable to have a single character looping construct take U than
; to have UNLESS take it.
;
ut: :until
ux: macro [] [[until dx]]
u: :ux
w: :while
uz: macro [] [[until .zero?]]
ue: macro [] [[until .equal?]]
ul: macro [] [[until .lesser?]]
ug: macro [] [[until .greater?]]
cn: :continue
br: :break ; BK is BACK
tr: :trap
ct: :catch
am: :attempt
qt: :quit
r: :qt ; inside functions, R is return; this is done to save Q for quote
;----------------------------------------------------------------------
; OBJECTS AND CONTEXTS
;----------------------------------------------------------------------
lt: :let
l: :lt
us: :use
ob: specialize :make [type: object!]
;----------------------------------------------------------------------
; SERIES OPERATIONS
;----------------------------------------------------------------------
po: :poke
pc: :pick
ap: :append
ir: :insert ; IN, IS, IT are standalone words
irp: :insert/part
ird: :insert/part/dup
tk: :take
mno: :minimum-of
mxo: :maximum-of
se: :select
rv: :reverse
sl: :split
rm: :remove
rl: :replace ; RP is repeat, REPEND is deprecated in Ren-C
rla: :replace/all
rlac: :replace/all/case
hd: :head
tl: :tail
bk: :back-mu
nx: :next-mu
ch: :change
chp: :change/part
sk: :skip
fi: :find
fis: :find/skip
uq: :unique
pa: :parse-mu
pp: :pre-parse-mu
ln: specialize :reflect [property: 'length]
os: :offset-of ; OF is used in the language
ix: :index-of
ty: specialize :reflect [property: 'type]
t?: :tail?
h?: :head?
m?: :empty?
; v?: :value? ; there's no value? anymore
fs: :first ; FR might be confused with fourth
sc: :second
th: :third
fh: :fourth ; FR might be confused with first
ff: :fifth
sx: :sixth
sv: :seventh
eh: :eighth ; EI is either, and EG is either-greater
nh: :ninth
tt: :tenth
ls: :last ; override LS list directory? We need SHELL dialect
;----------------------------------------------------------------------
; PORTS
;----------------------------------------------------------------------
del: :delete ; If shipping in console, why not use the matching term?
dl: :delete ; Corresponding to the act of protest of changing RM
;----------------------------------------------------------------------
; METAPROGRAMMING
;----------------------------------------------------------------------
co: :compose
cod: :compose/deep
mo: :mush-and-mold-compact
jn: :join
re: :reduce
usp: :unspaced
cl: :collect-mu
qo: :quote ; QU is QUIT
q: :qo ; more useful to abbreviate further than Q for QUIT
el: :elide ; ES is ELSE
;----------------------------------------------------------------------
; MATH AND LOGIC OPERATIONS
;----------------------------------------------------------------------
ad: :add-mu
sb: :subtract-mu
mp: :multiply
dv: :div-mu
dd: :divide
ng: :negate-mu
z?: :zero?
md: :mod
e?: :equal?
lg10: :log-10
lg2: :log-2
; is L2: LG2 worth it, or L+digit be used for something else?
lge: :log-e ; can't do "(L)og (N)atural" as LN, due to LN: LENGTH-OF
; is LE: LGE worth it, or is LE better used or something else?
lg: :lg10 ; Rebmu's 10-fingered-human bias, also shortens LG10 more
; POW is the infix power operator, but infix is sometimes not what you
; want so Rebol also has power as a prefix variant.
pw: :power
; CONDITIONAL LOGIC
;
; There may be a slight desire to use abbreviated infix logic, as it would
; cause a different evaluation ordering which might be desirable to have
; at no extra character cost. But OR is already a 2-letter word, and XOR
; and AND are only 3-letter. So it's probably better to save AN/AD for
; other purposes (XO/XR less useful...)
; NT is prefix NOT (itself an alias for NOT?), we took N? for NEGATIVE?
a?: :and?
; o?: :or? ; !!! Does not exist, needs to be added
; x?: :xor? ; !!! same
; BITWISE
;
; These operators are the generalized ones, laid out for the day when
; AND/OR/XOR become "conditional"...they work on bitsets etc.
;
; http://curecode.org/rebol3/ticket.rsp?id=1879
cm: :complement
ic: :intersect
un: :union
df: :difference
ev?: :even?
od?: :odd?
inc: :increment-mu
ic: :inc
dec: :decrement-mu
dc: :dec
g?: :greater?
ge?: :greater-or-equal?
l?: :lesser?
le?: :lesser-or-equal?
se?: :strict-equal?
n?: :negative?
p?: :positive?
sg: :sign-of
y?: :did
n?: :not
mn: :min
mx: :max
; The void-tolerating forms are more useful
ay: :any ; AN is its own word
al: :all
; to-integer (TI) always rounds down. A "CEIL" operator is useful,
; though it's a bit verbose in Rebol as TO-INTEGER ROUND/CEILING VALUE.
; May be common enough in Code Golf math to warrant inclusion.
;
ce: :ceiling-mu
;----------------------------------------------------------------------
; CONVERTERS
;----------------------------------------------------------------------
; Converters end in "-", so for instance "em-" is equivalent to
; TO-EMAIL. I decided that minus signs on the end would indicate
; conversions because this is one place where default Rebol functions
; use a lot of hyphens. The general goal of these functions is
; unlike modifiers, to not change their inputs. It might be nice
; to have some
;----------------------------------------------------------------------
; INPUT/OUTPUT
;----------------------------------------------------------------------
pr: :print
p: :pr
rd: :read
wr: :write
wrs: :write-stdout ;-- was PRINT/ONLY
pb: :probe
ri: :readin-mu
r: :ri
rl: :read/lines
nl: :newline ; already abbreviated as LF for line feed (?)
;----------------------------------------------------------------------
; STRINGS
;----------------------------------------------------------------------
tm: :trim
tmt: :trim/tail
tmh: :trim/head
tma: :trim/all
up: :uppercase
upp: :uppercase/part
lw: :lowercase
lwp: :lowercase/part
;----------------------------------------------------------------------
; CONSTRUCTION FUNCTIONS
; Letter and a tilde means "factory". This convention is not in Rebol
; but I thought that even if AR and AI were available for ARRAY and
; ARRAY/INITIAL the use of the tilde would allow the pattern to
; continue for some other things which *would* collide.
;
; This used to be done with carets, but Christopher Ross-Gill thought
; tildes looked better.
;----------------------------------------------------------------------
mk: :make
cp: :copy
c: :cp
cpd: :copy/deep
cpp: :copy/part
cppd: :copy/part/deep
; !!! ~ became reserved for use with BAD-WORD!.
; A~: :array
; AI~: :array/initial
; B~: does [copy []] ; two chars cheaper than cp[]
; H~: :to-http-url-mu
; HS~: :to-http-url-mu/secure
; I~: :make-integer-mu
; M~: :make-matrix-mu
; S~: does [copy ""] ; two chars cheaper than cp""
; SI~: :make-string-initial-mu
;----------------------------------------------------------------------
; MISC
;----------------------------------------------------------------------
bn: :blank
st: :set
gt: :get
en: :encode
swp: :swap-exchange-mu
fm: :format
; os: :onesigned-mu
sp: :space
; !!! Predefined character sets are something that has never been fully
; worked out or worked through. They were in the "proposals" module but
; that has been removed. New ideas for BITSET! implementation would allow
; sparse character sets in Unicode at lower cost.
;
comment [
ws: :whitespace
dg: :digit
dgh: :digit/hex
dghu: :digit/hex/uppercase
dghl: :digit/hex/lowercase
dgb: :digit/binary
lt: :letter
ltu: :letter/latin/uppercase
ltl: :letter/latin/lowercase
]
;----------------------------------------------------------------------
; MICRO MATH
;
; These can be overridden, but are helpful because mushing tries not to
; overload single-symbol/digit terminal semantics, in favor of giving us
; things like +a and a+. We should automatically generate these for all
; single digits, although figuring out special meanings for a0, s0, m0,
; d1 etc. would be a good idea.
e0: func [value] [value == 0]
e1: func [value] [value == 1]
e2: func [value] [value == 2]
; ...
e9: func [value] [value == 9]
a1: func [value] [add-mu value 1]
a2: func [value] [add-mu value 2]
; ...
s1: func [value] [subtract-mu value 1]
s2: func [value] [subtract-mu value 2]
; ...
d2: func [value] [divide value 2]
; ...
m2: func [value] [multiply value 2]
m3: func [value] [multiply value 3]
; ...
p2: func [value] [value ** 2]
; ...
;----------------------------------------------------------------------
; PREFIX PLUS
;
; These operations work particularly well as the source of an assignment
; because of the way that unmushing turns [A+b] into [a: +b]
;
; Haven't defined them yet... what will this family do?
;----------------------------------------------------------------------
; POSTFIX PLUS
;
; These are not easy to assign to in mushed code, because the bias
; gives the symbol to the next word e.g. [A+b] => [a: +b] instead of
; [a+: b].
;
; Idea is that these modify their arguments to save you from situations
; where you might otherwise have to make things the target of an assignment,
; like [m: add m 2]. Shorter code with a+M2 than Ma+M2, and you also
; are less likely to cause a mushing break. Note that the plus doesn't
; mean "advance" or "add" in this context, LAST+ is actually an
; operator which traverses the series backwards.
a+: :add-modify-mu
; f+: :first+
; s+: :subtract-modify-mu
; n+: :next-modify-mu
; b+: :back-modify-mu
; How strange could we get? Is it useful to do [Z: EQUALS? Z 3] on any
; kind of regular basis? Maybe if you do that test often after but
; don't need the value
;
=+: :equal-modify-mu
; what about two character functions? can they return different
; things than their non-modifier counterparts?
;
ch+: :change-modify-mu
hd+: :head-modify-mu
tl+: :tail-modify-mu
sk+: :skip-modify-mu
;----------------------------------------------------------------------
; REDEFINING HELPER
;
; While many of the original code-golf specific aspects of Rebmu that
; were imagined were kicked out as useless (as say, compared to throwing
; in a mushed matrix library etc.) this one is still around for study.
; The idea was an operator to be helpful for quickly redefining symbols
; used repeatedly.
;
; rf[aBCdEF] => rf[a bc d ef] => a: :bc d: :ef
;
; If you noticed an unusual repeated need for a function you could throw
; that in. Considering the minimal case of rf[aBC] it's 7 characters, which
; is one more than `A: :bc` would be. However, you wind up at a
; close bracket that starts a new mushing point, so it saves on what would
; be a necessary trailing space.
rf: :redefine-mu
; REVIEW: what kinds of meanings might be given to prefix question mark?
]
export rebmu: function [
{Visit http://hostilefork.com/rebmu/}
code "The Rebmu or Rebol code"
[text! block! file! url!]
/args "argument A, unless block w/set-words; can be Rebmu format [X10Y20]"
[any-value!]
/nocopy "Disable the default copy/deep of arguments for safety"
/stats "Print out some statistical information"
/debug "Output debugging information"
/env "Return runnable object plus environment without executing main"
/inject "Run some test code in the environment after main function"
[block! text!]
/output "Implicitly print the output result"
<static>
context (null)
][
case [
text? code [
if stats [
print ["Input Rebmu string was:" length of code "characters."]
]
code: load code
]
any [
file? code
url? code
][
code: load code
all [
'Rebmu = first code
block? second code
] then [
; ignore the header for the moment... just pick offset
; the first two values from code
take code
take code
] else [
print "WARNING: Rebmu sources should start with Rebmu [...]"
; Keep running, hope the file was valid Rebmu anyway
]
]
block? code [
if stats [
print "NOTE: Pass in Rebmu as string, not a block."
print "(That will give you a canonical character count.)"
]
]
] else [
fail "Bad code parameter."
]
ensure block! code
code: my unmush
if debug [
print ["Executing:" mold code]
]
if stats [
print [
"Rebmu as mushed Rebol block molds to:"
length of mold/only code
"characters."
]
]
=== UNMUSH CODE INJECTION, DEFAULT TO EMPTY BLOCK ===
inject: default [copy []]
if text? inject [
inject: load inject
]
if not block? inject [
code: to block! inject
]
inject: my unmush
=== UNMUSH ARGUMENT FOR ARGUMENT CODE INJECTION, DEFAULT TO EMPTY BLOCK ===
either args [
either block? args [
args: unmush either nocopy [args] [copy/deep args]
if not set-word? first args [
; assign to a if the block doesn't start with a set-word
args: compose [a: (args)]
]
][
args: compose [a: (args)]
]
][
args: copy []
]
; see https://github.com/hostilefork/rebmu/issues/7
; We track the outermost Rebmu context via a variable in the user context.
; This allows us to effectively create a "new" user context holding all
; the Rebmu overrides.
outermost: null? context
if outermost [
context: copy rebmu-base-context
append context spread args
; Rebmu's own behavior replaces DO, no /NEXT support yet
extend context 'do func [value] [
either string? value [
rebmu value
][
do value
]
]
; When we load, we want default binding to override with this context
; over system.contexts.user
rebmu-load: func [source] [
bind load source context
]
extend context 'load :rebmu-load
extend context 'ld :rebmu-load
; Add LOAD-VALUE (LV) ?
]
bind code context
bind inject context
if env [ ; only asked for the environment (e.g. to debug it)
return context
]
result: sys.util.enrescue [
do inject
do code
]
; If we exit the last "Rebmu user" context, then clear it
if outermost [
context: ~
]
if error? result [
fail result
]
if output [
print [result]
]
return unmeta result
]