Exemple #1
0
 /// <summary>
 /// 向表中插入一条
 /// </summary>
 /// <param name="item"></param>
 public void Add(VariableItem item)
 {
     table.Add(item);
 }
Exemple #2
0
 /// <summary>
 /// 根据临时文件分析,并且填表
 ///     0       执行成功
 ///     -1      临时文件不存在
 ///     -2      遇到非法字符
 ///     -3      长度超过限值
 /// </summary>
 public int Analize()
 {
     if (fileInfo.Exists)
     {
         StreamReader readTempFile = new StreamReader(this.fileInfo.FullName);
         while (!readTempFile.EndOfStream)
         {
             string lineString        = readTempFile.ReadLine();
             char[] lineChars         = lineString.ToCharArray();
             int    currentLineNumber = 1;                              //原文本行号
             int    currentIndex      = -1;                             //当前索引位置
             int    endIndex          = 0;                              //检索到一个标识符结束位置
             while (++currentIndex < lineChars.Length)
             {
                 if (lineChars[currentIndex] == '#')
                 {
                     endIndex = this.TryGetLineNumber(lineChars, currentIndex);
                     if (endIndex == -2)
                     {
                         System.Console.WriteLine("非法符号#,该符号系统保留,位置第" + currentLineNumber + "行附近");
                         readTempFile.Close();
                         fileInfo.Delete();
                         return(-3);
                     }
                     currentLineNumber = int.Parse(lineString.Substring(currentIndex + 1, endIndex - currentIndex - 1));
                     currentIndex      = endIndex;
                 }
                 else if (isNumber(lineChars[currentIndex]))
                 {
                     endIndex = this.TryGetNumber(lineChars, currentIndex);
                     if (endIndex == -2)
                     {
                         System.Console.WriteLine("数字的长度超过限制" + ConstTable.NumberMaxLength + ",位置第" + currentLineNumber + "行");
                         readTempFile.Close();
                         fileInfo.Delete();
                         return(-3);
                     }
                     VariableItem numberItem = new VariableItem(ConstTable.VariableType.Number, lineString.Substring(currentIndex, endIndex - currentIndex + 1), currentLineNumber);
                     varTable.Add(numberItem);
                     currentIndex = endIndex;
                 }
                 else if (this.isLetter(lineChars[currentIndex]) || lineChars[currentIndex] == '_')
                 {
                     endIndex = this.TryGetIdentifier(lineChars, currentIndex);
                     if (endIndex == -2)
                     {
                         System.Console.WriteLine("标识符的长度超过限制" + ConstTable.IdentifyMaxLength + ",位置第" + currentLineNumber + "行");
                         readTempFile.Close();
                         fileInfo.Delete();
                         return(-3);
                     }
                     if (isReserve(lineString.Substring(currentIndex, endIndex - currentIndex + 1)))
                     {
                         VariableItem reserveItem = new VariableItem(ConstTable.VariableType.Reverse, lineString.Substring(currentIndex, endIndex - currentIndex + 1), currentLineNumber);
                         varTable.Add(reserveItem);
                     }
                     else
                     {
                         VariableItem idItem = new VariableItem(ConstTable.VariableType.Identity, lineString.Substring(currentIndex, endIndex - currentIndex + 1), currentLineNumber);
                         varTable.Add(idItem);
                     }
                     currentIndex = endIndex;
                 }
                 else if (this.isSingleOperator(lineChars[currentIndex]))
                 {
                     endIndex = currentIndex;
                     VariableItem singleOpeItem = new VariableItem(ConstTable.VariableType.Operation, lineString.Substring(currentIndex, endIndex - currentIndex + 1), currentLineNumber);
                     varTable.Add(singleOpeItem);
                 }
                 else if (lineChars[currentIndex] == '>')
                 {
                     endIndex = this.TryGetGreaterEqual(lineChars, currentIndex);
                     if (endIndex == -1)
                     {
                         endIndex = currentIndex;
                         VariableItem OpeItem = new VariableItem(ConstTable.VariableType.Operation, lineString.Substring(currentIndex, endIndex - currentIndex + 1), currentLineNumber);
                         varTable.Add(OpeItem);
                     }
                     else
                     {
                         VariableItem OpeItem = new VariableItem(ConstTable.VariableType.Operation, lineString.Substring(currentIndex, endIndex - currentIndex + 1), currentLineNumber);
                         varTable.Add(OpeItem);
                         currentIndex = endIndex;
                     }
                 }
                 else if (lineChars[currentIndex] == '<')
                 {
                     endIndex = this.TryGetSmallerEqual(lineChars, currentIndex);
                     if (endIndex != -1)
                     {
                         VariableItem OpeItem = new VariableItem(ConstTable.VariableType.Operation, lineString.Substring(currentIndex, endIndex - currentIndex + 1), currentLineNumber);
                         varTable.Add(OpeItem);
                         currentIndex = endIndex;
                     }
                     else
                     {
                         endIndex = this.TryGetNotEqual(lineChars, currentIndex);
                         if (endIndex != -1)
                         {
                             VariableItem OpeItem = new VariableItem(ConstTable.VariableType.Operation, lineString.Substring(currentIndex, endIndex - currentIndex + 1), currentLineNumber);
                             varTable.Add(OpeItem);
                             currentIndex = endIndex;
                         }
                         else
                         {
                             endIndex = currentIndex;
                             VariableItem OpeItem = new VariableItem(ConstTable.VariableType.Operation, lineString.Substring(currentIndex, endIndex - currentIndex + 1), currentLineNumber);
                             varTable.Add(OpeItem);
                         }
                     }
                 }
                 else if (lineChars[currentIndex] == ':')
                 {
                     endIndex = currentIndex + 1;
                     if (lineChars[endIndex] == '=')
                     {
                         VariableItem OpeItem = new VariableItem(ConstTable.VariableType.Operation, lineString.Substring(currentIndex, endIndex - currentIndex + 1), currentLineNumber);
                         varTable.Add(OpeItem);
                         currentIndex = endIndex;
                     }
                     else
                     {
                         System.Console.WriteLine("赋值号不完整,位置第" + currentLineNumber + "行");
                         readTempFile.Close();
                         fileInfo.Delete();
                         return(-2);
                     }
                 }
                 else if (lineChars[currentIndex] == ' ' || lineChars[currentIndex] == '\t' || lineChars[currentIndex] == '\n')
                 {
                 }
                 else
                 {
                     System.Console.WriteLine("遇到非法字符" + lineChars[currentIndex] + ",错误位置第" + currentLineNumber + "行");
                     readTempFile.Close();
                     fileInfo.Delete();
                     return(-2);
                 }
             }
         }
         readTempFile.Close();
         return(0);
     }
     else
     {
         System.Console.WriteLine("分析时临时文件不存在!");
         return(-1);
     }
 }