예제 #1
0
        private bool Match()
        {
            // Named Group Match will not set groups not found
            Group sym       = null;
            int   symIndex  = 0;
            int   symLength = 0;
            Group opc       = null;
            int   opcIndex  = 0;
            int   opcLength = 0;
            Group drc       = null;
            int   drcIndex  = 0;
            int   drcLength = 0;

            Symbol    = string.Empty;
            OpCode    = string.Empty;
            Directive = string.Empty;

            //evaluate results of Regex and for each match
            var matches = CompiledRegex.Matches(Code);

            foreach (Match m in matches)
            {
                //loop through all the groups in current match
                for (int x = 1; x < m.Groups.Count; x++)
                {
                    //print the names wherever there is a succesful match
                    if (m.Groups[x].Success)
                    {
                        var group     = m.Groups[x];
                        var groupName = CompiledRegex.GroupNameFromNumber(x);
                        switch (groupName)
                        {
                        case "sym":
                            sym       = group;
                            symIndex  = group.Index;
                            symLength = group.Length;
                            Symbol    = group.Value;
                            break;

                        case "op":
                            opc       = group;
                            opcIndex  = group.Index;
                            opcLength = group.Length;
                            OpCode    = group.Value;
                            break;

                        case "dr":
                            drc       = group;
                            drcIndex  = group.Index;
                            drcLength = group.Length;
                            Directive = group.Value;
                            break;
                        }
                    }
                }
            }

            // This is a hack.  Probably need to master conditional
            // RegEx so when we match an opcode, we don't try to match
            // any directive.  The basic problem is that a directive
            // name could also be the operand of a opcode, as in:
            //  STA PAGE
            // Worse, in a case like PAGE, the code wants to skip
            // the entire line.
            if (OpCode.Length > 0)
            {
                Directive = string.Empty;
                drcIndex  = 0;
                drcLength = 0;
            }

            if (sym != null || drc != null || opc != null)
            {
                // Find beginning of operand
                var startOfOperand = opcIndex + opcLength;
                var otherEnd       = drcIndex + drcLength;
                if (otherEnd > startOfOperand)
                {
                    startOfOperand = otherEnd;
                }
                otherEnd = symIndex + symLength;
                if (otherEnd > startOfOperand)
                {
                    startOfOperand = otherEnd;
                }

                Operand = Code.Substring(startOfOperand);

                return(true);
            }

            return(false);
        }