Advertisement
Guest User

Out Instruction

a guest
Oct 23rd, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace LunaInterpreter
  5. {
  6.     /// <summary>
  7.     /// The basic instruction for outputting data to the console.
  8.     /// </summary>
  9.     public class OutInstruction
  10.     {
  11.         /// <summary>
  12.         /// String started
  13.         /// </summary>
  14.         private bool stringStarted = false;
  15.  
  16.         /// <summary>
  17.         /// A list of the words for the output.
  18.         /// </summary>
  19.         private List<string> words;
  20.  
  21.         /// <summary>
  22.         /// Parses the instruction.
  23.         /// </summary>
  24.         /// <param name="cmdLine">The data.</param>
  25.         public void Parse (string cmdLine)
  26.         {
  27.             // "HELLO WORLD" + computerName;
  28.  
  29.             words = new List<string>();
  30.  
  31.             string lastWord = "";
  32.             bool isString = false;
  33.  
  34.             for (int i = 0; i < cmdLine.Length; i++)
  35.             {
  36.                 if (cmdLine[i] == '"' && !stringStarted)
  37.                 {
  38.                     stringStarted = true;
  39.                     isString = true;
  40.                 }
  41.                 else if (cmdLine[i] == '"' && stringStarted)
  42.                 {
  43.                     stringStarted = false;
  44.                 }
  45.                 else if (cmdLine[i] == ' ' && !stringStarted)
  46.                 {
  47.                     // Ignore the line, since a whitespace is 'useless' outside strings (except for readability)
  48.                 }
  49.                 else if (cmdLine[i] == '+' && !stringStarted)
  50.                 {
  51.                     if (isString)
  52.                     {
  53.                         words.Add(lastWord);
  54.                     }
  55.                     else
  56.                     {
  57.                         words.Add(Engine.VariableController.GetVariable(lastWord).Value.ToString());
  58.                     }
  59.  
  60.                     lastWord = "";
  61.                     isString = false;
  62.                 }
  63.                 else if (cmdLine[i] == ';' && !stringStarted)
  64.                 {
  65.                     if (isString)
  66.                     {
  67.                         words.Add(lastWord);
  68.                     }
  69.                     else
  70.                     {
  71.                         words.Add(Engine.VariableController.GetVariable(lastWord).Value.ToString());
  72.                     }
  73.  
  74.                     break;
  75.                 }
  76.                 else
  77.                 {
  78.                     lastWord += cmdLine[i];
  79.                 }
  80.             }
  81.  
  82.             foreach (string word in words)
  83.             {
  84.                 Console.Write(word);
  85.             }
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement