-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💾 Feat(REPL): Divided into
console
and run
two commands.
Interactive mode was moved to `console` command which is default and provide '-e' and '--execute' parameter to execute codes from arguments.
- Loading branch information
1 parent
ebec683
commit 642426f
Showing
4 changed files
with
104 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |