Skip to content

Commit

Permalink
(consoleapp) Enable nullability checks for the Program class (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
perlun authored Apr 15, 2023
1 parent 9331a81 commit 893c5be
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
4 changes: 4 additions & 0 deletions release-notes/v0.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
- Implement `AsciiString` for ASCII-only strings [[#377][377]]
- Implement `Utf8String` for strings with non-ASCII content [[#385][385]]

#### General
- Enable nullability checks for the `Program` class [[#386][386]]

#### Maintenance
- Bump TngTech.ArchUnitNET.xUnit from 0.5.0 to 0.10.5 [[#366][366]]
- Bump xunit from 2.4.1 to 2.4.2 [[#369][369]]
Expand All @@ -22,3 +25,4 @@
[377]: https://github.com/perlang-org/perlang/pull/377
[380]: https://github.com/perlang-org/perlang/pull/380
[385]: https://github.com/perlang-org/perlang/pull/385
[386]: https://github.com/perlang-org/perlang/pull/386
34 changes: 21 additions & 13 deletions src/Perlang.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.CommandLine;
Expand Down Expand Up @@ -86,7 +87,7 @@ public static int Main(string[] args)
/// <param name="console">A custom `IConsole` implementation to use. May be null, in which case the standard
/// streams of the calling process will be used.</param>
/// <returns>Zero if the program executed successfully; non-zero otherwise.</returns>
public static int MainWithCustomConsole(string[] args, IConsole console)
public static int MainWithCustomConsole(string[] args, IConsole? console)
{
var versionOption = new Option<bool>(new[] { "--version", "-v" }, "Show version information");
var detailedVersionOption = new Option<bool>("-V", "Show detailed version information");
Expand All @@ -98,7 +99,7 @@ public static int MainWithCustomConsole(string[] args, IConsole console)

noWarnAsErrorOption.AddValidator(result =>
{
string warningName = result.GetValueOrDefault<string>();
string warningName = result.GetValueOrDefault<string>()!;

if (!WarningType.KnownWarning(warningName))
{
Expand Down Expand Up @@ -166,7 +167,7 @@ public static int MainWithCustomConsole(string[] args, IConsole console)

if (parseResult.HasOption(evalOption))
{
string source = parseResult.GetValueForOption(evalOption);
string source = parseResult.GetValueForOption(evalOption)!;

var program = new Program(
replMode: true,
Expand All @@ -180,7 +181,7 @@ public static int MainWithCustomConsole(string[] args, IConsole console)
}
else if (parseResult.HasOption(printOption))
{
string source = parseResult.GetValueForOption(printOption);
string source = parseResult.GetValueForOption(printOption)!;

new Program(
replMode: true,
Expand Down Expand Up @@ -263,15 +264,15 @@ public static int MainWithCustomConsole(string[] args, IConsole console)

internal Program(
bool replMode,
IEnumerable<string> arguments = null,
IEnumerable<WarningType> disabledWarningsAsErrors = null,
Action<string> standardOutputHandler = null,
Action<RuntimeError> runtimeErrorHandler = null)
Action<string> standardOutputHandler,
IEnumerable<string>? arguments = null,
IEnumerable<WarningType>? disabledWarningsAsErrors = null,
Action<RuntimeError>? runtimeErrorHandler = null)
{
// TODO: Make these be separate handlers at some point, so the caller can separate between these types of
// output.
this.standardOutputHandler = standardOutputHandler ?? Console.WriteLine;
this.standardErrorHandler = standardOutputHandler ?? Console.Error.WriteLine;
this.standardOutputHandler = standardOutputHandler;
this.standardErrorHandler = standardOutputHandler;
this.disabledWarningsAsErrors = (disabledWarningsAsErrors ?? Enumerable.Empty<WarningType>()).ToHashSet();

interpreter = new PerlangInterpreter(
Expand Down Expand Up @@ -386,19 +387,26 @@ private void ShowHelp()

internal int Run(string source, CompilerWarningHandler compilerWarningHandler)
{
object result = interpreter.Eval(source, ScanError, ParseError, NameResolutionError, ValidationError, ValidationError, compilerWarningHandler);
object? result = interpreter.Eval(source, ScanError, ParseError, NameResolutionError, ValidationError, ValidationError, compilerWarningHandler);

if (result != null && result != VoidObject.Void)
{
standardOutputHandler(result.ToString());
standardOutputHandler(result.ToString() ?? String.Empty);
}

return (int)ExitCodes.SUCCESS;
}

private void ParseAndPrint(string source)
{
string result = interpreter.Parse(source, ScanError, ParseError);
string? result = interpreter.Parse(source, ScanError, ParseError);

if (result == null)
{
// Parse() returns `null` when one or more errors occurred. These errors have already been reported to
// the user at this point, so we can safely just return here.
return;
}

standardOutputHandler(result);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Perlang.Tests/ConsoleApp/ProgramTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,8 @@ public void invalid_expression()
{
CallWithPrintParameter("hej hej");

// TODO: Hmm, I wonder why this prints an extra linebreak?
StdoutLines.Should().Equal(
"[line 1] Error at 'hej': Expect ';' after expression.", String.Empty, String.Empty
"[line 1] Error at 'hej': Expect ';' after expression.", String.Empty
);
}

Expand Down

0 comments on commit 893c5be

Please sign in to comment.