Add interactive exit function

This commit is contained in:
Jordan Wages 2026-06-30 03:03:11 -05:00
commit 1794d4ed8a
3 changed files with 64 additions and 6 deletions

View file

@ -0,0 +1,7 @@
namespace cs_mic_cli
{
internal class ExitRequestedState
{
public bool Requested { get; set; }
}
}

View file

@ -0,0 +1,45 @@
using CSMic;
namespace cs_mic_cli.Functions
{
internal class Exit : ICodedFunction
{
private readonly ExitRequestedState exitRequestedState;
public string Name
{
get
{
return "exit";
}
}
public IEnumerable<FunctionArgument> ExpectedArguments
{
get
{
yield break;
}
}
public FunctionValue ReturnValue
{
get
{
return FunctionValue.NONE;
}
}
public Exit(ExitRequestedState exitRequestedState)
{
this.exitRequestedState = exitRequestedState;
}
public FunctionValue Execute(params FunctionArgument[] args)
{
this.exitRequestedState.Requested = true;
return FunctionValue.NONE;
}
}
}

View file

@ -71,16 +71,17 @@ namespace cs_mic_cli
if (string.IsNullOrWhiteSpace(fullExpression) || options.Interactive) if (string.IsNullOrWhiteSpace(fullExpression) || options.Interactive)
{ {
// console mode // console mode
bool exitRequested = false; var exitRequestedState = new ExitRequestedState();
inputInterpreter.RegisterFunction(new Exit(exitRequestedState));
Console.CancelKeyPress += (_, e) => Console.CancelKeyPress += (_, e) =>
{ {
e.Cancel = true; e.Cancel = false;
exitRequested = true; exitRequestedState.Requested = true;
Console.WriteLine(); Console.WriteLine();
}; };
while (!exitRequested) while (!exitRequestedState.Requested)
{ {
Console.Write("csmic> "); Console.Write("csmic> ");
@ -92,7 +93,7 @@ namespace cs_mic_cli
break; break;
} }
Calculate(inputInterpreter, options, fullExpression); Calculate(inputInterpreter, options, fullExpression, exitRequestedState);
} }
} }
else else
@ -104,10 +105,15 @@ namespace cs_mic_cli
return 0; return 0;
} }
private static void Calculate(InputInterpreter inputInterpreter, Options options, string expression) private static void Calculate(InputInterpreter inputInterpreter, Options options, string expression, ExitRequestedState? exitRequestedState = null)
{ {
inputInterpreter.Interpret(expression); inputInterpreter.Interpret(expression);
if (exitRequestedState?.Requested == true)
{
return;
}
Console.WriteLine(inputInterpreter.NumericValue); Console.WriteLine(inputInterpreter.NumericValue);
if(options.Verbose) if(options.Verbose)
{ {