public static Machine GetSampleMachine() { var machine = new Machine(6); Instruction ix = new AssignValueInstruction(); ix.SetParameters(new List <int> { 0, 5 }); machine.AddInstruction(ix); Instruction iy = new AssignValueInstruction(); iy.SetParameters(new List <int> { 1, 6 }); machine.AddInstruction(iy); Instruction i0 = new AddInstruction(); i0.SetParameters(new List <int> { 2, 3, 0 }); // M2 = 0 + M0 machine.AddInstruction(i0); Instruction i1 = new AssignValueInstruction(); i1.SetParameters(new List <int> { 4, 1 }); //M4 = 1 machine.AddInstruction(i1); Instruction i2 = new GotoIfInstruction(); i2.SetParameters(new List <int> { 7, 2 }); // goto 7 if M2 > 0 machine.AddInstruction(i2); Instruction i3 = new AddInstruction(); i3.SetParameters(new List <int> { 0, 3, 5 }); // M0 = M3 + 5 machine.AddInstruction(i3); Instruction i4 = new HaltInstruction(); machine.AddInstruction(i4); //halt Instruction i5 = new AddInstruction(); i5.SetParameters(new List <int> { 3, 3, 1 }); // M3 = M3 + M1 machine.AddInstruction(i5); Instruction i6 = new SubstractInstruction(); i6.SetParameters(new List <int> { 2, 2, 4 }); // M2 = M2 - 1 machine.AddInstruction(i6); Instruction i7 = new GotoIfInstruction(); i7.SetParameters(new List <int> { 4, 4 }); // goto 4 if M4 > 0 machine.AddInstruction(i7); // when machine halts result will be in M0 return(machine); }
public Instruction Translate() { Instruction instruction; switch (Type) { case CommandType.AssignValue: instruction = new AssignValueInstruction(); instruction.SetParameters(new List <int> { Arg1.Value, Arg2.Value }); break; case CommandType.Add: instruction = new AddInstruction(); instruction.SetParameters(new List <int> { Arg1.Value, Arg2.Value, Arg3.Value }); break; case CommandType.Substract: instruction = new SubstractInstruction(); instruction.SetParameters(new List <int> { Arg1.Value, Arg2.Value, Arg3.Value }); break; case CommandType.Divide: instruction = new DivideByTwoInstruction(); instruction.SetParameters(new List <int> { Arg1.Value }); break; case CommandType.CopyValue: instruction = new CopyValueInstruction(); instruction.SetParameters(new List <int> { Arg1.Value, Arg2.Value }); break; case CommandType.CopyValue2: instruction = new CopyValue2Instruction(); instruction.SetParameters(new List <int> { Arg1.Value, Arg2.Value }); break; case CommandType.GotoIf: instruction = new GotoIfInstruction(); instruction.SetParameters(new List <int> { Arg1.Value, Arg2.Value }); break; case CommandType.Halt: instruction = new HaltInstruction(); instruction.SetParameters(new List <int>()); break; default: throw new InvalidEnumArgumentException(); } return(instruction); }