Skip to content

Commit

Permalink
(consoleapp) Removed custom IConsole implementations (#456)
Browse files Browse the repository at this point in the history
This is a preparation for rewriting the argument handling in Perlang, as
one of the very first elements of the compiler to become self-hosted.
Regretfully, this means that we will lose some of our test coverage, but
we will have to revisit that aspect later.
  • Loading branch information
perlun authored Apr 12, 2024
1 parent 682475f commit 08cc476
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 710 deletions.
8 changes: 0 additions & 8 deletions src/Perlang.ConsoleApp/IPerlangConsole.cs

This file was deleted.

117 changes: 0 additions & 117 deletions src/Perlang.ConsoleApp/PerlangConsole.cs

This file was deleted.

56 changes: 12 additions & 44 deletions src/Perlang.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.IO;
using System.CommandLine.NamingConventionBinder;
using System.CommandLine.Parsing;
using System.IO;
Expand All @@ -26,13 +25,6 @@ namespace Perlang.ConsoleApp
{
public class Program
{
private static readonly ISet<string> ReplCommands = new HashSet<string>(new List<string>
{
"exit",
"help",
"quit"
}.Select(s => "/" + s));

internal enum ExitCodes
{
/// <summary>
Expand Down Expand Up @@ -103,21 +95,6 @@ internal enum ExitCodes
/// <param name="args">The command line arguments.</param>
/// <returns>Zero if the program executed successfully; non-zero otherwise.</returns>
public static int Main(string[] args)
{
return MainWithCustomConsole(args, console: new PerlangConsole());
}

/// <summary>
/// Entry point for `perlang`, with the possibility to override the console streams (stdin, stdout, stderr).
///
/// This constructor is typically used from tests, wishing to intercept the output to stdout and/or stderr.
/// </summary>
/// <param name="args">The command line arguments.</param>
/// <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>
// TODO: Replace IConsole here with interface which is "Perlang string-aware", so we can delegate to libc write() instead of using Console.WriteLine.
public static int MainWithCustomConsole(string[] args, IPerlangConsole console)
{
var versionOption = new Option<bool>(new[] { "--version", "-v" }, "Show version information");
var detailedVersionOption = new Option<bool>("-V", "Show detailed version information");
Expand Down Expand Up @@ -169,18 +146,18 @@ public static int MainWithCustomConsole(string[] args, IPerlangConsole console)
{
if (parseResult.HasOption(versionOption))
{
console.Out.WriteLine(CommonConstants.InformationalVersion);
Console.Out.WriteLine(CommonConstants.InformationalVersion);
return Task.FromResult(0);
}

if (parseResult.HasOption(detailedVersionOption))
{
console.Out.WriteLine($"Perlang {CommonConstants.InformationalVersion} (built from git commit {CommonConstants.GitCommit}) on .NET {Environment.Version}");
console.Out.WriteLine();
console.Out.WriteLine($" Number of detected (v)CPUs: {Environment.ProcessorCount}");
console.Out.WriteLine($" Running in 64-bit mode: {Environment.Is64BitProcess}");
console.Out.WriteLine($" Operating system info: {Environment.OSVersion.VersionString}");
console.Out.WriteLine();
Console.Out.WriteLine($"Perlang {CommonConstants.InformationalVersion} (built from git commit {CommonConstants.GitCommit}) on .NET {Environment.Version}");
Console.Out.WriteLine();
Console.Out.WriteLine($" Number of detected (v)CPUs: {Environment.ProcessorCount}");
Console.Out.WriteLine($" Running in 64-bit mode: {Environment.Is64BitProcess}");
Console.Out.WriteLine($" Operating system info: {Environment.OSVersion.VersionString}");
Console.Out.WriteLine();

return Task.FromResult(0);
}
Expand All @@ -191,7 +168,7 @@ public static int MainWithCustomConsole(string[] args, IPerlangConsole console)

new Program(
replMode: true,
standardOutputHandler: console.WriteStdoutLine,
standardOutputHandler: Console.WriteLine,
disabledWarningsAsErrors: disabledWarningsAsErrorsList
).ParseAndPrint(source);

Expand All @@ -209,18 +186,9 @@ public static int MainWithCustomConsole(string[] args, IPerlangConsole console)
{
string scriptName = parseResult.GetValueForArgument(scriptNameArgument);

// Console.Out.WriteLine(parseResult.Tokens);
// Console.Out.WriteLine(parseResult.GetValueForArgument(scriptArguments));
//
// if (parseResult.Tokens.Count != 1)
// {
// Console.Error.WriteLine("ERROR: When the -c option is used, no script arguments can be provided since the Perlang script/program will not get executed");
// return Task.FromResult(1);
// }

var program = new Program(
replMode: false,
standardOutputHandler: console.WriteStdoutLine,
standardOutputHandler: Console.WriteLine,
disabledWarningsAsErrors: disabledWarningsAsErrorsList,
experimentalCompilation: PerlangMode.ExperimentalCompilation
);
Expand All @@ -237,7 +205,7 @@ public static int MainWithCustomConsole(string[] args, IPerlangConsole console)
{
var program = new Program(
replMode: false,
standardOutputHandler: console.WriteStdoutLine,
standardOutputHandler: Console.WriteLine,
disabledWarningsAsErrors: disabledWarningsAsErrorsList,
experimentalCompilation: PerlangMode.ExperimentalCompilation
);
Expand All @@ -251,7 +219,7 @@ public static int MainWithCustomConsole(string[] args, IPerlangConsole console)
var program = new Program(
replMode: false,
arguments: remainingArguments,
standardOutputHandler: console.WriteStdoutLine,
standardOutputHandler: Console.WriteLine,
disabledWarningsAsErrors: disabledWarningsAsErrorsList,
experimentalCompilation: PerlangMode.ExperimentalCompilation
);
Expand Down Expand Up @@ -283,7 +251,7 @@ public static int MainWithCustomConsole(string[] args, IPerlangConsole console)
return new CommandLineBuilder(rootCommand)
.UseHelp()
.Build()
.Invoke(args, console);
.Invoke(args);
}

internal Program(
Expand Down
7 changes: 0 additions & 7 deletions src/Perlang.Stdlib/Stdlib/Libc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ internal static partial class Internal
#endif
internal static unsafe partial int memcmp(byte* b1, byte* b2, uint count);

#if _WINDOWS
[LibraryImport("ucrtbase", EntryPoint = "_write")]
#else // POSIX
[LibraryImport("libc", SetLastError = true)]
#endif
internal static partial int write(int fd, IntPtr s, int count);

#if _WINDOWS
[LibraryImport("ucrtbase")]
#else // POSIX
Expand Down
Loading

0 comments on commit 08cc476

Please sign in to comment.