A Theory of the IMF for Star Formation in Molecular Clouds