Advertisement
sylviapsh

Basic Language

Feb 4th, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class BasicLanguage
  6. {
  7.   static StringBuilder result = new StringBuilder();
  8.  
  9.   static void Main()
  10.   {
  11.  
  12.     bool inBrackets = false;
  13.     StringBuilder buffer = new StringBuilder();
  14.  
  15.     while (true)
  16.     {
  17.       int nextSymbol = Console.Read();
  18.       if (nextSymbol == -1)
  19.       {
  20.         break;
  21.       }
  22.       char nextChar = (char)nextSymbol;
  23.       if (nextChar =='(')
  24.       {
  25.         inBrackets = true;
  26.       }
  27.       if (nextChar == ')')
  28.       {
  29.         inBrackets = false;
  30.       }
  31.       if (nextChar == ';' && inBrackets == false)
  32.       {
  33.         string statement = buffer.ToString();
  34.         if (ProcessConstruct(statement))
  35.         {
  36.           break;
  37.         }
  38.         buffer.Remove(0, buffer.Length); //buffer clear in .net 4.5
  39.       }
  40.       else
  41.       {
  42.         if (inBrackets)
  43.         {
  44.           buffer.Append(nextChar);
  45.         }
  46.         else if (!char.IsWhiteSpace(nextChar))
  47.         {
  48.           buffer.Append(nextChar);
  49.         }
  50.        
  51.       }
  52.     }
  53.     Console.WriteLine(result.ToString());
  54.   }
  55.  
  56.   private static bool ProcessConstruct(string statement)
  57.   {
  58.     string[] commands = statement.Split(')');
  59.     long count = 1;
  60.     for (int i = 0; i < commands.Length; i++)
  61.     {
  62.       string cmd = commands[i];
  63.       cmd = cmd.TrimStart();
  64.       if (cmd.StartsWith("EXIT"))
  65.       {
  66.         return true;
  67.       }
  68.       else if (cmd.StartsWith("PRINT"))
  69.       {
  70.         int start = cmd.IndexOf('(') + 1;
  71.         string content = cmd.Substring(start);
  72.         if (content.Length > 0 && count>0)
  73.         {
  74.           for (int j = 0; j < count; j++)
  75.           {
  76.             result.Append(content);
  77.           }
  78.         }
  79.       }
  80.       else if (cmd.StartsWith("FOR"))
  81.       {
  82.         int startIndex = cmd.IndexOf('(') + 1;
  83.         int commaIndex = cmd.IndexOf(',');
  84.         if (commaIndex == -1)
  85.         {
  86.           string forCountStr = cmd.Substring(startIndex);
  87.           int forCount = int.Parse(forCountStr);
  88.           count *= forCount;
  89.         }
  90.         else
  91.         {
  92.           string forStartCountStr = cmd.Substring(startIndex, commaIndex - startIndex);
  93.           int forStartCount = int.Parse(forStartCountStr);
  94.           string forEndCountStr = cmd.Substring(commaIndex + 1);
  95.           int forEndCount = int.Parse(forEndCountStr);
  96.  
  97.           int forCount = forEndCount - forStartCount + 1;
  98.           count *= forCount;
  99.         }
  100.       }
  101.     }
  102.     return false;
  103.   }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement