Exemplo n.º 1
0
        public static StrSpan[] 从右向左寻找第一个空白字符以此Split将得到的两个字符串Trim返回(char[] chars, int beginIndex, int endIndex)
        {
            int i = StrUtil.FindBackward(chars, beginIndex, endIndex, Parser._whiteSpaces);

            StrSpan 返回值类型;
            StrSpan 函数名;


            if (i == -1)
            {
                //返回值类型 = StrSpan.Empty;
                返回值类型 = new StrSpan(beginIndex, beginIndex, true);
                函数名   = new StrSpan(beginIndex, endIndex);
            }
            else
            {
                返回值类型 = StrUtil.Trim(chars, beginIndex, i - 1, Parser._whiteSpaces);
                函数名   = new StrSpan(i + 1, endIndex);
            }


            StrSpan[] spans = new StrSpan[2];

            spans[0] = 返回值类型;
            spans[1] = 函数名;

            return(spans);
        }
Exemplo n.º 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);

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

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

            return(new 结构体(结构体名, dic字段声明, span结构体名.iLeft));
        }
Exemplo n.º 3
0
 public 第一层_代码块(int iLeft, int iRight, 第一层_代码块_Type type, StrSpan 大括号块)
 {
     this.iLeft  = iLeft;
     this.iRight = iRight;
     this.type   = type;
     this.大括号块   = 大括号块;
 }
