예제 #1
0
파일: Assembler.cs 프로젝트: cartman300/E64
        public static byte[] Assemble(string Src, out Instruction[] UnusedInstructions)
        {
            Assembler Asm = new Assembler();
            List<Instruction> UI = new List<Instruction>();
            UI.AddRange(Enum.GetNames(typeof(Instruction)).Select((N) => (Instruction)Enum.Parse(typeof(Instruction), N)));

            string[] Lines = SplitLines(Src);
            for (int i = 0; i < Lines.Length; i++) {
                if (Lines[i].StartsWith("#include")) {
                    string Pth = Lines[i].Substring(Lines[i].IndexOf('\"') + 1).Trim();
                    Pth = Pth.Substring(0, Pth.Length - 1);

                    Lines[i] = File.ReadAllText(Pth);
                    Lines = SplitLines(string.Join("\n", Lines));
                    i--;
                }
            }
            Src = string.Join("\n", Lines);

            string[] Tokens = SimpleTokenizer.Tokenize(Src, ';', new char[] { '&', ',', ';' });

            for (int i = 0; i < Tokens.Length; i++) {
                string T = Tokens[i];
                if (T.Length == 0 || T == "," || T == ";")
                    continue;

                if (T.EndsWith(":")) {
                    Asm.Label(T.Substring(0, T.Length - 1));
                } else if (T == "&") {
                    Asm.AddressOf(Tokens[++i]);
                } else if (T == "ref") {
                    Asm.Data(ParseType(Tokens[++i]));
                } else {
                    Instruction I;
                    if (Enum.TryParse(T, out I)) {
                        if (UI.Contains(I))
                            UI.Remove(I);
                        Asm.Instr(I);
                    } else
                        Asm.Raw(ParseType(T));
                }
            }

            UnusedInstructions = UI.ToArray();
            return Asm.ToByteArray();
        }
예제 #2
0
        public static byte[] Assemble(string Src, out Instruction[] UnusedInstructions)
        {
            Assembler          Asm = new Assembler();
            List <Instruction> UI  = new List <Instruction>();

            UI.AddRange(Enum.GetNames(typeof(Instruction)).Select((N) => (Instruction)Enum.Parse(typeof(Instruction), N)));

            string[] Lines = SplitLines(Src);
            for (int i = 0; i < Lines.Length; i++)
            {
                if (Lines[i].StartsWith("#include"))
                {
                    string Pth = Lines[i].Substring(Lines[i].IndexOf('\"') + 1).Trim();
                    Pth = Pth.Substring(0, Pth.Length - 1);

                    Lines[i] = File.ReadAllText(Pth);
                    Lines    = SplitLines(string.Join("\n", Lines));
                    i--;
                }
            }
            Src = string.Join("\n", Lines);

            string[] Tokens = SimpleTokenizer.Tokenize(Src, ';', new char[] { '&', ',', ';' });

            for (int i = 0; i < Tokens.Length; i++)
            {
                string T = Tokens[i];
                if (T.Length == 0 || T == "," || T == ";")
                {
                    continue;
                }

                if (T.EndsWith(":"))
                {
                    Asm.Label(T.Substring(0, T.Length - 1));
                }
                else if (T == "&")
                {
                    Asm.AddressOf(Tokens[++i]);
                }
                else if (T == "ref")
                {
                    Asm.Data(ParseType(Tokens[++i]));
                }
                else
                {
                    Instruction I;
                    if (Enum.TryParse(T, out I))
                    {
                        if (UI.Contains(I))
                        {
                            UI.Remove(I);
                        }
                        Asm.Instr(I);
                    }
                    else
                    {
                        Asm.Raw(ParseType(T));
                    }
                }
            }

            UnusedInstructions = UI.ToArray();
            return(Asm.ToByteArray());
        }