import java.io.PrintWriter;
import rebelsky.pal.*;

public class TestPAL
{
  public static void main(String[] args)
    throws Exception
  {
    PrintWriter out = new PrintWriter(System.out,true);
    Computer hal = new Computer(1000);
    Temporary input = new Temporary();
    Temporary result = new Temporary();
    Label startLabel = new Label("START");
    Label endLabel = new Label("END");

    InstructionSequence convert = new InstructionSequence();
    Temporary f = new Temporary();
    convert.add(new Label("CONVERT"));
    convert.add(new Comment("Experiments with Conversion"));
    convert.add(new FWrite(input));
    convert.add(new I2F(input,f));
    convert.add(new FWrite(f));

    InstructionSequence count = new InstructionSequence();
    Label countloop = new Label("LOOP");
    Label endcount = new Label("END");
    Temporary counter = new Temporary();
    count.add(new Comment("Print numbers from 1 to 10"));
    count.add(new Label("COUNT"));
    count.add(new Move(new IConstant(1), counter));
    count.add(countloop);
    count.add(new IWrite(counter));
    count.add(new JumpLessEqual(new IConstant(10), counter, endcount));
    count.add(new IAdd(new IConstant(1), counter, counter));
    count.add(new Jump(countloop));
    count.add(endcount);

    InstructionSequence code = new InstructionSequence();
    code.add(new Comment("Read values and test until 0 entered."));
    code.add(startLabel);
    code.add(new IRead(input));
    code.add(new IWrite(input));
    code.add(new IAdd(new IConstant(3), input, result));
    code.add(new IWrite(result));
    code.add(convert);
    code.add(new JumpNotEqual(input, new IConstant(0), startLabel));
    code.add(count);
    code.add(endLabel);
    code.add(new Halt());

    hal.setCode(code);
    out.println("----- DUMP -----");
    hal.dump(out);
    out.println("----- RUN ------");
    hal.run(true);
  } // main(String[])
} // class TestPAL