Exemplo n.º 4
0
        //private static List<常量> Get_list_数组维度_Length(数组元素 数组元素, char[] chars)
        //{
        //    List<常量> list_数组维度_Length = new List<常量>();

        //    for (int i = 0; i < 数组元素.list下标.Count; i++)
        //    {
        //        //表达式 维度长度 = 数组元素.list下标[i];

        //        //常量 维度Length = 维度长度 as 常量;

        //        //if (维度Length == null)
        //        //    throw new InnerCException("数组维度长度声明应是常量 。", chars, 维度长度.参考位置_iLeft);

        //        //list_数组维度_Length.Add(维度Length);
        //    }

        //    return list_数组维度_Length;
        //}

        //private static bool Check_变量类型_KeyWord(char[] chars, int beginIndex, int endIndex, string str)
        //{
        //    bool b = 开头一段_Equals(chars, beginIndex, endIndex, str);

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

        //    char c = chars[str.Length + 1];

        //    if (StrUtil.IsOneOf(c, Parser._whiteSpaces) || c == '*')
        //        return true;

        //    return false;
        //}

        //private static bool Check_变量声明(char[] chars, int beginIndex, int endIndex, out string type)
        //{

        //    char c = chars[beginIndex];

        //    if (!(StrUtil.isLetter(c) || c == '_'))
        //    {
        //        type = null;
        //        return false;
        //    }


        //    int 空白和星号第一次出现的位置 = -1;

        //    for (int i=beginIndex + 1; i<=endIndex; i++)
        //    {
        //        c = chars[i];

        //        if (StrUtil.IsOneOf(c, _whiteSpaces和星号))
        //        {
        //            空白和星号第一次出现的位置 = i;

        //            break;
        //        }

        //        if (!(StrUtil.isLetter(c) || c == '_'))
        //        {
        //            type = null;
        //            return false;
        //        }
        //    }

        //    if (空白和星号第一次出现的位置 == -1)
        //    {
        //        type = null;
        //        return false;
        //    }

        //    for (int i = 空白和星号第一次出现的位置 + 1; i<=endIndex; i++)
        //    {
        //        c = chars[i];

        //        if (StrUtil.IsOneOf(c, _whiteSpaces和星号))
        //        {
        //            continue;
        //        }

        //        if (StrUtil.isLetter(c) || c == '_')
        //        {
        //            type = new string(chars, beginIndex, 空白和星号第一次出现的位置 - beginIndex);
        //            return true;
        //        }
        //        else
        //        {
        //            type = null;
        //            return false;
        //        }
        //    }

        //    type = null;
        //    return false;

        //}

        //private static bool 开头一段_Equals(char[] chars, int beginIndex, int endIndex, string str)
        //{
        //    if (endIndex - beginIndex >= str.Length)
        //        return false;

        //    int j;

        //    for (int i=0; i<str.Length; i++)
        //    {
        //        j = beginIndex + i;

        //        if (chars[j] != str[i])
        //            return false;
        //    }

        //    return true;
        //}

        //private static 语句 Parse_变量声明和赋值_语句(char[] chars, int beginIndex, int endIndex, string type, out int j, List<变量声明语句> list变量声明)
        //{
        //    int i = StrUtil.FindForwardUntilNot(chars, beginIndex + type.Length + 1, endIndex, _whiteSpaces和星号);

        //    if (i == -1)
        //        throw new InnerCException("未正确结束的 变量声明 语句 。", chars, beginIndex);

        //    StrSpan span = StrUtil.Trim(chars, beginIndex + type.Length, i - 1, Parser._whiteSpaces);

        //    int 星号Count = 0;

        //    for (int p = span.iLeft; p<= span.iRight; p++)
        //    {
        //        星号Count++;
        //    }

        //    j = Parser.Find_单引号对双引号对以外的内容(chars, i, endIndex, ';');

        //    if (j == -1)
        //        throw new InnerCException("未正确结束的 变量声明 语句 。", chars, i);

        //    int q = Parser.Find_单引号对双引号对以外的内容(chars, i, j - 1, '[');

        //    if (q == -1)
        //    {
        //        return Parse_普通变量声明(chars, i, j - 1, type, 星号Count, list变量声明);
        //    }
        //    else
        //    {
        //        return Parse_数组声明(chars, i, j - 1, q, type, 星号Count, list变量声明);
        //    }



        //}

        //private static 语句 Parse_普通变量声明(char[] chars, int beginIndex, int endIndex, string type, int 星号Count, List<变量声明语句>list变量声明)
        //{
        //    StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces);

        //    if (span.isEmpty)
        //        throw new InnerCException("未结束的变量声明语句 。", chars, beginIndex);

        //    表达式 express = C_Parser_3.Parse_表达式(chars, span.iLeft, span.iRight);

        //    变量 变量 = express as 变量;

        //    if (变量 != null)
        //    {
        //        list变量声明.Add(new 变量声明语句(type, 变量.name, 星号Count, null));
        //        return null;
        //    }

        //    赋值 赋值 = express as 赋值;

        //    if (赋值 != null)
        //    {
        //        list变量声明.Add(new 变量声明语句(type, 赋值.变量.name));
        //        return new 赋值语句(赋值);
        //    }

        //    throw new InnerCException("变量声明语句 的 类型 后应是 变量名 或 变量初始化 。", chars, span.iLeft);


        //}

        //private static 语句 Parse_数组声明(char[] chars, int beginIndex, int endIndex, int 第一个中括号出现位置, string type, int 星号Count, List<变量声明语句> list变量声明)
        //{
        //    StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces);

        //    if (span.isEmpty)
        //        throw new InnerCException("未结束的数组声明语句 。", chars, beginIndex);

        //    表达式 express = C_Parser_3.Parse_表达式(chars, span.iLeft, span.iRight);

        //    常量_变量_字段_指针字段_数组元素_函数调用_函数指针调用_调用链 express2 = express as 常量_变量_字段_指针字段_数组元素_函数调用_函数指针调用_调用链;

        //    变量声明语句 数组声明;

        //    if (express2 != null)
        //    {
        //        数组声明 = Get_数组声明(type, express2, 星号Count, chars, 第一个中括号出现位置);
        //        //list变量声明.Add(new 变量声明语句(type, 变量.name, 星号Count, null));
        //        list变量声明.Add(数组声明);
        //        return null;
        //    }

        //    赋值 赋值 = express as 赋值;

        //    if (赋值 == null)
        //        throw new InnerCException("无效的数组声明语句 。", chars, 第一个中括号出现位置);

        //    express2 = 赋值.等号左边的表达式 as 常量_变量_字段_指针字段_数组元素_函数调用_函数指针调用_调用链;

        //    if (express2 == null)
        //        throw new InnerCException("无效的数组声明语句 。", chars, 第一个中括号出现位置);

        //    数组声明 = Get_数组声明(type, express2, 星号Count, chars, 第一个中括号出现位置);

        //    list变量声明.Add(数组声明);

        //    return new 赋值语句(new 赋值(new 变量(数组声明.name), 赋值.值));

        //}

        //private static 变量声明语句 Get_数组声明(string type, 常量_变量_字段_指针字段_数组元素_函数调用_函数指针调用_调用链 express, int 星号Count, char[] chars, int 第一个中括号出现位置)
        //{
        //    if (express.list.Count < 2)
        //        throw new InnerCException("无效的数组声明语句 。", chars, 第一个中括号出现位置);

        //    变量 变量 = express.list[0] as 变量;

        //    if (变量 == null)
        //        throw new InnerCException("无效的数组声明语句 。", chars, 第一个中括号出现位置);

        //    数组元素 数组元素 = express.list[1] as 数组元素;

        //    if (数组元素 == null)
        //        throw new InnerCException("无效的数组声明语句 。", chars, 第一个中括号出现位置);

        //    List<常量> list_维度_Length = new List<常量>();

        //    for (int i = 0; i < 数组元素.listIndex.Count; i++)
        //    {
        //        常量 常量 = 数组元素.listIndex[i] as 常量;

        //        if (常量 == null)
        //             throw new InnerCException("数组声明 长度 应是 常量 。", chars, 第一个中括号出现位置);

        //        if (常量.type != "int")
        //            throw new InnerCException("数组声明 长度 应是 正整数 常量 。", chars, 第一个中括号出现位置);

        //        list_维度_Length.Add(常量);
        //    }

        //    return new 变量声明语句(type, 变量.name, 星号Count, list_维度_Length);
        //}

        public static 表达式语句 Parse_表达式语句(char[] chars, int beginIndex, int endIndex, out int 语句结束位置)
        {
            语句结束位置 = -1;

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

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

            语句结束位置 = Parser.Find_单引号对双引号对以外的内容(chars, beginIndex, endIndex, ';');

            if (语句结束位置 == -1)
            {
                throw new InnerCException("未结束的语句,缺少分号 \";\" 。", chars, span.iRight);
            }

            span = StrUtil.Trim(chars, span.iLeft, span.iRight, Parser._whiteSpaces);

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

            表达式 表达式 = 表达式_Parser.Parse(chars, span.iLeft, 语句结束位置 - 1);

            return(new 表达式语句(表达式));
        }
