Example #1
0
        public static for_语句 Parse(char[] chars, int beginIndex, int endIndex, out int 语句结束位置)
        {
            语句结束位置 = -1;

            StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces);

            if (span.isEmpty)
            {
                return(null);
            }

            if (!Parser.开头一段_Equals(chars, span.iLeft, span.iRight, "for"))
            {
                return(null);
            }

            int 左小括号位置 = StrUtil.FindForwardUntilNot(chars, span.iLeft + 3, span.iRight, Parser._whiteSpaces);

            if (左小括号位置 == -1)
            {
                throw new 语法错误_Exception("未结束的 for 语句 。", chars, span.iLeft + 1);
            }

            char c = chars[左小括号位置];

            if (c != '(')
            {
                throw new 语法错误_Exception("for 语句 缺少 左小括号 。", chars, 左小括号位置);
            }

            int 右小括号位置 = Parser.Find_小括号_右(chars, 左小括号位置 + 1, span.iRight);

            if (右小括号位置 == -1)
            {
                throw new 语法错误_Exception("for 语句 缺少 右小括号 。", chars, 左小括号位置 + 1);
            }


            //StrSpan span判断 = StrUtil.Trim(chars, 左小括号位置 + 1, 右小括号位置 - 1, Parser._whiteSpaces);

            //if (span判断.isEmpty)
            //    throw new 语法错误_Exception("for 语句 缺少表达式 。", chars, 左小括号位置);

            //表达式 for_表达式 = 表达式_Parser.Parse(chars, 左小括号位置 + 1, 右小括号位置 - 1);
            表达式[] 小括号表达式s = Parse_小括号(chars, 左小括号位置, 右小括号位置);


            StrSpan span子句 = StrUtil.Trim(chars, 右小括号位置 + 1, span.iRight, Parser._whiteSpaces);

            if (span子句.isEmpty)
            {
                throw new 语法错误_Exception("for 语句 缺少子句 。", chars, 右小括号位置 + 1);
            }

            块作用域 子句 = Parse_子句(chars, span子句.iLeft, span子句.iRight, out 语句结束位置);

            //j = span子句.iRight;

            return(new for_语句(小括号表达式s, 子句));
        }
Example #2
0
 public if_分句(表达式 if_判断, 块作用域 子句)
 {
     this.if_判断 = if_判断;
     this.子句    = 子句;
 }
Example #3
0
        public static while_语句 Parse(char[] chars, int beginIndex, int endIndex, out int 语句结束位置)
        {
            语句结束位置 = -1;

            StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces);

            if (span.isEmpty)
            {
                return(null);
            }

            if (!Parser.开头一段_Equals(chars, span.iLeft, span.iRight, "while"))
            {
                return(null);
            }

            int 左小括号位置 = StrUtil.FindForwardUntilNot(chars, span.iLeft + 5, span.iRight, Parser._whiteSpaces);

            if (左小括号位置 == -1)
            {
                throw new 语法错误_Exception("未结束的 while 语句 。", chars, span.iLeft + 1);
            }

            char c = chars[左小括号位置];

            if (c != '(')
            {
                throw new 语法错误_Exception("while 语句 缺少条件判断 左小括号 。", chars, 左小括号位置);
            }

            int 右小括号位置 = Parser.Find_小括号_右(chars, 左小括号位置 + 1, span.iRight);

            if (右小括号位置 == -1)
            {
                throw new 语法错误_Exception("while 语句 缺少条件判断 右小括号 。", chars, 左小括号位置 + 1);
            }


            StrSpan span判断 = StrUtil.Trim(chars, 左小括号位置 + 1, 右小括号位置 - 1, Parser._whiteSpaces);

            if (span判断.isEmpty)
            {
                throw new 语法错误_Exception("while 语句 缺少判断表达式 。", chars, 左小括号位置);
            }

            表达式 while_条件 = 表达式_Parser.Parse(chars, 左小括号位置 + 1, 右小括号位置 - 1);


            StrSpan span子句 = StrUtil.Trim(chars, 右小括号位置 + 1, span.iRight, Parser._whiteSpaces);

            if (span子句.isEmpty)
            {
                throw new 语法错误_Exception("while 语句 缺少子句 。", chars, 右小括号位置 + 1);
            }

            块作用域 子句 = Parse_子句(chars, span子句.iLeft, span子句.iRight, out 语句结束位置);

            //j = span子句.iRight;

            return(new while_语句(while_条件, 子句));
        }
Example #4
0
        public static 函数 Parse(char[] chars, 第一层_代码块 代码块)
        {
            //int i;

            //int beginIndex = 上一个大括号块 == null ? 0 : 上一个大括号块.iRight + 1;
            //int endIndex = 大括号块.iLeft - 1;

            //i = Parser.从右向左寻找_分号_单引号_双引号(chars, beginIndex, endIndex);

            //if (i != -1)
            //    beginIndex = i + 1;

            //StrSpan span = new StrSpan(beginIndex, 大括号块.iRight);

            //int args_beginIndex;
            //int args_endIndex;

            StrSpan span = StrUtil.Trim(chars, 代码块.iLeft, 代码块.iRight, Parser._whiteSpaces);

            int 右小括号位置 = StrUtil.FindBackwardUntilNot(chars, span.iLeft, 代码块.大括号块.iLeft - 1, Parser._whiteSpaces);

            if (右小括号位置 == -1)
            {
                throw new 语法错误_Exception("函数定义错误: 函数头为空 。", chars, 代码块.大括号块.iLeft - 1);
            }

            if (chars[右小括号位置] != ')')
            {
                throw new 语法错误_Exception("函数定义错误: 函数头 缺少 右小括号 \"(\" 。", chars, 右小括号位置);
            }

            //int 右小括号位置 = StrUtil.FindBackward(chars, 代码块.iLeft, 代码块.大括号块.iLeft - 1, ')');

            //if (右小括号位置 == -1)
            //    throw new InnerCException("函数定义错误: 函数头 缺少 右小括号 \"(\" 。", chars, beginIndex - 1);

            //args_endIndex = i - 1;
            //beginIndex = i - 1;

            int 左小括号位置 = StrUtil.FindBackward(chars, span.iLeft, 右小括号位置 - 1, '(');

            if (左小括号位置 == -1)
            {
                throw new 语法错误_Exception("函数定义错误: 函数头 缺少 左小括号 \"(\" 。", chars, span.iLeft);
            }

            int 函数名结束位置 = StrUtil.FindBackwardUntilNot(chars, span.iLeft, 左小括号位置 - 1, Parser._whiteSpaces);

            if (!Util.Check_是否_下划线字母数字(chars[函数名结束位置]))
            {
                throw new 语法错误_Exception("函数定义错误: 缺少 函数名 。", chars, 函数名结束位置);
            }

            int 函数名开始位置的前一个位置 = Parser.向左寻找_不是_下划线字母数字_的字符(chars, span.iLeft, 函数名结束位置);



            int 函数名开始位置;

            if (函数名开始位置的前一个位置 == -1)
            {
                函数名开始位置 = span.iLeft;
            }
            else
            {
                函数名开始位置 = 函数名开始位置的前一个位置 + 1;
            };

            char c = chars[函数名开始位置];

            string 函数名 = new string(chars, 函数名开始位置, 函数名结束位置 - 函数名开始位置 + 1);

            if (!(c == '_' || StrUtil.IsLetter(c)))
            {
                throw new 语法错误_Exception("无效的 函数名 \"" + 函数名 + "\",函数名 应由 下划线字母数字 组成且以 下划线或字母 开头 。", chars, 函数名开始位置);
            }

            if (Util.Check_是否关键字(函数名))
            {
                throw new 语法错误_Exception("无效的 函数名 \"" + 函数名 + "\",函数名 不能和 关键字 相同 。", chars, 函数名开始位置);
            }

            //变量声明 返回类型;

            //if (函数名开始位置 == span.iLeft)
            //{
            //    return Parse(chars, -1, -1, span.iLeft, 函数名结束位置, 左小括号位置, 右小括号位置, 代码块.大括号块.iLeft, 代码块.大括号块.iRight);
            //}


            //c = chars[函数名开始位置 - 1];

            //if (!StrUtil.IsOneOf(c, Parser._whiteSpaces和星号))
            //    throw new InnerCException("未结束的语句,可能缺少分号 \";\" 。", chars, 函数名开始位置 - 1);

            int 报错位置;

            类型 返回类型 = Parse_返回类型(chars, span.iLeft, 函数名开始位置 - 1, out 报错位置);

            if (报错位置 != -1)
            {
                throw new 语法错误_Exception("未结束的语句,可能缺少分号 \";\" 。", chars, 报错位置);
            }


            作用域  形参列表 = Parse_形参列表(chars, 左小括号位置, 右小括号位置);
            块作用域 函数体  = Parse_函数体(chars, 代码块.大括号块);



            return(new 函数(返回类型, 函数名, 形参列表, 函数体, 函数名开始位置));

            //int 空白和星号开始的位置的前一个位置 = StrUtil.FindBackwardUntilNot(chars, span.iLeft, 函数名开始位置 - 1, Parser._whiteSpaces和星号);

            //int 空白和星号开始位置;

            //if (空白和星号开始的位置的前一个位置 == -1)
            //{
            //    K
            //}


            //args_beginIndex = i + 1;
            //beginIndex = i - 1;

            //StrSpan 函数名和返回值 = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces);

            //if (函数名和返回值.isEmpty)
            //    throw new InnerCException("函数定义错误: 缺少 函数名 。", chars, beginIndex - 1);

            //StrSpan[] spans = Parser.从右向左寻找第一个空白字符以此Split将得到的两个字符串Trim返回(chars, 函数名和返回值.iLeft, 函数名和返回值.iRight);

            //StrSpan 参数列表 = new StrSpan(args_beginIndex, args_endIndex);
            //StrSpan 函数体 = StrUtil.Trim(chars, 大括号块.iLeft + 1, 大括号块.iRight - 1, Parser._whiteSpaces);

            //return new 函数块(span, spans[0], spans[1], 参数列表, 函数体);
        }
