Beispiel #1
0
    public static void B_While(ref WordType[] tt, ref int tt_i, ref MyArray[] a, ref int a_i, ref string parseresult, ref Dictionary <string, int> varmap, ref Dictionary <string, string> varmap2, ref Dictionary <string, MyArray> varmap3, ref Dictionary <string, int> markmap, ref bool while_or)
    {
        // while 判断条件开始的地方
        int start = 0;
        // while 判断条件结束的地方
        int end = 0;
        // while 循环体开始的地方
        int start_num = 0;
        // while 循环体结束的地方
        int end_num = 0;
        // 存储左花括号的个数
        int hasQH = 0;

        // 如果在while关键字后不是左括号,则报出语法错误
        if ((!tt[tt_i + 1].Code.Equals("(")))
        {
            parseresult += "语法错误 while语句左括号不存在\r\n";
            throw new WrongGrammarException("语法错误 while语句左括号不存在");
            // 否则将左括号下一位的下标值赋值给判断条件开始处start的值
        }
        else
        {
            start = tt_i + 1;
        }
        // 找到第一个左花括号
        for (; !(tt[tt_i].Code.Equals("{")); tt_i++)
        {
            if (tt[tt_i + 1].Code.Equals("{"))
            {
                // 读到左花括号,将hasQH值加1
                hasQH += 1;
                // 判断左花括号前是否为右括号
                // 如果是,则将右括号前一位的下标赋值给判断条件结束end的值
                // 并且将左花括号下一位的下标赋值给标志循环体开始的地方start_num的值
                // 如果不是,则报错并终止程序
                if ((tt[tt_i].Code.Equals(")")))
                {
                    start_num = tt_i + 2;
                    end       = tt_i;
                }
                else
                {
                    parseresult += "语法错误 左右括号不匹配\r\n";
                    throw new WrongGrammarException("语法错误 左右括号不匹配");
                }
            }
        }

        tt_i = tt_i + 1;
        // 判断while语句是否正确
        for (; tt[tt_i + 1] != null; tt_i++)
        {
            // 当读到左花括号时 判断 前面一个是不是右括号
            // 如果不是就报错并终止程序
            if (tt[tt_i].Code.Equals("{"))
            {
                // 读到左花括号,将hasQH值加1
                hasQH += 1;
                // 判断左花括号前是否为右括号
                // 如果不是,则报错并终止程序
                if ((tt[tt_i - 1].Code.Equals(")")))
                {
                    continue;
                }
                else
                {
                    parseresult += "语法错误 左右括号不匹配\r\n";
                    throw new WrongGrammarException("语法错误 左右括号不匹配");
                }
                // 如果没有读取到左花括号而是读取到了右花括号 报错并终止程序
                // 并且如果右花括号前面不是分号 报错并终止程序
            }
            if (tt[tt_i + 1].Code.Equals("}"))
            {
                hasQH -= 1;
                Console.WriteLine(hasQH + " .............");
                if (hasQH == 0)
                {
                    end_num = tt_i;
                    break;
                }
                else
                {
                    continue;
                }
            }
        }
        if (hasQH != 0)
        {
            parseresult += "语法错误\r\n";
            throw new WrongGrammarException("语法错误");
        }

        /*
         * 将判断条件之间的语句传入WCheck 并且传入while语句循环体的结尾标志end_num 首先检查经过处理之后的判断条件是否为真
         * 如果为真则进入循环体执行 --调用Parser函数 否则直接将全局下标 tt_i指向while循环体的末尾
         */
        // 进行while循环语句
        if (WCheck.W_Check(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref start, ref end, ref end_num) == true)
        {
            while (WCheck.W_Check(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref start, ref end, ref end_num) && while_or)
            {
                Parser_ReNamed.Parser_ReNamed1(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref markmap, start_num, end_num, ref while_or);
            }
            while_or = true;
        }
        else
        {
            tt_i = end_num;
            return;
        }
        tt_i = end_num + 1;
        return;
    }
    public static void B_Dowhile(ref WordType[] tt, ref int tt_i, ref MyArray[] a, ref int a_i, ref string parseresult, ref Dictionary <string, int> varmap, ref Dictionary <string, string> varmap2, ref Dictionary <string, MyArray> varmap3, ref Dictionary <string, int> markmap, ref bool while_or)
    {
        // if 判断条件开始的地方
        int start = 0;
        // if 判断条件结束的地方
        int end = 0;
        // if 循环体开始的地方
        int start_num = 0;
        // if 循环体结束的地方
        int end_num = 0;
        // 判断是否有while语句
        int has_while = tt_i;

        // 判断do后面是否是左花括号
        if (!tt[tt_i + 1].Code.Equals("{"))
        {
            // 如果不是报出异常并停止程序
            parseresult += "语法错误 do_while语句左花括号不存在\r\n";
            throw new WrongGrammarException("语法错误 do_while语句左花括号不存在");
        }
        else
        {
            // 当花括号输入正常时
            // 全局指针后移至do需要执行的语句
            tt_i += 2;
            // 将全局指针赋值给循环开始标志
            start_num = tt_i;
            // 指针后移,直到寻找到对应右花括号
            for (; !tt[tt_i].Code.Equals("}"); tt_i++)
            {
                // 将全局指针赋值给循环结束变量
                // 将判断语句传入Parser时包括判断语句前后的圆括号
                end_num = tt_i;
            }
            // 判断右花括号后面是否存在while语句
            // 当存在while语句时
            if (tt[tt_i + 1].Code.Equals("while"))
            {
                // 当while关键字后没有左括号时
                if (!tt[tt_i + 2].Code.Equals("("))
                {
                    // 报出异常并停止程序
                    parseresult += "语法错误 do_while语句左括号不存在\r\n";
                    throw new WrongGrammarException("语法错误 do_while语句左括号不存在");
                }
                else
                {
                    // while后括号正常
                    // 全局指针移至判断语句
                    tt_i += 3;
                    // 将左括号下标赋给判断开始语句
                    start = tt_i - 1;
                    // 指针后移,直到寻找到对应右括号
                    for (; !tt[tt_i].Code.Equals(")"); tt_i++)
                    {
                        // 将全局指针赋值给判断结束变量
                        end = tt_i + 1;
                    }
                    // 判断结束变量的值最终为右括号
                }
            }
            else
            {
                // 当循环语句结束后没有找到while关键字时
                // 报出异常并停止程序
                parseresult += "语法错误 do_while语句while关键字不存在\r\n";
                throw new WrongGrammarException("语法错误 do_while语句while关键字不存在");
            }
        }
        // 先将循环语句执行一次
        Parser_ReNamed.Parser_ReNamed1(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref markmap, start_num, end_num, ref while_or);
        // 调用WCheck方法,进行判断语句的判断
        // 判断语句为真时
        if (WCheck.W_Check(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref start, ref end, ref end_num) == true)
        {
            // 进入循环语句
            while (WCheck.W_Check(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref start, ref end, ref end_num))
            {
                // 调用Parser,进行循环语句的执行
                Parser_ReNamed.Parser_ReNamed1(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref markmap, start_num, end_num, ref while_or);
            }
        }
        else
        {
            // 当判断语句为假时直接将全局指针指向do-while语句末尾
            tt_i = end + 1;
            return;
        }
        // 将全局指针指向do-while语句末尾
        tt_i = end + 1;
        return;
    }
    public static void B_for(ref WordType[] tt, ref int tt_i, ref MyArray[] a, ref int a_i, ref string parseresult, ref Dictionary <string, int> varmap, ref Dictionary <string, string> varmap2, ref Dictionary <string, MyArray> varmap3)
    {
        // 圆括号中分号的数目
        int num = 0;
        // 第一部分开始与结束指针
        int exp1_start;
        int exp1_end;
        // 第二部分开始与结束指针
        int exp2_start;
        int exp2_end;
        // 第三部分开始与结束指针
        int exp3_start;
        int exp3_end = 0;
        // 循环部分开始与结束指针
        int start;
        int end = 0;

        // 判断for语句中分号的数目
        for (int temp = tt_i; !tt[temp].Code.Equals(")"); temp++)
        {
            if (tt[temp].Code.Equals(";"))
            {
                num++;
            }
        }
        // 对于for语句的检查
        if (num != 2)
        {
            Console.WriteLine("wrong!5 -- for语句不正确");
            Environment.Exit(0);
        }
        else
        {
            if (!tt[tt_i + 1].Code.Equals("("))
            {
                Console.WriteLine("wrong!4 -- 没有左括号");
                Environment.Exit(0);
            }
            else
            {
                // 第一部分
                exp1_start = tt_i + 2;
                for (; !tt[tt_i].Code.Equals(";"); tt_i++)
                {
                }
                exp1_end = tt_i;
                // 判断语句
                exp2_start = tt_i + 1;
                tt_i++;
                for (; !tt[tt_i].Code.Equals(";"); tt_i++)
                {
                }
                exp2_end = tt_i;
                // 第三部分
                exp3_start = tt_i + 1;
                for (; !tt[tt_i].Code.Equals(")"); tt_i++)
                {
                }
                exp3_end = tt_i;
                // 为了使语句完整,把最后一条语句后右括号变成分号
                tt[tt_i].Code = ";";
                tt[tt_i].Type = "quotation";
                // 对于for语句的检查
                if (!tt[tt_i + 1].Code.Equals("{"))
                {
                    Console.WriteLine("wrong!6 -- 没有左花括号");
                    Environment.Exit(0);
                }
                else
                {
                    // 循环部分
                    start = tt_i + 2;
                    for (; !tt[tt_i].Code.Equals("}"); tt_i++)
                    {
                        end = tt_i;
                    }
                    // System.out.println("suc");
                    // 先执行第一部分
                    FParser.F_Parser(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, exp1_start, exp1_end);
                    try
                    {
                        if (WCheck.W_Check(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref exp2_start, ref exp2_end, ref end) == true)
                        {
                            // 进入循环语句
                            while (WCheck.W_Check(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref exp2_start, ref exp2_end, ref end))
                            {
                                // 调用FParser,进行循环语句的执行
                                FParser.F_Parser(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, start, end);
                                FParser.F_Parser(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, exp3_start, exp3_end);
                            }
                        }
                        else
                        {
                            // 当判断语句为假时直接将全局指针指向do-while语句末尾
                            tt_i = end + 1;
                            return;
                        }
                    }
                    catch (NotDefineVarException e)
                    {
                        Console.WriteLine(e.ToString());
                        Console.Write(e.StackTrace);
                    }
                    catch (NotAVarException e)
                    {
                        Console.WriteLine(e.ToString());
                        Console.Write(e.StackTrace);
                    }
                    // 将全局指针指向do-while语句末尾
                    tt_i = end + 1;
                    return;
                }
            }
        }
    }
