示例#1
0
文件: Statement.cs 项目: Y2JB/8-Bit
        private void Parse()
        {
            string[] components = SourceAsm.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
            if (components.Length == 0 || components.Length > 3)
            {
                throw new Exception(String.Format("ERROR: Line {0} : Too many arguments", LineNumber));
            }

            // Parse the op code and 2 params
            Instruction = new Instruction(LineNumber, components[0]);


            // If params are passed a null string to parse they will default to 'unused' type and value of zero
            string lParam = (components.Length >= 2) ? components[1] : null;
            string rParam = (components.Length == 3) ? components[2] : null;


            // Bit of a hack for jump instructions
            // JMP has one param but the parm has to be forced into the 8 bit rparam
            if (Instruction._OpCode == OpCode.JMP ||
                Instruction._OpCode == OpCode.JE ||
                Instruction._OpCode == OpCode.JNE ||
                Instruction._OpCode == OpCode.JZ ||
                Instruction._OpCode == OpCode.JNZ ||
                Instruction._OpCode == OpCode.CMP)
            {
                rParam = lParam;
                lParam = null;
            }

            LeftParam  = new InstructionParameter(LineNumber, lParam);
            RightParam = new InstructionParameter(LineNumber, rParam);

            Validate();
        }
示例#2
0
        public void Parse()
        {
            int intValue = 0;

            // Param is a register
            GeneralPurposeRegisterId reg;

            if (Int32.TryParse(SourceAsm, out intValue) == false &&
                Enum.TryParse(SourceAsm, out reg) &&
                Enum.IsDefined(reg.GetType(), reg))
            {
                Value = (byte)reg;
                Type  = ParamType.Reg;
                return;
            }

            // Check if param is a number (hex or dec)
            if (SourceAsm.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
            {
                intValue = Convert.ToInt32(SourceAsm, 16);
                Type     = ParamType.Int;
            }
            else if (Int32.TryParse(SourceAsm, out intValue))
            {
                Type = ParamType.Int;
            }

            if (Type == ParamType.Int)
            {
                if (intValue < -128 || intValue > 255)
                {
                    throw new Exception(String.Format("ERROR: Line {0} : Param out of range", LineNumber));
                }

                Value = (byte)intValue;
                return;
            }

            throw new Exception(String.Format("ERROR: Line {0} : Unknown Parameter value - {1}", LineNumber, SourceAsm));
        }