Parsing/RPN to infix conversion: Difference between revisions
m
→{{header|Sidef}}: updated code
m (added a fourth column to the operator precedence table, added highlighting to better read the operator symbols, elided the necessity of a "Note".) |
m (→{{header|Sidef}}: updated code) |
||
Line 2,822:
{{trans|Perl 6}}
<lang ruby>func p(pair, prec) {
pair[0] < prec
}
func rpm_to_infix(string) {
say "#{'='*17}\n#{string}"
var stack = []
string.each_word { |w|
if (w ~~ /\d/) {
stack << [9, Num(w
}
else {
var y = stack.pop
var x = stack.pop
given(w) {
when ('^') { stack << [4, [p(x,5), w, p(y,4)].join(' ')] }
Line 2,840:
when (<+ ->) { stack << [2, [p(x,2), w, p(y,2)].join(' ')] }
}
say stack
}
}
say '-'*17
stack.map{_[1]}
}
var tests = [
'3 4 2 * 1 5 - 2 3 ^ ^ / +',
'1 2 + 3 4 + ^ 5 6 + ^',
]
tests.each { say rpm_to_infix(_).join(' ') }</lang>
{{out}}
|