Better evaluation error handling

This commit is contained in:
Deukhoofd 2019-01-25 17:22:07 +01:00
parent 575889bed1
commit 7e50111724
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 14 additions and 5 deletions

View File

@ -28,7 +28,7 @@ namespace Upsilon.BaseTypes.ScriptFunction
if (option == null)
{
throw new EvaluationException(script.FileName,
$"No valid function found");
$"No valid function found", span);
}
return option.Run(diagnostics, variables, script, scope, span);
}

View File

@ -89,7 +89,8 @@ namespace Upsilon.Evaluator
var option = function.GetValidOption(parameters);
if (option == null)
throw new EvaluationException(_script.FileName,
$"No function found with name '{functionName}' and available parameters {string.Join(", ", parameters.Select(x => $"{x.GetType().Name}"))}");
$"No function found with name '{functionName}' and available parameters {string.Join(", ", parameters.Select(x => $"{x.GetType().Name}"))}",
e.Span);
var result = option.Run(_diagnostics, parameters?.Select(x => x.ToScriptType()).ToArray(), _script, Scope, new TextSpan());
return result;
@ -625,9 +626,14 @@ namespace Upsilon.Evaluator
private ScriptType EvaluateFullStopIndexExpression(BoundFullStopIndexExpression e)
{
var variable = EvaluateExpression(e.Expression);
if (variable.Type == Type.Nil)
{
throw new EvaluationException(_script.FileName, $"Nil variable can't be indexed", e.Span);
}
if (!(variable is IIndexable indexable))
{
throw new Exception("Variable is not indexable.");
throw new EvaluationException(_script.FileName,
$"Variable of type '{variable.Type}' is not indexable.", e.Span);
}
var scope = Scope;

View File

@ -1,4 +1,5 @@
using System;
using Upsilon.Text;
namespace Upsilon.Exceptions
{
@ -6,16 +7,18 @@ namespace Upsilon.Exceptions
{
public string FileName { get; }
public string ErrorMessage { get; }
public TextSpan Span { get; }
public EvaluationException(string fileName, string message)
public EvaluationException(string fileName, string message, TextSpan span)
{
FileName = fileName;
ErrorMessage = message;
Span = span;
}
public override string ToString()
{
return $"[{FileName}] {ErrorMessage}";
return $"[{FileName}] ({Span.StartLine},{Span.StartPosition}) {ErrorMessage}";
}
public override string Message => ToString();