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;
    }
    /// <summary>
    /// switch语句的使用
    /// </summary>
    public static void B_switch(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)
    {
        // switch判断条件开始的地方
        int start = 0;
        // switch判断条件结束的地方
        int end = 0;
        // switch 循环体开始的地方
        int start_num = 0;
        // switch 循环体结束的地方
        int end_num = 0;
        // case语句开始的地方
        int cstart_num = 0;
        // case语句结束的地方
        int cend_num = 0;
        //用来存储tt_i的中间取值,回到switch开始的地方
        int  Stemp = 0;
        bool hasQH = false;
        // 是否有左花括号
        bool hasCase = false;
        // 是否有case语句
        bool hasMH = false;

        //是否有冒号

        if ((!tt[tt_i + 1].Code.Equals("(")))
        {
            Console.WriteLine("wrong!173 -- 找不到左括号");
            Environment.Exit(0);
        }
        else
        {
            start = tt_i + 1;
        }

        //判断switch语句的条件是否存在
        //保留当前tt_i的取值
        Stemp = tt_i;
        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
                {
                    Console.WriteLine("wrong!201 -- 请检查switch语句是否正确");
                    Environment.Exit(0);
                }
            }
            // 如果没有读取到左花括号而是读取到了右花括号 报错并终止程序
            // 并且如果右花括号前面不是分号 报错并终止程序
            // 在判断结束后,将左花括号是否存在的标志设为错误
            if (tt[tt_i + 1].Code.Equals("}"))
            {
                end_num = tt_i;
                if (hasQH == false || !(tt[tt_i].Code.Equals(";")))
                {
                    Console.WriteLine("wrong!201 -- 请检查switch语句是否正确");
                    Environment.Exit(0);
                }
                else
                {
                    hasQH = false;
                }
            }
        }

        //回到当初的tt_i取值
        tt_i = Stemp;
        if (tt[tt_i + 1] != null)
        {
            tt_i += 1;
            // 判断是否存在case语句
            if (tt[tt_i].Code.Equals("case"))
            {
                hasCase = true;
                // 判断case语句是否正确
                for (; !(tt[tt_i].Code.Equals("break")); tt_i++)
                {
                    if (tt[tt_i].Code.Equals(":"))
                    {
                        hasMH      = true;
                        cstart_num = tt_i + 1;
                    }
                    //判断单个case语句何时结束
                    if (tt[tt_i + 1].Code.Equals("break"))
                    {
                        cend_num = tt_i;
                        if (!(tt[tt_i].Code.Equals(";")))
                        {
                            Console.WriteLine("wrong!233 -- 请检查case语句是否正确");
                            Environment.Exit(0);
                        }
                    }
                }
            }
            else
            {
                hasCase = false;
            }
        }

        //执行switch语句
        tt_i = Stemp;
        //创建一个整数型数组,用于存储每次出现case时的位置
        int[] case_position = new int[100];
        //case_position数组的下标
        int cposition = 0;

        //循环记录每次出现case的地方
        for (; !tt[tt_i].Code.Equals("}"); tt_i++)
        {
            if (tt[tt_i].Code.Equals("case"))
            {
                case_position[cposition] = tt_i;
                cposition++;
            }
        }
        //调取条件变量的取值
        //如果条件变量是数字型
        if (tt[start].Type.Equals("number"))
        {
            for (int i = 0; i <= cposition; cposition++)
            {
                if (Scheck1(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref start) == tt[case_position[i] + 1].GetHashCode())
                {
                    Parser_ReNamed.Parser_ReNamed1(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref markmap, case_position[i] + 3, case_position[i + 1] - 1, ref while_or);
                }
            }
        }
        else
        {
            //如果条件变量是字符型
            for (int i = 0; i <= cposition; cposition++)
            {
                if (string.ReferenceEquals(Scheck2(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref start), tt[case_position[i] + 2].Code))
                {
                    Parser_ReNamed.Parser_ReNamed1(ref tt, ref tt_i, ref a, ref a_i, ref parseresult, ref varmap, ref varmap2, ref varmap3, ref markmap, case_position[i] + 4, case_position[i + 1] - 1, ref while_or);
                }
            }
        }
    }
Exemple #3
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;
        }
    }