Skip to content

Commit

Permalink
💾 Feat(REPL): Divided into console and run two commands.
Browse files Browse the repository at this point in the history
Interactive mode was moved to `console` command which is default and provide '-e' and '--execute' parameter to execute codes from arguments.
  • Loading branch information
Dynesshely committed Feb 18, 2024
1 parent ebec683 commit 642426f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Csharpell/Csharpell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<Version>
0.4.$([System.DateTime]::UtcNow.Date.Subtract($([System.DateTime]::Parse("2023-07-20"))).TotalDays).$([System.Math]::Floor($([System.DateTime]::UtcNow.TimeOfDay.TotalMinutes)))
0.5.$([System.DateTime]::UtcNow.Date.Subtract($([System.DateTime]::Parse("2023-07-20"))).TotalDays).$([System.Math]::Floor($([System.DateTime]::UtcNow.TimeOfDay.TotalMinutes)))
</Version>
</PropertyGroup>

Expand Down
2 changes: 2 additions & 0 deletions Csharpell/Options.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using CommandLine;

namespace Csharpell;

public class Options
{
[Option('v', "verbose", Required = false, HelpText = "Set output to verbose messages.")]
Expand Down
137 changes: 88 additions & 49 deletions Csharpell/Program.cs
Original file line number Diff line number Diff line change
@@ -1,85 +1,124 @@

// This project is a shell to execute csharp code immediately like python.
// This project is a shell to execute csharp code immediately like python.
// You can use top-level codes with this tool to write pythonike codes.

using CommandLine;
using Csharpell;
using Csharpell.Core;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using System.Reflection;

Parser.Default.ParseArguments<Options, Verbs.RunVerbOptions>(args)
Parser.Default.ParseArguments<Options, ConsoleVerbOptions, RunVerbOptions>(args)
.WithParsed<Options>(options =>
{
})
.WithParsed<Verbs.RunVerbOptions>(async options =>
.WithParsed<ConsoleVerbOptions>(async options =>
{
var verbose = options.Verbose ? " (Verbose)" : "";
if (options.Interactive)
{
Console.WriteLine($"Entering interactive mode.{verbose}");
var keepRunning = true;
var keepRunning = true;
var engine = new CSharpScriptEngine();
var engine = new CSharpScriptEngine();
while (keepRunning)
{
Console.Write(">>> ");
await engine.ExecuteAsync("", addDefaultImports: false, runInReplMode: true);
var line = Console.ReadLine() ?? "";
if (options.Command is not null)
{
try
{
var result = (await engine.ExecuteAsync(
options.Command,
options => options
.WithReferences(Assembly.GetExecutingAssembly())
.WithLanguageVersion(LanguageVersion.Preview),
addDefaultImports: true,
runInReplMode: false
))?.ToString();
if (line.ToLower().Equals("exit"))
{
keepRunning = false;
}
else
{
try
{
var result = await engine.ExecuteAsync(line);
// Check if result is end with new line.
if (result?.ToString()?.EndsWith(Environment.NewLine) ?? true)
{
Console.Write(result);
}
else
{
Console.WriteLine(result);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
if (string.IsNullOrWhiteSpace(result) == false)
Console.WriteLine(result);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
return;
}
else
Console.WriteLine($"Entered C# REPL console.{verbose}");
while (keepRunning)
{
Console.WriteLine($"Executing file{verbose}: \"{options?.Path}\"");
Console.Write(">>> ");
var line = Console.ReadLine() ?? "";
if (File.Exists(options?.Path))
if (line.ToLower().Equals("exit"))
{
keepRunning = false;
}
else
{
try
{
var content = File.ReadAllText(options?.Path ?? "");
var engine = new CSharpScriptEngine();
var result = engine.ExecuteAsync(content);
var result = await engine.ExecuteAsync(
line,
options => options
.WithReferences(Assembly.GetExecutingAssembly())
.WithLanguageVersion(LanguageVersion.Preview),
addDefaultImports: true,
runInReplMode: true
);
Console.WriteLine(result);
// Check if result is end with new line.
if (result?.ToString()?.EndsWith(Environment.NewLine) ?? true)
{
Console.Write(result);
}
else
{
Console.WriteLine(result);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
else
}
})
.WithParsed<RunVerbOptions>(options =>
{
var verbose = options.Verbose ? " (Verbose)" : "";
Console.WriteLine($"Executing file{verbose}: \"{options?.Path}\"");
if (File.Exists(options?.Path))
{
try
{
Console.WriteLine($"File not found: \"{options?.Path}\"");
var content = File.ReadAllText(options?.Path ?? "");
var engine = new CSharpScriptEngine();
var result = engine.ExecuteAsync(content, runInReplMode: false);
Console.WriteLine(result);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
else
{
Console.WriteLine($"File not found: \"{options?.Path}\"");
}
})
.WithNotParsed(errs => Console.WriteLine("Failed to parse arguments."))
;
23 changes: 13 additions & 10 deletions Csharpell/Verbs.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using CommandLine;
using CommandLine;

public class Verbs
namespace Csharpell;

[Verb("console", isDefault: true, HelpText = "Enter C# REPL console.")]
public class ConsoleVerbOptions : Options
{
[Verb("run", isDefault: true, HelpText = "Run C# file as script.")]
public class RunVerbOptions: Options
{
[Option('p', "path", Default = "./Program.cs", Required = false, HelpText = "Path to C# file to run.")]
public string? Path { get; set; }
[Option('e', "execute", Default = null, Required = false, HelpText = "Execute command from arguments.")]
public string? Command { get; set; }
}

[Option('i', "interactive", Default = false, Required = false, HelpText = "Run with interactive mode.")]
public bool Interactive { get; set; }
}
[Verb("run", HelpText = "Run C# file as script.")]
public class RunVerbOptions : Options
{
[Option('p', "path", Default = "./Program.cs", Required = false, HelpText = "Path to C# file to run.")]
public string? Path { get; set; }
}

0 comments on commit 642426f

Please sign in to comment.