Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to execute JavaScript files from the CLI #9

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions JSS/CommandLineOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using CommandLine;

namespace JSS.CLI;

internal interface IExecutionOptions
{
[Option('s', "script", HelpText = "Path to a JavaScript file to execute.")]
public string? Script { get; }
}

internal sealed class CommandLineOptions : IExecutionOptions
{
public CommandLineOptions(string? script)
{
Script = script;
}

public string? Script { get; }
}
35 changes: 35 additions & 0 deletions JSS/FileExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using JSS.Lib;
using JSS.Lib.Execution;

namespace JSS.CLI;

internal sealed class FileExecutor
{
public FileExecutor(VM globalVm, string filePath)
{
_vm = globalVm;
_filePath = filePath;
}

public void ExecuteFile()
{
try
{
var fileContent = File.ReadAllText(_filePath);
var parser = new Parser(fileContent);
var script = parser.Parse(_vm);
var result = script.ScriptEvaluation();
Print.PrintCompletion(result);
}
catch (Exception e)
{
Print.PrintException(e);
}

Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}

private readonly VM _vm;
private readonly string _filePath;
}
4 changes: 4 additions & 0 deletions JSS/JSS.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\JSS.Lib\JSS.Lib.csproj" />
</ItemGroup>
Expand Down
35 changes: 15 additions & 20 deletions JSS/Program.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
using JSS.Lib;
using CommandLine;
using JSS.CLI;
using JSS.Lib.Execution;

var completion = Realm.InitializeHostDefinedRealm(out VM globalVm);
if (completion.IsAbruptCompletion())
{
Print.PrintCompletion(completion);
return;
}

while (true)
{
Console.Write("> ");

try
Parser.Default.ParseArguments<CommandLineOptions>(args)
.WithParsed(options =>
{
var input = Console.ReadLine() ?? "";
var parser = new Parser(input);
var script = parser.Parse(globalVm);
var result = script.ScriptEvaluation();
Print.PrintCompletion(result);
}
catch (Exception e)
{
Print.PrintException(e);
}

// NOTE: We need this empty write line to prevent the background color from spilling over
Console.WriteLine();
}
if (!string.IsNullOrEmpty(options.Script))
{
var fileExecutor = new FileExecutor(globalVm, options.Script);
fileExecutor.ExecuteFile();
}
else
{
var repl = new Repl(globalVm);
repl.ExecuteRepl();
}
});
42 changes: 42 additions & 0 deletions JSS/Repl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using JSS.Lib;
using JSS.Lib.Execution;

namespace JSS.CLI;

internal sealed class Repl
{
public Repl(VM globalVm)
{
_vm = globalVm;
}

public void ExecuteRepl()
{
while (true)
{
Console.Write("> ");
EvaluateLine();
}
}

private void EvaluateLine()
{
try
{
var input = Console.ReadLine() ?? "";
var parser = new Parser(input);
var script = parser.Parse(_vm);
var result = script.ScriptEvaluation();
Print.PrintCompletion(result);
}
catch (Exception e)
{
Print.PrintException(e);
}

// NOTE: We need this empty write line to prevent the background color from spilling over
Console.WriteLine();
}

private readonly VM _vm;
}
Loading