예제 #1
0
        public static 结构体 Parse(char[] chars, 第一层_代码块 代码块)
        {
            StrSpan span = StrUtil.Trim(chars, 代码块.iLeft, 代码块.iRight, Parser._whiteSpaces);

            int struct关键字位置;

            int j = 代码块.大括号块.iLeft - 1;

            while (true)
            {
                struct关键字位置 = StrUtil.FindBackward(chars, span.iLeft, j, "struct");

                if (struct关键字位置 == -1)
                {
                    return(null);
                }

                if (StrUtil.IsOneOf(chars[struct关键字位置 + 6], Parser._whiteSpaces))
                {
                    break;
                }

                j = struct关键字位置 - 1;
            }

            StrSpan span结构体名 = StrUtil.Trim(chars, struct关键字位置 + 6, 代码块.大括号块.iLeft - 1, Parser._whiteSpaces);

            Dictionary <string, 字段声明> dic字段声明 = Parse_字段声明(chars, 代码块.大括号块);

            string 结构体名 = new string(chars, span结构体名.iLeft, span结构体名.iRight - span结构体名.iLeft + 1);

            return(new 结构体(结构体名, dic字段声明, span结构体名.iLeft));
        }
예제 #2
0
        public static 结构体 Parse(char[] chars, 第一层_代码块 代码块)
        {
            StrSpan span = StrUtil.Trim(chars, 代码块.iLeft, 代码块.iRight, Parser._whiteSpaces);

            int struct关键字位置;

            int j = 代码块.大括号块.iLeft - 1;

            while (true)
            {
                struct关键字位置 = StrUtil.FindBackward(chars, span.iLeft, j, "struct");

                if (struct关键字位置 == -1)
                {
                    return(null);
                }

                if (StrUtil.IsOneOf(chars[struct关键字位置 + 6], Parser._whiteSpaces))
                {
                    break;
                }

                j = struct关键字位置 - 1;
            }

            StrSpan span结构体名 = StrUtil.Trim(chars, struct关键字位置 + 6, 代码块.大括号块.iLeft - 1, Parser._whiteSpaces);

            if (span结构体名.isEmpty)
            {
                throw new 语法错误_Exception("缺少 结构体名 。", chars, struct关键字位置 + 6);
            }

            string 结构体名 = new string(chars, span结构体名.iLeft, span结构体名.iRight - span结构体名.iLeft + 1);

            if (!Util.Check_是否_下划线字母数字_且以_下划线字母_开头(结构体名))
            {
                throw new 语法错误_Exception("无效的 结构体名 \"" + 结构体名 + "\",结构体名 应由 下划线字母数字 组成且以 下划线或字母 开头 。", chars, span结构体名.iLeft);
            }

            if (Util.Check_是否关键字(结构体名))
            {
                throw new 语法错误_Exception("无效的 结构体名 \"" + 结构体名 + "\",结构体名 不能和 关键字 相同 。", chars, span结构体名.iLeft);
            }

            //Dictionary<string, 字段声明> dic字段声明 = Parse_字段声明(chars, 代码块.大括号块);
            作用域 字段声明 = Parse_字段声明(chars, 代码块.大括号块);

            return(new 结构体(结构体名, 字段声明, span结构体名.iLeft));
            //return new 结构体(结构体名, dic字段声明, span结构体名.iLeft);
        }
예제 #3
0
        private static 类型 Parse_返回类型(char[] chars, int beginIndex, int endIndex, out int 报错位置)
        {
            报错位置 = -1;

            类型 返回类型 = new 类型("void", 0, null, chars, endIndex);

            //if (beginIndex > endIndex)
            //{
            //    报错位置 = -1;
            //    return 返回类型;
            //}
            StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces);

            if (span.isEmpty)
            {
                //报错位置 = -1;
                return(返回类型);
            }

            char c = chars[endIndex];

            if (!StrUtil.IsOneOf(c, Parser._whiteSpaces和星号))
            {
                报错位置 = endIndex;
                return(返回类型);
            }

            int 空白和星号开始位置的前一个位置 = StrUtil.FindBackwardUntilNot(chars, span.iLeft, span.iRight, Parser._whiteSpaces和星号);

            if (空白和星号开始位置的前一个位置 == -1)
            {
                报错位置 = span.iRight;
                return(返回类型);
            }

            c = chars[空白和星号开始位置的前一个位置];

            if (!Util.Check_是否_下划线字母数字(c))
            {
                报错位置 = span.iRight;
                return(返回类型);
            }

            int 类型开始位置的前一个位置 = Parser.向左寻找_不是_下划线字母数字_的字符(chars, span.iLeft, 空白和星号开始位置的前一个位置);

            int 类型开始位置;

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

            c = chars[类型开始位置];

            if (!(c == '_' || StrUtil.IsLetter(c)))
            {
                报错位置 = span.iRight;
                return(返回类型);
            }

            string type = new string(chars, 类型开始位置, 空白和星号开始位置的前一个位置 - 类型开始位置 + 1);

            int 星号Count = 0;

            for (int i = 空白和星号开始位置的前一个位置 + 1; i <= span.iRight; i++)
            {
                c = chars[i];

                if (c == '*')
                {
                    星号Count++;
                }
            }

            返回类型 = new 类型(type, 星号Count, null, chars, 类型开始位置);

            if (span.iLeft < 类型开始位置)
            {
                报错位置 = 类型开始位置 - 1;
            }

            return(返回类型);
        }
예제 #4
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_分句));
        }
예제 #5
0
        public static 变量声明和初始化 Parse_变量声明(char[] chars, int beginIndex, int endIndex)
        {
            StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces);

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

            char c = chars[span.iLeft];

            if (!(c == '_' || StrUtil.IsLetter(c)))
            {
                return(null);
            }

            int 类型结束的位置的下一个字符位置 = Parser.向右寻找_不是_下划线字母数字_的字符(chars, span.iLeft + 1, span.iRight);

            if (类型结束的位置的下一个字符位置 == -1)
            {
                return(null);
            }

            c = chars[类型结束的位置的下一个字符位置];

            if (!(StrUtil.IsOneOf(c, Parser._whiteSpaces)) || c == '*')
            {
                return(null);
            }

            int 类型后不是空白和星号的位置 = StrUtil.FindForwardUntilNot(chars, 类型结束的位置的下一个字符位置, span.iRight, Parser._whiteSpaces和星号);

            if (类型后不是空白和星号的位置 == -1)
            {
                return(null);
            }

            c = chars[类型后不是空白和星号的位置];

            if (!(c == '_' || StrUtil.IsLetter(c)))
            {
                return(null);
            }

            int 星号Count = 0;

            for (int i = 类型结束的位置的下一个字符位置; i < 类型后不是空白和星号的位置; i++)
            {
                if (chars[i] == '*')
                {
                    星号Count++;
                }
            }

            表达式 表达式 = 表达式_Parser.Parse(chars, 类型后不是空白和星号的位置, span.iRight);

            int 类型字符串长度 = 类型结束的位置的下一个字符位置 - span.iLeft;

            变量声明和初始化 变量声明 = Parse_普通变量声明(chars, 表达式, span.iLeft, 类型字符串长度, 星号Count);

            if (变量声明 != null)
            {
                return(变量声明);
            }

            变量声明 = Parse_数组变量声明(chars, 表达式, span.iLeft, 类型字符串长度, 星号Count);

            if (变量声明 != null)
            {
                return(变量声明);
            }

            return(null);
        }