Exemplo n.º 5
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, 子句));
        }
Exemplo n.º 6
0
        private static 作用域 Parse_形参列表(char[] chars, int 左小括号位置, int 右小括号位置)
        {
            作用域 形参列表 = new 作用域();

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

            if (span.isEmpty)
            {
                return(形参列表);
            }

            List <StrSpan> list = Parser.在_单引号双引号_以外_Split(chars, span.iLeft, span.iRight, ',');

            StrSpan span形参;

            for (int i = 0; i < list.Count; i++)
            {
                span形参 = list[i];

                形参 形参 = Parse_形参(chars, span形参);

                形参列表.Add_变量定义(形参, chars);
                //if (形参列表.dic变量声明.ContainsKey(形参.name))
                //    throw new 语法错误_Exception("参数名 \"" + 形参.name + "\" 重复 。", chars, 形参.变量名位置);

                //形参列表.dic变量声明.Add(形参.name, 形参);
            }

            return(形参列表);
        }
Exemplo n.º 7
0
        private static Dictionary <string, 字段声明> Parse_字段声明(char[] chars, StrSpan 大括号块)
        {
            Dictionary <string, 字段声明> dic字段声明 = new Dictionary <string, 字段声明>();


            StrSpan span = StrUtil.Trim(chars, 大括号块.iLeft + 1, 大括号块.iRight - 1, Parser._whiteSpaces);

            if (span.isEmpty)
            {
                return(dic字段声明);
            }

            List <StrSpan> list = Parser.在_单引号双引号_以外_Split(chars, span.iLeft, span.iRight, ',');

            StrSpan span字段;

            for (int i = 0; i < list.Count; i++)
            {
                span字段 = list[i];

                字段声明 字段 = Parse_字段(chars, span字段);

                if (dic字段声明.ContainsKey(字段.name))
                {
                    throw new InnerCException("字段名 \"" + 字段.name + "\" 重复 。", chars, 字段.变量位置_iLeft);
                }

                dic字段声明.Add(字段.name, 字段);
            }

            return(dic字段声明);
        }
