Skip to content

Commit

Permalink
Implemented backwards compatibility with Rebol 2, to widen the audien…
Browse files Browse the repository at this point in the history
…ce and open up possibilities for graphics dialect stuff.
  • Loading branch information
hostilefork committed Jun 25, 2010
1 parent 293561e commit eed98b9
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 53 deletions.
2 changes: 2 additions & 0 deletions examples/connect-dots-data.r
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
REBOL []

kitty: [
{ 8 }
{ }
Expand Down
2 changes: 2 additions & 0 deletions examples/hourglass.r
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
; http://stackoverflow.com/questions/1683857/code-golf-hourglass


; read J from the user, this is our height which will be greater than 1
; NOTE: if we are using rebmu/args we'd do JfsA
rJ

; read N from the user, this is our percentage sand
; NOTE: if we are using rebmu/args we'd do NscA
; NOTE: Percentages are a new type introduced in Rebol 3, this won't work in Rebol 2
N 0% rN

; Here's a doomsday hourglass... it tells you how much of your life is left until 21-dec-2012
Expand Down
8 changes: 4 additions & 4 deletions examples/line-padding.r
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

; could bracket in us's[ ... ] for a use block that protected s

; 35 chars
wh[SfiTlf][loADlO?sT[SisSc]TntS]hdT

comment [
; >> unmush [wh[SfiTlf][loADlO?sT[SisSc]TntS]hdT]
; == [wh [s: fi t lf] [lo ad l o? s t [s: is s c] t: nt s] hd t]
Expand All @@ -26,4 +23,7 @@ comment [
t: next s
head t
]
]
]

; 35 chars
wh[SfiTlf][loADlO?sT[SisSc]TntS]hdT
2 changes: 2 additions & 0 deletions examples/rotating-maze-data.r
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
REBOL []

