Ejemplo n.º 1
0
        public List<ParsedCommand> Parse()
        {
            List<ParsedCommand> parsedCommands = new List<ParsedCommand>();

            CleanCommands();

            foreach (var command in _commands)
            {
                ParsedCommand pc = new ParsedCommand();

                pc.CommandType = GetCommandType(command);

                switch (pc.CommandType)
                {
                    case CommandType.A_COMMAND:
                    case CommandType.L_COMMAND:
                        pc.Symbol = GetSymbol(command);
                        break;
                    case CommandType.C_COMMAND:
                        pc.Dest = GetDestination(command);
                        pc.Comp = GetComputation(command);
                        pc.Jump = GetJump(command);
                        break;
                }
                parsedCommands.Add(pc);
            }

            return parsedCommands;
        }
Ejemplo n.º 2
0
        private string GetAddressInstruction(ParsedCommand pc)
        {
            int symbol = int.Parse(pc.Symbol);

            string leadingZeroes = "";
            string binarySymbol = Convert.ToString(symbol, 2);

            for (int i = 0; i < (16 - binarySymbol.Length); i++)
            {
                leadingZeroes += "0";
            }

            return leadingZeroes + binarySymbol;
        }
Ejemplo n.º 3
0
        private string GetComputation(ParsedCommand pc)
        {
            string compBits = "";

            if (pc.Comp.IndexOf('A') >= 0) compBits += "0";
            else if (pc.Comp.IndexOf('M') >= 0) compBits += "1";
            else if (pc.Comp == "D") compBits += "0";
            else compBits += "0";

            switch (pc.Comp)
            {
                case "0":
                    compBits += "101010";
                    break;
                case "1":
                    compBits += "111111";
                    break;
                case "-1":
                    compBits += "111010";
                    break;
                case "D":
                    compBits += "001100";
                    break;
                case "A":
                case "M":
                    compBits += "110000";
                    break;
                case "!D":
                    compBits += "001101";
                    break;
                case "!A":
                case "!M":
                    compBits += "110001";
                    break;
                case "-D":
                    compBits += "001111";
                    break;
                case "-A":
                case "-M":
                    compBits += "110011";
                    break;
                case "D+1":
                    compBits += "011111";
                    break;
                case "A+1":
                case "M+1":
                    compBits += "110111";
                    break;
                case "D-1":
                    compBits += "001110";
                    break;
                case "A-1":
                case "M-1":
                    compBits += "110010";
                    break;
                case "D+A":
                case "D+M":
                    compBits += "000010";
                    break;
                case "D-A":
                case "D-M":
                    compBits += "010011";
                    break;
                case "A-D":
                case "M-D":
                    compBits += "000111";
                    break;
                case "D&A":
                case "D&M":
                    compBits += "000000";
                    break;
                case "D|A":
                case "D|M":
                    compBits += "010101";
                    break;
            }

            return compBits;
        }
Ejemplo n.º 4
0
        private string GetJump(ParsedCommand pc)
        {
            string jumpBits = "";

            switch (pc.Jump)
            {
                case "JGT":
                    jumpBits = "001";
                    break;
                case "JEQ":
                    jumpBits = "010";
                    break;
                case "JGE":
                    jumpBits = "011";
                    break;
                case "JLT":
                    jumpBits = "100";
                    break;
                case "JNE":
                    jumpBits = "101";
                    break;
                case "JLE":
                    jumpBits = "110";
                    break;
                case "JMP":
                    jumpBits = "111";
                    break;
                default:
                    jumpBits = "000";
                    break;
            }

            return jumpBits;
        }
Ejemplo n.º 5
0
        private string GetDestination(ParsedCommand pc)
        {
            string destBits = "";

            if (!string.IsNullOrEmpty(pc.Dest))
            {
                if (pc.Dest.Contains("A")) destBits += "1";
                else destBits += "0";

                if (pc.Dest.Contains("D")) destBits += "1";
                else destBits += "0";

                if (pc.Dest.Contains("M")) destBits += "1";
                else destBits += "0";
            }
            else
            {
                destBits = "000";
            }

            return destBits;
        }