Exemplo n.º 8
0
        private static 变量声明和初始化语句 Parse_变量声明和初始化_语句(char[] chars, int beginIndex, int endIndex, out int 语句结束位置)
        {
            语句结束位置 = -1;

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

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

            语句结束位置 = Parser.Find_单引号对双引号对以外的内容(chars, span.iLeft, span.iRight, ';');

            if (语句结束位置 == -1)
            {
                return(null);
            }

            变量声明和初始化 变量声明 = Parse_变量声明(chars, span.iLeft, 语句结束位置 - 1);

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

            return(new 变量声明和初始化语句(变量声明));
            //变量 变量 = 表达式 as 变量;

            //if (变量 != null)
            //{
            //    return new 变量声明语句(new string(chars, beginIndex, 类型结束的位置的下一个字符位置 - beginIndex), 变量.name, 星号Count, null);
            //}

            //赋值 赋值 = 表达式 as 赋值;

            //if (赋值 != null)
            //{
            //    变量 = 赋值.等号左边的表达式 as 变量;

            //    if (变量 != null)
            //    {
            //        赋值语句 = new 赋值语句(赋值);
            //        return new 变量声明语句(new string(chars, beginIndex, 类型结束的位置的下一个字符位置 - beginIndex), 变量.name, 星号Count, null);
            //    }

            //    数组元素 数组元素 = 赋值.等号左边的表达式 as 数组元素;

            //    if (数组元素 != null)
            //    {
            //        变量 = 数组元素.左边的表达式 as 变量;

            //        if (变量 != null)
            //        赋值语句 = new 赋值语句(new 赋值());
            //        return new 变量声明语句(new string(chars, beginIndex, 类型结束的位置的下一个字符位置 - beginIndex), 变量.name, 星号Count, null);
            //    }
            //}
        }
Exemplo n.º 9
0
        //private static 变量声明 Parse_形参(char[] chars, StrSpan span)
        //{
        //    if (span.isEmpty)
        //        throw new InnerCException("缺少参数定义 。", chars, span.iLeft);

        //    //span = StrUtil.Trim(chars, span.iLeft, span.iRight, Parser._whiteSpaces);

        //    //if (span.isEmpty)
        //    //    throw new InnerCException("缺少参数定义 。", chars, span.iLeft);

        //    char c = chars[span.iLeft];

        //    if (!(c == '_' || StrUtil.IsLetter(c)))
        //        throw new InnerCException("错误的参数定义 。", chars, span.iLeft);

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

        //    if (类型结束位置的下一个位置 == -1)
        //        throw new InnerCException("错误的参数定义 。", chars, span.iLeft);

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

        //    if (空白和星号结束位置的下一个位置 == -1)
        //        throw new InnerCException("错误的参数定义 。", chars, span.iLeft);

        //    StrSpan span参数名 = StrUtil.Trim(chars, 空白和星号结束位置的下一个位置, span.iRight, Parser._whiteSpaces);

        //    if (span参数名.isEmpty)
        //        throw new InnerCException("错误的参数定义 。", chars, span.iLeft);

        //    if (!Util.Check_是否_下划线字母数字_且以_下划线字母_开头(chars, span参数名.iLeft, span参数名.iRight))
        //        throw new InnerCException("错误的参数定义 。", chars, span.iLeft);

        //    string type = new string(chars, span.iLeft, 类型结束位置的下一个位置 - span.iLeft);

        //    int 星号Count = 0;

        //    for (int i = 类型结束位置的下一个位置; i <= 空白和星号结束位置的下一个位置 - 1; i++)
        //    {
        //        c = chars[i];

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

        //    string 参数名 = new string(chars, 空白和星号结束位置的下一个位置, span.iRight - 空白和星号结束位置的下一个位置 + 1);

        //    return new 变量声明(new 类型(type, 星号Count, null), 参数名, -1);
        //}

        private static 块作用域 Parse_函数体(char[] chars, StrSpan 大括号块)
        {
            //StrSpan span = StrUtil.Trim(chars, 大括号块.iLeft + 1, 大括号块.iRight - 1, Parser._whiteSpaces);

            //if (span.isEmpty)
            //    return new 块作用域();

            //return C_Parser.Parse_块作用域(chars, span.iLeft, span.iRight);

            return(语句_Parser.Parse_块作用域(chars, 大括号块.iLeft + 1, 大括号块.iRight - 1));
        }
Exemplo n.º 10
0
        public static return_语句 Parse_return_语句(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, "return"))
            {
                return(null);
            }

            int return后的位置 = span.iLeft + 6;

            if (return后的位置 > span.iRight)
            {
                throw new InnerCException("未结束的 return 语句 。", chars, span.iLeft);
            }

            char c = chars[return后的位置];

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

            语句结束位置 = Parser.Find_单引号对双引号对以外的内容(chars, return后的位置, span.iRight, ';');

            if (语句结束位置 == -1)
            {
                throw new InnerCException("未结束的 return 语句,缺少分号 \";\" 。", chars, span.iLeft);
            }

            StrSpan span返回值 = StrUtil.Trim(chars, return后的位置, 语句结束位置 - 1, Parser._whiteSpaces);

            表达式 返回值;

            if (span返回值.isEmpty)
            {
                返回值 = null;
            }
            else
            {
                返回值 = 表达式_Parser.Parse(chars, span返回值.iLeft, span返回值.iRight);
            }

            return(new return_语句(返回值));
        }
