static void Main(string[] args) { var input = ""; var pattern = new Regex(@"(?<commandType>[A-Za-z]+)\((?<commandArgs>.*)\)"); var interpreter = new Interpreter(); while ((input = Console.ReadLine()) != "System Split") { var match = pattern.Match(input); var command = match.Groups["commandType"].Value; var arguments = GetArgs(match.Groups["commandArgs"].Value); switch (command) { case "RegisterPowerHardware": interpreter.RegisterHardware <PowerHardware>(arguments[0], int.Parse(arguments[1]), int.Parse(arguments[2])); break; case "RegisterHeavyHardware": interpreter.RegisterHardware <HeavyHardware>(arguments[0], int.Parse(arguments[1]), int.Parse(arguments[2])); break; case "RegisterExpressSoftware": interpreter.RegisterSoftware <ExpressSoftware>(arguments[0], arguments[1], int.Parse(arguments[2]), int.Parse(arguments[3])); break; case "RegisterLightSoftware": interpreter.RegisterSoftware <LightSoftware>(arguments[0], arguments[1], int.Parse(arguments[2]), int.Parse(arguments[3])); break; case "ReleaseSoftwareComponent": interpreter.ReleaseSoftwareComponent(arguments[0], arguments[1]); break; case "Analyze": Console.WriteLine(interpreter.Analyze()); break; case "Dump": interpreter.Dump(arguments[0]); break; case "Restore": interpreter.Restore(arguments[0]); break; case "Destroy": interpreter.Destroy(arguments[0]); break; case "DumpAnalyze": Console.WriteLine(interpreter.DumpAnalyze()); break; } } Console.Write(interpreter.SystemSplit()); }
public void Analyze_a_valid_expression() { var target = new Interpreter() .SetVariable("x", 10.0); var analysis = target.Analyze("x + y * Math.Pow(x, 2)", typeof(void), new Parameter("y", 5.0)); Assert.IsTrue(analysis.Success); Assert.IsNull(analysis.Exception); Assert.AreEqual(typeof(double), analysis.ReturnType); }
public void Analyze_an_invalid_expression_unknown_identifier_y() { var target = new Interpreter() .SetVariable("x", 10.0); var analysis = target.Analyze("x + y * Math.Pow(x, 2)", typeof(void)); Assert.IsFalse(analysis.Success); Assert.IsNull(analysis.ReturnType); Assert.IsInstanceOfType(analysis.Exception, typeof(UnknownIdentifierException)); var exception = (UnknownIdentifierException)analysis.Exception; Assert.AreEqual("y", exception.Identifier); Assert.AreEqual(4, exception.Position); }
public void Analyze_an_invalid_expression_unknown_identifier_x() { var target = new Interpreter(); var analysis = target.Analyze("x + y * Math.Pow(x, 2)", typeof(void)); Assert.IsFalse(analysis.Success); Assert.AreEqual("Unknown identifier 'x' (at index 0).", analysis.Exception.Message); Assert.IsNull(analysis.ReturnType); Assert.IsInstanceOfType(analysis.Exception, typeof(UnknownIdentifierException)); var exception = (UnknownIdentifierException)analysis.Exception; Assert.AreEqual("x", exception.Identifier); Assert.AreEqual(0, exception.Position); }
public void Analyze_an_invalid_expression_unknown_method() { var target = new Interpreter() .SetVariable("x", 10.0); var analysis = target.Analyze("Math.NotExistingMethod(x, 2)", typeof(void)); Assert.IsFalse(analysis.Success); Assert.IsNull(analysis.ReturnType); Assert.IsInstanceOfType(analysis.Exception, typeof(NoApplicableMethodException)); var exception = (NoApplicableMethodException)analysis.Exception; Assert.AreEqual("NotExistingMethod", exception.MethodName); Assert.AreEqual("Math", exception.MethodTypeName); Assert.AreEqual(5, exception.Position); }