string Evaluate(string code) { if (process == null) { return("No process is being debugged"); } if (process.IsRunning) { return("The process is running"); } try { Value val = AstEvaluator.Evaluate(code, SupportedLanguage.CSharp, process.SelectedStackFrame); if (val == null) { return(string.Empty); } if (val.IsNull) { return("null"); } else { return(val.InvokeToString()); } } catch (GetValueException e) { return(e.Message); } }
/// <summary> /// Gets variable of given name. /// Returns null if unsuccessful. /// </summary> public Value GetValueFromName(string variableName) { if (debuggedProcess == null || debuggedProcess.IsRunning || debuggedProcess.SelectedStackFrame == null) { return(null); } else { return(AstEvaluator.Evaluate(variableName, SupportedLanguage.CSharp, debuggedProcess.SelectedStackFrame)); } }
public override void RefreshPad() { if (debuggedProcess == null || debuggedProcess.IsRunning || debuggedProcess.SelectedStackFrame == null) { return; } using (new PrintTimes("Watch Pad refresh")) { try { watchList.BeginUpdate(); Utils.DoEvents(debuggedProcess); List <TreeViewVarNode> nodes = new List <TreeViewVarNode>(); foreach (var nod in watches) { try { LoggingService.Info("Evaluating: " + (string.IsNullOrEmpty(nod.Name) ? "is null or empty!" : nod.Name)); Value val = AstEvaluator.Evaluate(nod.Name, SupportedLanguage.CSharp, debuggedProcess.SelectedStackFrame); ValueNode valNode = new ValueNode(val); valNode.SetName(nod.Name); nodes.Add(new TreeViewVarNode(debuggedProcess, watchList, valNode)); } catch (GetValueException) { string error = String.Format(StringParser.Parse("${res:MainWindow.Windows.Debug.Watch.InvalidExpression}"), nod.Name); ErrorInfoNode infoNode = new ErrorInfoNode(nod.Name, error); nodes.Add(new TreeViewVarNode(debuggedProcess, watchList, infoNode)); } } watchList.Root.Children.Clear(); foreach (TreeViewVarNode nod in nodes) { watchList.Root.Children.Add(nod); } } catch (AbortedBecauseDebuggeeResumedException) { } catch (Exception ex) { if (debuggedProcess == null || debuggedProcess.HasExited) { // Process unexpectedly exited } else { MessageService.ShowError(ex); } } } watchList.EndUpdate(); }
public void Evaluate_PowerOfNegative_ReturnsCorrectResult() { // Arrange var input = new BinaryNode( new UnaryPrefixNode(UnaryOperatorType.Minus, new UnaryValueNode(2)), new[] { new BinaryNodeItem(BinaryOperatorType.Power, new UnaryValueNode(2)) }); var parser = new AstEvaluator(); // Act var result = parser.Evaluate(input); // Assert Assert.Equal(4, result); }
public void Evaluate_SameOperatorRightOrder_ReturnsCorrectResult() { // Arrange var input = new BinaryNode( new UnaryValueNode(2), new[] { new BinaryNodeItem(BinaryOperatorType.Power, new UnaryValueNode(3)), new BinaryNodeItem(BinaryOperatorType.Power, new UnaryValueNode(2)) }); var parser = new AstEvaluator(); // Act var result = parser.Evaluate(input); // Assert Assert.Equal(512, result); }
public void Evaluate_SimpleOneLevelAst_ReturnsCorrectResult() { // Arrange var input = new BinaryNode( new UnaryValueNode(2), new[] { new BinaryNodeItem(BinaryOperatorType.Add, new UnaryValueNode(3)), new BinaryNodeItem(BinaryOperatorType.Multiply, new UnaryValueNode(3)) }); var parser = new AstEvaluator(); // Act var result = parser.Evaluate(input); // Assert Assert.Equal(11, result); }
public void Evaluate_NegativePowerOfPositive_ReturnsCorrectResult() { // Arrange var power = new BinaryNode( new UnaryValueNode(2), new[] { new BinaryNodeItem(BinaryOperatorType.Power, new UnaryValueNode(2)) }); var input = new BinaryNode(new UnaryPrefixNode(UnaryOperatorType.Minus, new UnaryExpressionNode(power))); var parser = new AstEvaluator(); // Act var result = parser.Evaluate(input); // Assert Assert.Equal(-Math.Pow(2, 2), result); }
bool Evaluate(string code, string language) { try { SupportedLanguage supportedLanguage = (SupportedLanguage)Enum.Parse(typeof(SupportedLanguage), language.Replace("#", "Sharp"), true); Value val = AstEvaluator.Evaluate(code, supportedLanguage, debuggedProcess.SelectedStackFrame); if (val != null && val.Type.IsPrimitive && val.PrimitiveValue is bool) { return((bool)val.PrimitiveValue); } else { return(false); } } catch (GetValueException e) { string errorMessage = "Error while evaluating breakpoint condition " + code + ":\n" + e.Message + "\n"; DebuggerService.PrintDebugMessage(errorMessage); WorkbenchSingleton.SafeThreadAsyncCall(MessageService.ShowWarning, errorMessage); return(true); } }
public void Evaluate_ComplexOneLevelAst_ReturnsCorrectResult() { // Arrange var input = new BinaryNode( new UnaryValueNode(2), new[] { new BinaryNodeItem(BinaryOperatorType.Multiply, new UnaryValueNode(2)), new BinaryNodeItem(BinaryOperatorType.Subtract, new UnaryValueNode(3)), new BinaryNodeItem(BinaryOperatorType.Power, new UnaryValueNode(3)), new BinaryNodeItem(BinaryOperatorType.Subtract, new UnaryValueNode(4)), new BinaryNodeItem(BinaryOperatorType.Multiply, new UnaryValueNode(4)), }); var parser = new AstEvaluator(); // Act var result = parser.Evaluate(input); // Assert Assert.Equal(-39, result); }
public void Evaluate_PowerOfNegativeExpression_ReturnsCorrectResult() { // Arrange var subtract = new BinaryNode( new UnaryValueNode(1), new[] { new BinaryNodeItem(BinaryOperatorType.Subtract, new UnaryValueNode(2)) }); var input = new BinaryNode( new UnaryExpressionNode(subtract), new[] { new BinaryNodeItem(BinaryOperatorType.Power, new UnaryValueNode(3)) }); var parser = new AstEvaluator(); // Act var result = parser.Evaluate(input); // Assert Assert.Equal(Math.Pow(1 - 2, 3), result); }