Trying to make UpdateVariable work

This commit is contained in:
Lukasz Baldyga committed 2020-11-28 20:46:23 +00:00
1 parent 4899fbf9c8
commit b38b8aa006
2 files changed
+17 -18

No files matched your search

@@ -1,24 +1,21 @@
namespace Advanced_Software_Engineering.Verbs.Value {
using System;
namespace Advanced_Software_Engineering.Verbs.Value {
public class UpdateVariable : IVerb {
protected Drawer drawer;
protected string name;
protected string value;
protected IValue value;
public UpdateVariable(Drawer drawer, string name, string value) {
public UpdateVariable(Drawer drawer, string name, IValue value) {
this.name = name;
this.value = value;
this.drawer = drawer;
}
public UpdateVariable(Drawer drawer, string name, IValue value) {
}
public void ExecuteVerb() {
IValue variable = drawer.GetVariable(name);
string type = variable.GetOriginalType();
drawer.SetVariable(name, ValueFactory.CreateValue(value, type));
if (!drawer.CheckVariableExists(name)) throw new Exception("Cannot update variable because it is not declared");
drawer.SetVariable(name, value);
}
public string GetDescription() {
@@ -41,6 +41,15 @@ namespace Advanced_Software_Engineering {
if (commandAndParameters["parameters"].Length == 1) {
return new DeclareVariable(drawer, command, commandAndParameters["parameters"][0]);
} else throw new Exception(command + "s need to be initialised");
default:
//assignment
if (command.Contains("=") && !command.Contains("==")) {
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]));
}
break;
}
@@ -178,15 +187,8 @@ namespace Advanced_Software_Engineering {
} else throw new Exception(command + " has an incorrect number of parameters");
default:
//assignment
if (command.Contains("=") && !command.Contains("==")) {
string[] assignmentCommands = command.Split("="[0]);
if (assignmentCommands.Length > 2) throw new Exception("Cannot have two assignments");
return new UpdateVariable(drawer, assignmentCommands[0], assignmentCommands[1]);
}
//not assignment
throw new Exception("Unknown command or cannot parse");
}
}