Beispiel #4
0
    public static void B_if(ref WordType[] tt, ref int tt_i, ref MyArray[] a, ref int a_i, ref string parseresult, ref Dictionary <string, int> varmap, ref Dictionary <string, string> varmap2, ref Dictionary <string, MyArray> varmap3, ref Dictionary <string, int> markmap, ref bool while_or)
    {
        // if 判断条件开始的地方
        int start = 0;
        // if 判断条件结束的地方
        int end = 0;
        // if 循环体开始的地方
        int start_num = 0;
        // if 循环体结束的地方
        int end_num = 0;
        // else 循环体开始的地方
        int estart_num = 0;
        // else 循环体结束的地方
        int eend_num = 0;
        // 是否有左花括号
        bool hasQH = false;
        // 是否有else语句
        bool hasElse = false;

        if ((!tt[tt_i + 1].Code.Equals("(")))
        {
            parseresult += "语法错误 if语句左括号不存在\r\n";
            throw new WrongGrammarException("语法错误 if语句左括号不存在");
        }
        else
        {
            start = tt_i + 1;
        }

        // 判断if语句是否正确
        for (; !(tt[tt_i].Code.Equals("}")); tt_i++)
        {
            // 当读到左花括号时 判断 前面一个是不是右括号
            // 如果不是就报错并终止程序
            if (tt[tt_i].Code.Equals("{"))
            {
                hasQH = true;
                if ((tt[tt_i - 1].Code.Equals(")")))
                {
                    start_num = tt_i + 1;
                    end       = tt_i - 1;
                }
                else
                {
                    parseresult += "语法错误 if语句左右括号不匹配\r\n";
                    throw new WrongGrammarException("语法错误 if语句左右括号不匹配");
                }
                // 如果没有读取到左花括号而是读取到了右花括号 报错并终止程序
                // 并且如果右花括号前面不是分号 报错并终止程序
                // 在判断结束后,将左花括号是否存在的标志设为错误
                // 以便于进行else语句的判断
            }
            if (tt[tt_i + 1].Code.Equals("}"))
            {
                end_num = tt_i;
                if (hasQH == false || !(tt[tt_i].Code.Equals(";")))
                {
                    parseresult += "语法错误 if语句左右花括号不匹配\r\n";
                    throw new WrongGrammarException("语法错误 if语句左右花括号不匹配");
                }
                else
                {
                    hasQH = false;
                }
            }
        }

        // 判断是否存在else语句
        if (tt[tt_i + 1] != null)
        {
            tt_i += 1;
            if (tt[tt_i].Code.Equals("else"))
            {
                hasElse = true;
                // 判断else语句是否正确
                for (; !(tt[tt_i].Code.Equals("}")); tt_i++)
                {
                    if (tt[tt_i].Code.Equals("{"))
                    {
                        hasQH      = true;
                        estart_num = tt_i + 1;
                    }
                    if (tt[tt_i + 1].Code.Equals("}"))
                    {
                        eend_num = tt_i;
                        if (hasQH == false || !(tt[tt_i].Code.Equals(";")))
                        {
                            parseresult += "语法错误 else语句异常\r\n";
                            throw new WrongGrammarException("语法错误 else语句异常");
                        }
                    }
                }
            }
            else
            {
                hasElse = false;
            }
        }

        // 执行if-else语句
        // 当else语句存在 并且if语句的判断条件错误
        if (hasElse == true && WCheck.W_Check(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref start, ref end, ref end_num) == false)
        {
            // 传入的结尾标志为 else语句的结尾
            Parser_ReNamed.Parser_ReNamed1(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref markmap, estart_num, eend_num, ref while_or);
            return;
            // 当else语句不存在 并且if语句的判断条件为真
        }
        else if (hasElse == false && WCheck.W_Check(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref start, ref end, ref end_num) == true)
        {
            // 传入的结尾标志为 if语句的结尾
            Parser_ReNamed.Parser_ReNamed1(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref markmap, start_num, end_num, ref while_or);
            return;
            // 当else语句不存在 并且if语句的判断条件为真
        }
        else if (hasElse == false && WCheck.W_Check(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref start, ref end, ref end_num) == false)
        {
            // 直接返回
            return;
            // 当else语句存在 并且if语句的判断条件为真
        }
        else if (hasElse == true && WCheck.W_Check(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref start, ref end, ref end_num) == true)
        {
            // 传入的结尾标志为 else语句的结尾
            Parser_ReNamed.Parser_ReNamed1(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref markmap, start_num, end_num, ref while_or);
            return;
        }
    }