public double Func(params object[] x)
 {
     return((double)FuncMethodInfo.Invoke(_mathFuncObj, x));
 }
Example #2
0
        public static FuncMethodInfo ParseFuncMethod(string code_text, int start_index, string func_name, string func_type, string func_params)
        {
            FuncMethodInfo result_struct = new FuncMethodInfo(func_name);

            int cur_pos        = start_index;
            int braces_counter = 0;
            int line_counter   = 0;
            int body_start     = start_index;

            while (code_text[cur_pos] != '{')
            {
                cur_pos++;
            }
            if (code_text[cur_pos] == '{')
            {
                body_start     = cur_pos;
                braces_counter = 1;
                cur_pos++;
            }

            while (braces_counter != 0)
            {
                if (code_text[cur_pos] == '{')
                {
                    braces_counter += 1;
                }
                if (code_text[cur_pos] == '}')
                {
                    braces_counter -= 1;
                }
                if (code_text[cur_pos] == '\n')
                {
                    line_counter++;
                }
                cur_pos++;
            }

            if (line_counter == 0)
            {
                line_counter = 1;
            }
            else if (line_counter > 1)
            {
                line_counter--;
            }

            string func_body  = code_text.Substring(body_start + 1, cur_pos - body_start - 2);
            string func_body2 = code_text.Substring(body_start, cur_pos - body_start);

            Regex empty_lines_rx = new Regex(@"([\t ]*\n)");
            Regex empty_line_rx  = new Regex(@"\S");

            var func_lines = func_body.Split('\n');

            int usfl_lines = 0;

            for (int i = 0; i < func_lines.Length; i++)
            {
                var word_mathes = empty_line_rx.Matches(func_lines[i]);
                if (word_mathes.Count != 0)
                {
                    usfl_lines++;
                }
            }

            var empty_matches = empty_lines_rx.Matches(func_body);

            if (usfl_lines == 0 && (line_counter == 1 || line_counter == 2))
            {
                line_counter = 0;
            }



            List <string> KeyWords = new List <string>()
            {
                "alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand",
                "bitor", "bool", "break", "case", "catch", "char", "char16_t",
                "char32_t", "class", "compl", "const", "constexpr", "const_cast",
                "continue", "decltype", "default", "delete", "do", "double", "dynamic_cast",
                "else", "enum", "explicit", "export", "extern", "false", "float", "for",
                "friend", "goto", "if", "inline", "int", "long", "mutable", "namespace",
                "new", "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq",
                "private", "protected", "public", "register", "reinterpret_cast", "return",
                "short", "signed", "sizeof", "static", "static_assert", "static_cast",
                "struct", "switch", "template", "this", "thread_local", "throw", "true",
                "try", "typedef", "typeid", "typename", "union", "unsigned", "using",
                "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq"
            };

            Regex kword_rgx = new Regex(@"\b(" + string.Join("|", KeyWords.Select(Regex.Escape).ToArray()) + @"\b)");

            List <string> match_list = new List <string>();

            int kword_num = 0;

            int kword_num2 = 0;


            Regex mlt_str_rgx      = new Regex("\".*\"");
            Regex sgl_str_rgx      = new Regex("\'.*\'");
            Regex fake_str_rgx     = new Regex("\"");
            Regex fake_sgl_str_rgx = new Regex("\'");


            //\b[=;*() ]* *(const|char|int|return)[=;*()\ ]+(?!"+)\b

            bool next_line_str_flag = false;

            for (int i = 0; i < func_lines.Length; i++)
            {
                var kw_matches = kword_rgx.Matches(func_lines[i]);
                //if (kw_mathes.Count != 0)
                //  kword_num+=kw_mathes.Count;
                var test_str_matches     = fake_str_rgx.Matches(func_lines[i]);
                var test_sgl_str_matches = fake_sgl_str_rgx.Matches(func_lines[i]);


                //1
                if (test_str_matches.Count == 0 && next_line_str_flag == false && test_sgl_str_matches.Count == 0)
                {
                    kword_num += kw_matches.Count;
                    Console.WriteLine("FIRST IF.This line add whole kwords = " + func_lines[i] + " " + next_line_str_flag.ToString());
                    continue;
                }
                //1.HALF
                else if (test_sgl_str_matches.Count % 2 == 0 && next_line_str_flag == false && test_sgl_str_matches.Count > 0)
                {
                    var sgl_str_matches = sgl_str_rgx.Matches(func_lines[i]);
                    foreach (Match kw_match in kw_matches)
                    {
                        bool not_str_flag = true;
                        foreach (Match sgl_str_match in sgl_str_matches)
                        {
                            //3.2
                            if (kw_match.Index > sgl_str_match.Index && (kw_match.Index < (sgl_str_match.Index + sgl_str_match.Value.Length)))
                            {
                                not_str_flag = false;
                                Console.WriteLine("1.HALF CLOSING COMMENT IN");
                                break;
                            }
                        }
                        //3.3
                        if (not_str_flag)
                        {
                            kword_num++;
                            Console.WriteLine("1.HALF .This line = " + func_lines[i]);
                            continue;
                        }
                    }
                }

                //2
                else if (test_str_matches.Count == 0 && next_line_str_flag == true)
                {
                    Console.WriteLine("GOT HERE");
                    continue;
                }

                //3
                else if (test_str_matches.Count % 2 == 0 && next_line_str_flag == false && test_str_matches.Count > 0)
                {
                    var mlt_str_matches = mlt_str_rgx.Matches(func_lines[i]);
                    var sgl_str_matches = sgl_str_rgx.Matches(func_lines[i]);

                    foreach (Match kw_match in kw_matches)
                    {
                        bool not_str_flag = true;
                        foreach (Match str_match in mlt_str_matches)
                        {
                            //3.1
                            if (kw_match.Index > str_match.Index && (kw_match.Index < (str_match.Index + str_match.Value.Length)))
                            {
                                not_str_flag = false;
                                Console.WriteLine("3.1  CLOSING COMMENT IN " + str_match.Index.ToString() + " " + kw_match.Index.ToString());
                                break;
                            }
                        }
                        foreach (Match sgl_str_match in sgl_str_matches)
                        {
                            //3.2
                            if (kw_match.Index > sgl_str_match.Index && (kw_match.Index < (sgl_str_match.Index + sgl_str_match.Value.Length)))
                            {
                                not_str_flag = false;
                                Console.WriteLine("3.2 CLOSING COMMENT IN");
                                break;
                            }
                        }
                        //3.3
                        if (not_str_flag)
                        {
                            kword_num++;
                            Console.WriteLine("3.3 .This line = " + func_lines[i]);
                            continue;
                        }
                    }
                }
                //4
                else if (test_str_matches.Count % 2 == 1)
                {
                    //4.1
                    if (test_str_matches.Count == 1 && next_line_str_flag == false)
                    {
                        foreach (Match kw_match in kw_matches)
                        {
                            bool good_kw = true;
                            if (kw_match.Index > test_str_matches[0].Index)
                            {
                                good_kw = false;
                            }
                            //4.1.1
                            if (good_kw)
                            {
                                kword_num++;
                                Console.WriteLine("4.1.1  This line = " + func_lines[i]);
                                continue;
                            }
                        }
                        //4.1.2
                        if (func_lines[i][func_lines[i].Length - 1] == '\\')
                        {
                            next_line_str_flag = true;
                            Console.WriteLine("4.1.2  Opened\\continued comment ");
                        }
                    }
                    //4.2
                    else if (test_str_matches.Count == 1 && next_line_str_flag == true)
                    {
                        foreach (Match kw_match in kw_matches)
                        {
                            bool good_kw = true;
                            if (kw_match.Index < test_str_matches[0].Index)
                            {
                                good_kw = false;
                            }
                            //4.2.1
                            if (good_kw)
                            {
                                kword_num++;
                                Console.WriteLine("4.2.1  This line = " + func_lines[i]);
                                continue;
                            }
                        }
                        //4.2.2
                        if (func_lines[i][func_lines[i].Length - 1] != '\\')
                        {
                            next_line_str_flag = false;
                            Console.WriteLine("4.2.2  closed comment ");
                        }
                    }
                }
            }

            for (int i = 0; i < match_list.Count; i++)
            {
                Console.WriteLine(match_list[i]);
            }

            Console.WriteLine(match_list.ToString());

            Match ft_match = kword_rgx.Match(func_type);

            if (ft_match.Success && ft_match.Index == 0)
            {
                kword_num++;
                kword_num2++;
            }

            var param_match = kword_rgx.Matches(func_params);

            if (param_match.Count != 0)
            {
                kword_num  += param_match.Count;
                kword_num2 += param_match.Count;
            }

            string test_message = string.Format(CultureInfo.CurrentCulture, "for this func {0} found next line of lines - {1}, useful between them - {5} ,  keywords - 1st var - {6}  2nd var - {7},\n {3} testing {4} \nit contains next code:\n{2}", func_name, line_counter, func_body, body_start, cur_pos, usfl_lines, kword_num, kword_num2);

            result_struct.SetInfo(line_counter, usfl_lines);
            Console.WriteLine(test_message);



            return(result_struct);
        }