public static Machine getSampleMachine() { Machine machine = new Machine(6); Instruction i0 = new AddInstruction(); List <int> p = new List <int>(); p.Add(2); p.Add(3); p.Add(0); // M2 = 0 + M0 i0.setParameters(p); machine.addInstruction(i0); Instruction i1 = new AssignValueInstruction(); p.Clear(); p.Add(4); p.Add(1); //M4 = 1 i1.setParameters(p); machine.addInstruction(i1); Instruction i2 = new GotoIfInstruction(); p.Clear(); p.Add(5); p.Add(2); // goto 5 if M2 > 0 i2.setParameters(p); machine.addInstruction(i2); Instruction i3 = new AddInstruction(); p.Clear(); p.Add(0); p.Add(3); p.Add(5); // M0 = M3 + 0 i3.setParameters(p); machine.addInstruction(i3); Instruction i4 = new HaltInstruction(); machine.addInstruction(i4); //halt Instruction i5 = new AddInstruction(); p.Clear(); p.Add(3); p.Add(3); p.Add(1); // M3 = M3 + M1 i5.setParameters(p); machine.addInstruction(i5); Instruction i6 = new SubstractInstruction(); p.Clear(); p.Add(2); p.Add(2); p.Add(4); // M2 = M2 - 1 i6.setParameters(p); machine.addInstruction(i6); Instruction i7 = new GotoIfInstruction(); p.Clear(); p.Add(2); p.Add(4); // goto 2 i7.setParameters(p); machine.addInstruction(i7); int[] input = { 5, 6 }; machine.insertInput(input); // when machine halts result will be in M0 return(machine); }
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); }