input = (ARGV[1] && ARGV[1] != '-') ? File.open(ARGV[1], "r") : STDIN

statements = []
input.each_line.each_with_index do |line,line_num|
  vars = [{ var: 'b', var2: 'c' }]
  ce = ''
  line.each_char do |c|
    if /[0-9]/ === c
      ce += c
    elsif ce.length > 0
      var = vars.last
      case ARGV[0]
      when '1'
        statements << "#{var[:var]} #{var[:op]}= #{ce}"
      when '2'
        statements << "#{var[:var2]} #{var[:o2]}= #{ce}"
        var[:o2] = '+'
      end
      ce = ''
    end
    if /[\*\+]/ === c
      case ARGV[0]
      when '1'
        vars.last[:op] = c
      when '2'
        var = vars.last
        if '*' === c
          statements << "#{var[:var]} #{var[:o1]}= #{var[:var2]}"
          var[:o2] = nil
          var[:o1] = '*'
        else
        end
      end
    elsif '(' === c
      var = vars.last
      vars << case ARGV[0]
      when '1'
        { var: var[:var].succ }
      when '2'
        s = var[:var2].succ
        { var: s, var2: s.succ }
      end
    elsif ')' === c
      var = vars.pop
      next_var = vars.last
      case ARGV[0]
      when '1'
        statements << "#{next_var[:var]} #{next_var[:op]}= #{var[:var]}"
      when '2'
        statements << "#{var[:var]} #{var[:o1]}= #{var[:var2]}"
        statements << "#{next_var[:var2]} #{next_var[:o2]}= #{var[:var]}"
        next_var[:o2] = '+'
      end
    end
  end
  if '2' === ARGV[0]
    if ce.length > 0
      statements << "#{vars[0][:var2]} #{vars[0][:o2]}= #{ce}"
    end
    statements << "#{vars[0][:var]} #{vars[0][:o1]}= #{vars[0][:var2]}"
  end
  statements << "a #{line_num > 0 ? '+' : ''}= b"
end
func = "-> do\n#{statements.map{|s| "  #{s}" }.join("\n")}\n  a\nend"
func = binding.eval func

puts func.call