An algorithm for nonsmooth optimization by successive piecewise linearization