Exemplo n.º 11
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);
        }
        private static 表达式 Parse_指针字段(char[] chars, List <表达式段> list)
        {
            表达式段 段 = list[list.Count - 1];

            if (段.type != 表达式段_Type.普通段)
            {
                return(null);
            }

            string 点或箭头;

            int 点或箭头的位置 = 寻找最右边的点或箭头的位置(chars, 段.iLeft, 段.iRight, out 点或箭头);

            if (点或箭头的位置 == -1)
            {
                return(null);
            }

            if (点或箭头 != "->")
            {
                return(null);
            }

            StrSpan 右边的部分 = StrUtil.Trim(chars, 点或箭头的位置 + 2, 段.iRight, Parser._whiteSpaces);

            if (右边的部分.isEmpty)
            {
                throw new 语法错误_Exception("\".\" 后面缺少 字段名 。", chars, 点或箭头的位置);
            }

            StrSpan 左边的部分 = StrUtil.Trim(chars, 段.iLeft, 点或箭头的位置 - 1, Parser._whiteSpaces);

            if (左边的部分.isEmpty)
            {
                throw new 语法错误_Exception("\".\" 前面缺少 变量 或者 表达式 。", chars, 点或箭头的位置);
            }

            表达式 express = Parse(chars, 左边的部分.iLeft, 左边的部分.iRight);

            //if (!(express is 变量 || express is 指针取值))
            //    throw new InnerCException("\".\" 前面应该是 变量 或者 指针取值 表达式 。", chars, 左边的部分.iRight);

            if (!Util.Check_是否_下划线字母数字_且以_下划线字母_开头(chars, 右边的部分.iLeft, 右边的部分.iRight))
            {
                throw new 语法错误_Exception("无效的字段名 。", chars, 右边的部分.iLeft);
            }

            return(new 指针字段(express, new string(chars, 右边的部分.iLeft, 右边的部分.iRight - 右边的部分.iLeft + 1), chars, 点或箭头的位置));
        }
Exemplo n.º 13
0
        public static continue_语句 Parse_continue_语句(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, "continue"))
            {
                return(null);
            }

            int break后的位置 = span.iLeft + 5;

            if (break后的位置 > span.iRight)
            {
                throw new InnerCException("未结束的 continue 语句 。", chars, span.iLeft);
            }

            char c = chars[break后的位置];

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

            int break后不是空白的位置 = StrUtil.FindForwardUntilNot(chars, break后的位置, span.iRight, Parser._whiteSpaces);

            if (break后不是空白的位置 == -1)
            {
                throw new InnerCException("未结束的 continue 语句,缺少分号 \";\" 。", chars, span.iLeft);
            }

            c = chars[break后不是空白的位置];

            if (c != ';')
            {
                throw new InnerCException("无效的 continue 语句,无效的字符 \"" + c + "\" 。", chars, break后不是空白的位置);
            }

            语句结束位置 = break后不是空白的位置;

            return(new continue_语句());
        }
        private static 表达式 Parse_常量(char[] chars, List <表达式段> list)
        {
            if (list.Count > 1)
            {
                return(null);
            }

            表达式段 段 = list[0];

            if (段.type == 表达式段_Type.单引号段)
            {
                return(new 常量(常量_Type._char, new string(chars, 段.iLeft, 段.iRight - 段.iLeft + 1), chars, 段.iLeft));
            }

            if (段.type == 表达式段_Type.双引号段)
            {
                return(new 常量(常量_Type._String, new string(chars, 段.iLeft, 段.iRight - 段.iLeft + 1), chars, 段.iLeft));
            }

            if (段.type == 表达式段_Type.中括号段)
            {
                return(new 常量(常量_Type.中括号数组常量, new string(chars, 段.iLeft, 段.iRight - 段.iLeft + 1), chars, 段.iLeft));
            }

            if (段.type != 表达式段_Type.普通段)
            {
                return(null);
            }

            StrSpan span = StrUtil.Trim(chars, 段.iLeft, 段.iRight, Parser._whiteSpaces);

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

            if (Util.Check_int(chars, span.iLeft, span.iRight))
            {
                return(new 常量(常量_Type._int, new string(chars, span.iLeft, span.iRight - span.iLeft + 1), chars, span.iLeft));
            }

            if (Util.Check_float(chars, span.iLeft, span.iRight))
            {
                return(new 常量(常量_Type._float, new string(chars, span.iLeft, span.iRight - span.iLeft + 1), chars, span.iLeft));
            }

            return(null);
        }
