[REBOL] Re: Looking for advice
From: rgombert:essentiel at: 18-Jan-2003 22:10
Hi Patrick,
Here is a thitd solution, writing a sort of basic parser "by hand" :
simplify: function [expr][state buffer][
state: 'op
buffer: copy []
foreach elt expr [
switch state [
op [if number? elt [append buffer elt state: 'number]]
number [if find [+ - * /] elt [append buffer elt state: 'op]]
]
]
if state = 'op [remove back tail buffer]
return buffer
]
>> simplify [3 none 1 + 9 4 + 1 -]
== [3 + 9 + 1]
>> simplify [0 4 0 + 0 none none 2 /]
== [0 + 0]
>> simplify [none / 8 1 7 none 0 * 5]
== [8 * 5]
>> simplify [7 1 9 3 4 9 none - none]
== [7]
>> simplify [2 3 - 7 + none 0 / none]
== [2 - 7 + 0]
>> simplify [1 2 3 4 5]
== [1]
>> simplify [1 + - 5 7 * 9 /]
== [1 + 5 * 9]
Renaud GOMBERT
--------------------------------
Ma maison : www.essentiel.net
Résidence secondaire : www.frenchcinema4d.com