Advertisement
Margoshinka

io module

Oct 10th, 2022
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace pascal_compiler
  8. {
  9.     public class IO_module
  10.     {
  11.         List<string> list;
  12.         int position;
  13.         string filename;
  14.         public IO_module(string filename)
  15.         {
  16.             position = 0;
  17.             list = new List<string>();
  18.             this.filename = filename;
  19.             ReadText();
  20.         }
  21.         public string GetNext()
  22.         {
  23.             if (position == list.Count) return "";
  24.             else
  25.             {
  26.                 string s = list[position];
  27.                 ++position;
  28.                 return s;
  29.             }
  30.         }
  31.  
  32.         private void WriteInList(string s)
  33.         {
  34.             string str = "";
  35.             for (int i = 0; i < s.Length; i++)
  36.             {
  37.                 switch (s[i])
  38.                 {
  39.                     case ';':
  40.                     case '(': case ')':
  41.                     case '{':
  42.                     case '}':
  43.                     case '[': case ']':
  44.                    
  45.                     case '.':
  46.                     case ',':
  47.                     case '/':
  48.                     case '*':
  49.                     case '+':
  50.                     case '-':
  51.                     case '=':
  52.                         {
  53.                             if (str != "") list.Add(str);
  54.                             str = "" + s[i];
  55.                             list.Add(str);
  56.                             str = "";
  57.                         }
  58.                         break;
  59.                    
  60.                     case '\"':
  61.                         {
  62.                             if (str != "") list.Add(str);
  63.                             str = "" + s[i]; i++;
  64.                             while (i < s.Length && s[i] != '\"') {
  65.                                 str += s[i]; i++; }
  66.                             if (i < s.Length) { str += s[i]; list.Add(str); }
  67.                         }
  68.                         break;
  69.                     case '\'':
  70.                         {
  71.                             if (str != "") list.Add(str);
  72.                             str = "" + s[i]; i++;
  73.                             while (i < s.Length && s[i] != '\'')
  74.                             {
  75.                                 str += s[i]; i++;
  76.                             }
  77.                             if (i < s.Length) { str += s[i]; list.Add(str); }
  78.                         }
  79.                         break;
  80.                     case ':':
  81.                         {
  82.  
  83.                             if (str != "") list.Add(str);
  84.                             if (i < s.Length - 1 && s[i + 1] == '=')
  85.                             {
  86.                                 list.Add(":="); i++;
  87.                             }
  88.                             else {
  89.                                 list.Add(":");
  90.                             }
  91.                             str = "";
  92.                         }
  93.                         break;
  94.                     case '<':
  95.                         {
  96.                             if (str != "") list.Add(str);
  97.                             if (i < s.Length - 1 && s[i + 1] == '=')
  98.                             {
  99.                                 list.Add("<="); i++;
  100.                             }
  101.                             else if (i < s.Length - 1 && s[i + 1] == '>')
  102.                             {
  103.                                 list.Add("<>"); i++;
  104.                             }
  105.                             else
  106.                             {
  107.                                 list.Add("<");
  108.                             }
  109.                             str = "";
  110.                         }
  111.                         break;
  112.                     case '>':
  113.                         {
  114.                             if (str != "") list.Add(str);
  115.                             if (i < s.Length - 1 && s[i + 1] == '=')
  116.                             {
  117.                                 list.Add(">="); i++;
  118.                             }
  119.                            
  120.                             else
  121.                             {
  122.                                 list.Add(">");
  123.                             }
  124.                             str = "";
  125.                         }
  126.                         break;
  127.                     case ' ':
  128.                         {
  129.                             if (str != "") list.Add(str); str = "";
  130.                         }
  131.                         break;
  132.                     default:
  133.                            str += s[i];
  134.                         if (i == s.Length - 1 && str != "") list.Add(str);
  135.                         break;
  136.  
  137.                        
  138.                
  139.                 }
  140.  
  141.  
  142.  
  143.             }
  144.         }
  145.         private void ReadText()
  146.         {
  147.             try
  148.             {
  149.                 using (StreamReader f = new StreamReader(filename))
  150.                 {
  151.                     string s;
  152.                     while ((s = f.ReadLine()) != null)  WriteInList(s);
  153.  
  154.                 }
  155.             }
  156.             catch (IOException e)
  157.             {
  158.                 Console.WriteLine("Error:" + e.Message);
  159.                 return;
  160.             }
  161.  
  162.         }
  163.     }
  164. }
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement