Better error reporter

This commit is contained in:
Deukhoofd 2019-01-17 17:48:10 +01:00
parent d341318989
commit f903a3ca58
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
5 changed files with 54 additions and 10 deletions

View File

@ -131,7 +131,7 @@ namespace Upsilon.Binder
case SyntaxKind.FunctionExpression:
return BindFunctionExpression((FunctionExpressionSyntax) e);
case SyntaxKind.BadExpression:
return new BoundLiteralExpression(new ScriptNull(), e.Span);
return new BoundBadExpression(e.Span);
case SyntaxKind.ScriptUnit:
break;
default:
@ -143,6 +143,8 @@ namespace Upsilon.Binder
private BoundExpression BindUnaryExpression(UnaryExpressionSyntax e)
{
var inExp = BindExpression(e.Expression);
if (inExp.Kind == BoundKind.BoundBadExpression)
return new BoundBadExpression(e.Span);
var op = BoundUnaryOperator.Bind(e.Operator.Kind, inExp.Type);
if (op == null)
{
@ -157,6 +159,8 @@ namespace Upsilon.Binder
{
var left = BindExpression(e.Left);
var right = BindExpression(e.Right);
if (left.Kind == BoundKind.BoundBadExpression || right.Kind == BoundKind.BoundBadExpression)
return new BoundBadExpression(e.Span);
var op = BoundBinaryOperator.Bind(e.Operator.Kind, left.Type, right.Type);
if (op == null)
{
@ -206,7 +210,7 @@ namespace Upsilon.Binder
if (expression.Type != Type.Function && expression.Type != Type.Unknown)
{
_diagnostics.LogError($"Unknown function called.", e.Span);
return new BoundLiteralExpression(new ScriptNull(), e.Span);
return new BoundBadExpression(e.Span);
}
var parameters = ImmutableArray.CreateBuilder<BoundExpression>();
@ -378,7 +382,7 @@ namespace Upsilon.Binder
if (!Scope.TryGetVariable(name, true, out var variable))
{
_diagnostics.LogUnknownVariable(e.Identifier.Span, name);
return new BoundLiteralExpression(new ScriptNull(), e.Span);
return new BoundBadExpression(e.Span);
}
var boundVariable = new BoundVariableSymbol(variable, false, e.Identifier.Span);
return new BoundVariableExpression(boundVariable, e.Span);
@ -480,7 +484,7 @@ namespace Upsilon.Binder
}
}
return new BoundExpressionStatement(new BoundLiteralExpression(new ScriptNull(), e.Span), e.Span);
return new BoundExpressionStatement(new BoundBadExpression(e.Span), e.Span);
}
private BoundStatement BindMultiAssignmentStatement(MultiAssignmentStatementSyntax s)
@ -642,7 +646,7 @@ namespace Upsilon.Binder
else
{
_diagnostics.LogCannotConvert(Type.Function, variable.Type, e.Span);
return new BoundExpressionStatement(new BoundLiteralExpression(new ScriptNull(), e.Span), e.Span);
return new BoundExpressionStatement(new BoundBadExpression(e.Span), e.Span);
}
}
}
@ -706,7 +710,7 @@ namespace Upsilon.Binder
if (index.Type != Type.Number && index.Type != Type.String && index.Type != Type.Unknown)
{
_diagnostics.LogInvalidIndexExpression(expression.Type, index.Type, e.Span);
return new BoundLiteralExpression(new ScriptNull(), e.Span);
return new BoundBadExpression(e.Span);
}
switch (expression.Type)
{
@ -756,7 +760,7 @@ namespace Upsilon.Binder
return new BoundIndexExpression(expression, index, Type.String, e.Span);
default:
_diagnostics.LogInvalidIndexExpression(expression.Type, index.Type, e.Span);
return new BoundLiteralExpression(new ScriptNull(), e.Span);
return new BoundBadExpression(e.Span);
}
}
@ -823,7 +827,7 @@ namespace Upsilon.Binder
return new BoundFullStopIndexExpression(expression, index, Type.String, e.Span);
default:
_diagnostics.LogInvalidIndexExpression(expression.Type, Type.String, e.Span);
return new BoundLiteralExpression(new ScriptNull(), e.Span);
return new BoundBadExpression(e.Span);
}
}

View File

@ -0,0 +1,26 @@
using System.Collections.Generic;
using Upsilon.BaseTypes;
using Upsilon.Text;
namespace Upsilon.Binder
{
public class BoundBadExpression : BoundExpression
{
public BoundBadExpression(TextSpan span) : base(span)
{
}
public override BoundKind Kind => BoundKind.BoundBadExpression;
public override IEnumerable<BoundNode> GetNodeAtPosition(int linePosition, int characterPosition)
{
yield break;
}
public override IEnumerable<BoundNode> GetChildren()
{
yield break;
}
public override Type Type => Type.Nil;
}
}

View File

@ -13,6 +13,7 @@ namespace Upsilon.Binder
BoundTableExpression,
BoundIndexExpression,
BoundFunctionExpression,
BoundBadExpression,
// Statements
BoundAssignmentStatement,
@ -29,6 +30,6 @@ namespace Upsilon.Binder
BoundNumericForStatement,
BoundGenericForStatement,
BoundBreakStatement,
BoundWhileStatement
BoundWhileStatement,
}
}

View File

@ -254,7 +254,7 @@ namespace Upsilon.Evaluator
case BoundKind.BoundFullstopIndexExpression:
return EvaluateFullStopIndexExpression((BoundFullStopIndexExpression) e);
default:
throw new NotImplementedException();
throw new NotImplementedException(e.Kind.ToString());
}
}

View File

@ -38,6 +38,19 @@ namespace Ycicle
Console.ResetColor();
continue;
}
script.Bind();
if (script.Diagnostics.Errors.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Errors were found during parsing");
foreach (var diagnosticsMessage in script.Diagnostics.Errors)
{
LogMessage(DiagnosticLevel.Error, diagnosticsMessage);
}
Console.ResetColor();
continue;
}
//Console.WriteLine(script.PrettyPrintSyntaxTree());
var evaluate = script.Evaluate();