More better error handling

This commit is contained in:
Deukhoofd 2019-01-26 12:15:15 +01:00
parent 7e50111724
commit eeecd0225a
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
1 changed files with 12 additions and 4 deletions

View File

@ -252,7 +252,7 @@ namespace Upsilon.Evaluator
} }
goto default; goto default;
default: default:
throw new Exception("Invalid Unary Operator: " + e.Operator.Kind); throw new EvaluationException(_script.FileName, "Invalid Unary Operator: " + e.Operator.Kind, e.Span);
} }
} }
@ -449,7 +449,9 @@ namespace Upsilon.Evaluator
{ {
return val; return val;
} }
throw new Exception($"Cannot find variable: '{e.Variable.VariableSymbol.Name}'");
throw new EvaluationException(_script.FileName, $"Cannot find variable: '{e.Variable.VariableSymbol.Name}'",
e.Span);
} }
private readonly Stack<BoundStatement> _todoStatements = new Stack<BoundStatement>(); private readonly Stack<BoundStatement> _todoStatements = new Stack<BoundStatement>();
@ -548,7 +550,8 @@ namespace Upsilon.Evaluator
var variable = EvaluateExpression(boundFunctionCallExpression.Identifier); var variable = EvaluateExpression(boundFunctionCallExpression.Identifier);
if (!(variable is ScriptFunction function)) if (!(variable is ScriptFunction function))
{ {
throw new Exception($"Variable is not a function."); throw new EvaluationException(_script.FileName, $"Variable is not a function.",
boundFunctionCallExpression.Identifier.Span);
} }
var ls = new List<ScriptType>(); var ls = new List<ScriptType>();
foreach (var t in boundFunctionCallExpression.Parameters) foreach (var t in boundFunctionCallExpression.Parameters)
@ -610,9 +613,14 @@ namespace Upsilon.Evaluator
private ScriptType EvaluateIndexExpression(BoundIndexExpression e) private ScriptType EvaluateIndexExpression(BoundIndexExpression e)
{ {
var variable = EvaluateExpression(e.Identifier); var variable = EvaluateExpression(e.Identifier);
if (variable.Type == Type.Nil)
{
throw new EvaluationException(_script.FileName, $"Nil variable can't be indexed", e.Span);
}
if (!(variable is IIndexable indexable)) 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; var scope = Scope;