Advertisement
Guest User

Untitled

a guest
Feb 13th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.29 KB | None | 0 0
  1.  
  2. using System.Text;
  3.  
  4. const string dataVis = @"
  5. copy(C:/test.txt, C:/output.txt)
  6. print(Hello)
  7. ";
  8.  
  9.  
  10. List<string> tokens = new List<string>();
  11.  
  12. // 1. Lex source
  13.  
  14. {
  15.     int position = 0;
  16.  
  17.     Func<char> Current = () => position >= dataVis.Length
  18.         ? '\0'
  19.         : dataVis[position];
  20.  
  21.     while (true) {
  22.         // End on null terminator - indicates end of string
  23.         if (Current() == '\0') {
  24.             break;
  25.         }
  26.  
  27.         // Ignore whitespace (inc. new lines)
  28.         if (char.IsWhiteSpace(Current())) {
  29.             position++;
  30.             continue;
  31.         }
  32.  
  33.         // Special punctuation can go into it's own token
  34.         if (new[] { '(', ')', ',' }.Contains(Current())) {
  35.             tokens.Add(Current().ToString());
  36.             position++;
  37.             continue;
  38.         }
  39.  
  40.         // Otherwise assume a string (represents a command name & any arguments)
  41.         ReadString();
  42.     }
  43.  
  44.     void ReadString() {
  45.         StringBuilder sb = new StringBuilder();
  46.  
  47.         bool done = false;
  48.  
  49.         while (!done) {
  50.             switch (Current()) {
  51.                 case ',':
  52.                 case ' ':
  53.                 case '\t':
  54.                 case '\0':
  55.                 case '(':
  56.                 case ')':
  57.                     done = true;
  58.                     break;
  59.                 default:
  60.                     sb.Append(Current());
  61.                     position++;
  62.                     break;
  63.             }
  64.         }
  65.  
  66.         tokens.Add(sb.ToString());
  67.     }
  68. }
  69.  
  70. // 2. Parse tokens
  71.  
  72. {
  73.     int position = 0;
  74.  
  75.     Func<string> Current = () => position >= tokens.Count
  76.         ? null
  77.         : tokens[position];
  78.  
  79.     StringBuilder sb = new StringBuilder();
  80.  
  81.     Func<string> Consume = () => {
  82.         string token = position < tokens.Count
  83.             ? tokens[position]
  84.             : null;
  85.  
  86.         position++;
  87.  
  88.         return token;
  89.     };
  90.  
  91.     while (true) {
  92.         // End of tokens
  93.         if (Current() == null) {
  94.             break;
  95.         }
  96.  
  97.         // If it's a copy token we can consume it & the expected trailing tokens
  98.         if (Current() == "copy") {
  99.             position++;
  100.  
  101.             _ = Consume(); // Open parenthesis
  102.  
  103.             string firstArg = Consume();
  104.  
  105.             _ = Consume(); // Comma
  106.  
  107.             string secondArg = Consume();
  108.  
  109.             _ = Consume(); // Close parenthesis
  110.  
  111.             sb.AppendLine($@"File.Copy(""{firstArg}"", ""{secondArg}"");");
  112.  
  113.             continue;
  114.         }
  115.  
  116.         if (Current() == "print") {
  117.             position++;
  118.  
  119.             _ = Consume(); // Open parenthesis
  120.  
  121.             string arg = Consume();
  122.  
  123.             Console.WriteLine(arg);
  124.  
  125.             _ = Consume(); // Close parenthesis
  126.  
  127.             continue;
  128.         }
  129.  
  130.         // Add more commands here
  131.     }
  132.  
  133.     // Write to data.vis - just printing for demonstration
  134.     Console.WriteLine(sb.ToString());
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement