Пример #1
0
        public bool IsFileValid()
        {
            // Prepare texts for comparison: remove comments and convert to lower case
            Console.WriteLine("Preparing to compile " + path_to_file);
            string[] lines_correct = File.ReadAllLines(path_to_correct);
            string[] lines         = File.ReadAllLines(path_to_file);
            for (int i = 0; i < lines_correct.Length; i++)
            {
                lines_correct[i] = TrimAndLower(lines_correct[i]);
            }
            for (int i = 0; i < lines.Length; i++)
            {
                lines[i] = TrimAndLower(lines[i]);
            }
            lines_correct = Array.FindAll(lines_correct, IsNotComment).ToArray();
            lines         = Array.FindAll(lines, IsNotComment).ToArray();

            // Init state of comparison
            int run_state = (int)Cmp_mod.Start;
            int j         = 0;

            // Parsing Analysis: Compare
            Console.WriteLine("Compiling...");
            for (int i = 0; i < lines.Length; i++)
            {
                // Skip empty lines
                lines_correct[j] = RemoveComment(lines_correct[j]);
                while (j < lines_correct.Length && lines_correct[j].Equals(""))
                {
                    j++;
                }
                // Finished current state
                if (lines_correct[j].Equals("0o0o0o0o0o0o0o0o0o0o0o0o0o00o0o0o0o0o0o00o0o0o0o0o0"))
                {
                    j = j + 2;
                    run_state++;
                }
                if (run_state == (int)Cmp_mod.Reg_names || run_state == (int)Cmp_mod.Reg_entrys)
                {
                    int    k;
                    string curr_group = "JACKSHIT", prev_group;
                    for (k = i + 1; k < lines.Length && !lines_correct[j - 1].Equals(lines[k]); k++)
                    {
                        // Save Groups
                        Match result = Regex.Match(lines[k], pattern);
                        if (result.Success)
                        {
                            prev_group = curr_group;
                            curr_group = Regex.Split(lines[k], pattern)[1];
                            RegisterEntry entry = RegisterEntry.RegEntryParse(curr_group, prev_group, lines[k + 1].Equals(lines_correct[j - 1]));
                            if (entry != null)
                            {
                                entry.SetIsComment(true);
                                if (entry.GetRegType() == RegisterEntry.type_field.FIELD)
                                {
                                    int           address = entry.GetAddress();
                                    RegisterEntry re      = FindAtAdress(address);
                                    if (re != null)
                                    {
                                        re.AddField(entry);
                                    }
                                }
                                else
                                {
                                    Registers.Add(entry);
                                }
                                curr_group = prev_group;
                            }
                            else if (!Groups.Contains(curr_group) && (curr_group == "" || curr_group[curr_group.Length - 1] != ','))
                            {
                                Groups.Add(curr_group);
                            }
                        }
                        // Save Names
                        else if (run_state == (int)Cmp_mod.Reg_names)
                        {
                            if (!IsValidRegName(lines[k]))
                            {
                                MessageBox.Show("COMPILATION 1: Parsing error at line " + (k + 1));
                                Console.WriteLine("COMPILATION 1: Parsing error at line " + (k + 1) + "\nFinishing compilation...");
                                return(false);
                            }
                        }
                        else if (run_state == (int)Cmp_mod.Reg_entrys)
                        {
                            RegisterEntry entry = RegisterEntry.RegEntryParse(lines[k], curr_group, lines[k + 1].Equals(lines_correct[j - 1]));
                            if (entry != null)
                            {
                                //MessageBox.Show(entry.GetName() + " " + entry.GetName().Length.ToString());
                                string type = entry.GetRegType().ToString();
                                if (type.Equals("FIELD") || type.Equals("field"))
                                {
                                    int           address = entry.GetAddress();
                                    RegisterEntry re      = FindAtAdress(address);
                                    if (re != null)
                                    {
                                        re.AddField(entry);
                                    }
                                    else
                                    {
                                        MessageBox.Show("COMPILATION 2: Address " + address + " doesn't exist (" + (k + 1) + ")");
                                        Console.WriteLine("COMPILATION 2: Address " + address + " doesn't exist (" + (k + 1) + ")" + "\nFinishing compilation...");
                                        return(false);
                                    }
                                }
                                else
                                {
                                    Registers.Add(entry);
                                }
                            }
                            else
                            {
                                MessageBox.Show("COMPILATION 3: Parsing error at line " + (k + 1));
                                Console.WriteLine("COMPILATION 3: Parsing error at line " + (k + 1) + "\nFinishing compilation...");
                                return(false);
                            }
                        }
                    }
                    run_state++;
                    i = k;
                }
                else
                {
                    lines[i] = RemoveComment(lines[i]);
                    while (i < lines.Length && lines[i].Equals(""))
                    {
                        i++;
                    }
                    if (!lines_correct[j].Equals(lines[i]))
                    {
                        MessageBox.Show("COMPILATION 4: Invalid file\n" + lines[i] + "\n" + lines_correct[j]);
                        Console.WriteLine("COMPILATION 4: Invalid file\n" + lines[i] + "!=" + lines_correct[j] + "\nFinishing compilation...");
                        return(false);
                    }
                    j++;
                }
            }
            Console.WriteLine("Logic analysis...");
            if (!NamesCrossValid())
            {
                return(false);
            }
            ValidRegLogic(); // Sematic Analysis, add everything from here
            Console.WriteLine("Compilation is complete");
            return(true);
        }