Upgrade to .NET 8 and code cleanup

This commit is contained in:
Stone_Red
2024-08-07 19:48:03 +02:00
parent 84afe70c96
commit 7078ed5637
33 changed files with 960 additions and 1008 deletions
@@ -9,105 +9,104 @@ using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Runtime;
using YesNt.Interpreter.Utilities;
namespace YesNt.Interpreter.Statements
namespace YesNt.Interpreter.Statements;
internal class SystemStatements : StatementRuntimeInformation
{
internal class SystemStatements : StatementRuntimeInformation
[Statement("exc", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta, Priority = Priority.Low, Separator = "|")]
public void ExecuteProgramWithArgs(string input)
{
[Statement("exc", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta, Priority = Priority.Low, Seperator = "|")]
public void ExecuteProgramWithArgs(string input)
string[] parts = input.FromSafeString().Split('|');
parts[0] = parts[0].Trim();
string[] functionArguments = parts[1].Split(',');
foreach (string argument in functionArguments)
{
string[] parts = input.FromSafeString().Split('|');
parts[0] = parts[0].Trim();
string[] functionArgumets = parts[1].Split(',');
foreach (string argumanet in functionArgumets)
{
RuntimeInfo.InParametersStack.Push(argumanet.Trim());
}
try
{
StartProcess(parts[0], string.Join(string.Empty, RuntimeInfo.InParametersStack.Reverse()));
}
catch (FileNotFoundException)
{
RuntimeInfo.Exit($"Cannot find file \"{parts[0]}\".", false);
}
catch (Win32Exception ex)
{
RuntimeInfo.Exit($"Failed to start \"{parts[0]}\". {ex.Message}", false);
}
//HACK: Clear line to avoid execution from other "exc" statement
RuntimeInfo.CurrentLine = string.Empty;
RuntimeInfo.InParametersStack.Push(argument.Trim());
}
[Statement("exc", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta, Priority = Priority.VeryLow)]
public void ExecuteProgram(string input)
try
{
try
{
StartProcess(input, string.Join(string.Empty, RuntimeInfo.InParametersStack.Reverse()));
}
catch (FileNotFoundException)
{
RuntimeInfo.Exit($"Cannot find file \"{input}\".", false);
}
catch (Win32Exception ex)
{
RuntimeInfo.Exit($"Failed to start \"{input}\". {ex.Message}", false);
}
StartProcess(parts[0], string.Join(string.Empty, RuntimeInfo.InParametersStack.Reverse()));
}
catch (FileNotFoundException)
{
RuntimeInfo.Exit($"Cannot find file \"{parts[0]}\".", false);
}
catch (Win32Exception ex)
{
RuntimeInfo.Exit($"Failed to start \"{parts[0]}\". {ex.Message}", false);
}
private void StartProcess(string name, string args)
//HACK: Clear line to avoid execution from other "exc" statement
RuntimeInfo.CurrentLine = string.Empty;
}
[Statement("exc", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta, Priority = Priority.VeryLow)]
public void ExecuteProgram(string input)
{
try
{
RuntimeInfo.OutParametersStack.Clear();
FixedProcess process = new FixedProcess
{
StartInfo = new ProcessStartInfo()
{
FileName = name,
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true
}
};
process.OutputDataReceived += Process_OutputDataReceived;
process.ErrorDataReceived += Process_ErrorDataReceived;
_ = process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
RuntimeInfo.InParametersStack.Clear();
RuntimeInfo.OutParametersStack.Push(process.ExitCode.ToString());
StartProcess(input, string.Join(string.Empty, RuntimeInfo.InParametersStack.Reverse()));
}
private void Process_ErrorDataReceived(object sender, Utilities.DataReceivedEventArgs e)
catch (FileNotFoundException)
{
if (string.IsNullOrWhiteSpace(e.Data))
{
return;
}
RuntimeInfo.OutParametersStack.Push(e.Data);
Console.Write("Error: " + e.Data);
RuntimeInfo.Exit($"Cannot find file \"{input}\".", false);
}
private void Process_OutputDataReceived(object sender, Utilities.DataReceivedEventArgs e)
catch (Win32Exception ex)
{
if (string.IsNullOrWhiteSpace(e.Data))
{
return;
}
RuntimeInfo.OutParametersStack.Push(e.Data);
Console.Write(e.Data);
RuntimeInfo.Exit($"Failed to start \"{input}\". {ex.Message}", false);
}
}
private void StartProcess(string name, string args)
{
RuntimeInfo.OutParametersStack.Clear();
FixedProcess process = new FixedProcess
{
StartInfo = new ProcessStartInfo()
{
FileName = name,
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true
}
};
process.OutputDataReceived += Process_OutputDataReceived;
process.ErrorDataReceived += Process_ErrorDataReceived;
_ = process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
RuntimeInfo.InParametersStack.Clear();
RuntimeInfo.OutParametersStack.Push(process.ExitCode.ToString());
}
private void Process_ErrorDataReceived(object sender, Utilities.DataReceivedEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.Data))
{
return;
}
RuntimeInfo.OutParametersStack.Push(e.Data);
Console.Write("Error: " + e.Data);
}
private void Process_OutputDataReceived(object sender, Utilities.DataReceivedEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.Data))
{
return;
}
RuntimeInfo.OutParametersStack.Push(e.Data);
Console.Write(e.Data);
}
}