Handle warnings, and add a warning if a function parameter has an unknown type

This commit is contained in:
Deukhoofd 2018-11-28 10:56:35 +01:00
parent 14e30d0855
commit 7ee230e20c
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 44 additions and 14 deletions

View File

@ -46,6 +46,7 @@ namespace Upsilon.Binder
foreach (var valueParameter in unboundFunctionStatement.Value.Parameters)
{
Scope.AssignToNearest(valueParameter.VariableSymbol);
_diagnostics.LogUnknownVariableType(valueParameter.VariableSymbol.Name, valueParameter.Span);
}
unboundFunctionStatement.Value.Block =
(BoundBlockStatement) BindBlockStatement(unboundFunctionStatement.Value.UnboundBlock);

View File

@ -9,7 +9,8 @@ namespace Upsilon
public class Diagnostics
{
public SourceText ScriptString { get; }
public readonly List<DiagnosticsMessage> Messages = new List<DiagnosticsMessage>();
public readonly List<DiagnosticsMessage> Errors = new List<DiagnosticsMessage>();
public readonly List<DiagnosticsMessage> Warnings = new List<DiagnosticsMessage>();
public bool ThrowsOnError { get; }
public Diagnostics(SourceText scriptString, bool throwsOnError)
@ -20,7 +21,10 @@ namespace Upsilon
public void Log(DiagnosticLevel level, string message, TextSpan location)
{
Messages.Add(new DiagnosticsMessage(this, level, message, location));
if (level == DiagnosticLevel.Error)
Errors.Add(new DiagnosticsMessage(this, level, message, location));
else if (level == DiagnosticLevel.Warning)
Warnings.Add(new DiagnosticsMessage(this, level, message, location));
}
public void LogError(string message, TextSpan location)
@ -34,6 +38,11 @@ namespace Upsilon
Log(DiagnosticLevel.Error, message, location);
}
public void LogWarning(string message, TextSpan location)
{
Log(DiagnosticLevel.Warning, message, location);
}
public void LogBadCharacter(TextSpan location, SyntaxKind expectedToken, SyntaxKind currentKind)
{
LogError($"Invalid character found. Expected: '{expectedToken}', Got: '{currentKind}'", location);
@ -87,18 +96,25 @@ namespace Upsilon
{
LogError($"Cannot index type '{expressionType}' with type '{indexType}'", span);
}
public void LogUnknownVariableType(string variableName, TextSpan span)
{
LogWarning(
$"Variable '{variableName}' has an unknown variable type, and as such errors won't be visible before evaluation.",
span);
}
}
public class DiagnosticsMessage
{
public Diagnostics Diagnostics { get; }
private readonly DiagnosticLevel _diagnosticLevel;
public DiagnosticLevel DiagnosticLevel { get; }
public string Message { get; }
public TextSpan Span { get; }
public DiagnosticsMessage(Diagnostics diagnostics, DiagnosticLevel diagnosticLevel, string message, TextSpan span)
{
_diagnosticLevel = diagnosticLevel;
DiagnosticLevel = diagnosticLevel;
Diagnostics = diagnostics;
Message = message;
Span = span;

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Upsilon.BaseTypes;
using Upsilon.Binder;

View File

@ -27,13 +27,13 @@ namespace Ycicle
? Executor.ParseInput(input, options)
: Executor.ContinueWith(script, input);
if (script.Diagnostics.Messages.Count > 0)
if (script.Diagnostics.Errors.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Errors were found during parsing");
foreach (var diagnosticsMessage in script.Diagnostics.Messages)
foreach (var diagnosticsMessage in script.Diagnostics.Errors)
{
LogError(diagnosticsMessage);
LogMessage(DiagnosticLevel.Error, diagnosticsMessage);
}
Console.ResetColor();
continue;
@ -41,18 +41,24 @@ namespace Ycicle
//Console.WriteLine(script.PrettyPrintSyntaxTree());
var evaluate = script.Evaluate();
if (script.Diagnostics.Messages.Count > 0)
if (script.Diagnostics.Errors.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Errors were found during evaluating");
foreach (var diagnosticsMessage in script.Diagnostics.Messages)
foreach (var diagnosticsMessage in script.Diagnostics.Errors)
{
LogError(diagnosticsMessage);
LogMessage(DiagnosticLevel.Error, diagnosticsMessage);
}
Console.ResetColor();
}
else
{
foreach (var diagnosticsMessage in script.Diagnostics.Warnings)
{
LogMessage(DiagnosticLevel.Warning, diagnosticsMessage);
}
Console.ResetColor();
if (evaluate == null)
continue;
Console.ForegroundColor = ConsoleColor.Cyan;
@ -63,16 +69,24 @@ namespace Ycicle
}
}
private static void LogError(DiagnosticsMessage message)
private static void LogMessage(DiagnosticLevel level, DiagnosticsMessage message)
{
Console.ForegroundColor = ConsoleColor.Red;
if (level == DiagnosticLevel.Error)
Console.ForegroundColor = ConsoleColor.Red;
else if (level == DiagnosticLevel.Warning)
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(message.Message);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(message.GetDiagnosticPosition() + " ");
Console.Write(message.LineBeforeError());
Console.ForegroundColor = ConsoleColor.Red;
if (level == DiagnosticLevel.Error)
Console.ForegroundColor = ConsoleColor.Red;
else if (level == DiagnosticLevel.Warning)
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.Write(message.AtError());
Console.ForegroundColor = ConsoleColor.Gray;