Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Problem with paren

 [1/9] from: patrick::philipot::laposte::net at: 1-Feb-2003 17:24


Hi List, I am trying to build a block including some parenthesis and I am surely missing something. It seems I am not able to transform the words into parens. See that: values: [ 7 "/" "(" 1 "+" 2 ")"] test: copy [] foreach v values [either integer! = type? v [append test v] [append test to-word v]] I have tried 'compose, 'compose/deep but I am at a loss... Any clue? Regards Patrick

 [2/9] from: joel:neely:fedex at: 1-Feb-2003 13:23


Hi, Patrick, I think the problem is that parentheses aren't words, but are part of the (minimal) REBOL syntax, in exactly the same way that the square brackets are simply the external syntax for blocks and the quotes or curly braces are the external syntax for strings. IFAIK what you'd have to do is either: 1) construct a string out of the concatenation of your block of values, and then load that string, or 2) write a recursive routine (or parse rules) that handles the presence of "(" by accumulating the block of content up to the matching ")" and then converts that block to a paren. The problem with 1, of course, is word scoping. -jn- pat665 wrote:

 [3/9] from: al:bri:xtra at: 2-Feb-2003 9:26


> I am trying to build a block including some parenthesis and I am surely
missing something.
> It seems I am not able to transform the words into parens.
Try something like:
>> compose [7 / (to-paren [1 + 2])]
== [7 / (1 + 2)] I hope that helps! Andrew Martin ICQ: 26227169 http://valley.150m.com/

 [4/9] from: carl:cybercraft at: 2-Feb-2003 9:37


Hi Patrick, On 02-Feb-03, pat665 wrote:
> Hi List, > I am trying to build a block including some parenthesis and I am
<<quoted lines omitted: 6>>
> I have tried 'compose, 'compose/deep but I am at a loss... > Any clue?
At one point there you're attempting to have this in your test block... [ 7 / ( ] which just won't work in REBOL, as you can't have half a paren. One way around this would be to append a paren, then insert your values into it. ie...
>> blk: []
== []
>> append/only blk make paren! []
== [()] So far, so good, but when we try to append to the paren we get this...
>> append last blk 1
** Script Error: tail expected series argument of type: series port ** Where: append ** Near: insert tail series :value yet, oddly, insert works...
>> insert last blk 1
== ()
>> blk
== [(1)] (Is this a bug people?) To get around that you could at first append a block then convert it to a paren when you've appended what you need to. ie...
>> blk: []
== []
>> append/only blk []
== [[]]
>> append last blk 1
== [1]
>> change/only back tail blk to-paren last blk
== []
>> blk
== [(1)] Have fun... -- Carl Read

 [5/9] from: al:bri:xtra at: 2-Feb-2003 10:35


Carl Read wrote:
> So far, so good, but when we try to append to the paren we get this... > >> append last blk 1
<<quoted lines omitted: 7>>
> == [(1)] > (Is this a bug people?)
You're probably using an older version of Rebol, that aggressively evaluates paren! values. The beta versions don't do this. You can get "around" this by using a different version of append like this:
>> source append
append: func [ {Appends a value to the tail of a series and returns the series.} Series [series! port!] Value [any-type!] /Only "Appends a block value as a block." ][ either only [ insert/only tail :Series :Value ] [ insert tail :Series :Value ] :Series ] I hope that helps! Andrew Martin ICQ: 26227169 http://valley.150m.com/

 [6/9] from: joel:neely:fedex at: 2-Feb-2003 0:37


Hi, Patrick, Here's one way... pat665 wrote:
> Hi List, > > I am trying to build a block including some parenthesis and I am > surely missing something. > > values: [ 7 "/" "(" 1 "+" 2 ")"]
...
> Any clue? >
bos2bov: make object! [ myblock: none _run: func [/local result front] [ result: copy [] while [0 < length? myblock] [ front: first myblock remove myblock if front = ")" [return result] append/only result either number? front [ front ][ either front = "(" [ to-paren _run ][ to-word front ] ] ] result ] run: func [b [block!]] [ myblock: copy/deep b _run ] ] ...which behaves as...
>> values: [ 7 "/" "(" 1 "+" 2 ")"]
== [7 "/" "(" 1 "+" 2 ")"]
>> foo: bos2bov/run values
== [7 / (1 + 2)]
>> length? foo
== 3
>> do foo
== 2.33333333333333 Hope this helps! -jn-

 [7/9] from: patrick:philipot:laposte at: 2-Feb-2003 11:05


Hi List, Joel: your bos2bov is impressive! Thanks others for their contributions. Actually, after playing a bit with 'parse, I prefer the simpler 'load solution. values: [ 7 "/" "(" 1 "+" 2 ")"] test: copy "" foreach v values [ either string! = type? v [ append test v append test " " ][ append test to-string v append test " " ] ] foo: load trim test do foo ; == 2.33333333333333 Regards Patrick

 [8/9] from: rotenca:telvia:it at: 2-Feb-2003 11:54


Hi patrik
> values: [ 7 "/" "(" 1 "+" 2 ")"]
...
> do foo ; == 2.33333333333333
Why not?
>> do load form [ 7 "/" "(" 1 "+" 2 ")"]
== 2.33333333333333 --- Ciao Romano

 [9/9] from: sunandadh:aol at: 2-Feb-2003 6:22


Patrick
> Actually, after playing a bit with 'parse, I prefer the simpler 'load > solution.
Just be aware that Do'ing untrusted strings can be dangerous. If the string comes from an untrusted source (typed by a user say), take some extra precautions. Example -- try this: values: [ rebol [quit]] test: copy "" foreach v values [ either string! = type? v [ append test v append test " " ][ append test to-string v append test " " ] ] foo: load trim test do foo ; == exits the console Sunanda

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted