Exemplo n.º 1
0
        public void TestCommandAdd()
        {
            Command c = Commands.Get("add");

            Assert.IsTrue(c is ArithmeticCommand);
            ArithmeticCommand ac = c as ArithmeticCommand;

            Assert.AreEqual(ac.Assembly(), "bum");
        }
Exemplo n.º 2
0
        public void Neg_IsCorrect()
        {
            var actual = ArithmeticCommand.Neg().HackInstructions;

            var expected = new[]
            {
                "@SP",
                "A=M-1",
                "M=-M"
            };

            Assert.Equal(expected, actual);
        }
Exemplo n.º 3
0
 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());
     }
 }
Exemplo n.º 4
0
 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());
     }
 }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        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();
            }
        }
Exemplo n.º 7
0
        /// <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();
            }
        }
Exemplo n.º 8
0
        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);
        }