Addressing Long-Horizon Tasks by Integrating Program Synthesis and State Machines