Improve error description for evaluation failure.

This commit is contained in:
2023-05-29 17:06:30 +08:00
parent 88a46127d7
commit 6e553b9ebb

View File

@@ -24,16 +24,21 @@ namespace Cryville.Common.Pdt {
/// <param name="exp">The expression to evaluate.</param>
/// <returns>Whether the evaluaton succeeded.</returns>
public bool Evaluate(PdtOperator target, PdtExpression exp) {
var prevFrameCount = _framecount;
_revokepttconst = false;
for (var ip = exp.Instructions.First; ip != null; ip = ip.Next)
ip.Value.Execute(this, ref ip);
if (exp.IsPotentialConstant) {
exp.IsConstant = exp.IsPotentialConstant = !_revokepttconst;
try {
var prevFrameCount = _framecount;
_revokepttconst = false;
for (var ip = exp.Instructions.First; ip != null; ip = ip.Next)
ip.Value.Execute(this, ref ip);
if (exp.IsPotentialConstant) {
exp.IsConstant = exp.IsPotentialConstant = !_revokepttconst;
}
var ret = Operate(target, _framecount - prevFrameCount, true);
for (var i = prevFrameCount; i < _framecount; i++) DiscardStack();
return ret;
}
catch (Exception ex) {
throw new EvaluationFailureException(exp, ex);
}
var ret = Operate(target, _framecount - prevFrameCount, true);
for (var i = prevFrameCount; i < _framecount; i++) DiscardStack();
return ret;
}
/// <summary>
/// Optimizes an expression by merging its instructions.
@@ -257,5 +262,16 @@ namespace Cryville.Common.Pdt {
public EvaluationFailureException(string message, Exception innerException) : base(message, innerException) { }
/// <inheritdoc />
protected EvaluationFailureException(SerializationInfo info, StreamingContext context) : base(info, context) { }
/// <summary>
/// Creates an instance of the <see cref="EvaluationFailureException" /> class with the failing expression.
/// </summary>
/// <param name="exp">The failing expression.</param>
public EvaluationFailureException(PdtExpression exp) : base("Evaluation failed for the expression: " + exp.ToString()) { }
/// <summary>
/// Creates an instance of the <see cref="EvaluationFailureException" /> class with the failing expression and the inner exception.
/// </summary>
/// <param name="exp">The failing expression.</param>
/// <param name="innerException">The inner exception.</param>
public EvaluationFailureException(PdtExpression exp, Exception innerException) : base("Evaluation failed for the expression: " + exp.ToString(), innerException) { }
}
}