Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace LunaInterpreter
- {
- /// <summary>
- /// The basic instruction for outputting data to the console.
- /// </summary>
- public class OutInstruction
- {
- /// <summary>
- /// String started
- /// </summary>
- private bool stringStarted = false;
- /// <summary>
- /// A list of the words for the output.
- /// </summary>
- private List<string> words;
- /// <summary>
- /// Parses the instruction.
- /// </summary>
- /// <param name="cmdLine">The data.</param>
- public void Parse (string cmdLine)
- {
- // "HELLO WORLD" + computerName;
- words = new List<string>();
- string lastWord = "";
- bool isString = false;
- for (int i = 0; i < cmdLine.Length; i++)
- {
- if (cmdLine[i] == '"' && !stringStarted)
- {
- stringStarted = true;
- isString = true;
- }
- else if (cmdLine[i] == '"' && stringStarted)
- {
- stringStarted = false;
- }
- else if (cmdLine[i] == ' ' && !stringStarted)
- {
- // Ignore the line, since a whitespace is 'useless' outside strings (except for readability)
- }
- else if (cmdLine[i] == '+' && !stringStarted)
- {
- if (isString)
- {
- words.Add(lastWord);
- }
- else
- {
- words.Add(Engine.VariableController.GetVariable(lastWord).Value.ToString());
- }
- lastWord = "";
- isString = false;
- }
- else if (cmdLine[i] == ';' && !stringStarted)
- {
- if (isString)
- {
- words.Add(lastWord);
- }
- else
- {
- words.Add(Engine.VariableController.GetVariable(lastWord).Value.ToString());
- }
- break;
- }
- else
- {
- lastWord += cmdLine[i];
- }
- }
- foreach (string word in words)
- {
- Console.Write(word);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement