Made a new class for parser result

This commit is contained in:
Lukasz Baldyga committed 2020-11-28 21:34:32 +00:00
1 parent b38b8aa006
commit 7c89cbbe99
2 files changed
+40 -23

No files matched your search

@@ -3,6 +3,24 @@ using System.Windows.Forms;
namespace Advanced_Software_Engineering {
public class CommandAndParameterParserResult {
string command;
string[] parameters;
public CommandAndParameterParserResult(string command, string[] parameters) {
this.command = command;
this.parameters = parameters;
}
public string getCommand() {
return command;
}
public string[] getParameters() {
return parameters;
}
}
/// <summary>
/// This class simply provides settings and some functions that are used across many components.
/// </summary>
@@ -98,39 +116,39 @@ namespace Advanced_Software_Engineering {
/// </summary>
/// <param name="text">A string command</param>
/// <returns>Directory with the command and its parameters cleanly separated</returns>
public static Dictionary<string, string[]> CommandAndParameterParser(string text) {
Dictionary<string, string[]> commandAndParameters = new Dictionary<string, string[]>();
public static CommandAndParameterParserResult CommandAndParameterParser(string text) {
//prepare the parameters by stripping them of spaces
string prepParameters = Strip(text);
if (prepParameters.Length == 0) return commandAndParameters;
if (prepParameters.Length == 0) return null;
//split the command from the parameters
string[] parameters = prepParameters.Split(new char[] { " "[0] }, 2);
string[] splitCommand = prepParameters.Split(new char[] { " "[0] }, 2);
string command = null;
string[] parameters = null;
//There are parameters, make the command
if (parameters.Length > 0) {
if (splitCommand.Length > 0) {
//set command var
string command = Strip(parameters[0]);
commandAndParameters["command"] = new string[] { command };
command = Strip(splitCommand[0]);
}
//There is at least one parameter
if (parameters.Length > 1) {
if (splitCommand.Length > 1) {
//seperate all of the parameters by a comma
parameters = parameters[1].Split(","[0]);
splitCommand = splitCommand[1].Split(","[0]);
//Remove spaces around parameters
parameters = StripStringArray(parameters).ToArray();
splitCommand = StripStringArray(splitCommand).ToArray();
//We don't want parameters if there aren't any
if (!(parameters.Length == 1 && parameters[0] == "")) {
if (!(splitCommand.Length == 1 && splitCommand[0] == "")) {
//set parameter list
commandAndParameters["parameters"] = parameters;
parameters = splitCommand;
}
}
return commandAndParameters;
return new CommandAndParameterParserResult(command, parameters);
}
}
}
@@ -23,28 +23,27 @@ namespace Advanced_Software_Engineering {
/// <param name="fullCommand">The command as a string</param>
/// <returns>A IVerb that maches the command</returns>
public static IVerb MakeVerb(Drawer drawer, string fullCommand) {
Dictionary<string, string[]> commandAndParameters = SettingsAndHelperFunctions.CommandAndParameterParser(fullCommand);
CommandAndParameterParserResult result = SettingsAndHelperFunctions.CommandAndParameterParser(fullCommand);
if (!commandAndParameters.Keys.Contains("command")) throw new Exception("There is no command to process");
string command = commandAndParameters["command"][0];
string command = result.getCommand();
string[] commandParameters = result.getParameters();
if (command == null) throw new Exception("There is no command to process");
//process declarations first
if(commandAndParameters.ContainsKey("parameters"))
if(!(commandParameters == null))
switch (command) {
case "int":
case "double":
case "bool":
case "color":
if (commandAndParameters["parameters"].Length == 1) {
return new DeclareVariable(drawer, command, commandAndParameters["parameters"][0]);
if (commandParameters.Length == 1) {
return new DeclareVariable(drawer, command, commandParameters[0]);
} else throw new Exception(command + "s need to be initialised");
default:
//assignment
if (command.Contains("=") && !command.Contains("==")) {
if (drawer.CheckVariableExists(command)) {
string[] assignmentCommands = command.Split("="[0]);
if (assignmentCommands.Length > 2) throw new Exception("Cannot have two assignments");
return new UpdateVariable(drawer, assignmentCommands[0], ValueFactory.CreateValue(drawer, assignmentCommands[1]));
@@ -54,7 +53,7 @@ namespace Advanced_Software_Engineering {
List<IValue> parameters = new List<IValue>();
if( commandAndParameters.ContainsKey("parameters") ) foreach(string parameter in commandAndParameters["parameters"]) {
if( !(commandParameters == null) ) foreach(string parameter in commandParameters) {
parameters.Add(ValueFactory.CreateValue(drawer, parameter));
}
int parameterLength = parameters.Count;