From 7ee230e20cc3566eb57dea88241613976c4b19ed Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Wed, 28 Nov 2018 10:56:35 +0100 Subject: [PATCH] Handle warnings, and add a warning if a function parameter has an unknown type --- Upsilon/Binder/Binder.cs | 1 + Upsilon/Diagnostics.cs | 24 ++++++++++++++++++++---- Upsilon/Evaluator/Script.cs | 1 - Ycicle/Program.cs | 32 +++++++++++++++++++++++--------- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/Upsilon/Binder/Binder.cs b/Upsilon/Binder/Binder.cs index ea3cb3e..1e4950b 100644 --- a/Upsilon/Binder/Binder.cs +++ b/Upsilon/Binder/Binder.cs @@ -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); diff --git a/Upsilon/Diagnostics.cs b/Upsilon/Diagnostics.cs index 7f0720d..ab7a0c3 100644 --- a/Upsilon/Diagnostics.cs +++ b/Upsilon/Diagnostics.cs @@ -9,7 +9,8 @@ namespace Upsilon public class Diagnostics { public SourceText ScriptString { get; } - public readonly List Messages = new List(); + public readonly List Errors = new List(); + public readonly List Warnings = new List(); 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; diff --git a/Upsilon/Evaluator/Script.cs b/Upsilon/Evaluator/Script.cs index a9e5a0b..75264d7 100644 --- a/Upsilon/Evaluator/Script.cs +++ b/Upsilon/Evaluator/Script.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Collections.Immutable; using Upsilon.BaseTypes; using Upsilon.Binder; diff --git a/Ycicle/Program.cs b/Ycicle/Program.cs index caaff17..6171520 100644 --- a/Ycicle/Program.cs +++ b/Ycicle/Program.cs @@ -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;