Example #5
0
 public if_语句(List <if_分句> if_分句_和_else_if_分句_List, 块作用域 最后结尾的_else_分句)
 {
     this.if_分句_和_else_if_分句_List = if_分句_和_else_if_分句_List;
     this.最后结尾的_else_分句           = 最后结尾的_else_分句;
 }
Example #6
0
        private static if_分句 Parse_if_分句(char[] chars, int beginIndex, int endIndex, out int j)
        {
            j = -1;

            StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces);

            if (span.isEmpty)
            {
                return(null);
            }

            bool b = Parser.开头一段_Equals(chars, span.iLeft, span.iRight, "if");

            if (!b)
            {
                return(null);
            }

            int 左小括号位置 = StrUtil.FindForwardUntilNot(chars, span.iLeft + 2, span.iRight, Parser._whiteSpaces);

            if (左小括号位置 == -1)
            {
                throw new InnerCException("未结束的 if 语句 。", chars, span.iLeft + 1);
            }

            char c = chars[左小括号位置];

            if (c != '(')
            {
                throw new InnerCException("if 语句 缺少条件判断 左小括号 。", chars, 左小括号位置);
            }

            int 右小括号位置 = Parser.Find_小括号_右(chars, 左小括号位置 + 1, span.iRight);

            if (右小括号位置 == -1)
            {
                throw new InnerCException("if 语句 缺少条件判断 右小括号 。", chars, 左小括号位置 + 1);
            }


            StrSpan span判断 = StrUtil.Trim(chars, 左小括号位置 + 1, 右小括号位置 - 1, Parser._whiteSpaces);

            if (span判断.isEmpty)
            {
                throw new InnerCException("if 语句 缺少判断表达式 。", chars, 左小括号位置);
            }

            表达式 if_判断 = 表达式_Parser.Parse(chars, 左小括号位置 + 1, 右小括号位置 - 1);


            StrSpan span子句 = StrUtil.Trim(chars, 右小括号位置 + 1, span.iRight, Parser._whiteSpaces);

            if (span子句.isEmpty)
            {
                throw new InnerCException("if 语句 缺少子句 。", chars, 右小括号位置 + 1);
            }

            块作用域 子句 = Parse_子句(chars, span子句.iLeft, span子句.iRight, out j);

            //j = span子句.iRight;

            return(new if_分句(if_判断, 子句));
        }
