public string ExecuteMemoryCommand(MemoryCommand cmd) { number = converter.ConvertBaseTo10(number.systemBase, editor.number); switch (cmd) { case MemoryCommand.add: memory.Add(number); break; case MemoryCommand.sub: memory.Sub(number); break; case MemoryCommand.clear: memory.Clear(number); break; case MemoryCommand.read: number = memory.ReadNumber(number); break; case MemoryCommand.write: memory.Write(number); break; default: break; } editor.number = converter.Convert10ToBase(number.systemBase, number.GetNumberString()); return(editor.number); }
public void Push_Pointer_IsCorrect(int index, string segment) { var actual = MemoryCommand.Push("", "pointer", index).HackInstructions; var expected = new[] { segment, "D=M", "@SP", "M=M+1", "A=M-1", "M=D" }; Assert.Equal(expected, actual); }
public void Push_Temp_IsCorrect(int index) { var actual = MemoryCommand.Push("", "temp", index).HackInstructions; var expected = new[] { $"@{index + 5}", "D=M", "@SP", "M=M+1", "A=M-1", "M=D" }; Assert.Equal(expected, actual); }
public void Push_StaticSegment_IsCorrect() { var actual = MemoryCommand.Push("Foo", "static", 2).HackInstructions; var expected = new[] { "@Foo.2", "D=M", "@SP", "M=M+1", "A=M-1", "M=D", }; Assert.Equal(expected, actual); }
public void Push_Constant_IsCorrect(int i) { var actual = MemoryCommand.Push("", "constant", i).HackInstructions; var expected = new[] { $"@{i}", "D=A", "@SP", "M=M+1", "A=M-1", "M=D", }; Assert.Equal(expected, actual); }
protected void given_command <T>(string verb, string noun, params Expression <Func <InputBuilder, InputBuilder> >[] properties) where T : MemoryCommand, new() { Command = new MemoryCommandDescriptor( properties.Select(property => property.Compile()( new InputBuilder(property.Parameters[0].Name) .Setter((cmd, val) => SaveProperty(property.Parameters[0].Name, val))) .Descriptor)) { Verb = verb, Noun = noun, Create = () => CommandInstance = new T { Execute = () => { CommandExecuted = true; return(Enumerable.Empty <ICommandOutput>()); } } }; }
public void Push_ConstantLocalArgumentThisThat_IsCorrect( string segment, string segmentBase, int index) { var actual = MemoryCommand.Push("", segment, index).HackInstructions; var expected = new[] { segmentBase, "D=M", $"@{index}", "A=D+A", "D=M", "@SP", "M=M+1", "A=M-1", "M=D", }; Assert.Equal(expected, actual); }
public void TestCommandPush() { Command c = Commands.Get("push"); MemoryCommand mc = c as MemoryCommand; Assert.IsNotNull(mc, "Mc should not be null"); string expected = ExpectedPush8(); string actual = mc.Assembly(MemorySegment.constantSegment, 8); Assert.AreEqual(expected, actual); /* push <segment> <value> (segment = constant === @<value>) * * push constant xxx * => * @xxx * D=A * @SP * A=M * M=D * @SP * M=M+1 * * push segment index * => * @segment * D=A * @index * D=D+A * @SP * A=M * M=D * @SP * M=M+1 */ }