Exemplo n.º 15
0
        public static List <StrSpan> Split(char[] str, int beginIndex, int endIndex, char c)
        {
            List <StrSpan> tokenList = new List <StrSpan>();

            StrSpan span;

            int iLeft = beginIndex;

            //int iRight;

            for (int i = iLeft; i <= endIndex; i++)
            {
                if (str[i] == c)
                {
                    if (i == iLeft)
                    {
                        span         = new StrSpan(i, i);
                        span.isEmpty = true;
                    }
                    else
                    {
                        span = new StrSpan(iLeft, i - 1);
                    }

                    tokenList.Add(span);

                    iLeft = i + 1;
                }
            }



            if (iLeft > endIndex)
            {
                span         = new StrSpan(endIndex, endIndex);
                span.isEmpty = true;
            }
            else
            {
                span = new StrSpan(iLeft, endIndex);
            }

            tokenList.Add(span);

            return(tokenList);
        }
Exemplo n.º 16
0
        private static 块作用域 Parse_子句(char[] chars, int beginIndex, int endIndex, out int j)
        {
            StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces);

            if (span.isEmpty)
            {
                throw new 语法错误_Exception("缺少子句 。", chars, span.iLeft);
            }

            char c = chars[span.iLeft];

            if (c == '{')
            {
                return(Parse_子句_大括号块(chars, span.iLeft, span.iRight, out j));
            }

            return(Parse_子句_单句(chars, span.iLeft, span.iRight, out j));
        }
Exemplo n.º 17
0
        private static 运算符 Check_IsCast(char[] chars, 表达式段 段)
        {
            StrSpan span = StrUtil.Trim(chars, 段.iLeft + 1, 段.iRight - 1, Parser._whiteSpaces);

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

            if (!Util.Check_是否_下划线字母数字_且以_下划线字母_开头(chars, span.iLeft, span.iRight))
            {
                return(null);
            }

            string op       = new string(chars, 段.iLeft, 段.iRight - 段.iLeft + 1);
            string castType = new string(chars, span.iLeft, span.iRight - span.iLeft + 1);

            return(new 运算符(op, 运算符_Type.Cast, 6, castType, 段.iLeft, 段.iRight));
        }
Exemplo n.º 18
0
        private static 表达式[] Parse_小括号(char[] chars, int 左小括号位置, int 右小括号位置)
        {
            //作用域 形参列表 = new 作用域();

            表达式[] 小括号表达式s = new 表达式[3];

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

            if (span.isEmpty)
            {
                throw new 语法错误_Exception("for() 小括号内应是两个分号 。", chars, 左小括号位置);
            }
            //return exppressArr;

            List <StrSpan> list = Parser.在_单引号双引号_以外_Split(chars, span.iLeft, span.iRight, ';');

            if (list.Count != 3)
            {
                throw new 语法错误_Exception("for() 小括号内应是两个分号 。", chars, 左小括号位置);
            }

            StrSpan spanExpress;

            for (int i = 0; i < list.Count; i++)
            {
                spanExpress = list[i];

                变量声明和初始化 变量声明 = 语句_Parser.Parse_变量声明(chars, spanExpress.iLeft, spanExpress.iRight);

                if (变量声明 != null)
                {
                    小括号表达式s[i] = 变量声明;
                    continue;
                }

                表达式 表达式 = 表达式_Parser.Parse(chars, spanExpress.iLeft, spanExpress.iRight);

                小括号表达式s[i] = 表达式;
            }

            return(小括号表达式s);
        }
Exemplo n.º 19
0
        private static 字段声明 Parse_字段(char[] chars, StrSpan span)
        {
            if (span.isEmpty)
            {
                throw new InnerCException("缺少字段定义 。", chars, span.iLeft);
            }

            //span = StrUtil.Trim(chars, span.iLeft, span.iRight, Parser._whiteSpaces);

            //if (span.isEmpty)
            //    throw new InnerCException("缺少字段定义 。", chars, span.iLeft);

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

            if (变量声明 == null)
            {
                throw new InnerCException("错误的字段定义 。", chars, span.iLeft);
            }

            return(变量声明.To_字段声明());
        }