Example #7
0
        public static if_语句 Parse(char[] chars, int beginIndex, int endIndex, out int j)
        {
            j = -1;

            StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces);

            if (span.isEmpty)
            {
                return(null);
            }

            //bool b = Parser.开头一段_Equals(chars, span.iLeft, span.iRight, "if");

            //if (!b)
            //    return null;

            //if ()

            if_分句 if_分句 = Parse_if_分句(chars, span.iLeft, span.iRight, out j);

            if (if_分句 == null)
            {
                return(null);
            }

            List <if_分句> if_分句_和_else_if_分句_List = new List <if_分句>();
            块作用域         最后结尾的_else_分句           = null;

            if_分句_和_else_if_分句_List.Add(if_分句);

            while (true)
            {
                //b = Parser.开头一段_Equals(chars, span.iLeft, span.iRight, "if");

                //if (!b)
                //    break;

                //if_分句 if_分句 = Parse_if_分句(chars, span.iLeft, span.iRight, out j);

                //if_分句_和_else_if_分句_List.Add(if_分句);

                span = StrUtil.Trim(chars, j + 1, span.iRight, Parser._whiteSpaces);

                if (span.isEmpty)
                {
                    break;
                }

                bool b = Parser.开头一段_Equals(chars, span.iLeft, span.iRight, "else");

                if (!b)
                {
                    break;
                }

                int else后的位置 = span.iLeft + 4;

                if (else后的位置 > span.iRight)
                {
                    break;
                }

                char c = chars[span.iLeft + 4];

                if (!StrUtil.IsOneOf(c, Parser._whiteSpaces))
                {
                    break;
                }

                //int p = StrUtil.FindForwardUntilNot(chars, span.iLeft + 4, span.iRight, Parser._whiteSpaces);

                //if (p == -1)
                //    break;

                int else结束位置 = span.iLeft + 3;

                span = StrUtil.Trim(chars, else后的位置, span.iRight, Parser._whiteSpaces);

                if (span.isEmpty)
                {
                    throw new InnerCException("\"else\" 后缺少子句 。", chars, else结束位置);
                }



                if_分句 = Parse_if_分句(chars, span.iLeft, span.iRight, out j);

                if (if_分句 != null)
                {
                    if_分句_和_else_if_分句_List.Add(if_分句);

                    continue;
                }
                //b = Parser.开头一段_Equals(chars, span.iLeft, span.iRight, "if");

                //if (b)
                //{

                //    if_分句 = Parse_if_分句(chars, span.iLeft, span.iRight, out j);

                //    if_分句_和_else_if_分句_List.Add(if_分句);

                //    continue;
                //}


                最后结尾的_else_分句 = Parse_子句(chars, span.iLeft, span.iRight, out j);

                //if (最后结尾的_else_分句.list语句.Count == 0)
                //    throw new InnerCException("无效的语句 。", chars, span.iLeft);

                break;
            }

            //if (1 == 1)
            //{

            //}
            //else
            //{

            //}

            return(new if_语句(if_分句_和_else_if_分句_List, 最后结尾的_else_分句));
        }
Example #8
0
        public for_语句(表达式[] 小括号表达式s, 块作用域 子句)
        {
            this.小括号表达式s = 小括号表达式s;

            this.子句 = 子句;
        }
Example #9
0
        public while_语句(表达式 while_条件, 块作用域 子句)
        {
            this.while_条件 = while_条件;

            this.子句 = 子句;
        }
