Assembler.Assembler.SectionCode.GetCode C# (CSharp) Method

GetCode() private static method

Gets the assembled machine code for section of code
private static GetCode ( string code, ushort>.Dictionary labelDict, RegexOpcode regexOpcodes ) : byte[]
code string The lines of code for this section
labelDict ushort>.Dictionary A dict of labels and their absolute compiled positions
regexOpcodes RegexOpcode A list of RegexOpcodes where each RegexOpcode matches with each corresponding line of code
return byte[]
            private static byte[] GetCode(string[] code, Dictionary<string, ushort> labelDict, RegexOpcode[] regexOpcodes)
            {
                var output = new List<byte>();

                for (int i = 0; i < code.Length; i++)
                {
                    RegexOpcode op = regexOpcodes[i];

                    if (op.Prefix != null)
                        output.Add((byte)op.Prefix);

                    output.Add(op.Code);

                    if (op.BytesFollowing > 0)
                    {
                        Match m = op.Regex.Match(code[i]);

                        // Group 1 is the number, either n, -n, nn, -nn, h or hh or a label
                        int n;
                        if (!int.TryParse(m.Groups[1].Value, out n))
                        {
                            // Special case for JP opcode, as it can have a label to jump to
                            var first2Chars = op.Op.Substring(0, 2);
                            if (first2Chars == "JP" || first2Chars == "CA")
                                n = labelDict[m.Groups[1].Value];
                            else
                                throw new ApplicationException(string.Format("The value {0} cannot be parsed", m.Groups[1].Value));
                        }

                        if (op.BytesFollowing >= 1)
                            output.Add(n > 0 ? (byte)n : (byte)(sbyte)n);

                        int top = n >> 8;
                        if (op.BytesFollowing == 2)
                            output.Add(top > 0 ? (byte)top : (byte)(sbyte)top);
                    }
                }

                return output.ToArray();
            }