Variables can now be assigned.

Next i need to make it so the variables can be accessed and used by other functions
This commit is contained in:
Lukasz Baldyga committed 2020-11-27 11:37:59 +00:00
1 parent 2acb57f229
commit 39489ec824
6 files changed
+42 -10

No files matched your search

@@ -122,7 +122,15 @@ namespace Advanced_Software_Engineering {
ProcessCommands(rawCommands, pardonCommands); ProcessCommands(rawCommands, pardonCommands);
for (; start < commands.Count; start++) { for (; start < commands.Count; start++) {
try {
commands[start].ExecuteVerb(); commands[start].ExecuteVerb();
} catch (Exception e) {
IVerb tmp = commands[start];
if (!pardonCommands) RemoveAllCommands();
else commands.Remove(commands[start]);
new ErrorWindow("Runtime Error", "Failed to " + tmp.GetDescription(), "While trying to '" + tmp.GetDescription() + "' an exception occured.\nMessage:\n" + e.Message + "\nStack Trace:\n" + e.StackTrace, ErrorWindow.ERROR_MESSAGE).Show();
break;
}
} }
} }
@@ -67,6 +67,7 @@ namespace Advanced_Software_Engineering {
pen = new Pen(defaultColor, defaultWidth); pen = new Pen(defaultColor, defaultWidth);
brush = new SolidBrush(defaultColor); brush = new SolidBrush(defaultColor);
fill = false; fill = false;
variables.Clear();
} }
/// <summary> /// <summary>
@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
namespace Advanced_Software_Engineering.Verbs.Value { namespace Advanced_Software_Engineering.Verbs.Value {
@@ -7,17 +8,34 @@ namespace Advanced_Software_Engineering.Verbs.Value {
private IValue value; private IValue value;
private string name; private string name;
public DeclareVariable(Drawer drawer, string type, string assgnment) { public DeclareVariable(Drawer drawer, string type, string assignment) {
this.drawer = drawer; this.drawer = drawer;
value = ValueFactory.CreateValue(type, assgnment); // get variable name
} // assignment looks something like this at this point:
// i = 20
// or
// i = 2 == 5
void IVerb.ExecuteVerb() { // bools have ==
} if(!(type == "bool")) {
//Seperate the assignment from the variables
List<string> assignmentList = SettingsAndHelperFunctions.StripStringArray(assignment.Split("="[0]));
name = assignmentList[0];
value = ValueFactory.CreateValue(assignmentList[1], type);
string IVerb.GetDescription() { } else {
throw new NotImplementedException(); throw new NotImplementedException();
} }
}
public void ExecuteVerb() {
drawer.SetVariable(name, value);
}
public string GetDescription() {
return "Declares the variable " + name;
}
} }
} }
@@ -25,7 +25,7 @@ namespace Advanced_Software_Engineering.Verbs.Value {
//Check for bad input //Check for bad input
if (operation > 3 && operation < 0) throw new Exception("Bad operation"); if (operation > 3 && operation < 0) throw new Exception("Bad operation");
if(variable1.)
} }
private IValue Evaluate() { private IValue Evaluate() {
@@ -103,18 +103,22 @@ namespace Advanced_Software_Engineering.Verbs.Value {
} }
public bool ToBool() { public bool ToBool() {
if (!evaluated) Evaluate();
return evaluatedValue.ToBool(); return evaluatedValue.ToBool();
} }
public Color ToColor() { public Color ToColor() {
if (!evaluated) Evaluate();
return evaluatedValue.ToColor(); return evaluatedValue.ToColor();
} }
public double ToDouble() { public double ToDouble() {
if (!evaluated) Evaluate();
return evaluatedValue.ToDouble(); return evaluatedValue.ToDouble();
} }
public int ToInt() { public int ToInt() {
if (!evaluated) Evaluate();
return evaluatedValue.ToInt(); return evaluatedValue.ToInt();
} }
} }
@@ -25,9 +25,6 @@ namespace Advanced_Software_Engineering.Verbs.Value {
case "bool": case "bool":
return new BoolValue(ValueHelper.ConvertToBool(value)); return new BoolValue(ValueHelper.ConvertToBool(value));
case "expression":
return new Exception(ValueHelper.ConvertToExpression(value));
default: default:
throw new Exception("Unknown expected type: " + type.ToString()); throw new Exception("Unknown expected type: " + type.ToString());
} }
@@ -147,5 +147,9 @@ namespace Advanced_Software_Engineering.Verbs.Value {
} }
throw new Exception("Could not convert " + text + " to any type"); throw new Exception("Could not convert " + text + " to any type");
} }
public static Exception ConvertToExpression(string expression) {
throw new NotImplementedException();
}
} }
} }