Example #10
0
        public static 块作用域 Parse_块作用域(char[] chars, int beginIndex, int endIndex, ParseOptions option, out int j)
        {
            j = -1;

            块作用域 块 = new 块作用域();

            StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces);

            if (span.isEmpty)
            {
                return(块);
            }

            //int j;

            //List<语句> list语句 = new List<语句>();


            for (int i = span.iLeft; ; i = j + 1)
            {
                span = StrUtil.Trim(chars, i, span.iRight, Parser._whiteSpaces);

                if (span.isEmpty)
                {
                    break;
                }
                //if (Check_变量类型_KeyWord(chars, i, 代码段.iRight, "int"))
                //{
                //    语句 = Parse_变量声明和赋值_语句(chars, i, 代码段.iRight, "int", out j, list变量声明);
                //}
                //else if (Check_变量类型_KeyWord(chars, i, 代码段.iRight, "float"))
                //{
                //    语句 = Parse_变量声明和赋值_语句(chars, i, 代码段.iRight, "float", out j, list变量声明);
                //}
                //else if (Check_变量类型_KeyWord(chars, i, 代码段.iRight, "double"))
                //{
                //    语句 = Parse_变量声明和赋值_语句(chars, i, 代码段.iRight, "double", out j, list变量声明);
                //}
                //else if (Check_变量类型_KeyWord(chars, i, 代码段.iRight, "char"))
                //{
                //    语句 = Parse_变量声明和赋值_语句(chars, i, 代码段.iRight, "char", out j, list变量声明);
                //}
                //if (Check_if_while_KeyWord(chars, i, 代码段.iRight, "if"))
                //{
                //    语句 = Parse_if_语句(chars, i, 代码段.iRight, out j, list变量声明);
                //}
                //else if (Check_if_while_KeyWord(chars, i, 代码段.iRight, "while"))
                //{
                //    语句 = Parse_while_语句(chars, i, 代码段.iRight, out j, list变量声明);
                //}
                //else if (Check_return_KeyWord(chars, i, 代码段.iRight))
                //{
                //    语句 = Parse_return_语句(chars, i, 代码段.iRight, out j, list变量声明);
                //}
                //else if (Check_变量声明(chars, i, 代码段.iRight, out 变量类型))
                //{
                //    语句 = Parse_变量声明和赋值_语句(chars, i, 代码段.iRight, 变量类型, out j, list变量声明);
                //}
                //else
                //{
                //    语句 = Parse_表达式_语句(chars, i, 代码段.iRight, out j, list变量声明);
                //}

                if_语句 if_语句 = if_语句_Parser.Parse(chars, span.iLeft, span.iRight, out j);

                if (if_语句 != null)
                {
                    if_语句.Set_作用域(块);

                    块.list语句.Add(if_语句);

                    continue;
                }

                while_语句 while_语句 = while_语句_Parser.Parse(chars, span.iLeft, span.iRight, out j);

                if (while_语句 != null)
                {
                    while_语句.Set_作用域(块);

                    块.list语句.Add(while_语句);

                    continue;
                }

                break_语句 break_语句 = Parse_break_语句(chars, span.iLeft, span.iRight, out j);

                if (break_语句 != null)
                {
                    break_语句.Set_作用域(块);

                    块.list语句.Add(break_语句);

                    continue;
                }

                return_语句 return_语句 = Parse_return_语句(chars, span.iLeft, span.iRight, out j);

                if (return_语句 != null)
                {
                    return_语句.Set_作用域(块);

                    块.list语句.Add(return_语句);

                    continue;
                }


                变量声明和初始化语句 变量声明语句 = Parse_变量声明和初始化_语句(chars, span.iLeft, span.iRight, out j);

                if (变量声明语句 != null)
                {
                    变量声明和初始化 变量声明 = 变量声明语句.变量声明;

                    if (块.dic变量声明.ContainsKey(变量声明.name))
                    {
                        throw new InnerCException("在当前作用域范围内已定义了名为 \"" + 变量声明.name + "\" 的变量 。", chars, 变量声明.变量位置_iLeft);
                    }

                    变量声明语句.Set_作用域(块);

                    块.dic变量声明.Add(变量声明.name, 变量声明);

                    块.list语句.Add(变量声明语句);

                    continue;
                }


                表达式语句 表达式语句 = Parse_表达式语句(chars, span.iLeft, span.iRight, out j);

                if (表达式语句 != null)
                {
                    表达式语句.Set_作用域(块);

                    块.list语句.Add(表达式语句);
                }



                if (option == ParseOptions.Parse_第一个语句)
                {
                    break;
                }
            }

            return(块);
        }