smallmaze: [
{######}
{#o @#}
Expand Down
52 changes: 34 additions & 18 deletions mulibrary.r
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ if-lesser?-mu: func [
unless-mu: func [
"Evaluates the block if condition is not TRUE."
condition
block [block!]
'block
] [
unless condition [do-mu block]
]
Expand Down Expand Up @@ -261,7 +261,7 @@ make-string-initial-mu: func [length value] [

; if a pair, then the first digit is the digit
make-integer-mu: func [value] [
switch/default type?/word get value [
switch/default type?/word :value [
pair! [to-integer first value * (10 ** second value)]
integer! [to-integer 10 ** value]
] [
Expand All @@ -274,7 +274,7 @@ make-integer-mu: func [value] [
; integer into helpful-mu will just call make-integer-mu. There's potential here for
; really shortening
helpful-mu: func ['arg] [
switch/default type?/word get arg [
switch/default type?/word :arg [
word! [
switch/default arg [
b: [0 1] ; binary digits
Expand Down Expand Up @@ -372,7 +372,8 @@ quoth-mu: funct [

index?-find-mu: funct [
{Same as index? find, but returns 0 if find returns none}
series [series! gob! port! bitset! typeset! object! none!]
series [series! ; gob! in r3 only... leave out for r2 compatibility for now
port! bitset! typeset! object! none!]
value [any-type!]
] [
pos: find series value
Expand All @@ -384,19 +385,29 @@ index?-find-mu: funct [
]

increment-mu: func ['word-or-path] [
either path? word-or-path [
old: get word-or-path
set word-or-path 1 + old
either path? :word-or-path [
; R2 doesn't support combination of "get/set" and path, but R3 does
comment [
old: get :word-or-path
set :word-or-path 1 + old
]
old: do :word-or-path
do reduce [to-set-path :word-or-path 1 + old]
old
] [
++ :word-or-path
]
]

decrement-mu: func ['word-or-path] [
either path? word-or-path [
old: get word-or-path
set word-or-path 1 - old
either path? :word-or-path [
; R2 doesn't support combination of "get/set" and path, but R3 does
comment [
old: :word-or-path
set :word-or-path 1 - old
]
old: do :word-or-path
do reduce [to-set-path :word-or-path 1 - old]
old
] [
-- :word-or-path
Expand All @@ -423,7 +434,7 @@ writeout-mu: funct [
value
] [
; better implementation coming...
switch/default type?/word get value [
switch/default type?/word :value [
block! [
foreach element value [
print element
Expand All @@ -439,7 +450,7 @@ writeout-mu: funct [
inversion-mu: func [
value
] [
switch/default type?/word get value [
switch/default type?/word :value [
string! [empty? value]
decimal!
integer! [
Expand All @@ -451,15 +462,15 @@ inversion-mu: func [
]

next-mu: funct [arg] [
switch/default type?/word get arg [
switch/default type?/word :arg [
integer! [arg + 1]
] [
next arg
]
]

back-mu: funct [arg] [
switch/default type?/word get arg [
switch/default type?/word :arg [
integer! [arg - 1]
] [
back arg
Expand All @@ -468,7 +479,12 @@ back-mu: funct [arg] [

swap-exchange-mu: funct [
"Swap contents of variables."
a [word! series! gob!] b [word! series! gob!]
a [word! series!
; gob! is in r3 only
]
b [word! series!
; gob! is in r3 only
]
][
if not equal? type? a type? b [
throw "swap-mu must be used with common types"
Expand All @@ -487,7 +503,7 @@ div-mu: funct [value1 value2] [
]

add-mu: funct [value1 value2] [
switch/default type?/word get value1 [
switch/default type?/word :value1 [
block! [
result: copy value1
while [(not tail? value1) and (not tail? value2)] [
Expand All @@ -503,7 +519,7 @@ add-mu: funct [value1 value2] [
]

subtract-mu: funct [value1 value2] [
switch/default type?/word get value1 [
switch/default type?/word :value1 [
block! [
result: copy value1
while [(not tail? value1) and (not tail? value2)] [
Expand All @@ -519,7 +535,7 @@ subtract-mu: funct [value1 value2] [
]

negate-mu: funct [value] [
switch/default type?/word get value [
switch/default type?/word :value [
block! [
result: copy value
while [not tail? value] [
Expand Down
47 changes: 27 additions & 20 deletions mushing.r
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ type-of-char: func [c [none! char!]] [
if none? c [
return none
]
if upper/(c) [
if find upper c [
return 'upper
]
if lower/(c) [
if find lower c [
return 'lower
]
if digit/(c) [
if find digit c [
return 'digit
]
if separatorsymbol/(c) [
if find separatorsymbol c [
; no spacing but separates
return 'separatorsymbol
]
if headsymbol/(c) [
if find headsymbol c [
; space before if not at start
return 'headsymbol
]
if tailsymbol/(c) [
if find tailsymbol c [
; space afterwards but not before (we use ~ for not)
return 'tailsymbol
]
if isolatedsymbol/(c) [
if find isolatedsymbol c [
; space before and after unless there's a run of identical ones
return 'isolatedsymbol
]
Expand All @@ -58,6 +58,13 @@ type-of-char: func [c [none! char!]] [
return 'caseless
]

; Rebol 2 complains if you try to next a position that's at the tail
; or if you try to pick from such a position. Rebol 3 returns none.
; This is for compatibility for as long as Rebmu supports Rebol 2.
firstnext: func [series] [
either any [tail? series tail? next series] [none] [first next series]
]

; Simplistic routine, open to improvements. Use PARSE dialect instead?
; IF unmush returns a block! (and you didn't pass in a block!) then it is a sequence
; There may be a better convention
Expand All @@ -71,10 +78,10 @@ unmush: funct [value /deep] [
thisIsSetWord: 'upper = thisType
nextCanSetWord: found? find [headsymbol symbol tailsymbol] thisType
lowerCaseRun: 'upper <> thisType
while [nextType: type-of-char first next pos] [
while [nextType: type-of-char firstnext pos] [
comment [
print [
"this:" first pos "next:" first next pos
"this:" first pos "next:" firstnext pos
"thisType:" to-string thisType "nextType:" to-string nextType
"thisIsSetWord:" thisIsSetWord "nextCanSetWord:" nextCanSetWord
"str:" str
Expand Down Expand Up @@ -115,40 +122,40 @@ unmush: funct [value /deep] [
; you instead get [a ++ b], [a +-+ b]
either nextPos = next pos [
; Break symbol on the right only
pos: back insert nextPos space
pos: back insert nextPos { }
thisIsSetWord: false
] [
; Break symbol on the left and on the right
insert pos space
insert pos { }
; We have to advance nextPos to compensate for the insertion
pos: back insert next nextPos space
pos: back insert next nextPos { }
]

thisIsSetWord: false
nextCanSetWord: false
]
]
isolatedsymbol [
either (first pos) == (first next pos) [
either (first pos) == (firstnext pos) [
mergedSymbol: true
] [
if thisIsSetWord [
pos: insert pos ":"
pos: insert pos {:}
either mergedSymbol [
mergedSymbol: false
] [
pos: insert pos space
pos: insert pos { }
]
lowerCaseRun: true
]
pos: back insert next pos space
pos: back insert next pos { }
thisIsSetWord: 'upper = nextType
nextCanSetWord: false
]
]
] [
lowerCaseRun: 'upper <> thisType
either ('digit = thisType) and found? find [#"x" #"X"] first next pos [
either ('digit = thisType) and found? find [#"x" #"X"] firstnext pos [
; need special handling if it's an x because of pairs
; want to support mushings like a10x20 as [a 10x20] not [a 10 x 20]
; for the moment lie and say its a digit
Expand All @@ -167,7 +174,7 @@ unmush: funct [value /deep] [
thisIsSetWord: 'upper = nextType
nextCanSetWord: false
]
pos: back insert next pos space
pos: back insert next pos { }
]
]
]
Expand All @@ -176,9 +183,9 @@ unmush: funct [value /deep] [
]
if thisIsSetWord [
either thisType = 'tailsymbol [
pos: insert pos ": "
pos: insert pos {: }
] [
pos: back insert next pos ":"
pos: back insert next pos {:}
]
]
load lowercase str
Expand Down

0 comments on commit eed98b9

Please sign in to comment.