bench-rejected-impls/18_operation_order.rb
# Since there's no precedence, can immediately apply all ops as they come in.
Since there's no precedence, can immediately apply all ops as they come in.
Since there ' s no precedence can immediately apply all ops as they come in .
def ltr_immediately_apply(expr, verbose: false)
  expr = expr.delete(' ')
  stack = [{val: nil, op: nil}]
  apply = ->v {
    last = stack[-1]
    if last[:val] && last[:op]
      puts "#{stack[-1][:val]} #{stack[-1][:op]} #{v}" if verbose
      last[:val] = last[:val].send(last[:op], v)
      last[:op] = nil
    else
      raise "improperly replaced #{last} #{expr}" if last[:val] || last[:op]
      last[:val] = v
    end
  }
  expr.each_char { |c|
    case c
    when ?(
      stack << {val: nil, op: nil}
    when ?)
      last = stack.pop
      raise "bad paren #{last} #{expr}" if !last[:val] || last[:op]
      apply[last[:val]]
    when /\d/
      apply[Integer(c)]
    when ?+, ?*
      last = stack[-1]
      raise "bad op #{c} #{last} #{expr}" if !last[:val] || last[:op]
      last[:op] = c
    else raise "bad #{c} in #{expr}"
    end
  }
  raise "unclosed paren #{stack} in #{expr}" if stack.size > 1
  raise "leftover op #{stack[-1]} in #{expr}" if stack[-1][:op]
  stack[-1][:val]
end
def ltr_immediately_apply ( expr , verbose : false ) expr = expr . delete ( ' ' ) stack = [ { val : nil , op : nil } ] apply = -> v { last = stack [ - 1 ] if last [ :val ] && last [ :op ] puts "#{stack[-1][:val]} #{stack[-1][:op]} #{v}" if verbose last [ :val ] = last [ :val ] . send ( last [ :op ] , v ) last [ :op ] = nil else raise "improperly replaced #{last} #{expr}" if last [ :val ] || last [ :op ] last [ :val ] = v end } expr . each_char { | c | case c when ?( stack << { val : nil , op : nil } when ?) last = stack . pop raise "bad paren #{last} #{expr}" if ! last [ :val ] || last [ :op ] apply [ last [ :val ] ] when / \d / apply [ Integer ( c ) ] when ?+ , ?* last = stack [ - 1 ] raise "bad op #{c} #{last} #{expr}" if ! last [ :val ] || last [ :op ] last [ :op ] = c else raise "bad #{c} in #{expr}" end } raise "unclosed paren #{stack} in #{expr}" if stack . size > 1 raise "leftover op #{stack[-1]} in #{expr}" if stack [ - 1 ] [ :op ] stack [ - 1 ] [ :val ] end
Since there's no precedence, can immediately apply all ops as they come in.
Since there ' s no precedence can immediately apply all ops as they come in .
expr
<not_specific>
verbose
<not_specific>