Exemplo n.º 20
0
        //private static Dictionary<string, 字段声明> Parse_字段声明(char[] chars, StrSpan 大括号块)
        private static 作用域 Parse_字段声明(char[] chars, StrSpan 大括号块)
        {
            //Dictionary<string, 字段声明> dic字段声明 = new Dictionary<string, 字段声明>();
            作用域 字段声明 = new 作用域();

            StrSpan span = StrUtil.Trim(chars, 大括号块.iLeft + 1, 大括号块.iRight - 1, Parser._whiteSpaces);

            //if (span.isEmpty)
            //    return dic字段声明;

            if (span.isEmpty)
            {
                return(字段声明);
            }

            List <StrSpan> list = Parser.在_单引号双引号_以外_Split(chars, span.iLeft, span.iRight, ',');

            StrSpan span字段;

            for (int i = 0; i < list.Count; i++)
            {
                span字段 = list[i];

                字段声明 字段 = Parse_字段(chars, span字段);

                //if (dic字段声明.ContainsKey(字段.name))
                if (字段声明.dic变量声明.ContainsKey(字段.name))
                {
                    throw new 语法错误_Exception("字段名 \"" + 字段.name + "\" 重复 。", chars, 字段.变量名位置);
                }

                //dic字段声明.Add(字段.name, 字段);
                字段声明.dic变量声明.Add(字段.name, 字段);
            }

            return(字段声明);
            //return dic字段声明;
        }
Exemplo n.º 21
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_分句));
        }
Exemplo n.º 22
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(块);
        }
Exemplo n.º 23
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], 参数列表, 函数体);
        }
Exemplo n.º 24
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(返回类型);
        }
Exemplo n.º 25
0
        public static 表达式 Parse(char[] chars, int beginIndex, int endIndex)
        {
            StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces);

            if (span.isEmpty)
            {
                throw new InnerCException("无效的表达式 。 缺少内容 。", chars, beginIndex);
            }


            //List<表达式段> list = 按_小括号中括号双引号单引号_分段(chars, span.iLeft, span.iRight);

            运算符 op = Get_优先级最小的左右结合运算符和右结合运算符(chars, span.iLeft, span.iRight);

            if (op == null)
            {
                return(常量_变量_字段_指针字段_数组元素_函数调用_函数指针调用_调用链_Parser.Parse(chars, span.iLeft, span.iRight));
            }

            表达式 op_左边的表达式 = null;
            表达式 op_右边的表达式;

            if (op.op == "!" || op.type == 运算符_Type.Cast || op.type == 运算符_Type.指针取值 || op.type == 运算符_Type.取地址)
            {
                StrSpan 左边的内容 = StrUtil.Trim(chars, span.iLeft, op.iLeft - 1, Parser._whiteSpaces);

                if (!左边的内容.isEmpty)
                {
                    throw new InnerCException("无效的内容,缺少运算符 。", chars, 左边的内容.iRight);
                }

                op_右边的表达式 = Parse(chars, op.iRight + 1, endIndex);
            }
            else
            {
                op_左边的表达式 = Parse(chars, span.iLeft, op.iLeft - 1);
                op_右边的表达式 = Parse(chars, op.iRight + 1, endIndex);
            }


            if (op.type == 运算符_Type.Cast)
            {
                return(new Cast(op.castType, op_右边的表达式));
            }
            if (op.type == 运算符_Type.指针取值)
            {
                return(new 指针取值(op_右边的表达式));
            }
            if (op.type == 运算符_Type.取地址)
            {
                return(new 取地址(op_右边的表达式));
            }

            if (op.op == "+")
            {
                return(new 加(op_左边的表达式, op_右边的表达式));
            }
            else if (op.op == "-")
            {
                return(new 减(op_左边的表达式, op_右边的表达式));
            }
            else if (op.op == "*")
            {
                return(new 乘(op_左边的表达式, op_右边的表达式));
            }
            else if (op.op == "/")
            {
                return(new 除(op_左边的表达式, op_右边的表达式));
            }
            else if (op.op == "==")
            {
                return(new 等于(op_左边的表达式, op_右边的表达式));
            }
            else if (op.op == ">")
            {
                return(new 大于(op_左边的表达式, op_右边的表达式));
            }
            else if (op.op == "<")
            {
                return(new 小于(op_左边的表达式, op_右边的表达式));
            }
            else if (op.op == ">=")
            {
                return(new 大于等于(op_左边的表达式, op_右边的表达式));
            }
            else if (op.op == "<=")
            {
                return(new 小于等于(op_左边的表达式, op_右边的表达式));
            }
            else if (op.op == "!=")
            {
                return(new  等于(op_左边的表达式, op_右边的表达式));
            }
            else if (op.op == "&&")
            {
                return(new 与(op_左边的表达式, op_右边的表达式));
            }
            else if (op.op == "||")
            {
                return(new 或(op_左边的表达式, op_右边的表达式));
            }
            else if (op.op == "!")
            {
                return(new 非(op_右边的表达式));
            }
            else if (op.op == "=")
            {
                return(new 赋值(op_左边的表达式, op_右边的表达式));
            }

            throw new InnerCException("无效的运算符 \"" + op.op + "\" 。", chars, op.iLeft);
        }
