예제 #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]);
                bool b1 = string.IsNullOrWhiteSpace(lines_correct[j]);
                bool b2 = Regex.Match(lines_correct[j], @"^[ \t]*--(.*)").Success;
                while (j < lines_correct.Length && (lines_correct[j].Equals("") || b1 || b2))
                {
                    j++;
                    if (j == lines_correct.Length - 1)
                    {
                        break;
                    }
                    b1 = string.IsNullOrWhiteSpace(lines_correct[j]);
                    //b1 = Regex.Match(lines_correct[j], @"^[\r\n]*").Success;
                    b2 = Regex.Match(lines_correct[j], @"^[ \t]*--(.*)").Success;
                }
                // Finished current state
                if (j == lines_correct.Length)
                {
                    break;
                }
                if (lines_correct[j].Equals("0o0o0o0o0o0o0o0o0o0o0o0o0o00o0o0o0o0o0o00o0o0o0o0o0"))
                {
                    j = j + 2;
                    run_state++;
                }
                else if (lines_correct[j].Equals("0o0o0o0o0o0o0o0o0o0o0o0o0o00o0o0o0o0o0o00o0o0o0o0o0o"))
                {
                    j = j + 1;
                    run_state++;
                }
                if (run_state == (int)Cmp_mod.Port_names || run_state == (int)Cmp_mod.Port_entrys)
                {
                    int k;
                    for (k = i; k < lines.Length && !lines_correct[j - 1].Equals(lines[k]); k++)
                    {
                        Match result = Regex.Match(lines[k], pattern);
                        if (result.Success)
                        {
                            if (run_state == (int)Cmp_mod.Port_names)
                            {
                                continue;
                            }
                            string    port_str = Regex.Split(lines[k], pattern)[1];
                            PortEntry pe       = PortEntry.PortEntryParse(port_str, lines[k + 1].Equals(lines_correct[j - 1]));
                            if (pe != null)
                            {
                                //MessageBox.Show("Comment port!");
                                pe.SetIsComment(true);
                                Ports.Add(pe);
                                continue;
                            }
                        }
                        // Save Names
                        if (run_state == (int)Cmp_mod.Port_names)
                        {
                            if (!IsValidPortName(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.Port_entrys)
                        {
                            bool      last  = lines[k + 1].Equals(lines_correct[j]);
                            PortEntry entry = PortEntry.PortEntryParse(lines[k], last);
                            if (entry != null)
                            {
                                //MessageBox.Show(entry.GetName() + " " + entry.GetName().Length.ToString());
                                string type = entry.GetPortType().ToString();
                                Ports.Add(entry);
                            }
                            else
                            {
                                b1 = string.IsNullOrWhiteSpace(lines[k]);
                                b2 = Regex.Match(lines[k], @"^[ \t]*--(.*)").Success;
                                if (k < lines.Length && (lines[k].Equals("") || b1 || b2))
                                {
                                    continue;
                                }
                                MessageBox.Show("COMPILATION 2: Parsing error at line " + (k + 1));
                                Console.WriteLine("COMPILATION 2: Parsing error at line " + (k + 1) + "\nFinishing compilation....");
                                return(false);
                            }
                            if (last)
                            {
                                //j--;
                                //k++;
                                break;
                            }
                        }
                    }
                    run_state++;
                    i = k;
                }
                else
                {
                    //lines[i] = RemoveComment(lines[i]);
                    b1 = string.IsNullOrWhiteSpace(lines[i]);
                    b2 = Regex.Match(lines[i], @"^[ \t]*--(.*)").Success;
                    bool b3 = Regex.Match(lines[i], @"^use(.)*").Success || Regex.Match(lines[i], @"^library(.*)").Success;
                    while (i < lines.Length && (lines[i].Equals("") || b1 || b2 || b3))
                    {
                        i++;
                        if (i == lines.Length - 1)
                        {
                            break;
                        }
                        b1 = string.IsNullOrWhiteSpace(lines[i]);
                        b2 = Regex.Match(lines[i], @"^[ \t]*--(.*)").Success;
                        b3 = Regex.Match(lines[i], @"^use(.)*").Success || Regex.Match(lines[i], @"^library(.*)").Success;
                    }
                    if (!lines_correct[j].Equals(lines[i]))
                    {
                        MessageBox.Show("COMPILATION 3: Invalid file\n" + lines[i] + "\n" + lines_correct[j]);
                        Console.WriteLine("COMPILATION 3: 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);
        }