Private
Public Access
Trying to make UpdateVariable work
This commit is contained in:
1 parent
4899fbf9c8
commit
b38b8aa006
2 files changed
+17
-18
No files matched your search
+7
-10
@@ -1,24 +1,21 @@
|
|||||||
namespace Advanced_Software_Engineering.Verbs.Value {
|
using System;
|
||||||
|
|
||||||
|
namespace Advanced_Software_Engineering.Verbs.Value {
|
||||||
|
|
||||||
public class UpdateVariable : IVerb {
|
public class UpdateVariable : IVerb {
|
||||||
protected Drawer drawer;
|
protected Drawer drawer;
|
||||||
protected string name;
|
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.name = name;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.drawer = drawer;
|
this.drawer = drawer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UpdateVariable(Drawer drawer, string name, IValue value) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ExecuteVerb() {
|
public void ExecuteVerb() {
|
||||||
IValue variable = drawer.GetVariable(name);
|
if (!drawer.CheckVariableExists(name)) throw new Exception("Cannot update variable because it is not declared");
|
||||||
string type = variable.GetOriginalType();
|
drawer.SetVariable(name, value);
|
||||||
drawer.SetVariable(name, ValueFactory.CreateValue(value, type));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetDescription() {
|
public string GetDescription() {
|
||||||
|
|||||||
@@ -41,6 +41,15 @@ namespace Advanced_Software_Engineering {
|
|||||||
if (commandAndParameters["parameters"].Length == 1) {
|
if (commandAndParameters["parameters"].Length == 1) {
|
||||||
return new DeclareVariable(drawer, command, commandAndParameters["parameters"][0]);
|
return new DeclareVariable(drawer, command, commandAndParameters["parameters"][0]);
|
||||||
} else throw new Exception(command + "s need to be initialised");
|
} 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");
|
} else throw new Exception(command + " has an incorrect number of parameters");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
default:
|
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");
|
throw new Exception("Unknown command or cannot parse");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user