Exemplo n.º 26
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_条件, 子句));
        }
        private static 表达式 Parse_数组元素(char[] chars, List <表达式段> list)
        {
            表达式段 段;

            List <表达式段> list右边连续的中括号段 = new List <表达式段>();

            int p = -1;

            for (int i = list.Count - 1; i >= 0; i--)
            {
                段 = list[i];

                if (段.type != 表达式段_Type.中括号段)
                {
                    break;
                }

                p = i;
                //list右边连续的中括号段.Add(段);
            }

            if (p != -1)
            {
                for (int i = p; i < list.Count; i++)
                {
                    段 = list[i];

                    list右边连续的中括号段.Add(段);
                }
            }

            if (list右边连续的中括号段.Count == 0)
            {
                return(null);
            }

            表达式 express;

            List <表达式> list下标 = new List <表达式>();

            for (int i = 0; i < list右边连续的中括号段.Count; i++)
            {
                段 = list右边连续的中括号段[i];

                StrSpan span下标 = StrUtil.Trim(chars, 段.iLeft + 1, 段.iRight - 1, Parser._whiteSpaces);

                if (span下标.isEmpty)
                {
                    list下标.Add(null);
                    continue;
                }

                express = 表达式_Parser.Parse(chars, span下标.iLeft, span下标.iRight);

                //express.参考位置_iLeft = 段.iLeft;

                list下标.Add(express);
            }

            StrSpan 左边的部分 = StrUtil.Trim(chars, list[0].iLeft, list右边连续的中括号段[0].iLeft - 1, Parser._whiteSpaces);

            if (左边的部分.isEmpty)
            {
                throw new 语法错误_Exception("\"[ ]\" 前面缺少 数组变量 或者 可以返回一个数组指针的表达式 。", chars, list[0].iLeft);
            }

            表达式 左边的表达式 = Parse(chars, 左边的部分.iLeft, 左边的部分.iRight);

            return(new 数组元素(左边的表达式, list下标, chars, 左边的部分.iLeft));
        }
Exemplo n.º 28
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);
        }
        public static 表达式 Parse(char[] chars, int beginIndex, int endIndex)
        {
            StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces);

            if (span.isEmpty)
            {
                throw new 语法错误_Exception("无效的表达式 。 缺少内容 。", chars, beginIndex);
            }


            List <表达式段> list = Parser._小括号中括号单引号双引号_分段(chars, span.iLeft, span.iRight);

            list = Parser.去掉空白段(chars, list);


            表达式 express = Parse_变量(chars, list);

            if (express != null)
            {
                return(express);
            }

            express = Parse_常量(chars, list);

            if (express != null)
            {
                return(express);
            }

            express = Parse_小括号表达式(chars, list);

            if (express != null)
            {
                return(express);
            }

            express = Parse_字段(chars, list);

            if (express != null)
            {
                return(express);
            }

            express = Parse_指针字段(chars, list);

            if (express != null)
            {
                return(express);
            }

            express = Parse_数组元素(chars, list);

            if (express != null)
            {
                return(express);
            }

            express = Parse_函数调用(chars, list);

            if (express != null)
            {
                return(express);
            }

            //express = Parse_函数指针调用(chars, list);

            //if (express != null)
            //    return express;

            throw new 语法错误_Exception("无效的表达式 。", chars, span.iLeft);
        }
Exemplo n.º 30
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_判断, 子句));
        }