public void TestCommandAdd() { Command c = Commands.Get("add"); Assert.IsTrue(c is ArithmeticCommand); ArithmeticCommand ac = c as ArithmeticCommand; Assert.AreEqual(ac.Assembly(), "bum"); }
public void Neg_IsCorrect() { var actual = ArithmeticCommand.Neg().HackInstructions; var expected = new[] { "@SP", "A=M-1", "M=-M" }; Assert.Equal(expected, actual); }
public void WriteArithmetic(ArithmeticCommand arithmeticCommand) { if (arithmeticCommand == ArithmeticCommand.Mult) { this.WriteCall("Math.multiply", 2); } else if (arithmeticCommand == ArithmeticCommand.Divide) { this.WriteCall("Math.divide", 2); } else { _streamWriter.WriteLine(arithmeticCommand.ToString().ToLower()); } }
public void WriteArithmetic(ArithmeticCommand arithmeticCommand) { if (arithmeticCommand == ArithmeticCommand.Mult) { this.WriteCall("Math.multiply", 2); } else if (arithmeticCommand == ArithmeticCommand.Divide) { this.WriteCall("Math.divide", 2); } else { _streamWriter.WriteLine(arithmeticCommand.ToString().ToLower()); } }
public void Add_IsCorrect() { var actual = ArithmeticCommand.Add().HackInstructions; var expected = new[] { "@SP", "M=M-1", "A=M", "D=M", "A=A-1", "M=D+M" }; Assert.Equal(expected, actual); }
public void Execute() { if (!CanExecute) { throw new InvalidOperationException("Cannot execute in the current state"); } if (CurrentLine != null) { Command cmd; switch (CurrentLine.Instruction) { case MoveInstruction _: cmd = new MoveCommand(this); break; case ArithmeticInstruction _: cmd = new ArithmeticCommand(this); break; case BranchInstruction _: cmd = new BranchCommand(this); break; case HaltInstruction _: cmd = new HaltCommand(this); break; case StoreInstruction _: cmd = new StoreCommand(this); break; case LoadInstruction _: cmd = new LoadCommand(this); break; case NoOperation _: cmd = new NoCommand(this); break; default: throw new ArgumentOutOfRangeException(); } cmd.Execute(); } }
/// <summary> /// Compiles an expression. /// </summary> private void CompileExpression() { // TODO this needs refactored - see page 209 if (!this.IsOperator()) { this.CompileTerm(); } if (this.IsOperator()) { ArithmeticCommand arithmeticCommand = this.CompileArithmeticCommand(); this.CompileTerm(); vmWriter.WriteArithmetic(arithmeticCommand); CompileExpression(); } }
private ArithmeticCommand CompileArithmeticCommand() { ArithmeticCommand vmOp = ArithmeticCommand.Add; Pair <string, string> operatorToken = this.CompileExpressionTerminal(); switch (operatorToken.Value2) { case ("+"): { vmOp = ArithmeticCommand.Add; break; } case ("-"): { vmOp = ArithmeticCommand.Sub; break; } case ("*"): { vmOp = ArithmeticCommand.Mult; break; } case ("/"): { vmOp = ArithmeticCommand.Divide; break; } case ("&"): { vmOp = ArithmeticCommand.And; break; } case ("|"): { vmOp = ArithmeticCommand.Or; break; } case (">"): { vmOp = ArithmeticCommand.Gt; break; } case ("<"): { vmOp = ArithmeticCommand.Lt; break; } case ("="): { vmOp = ArithmeticCommand.Eq; break; } } return(vmOp); }