139 lines
4.7 KiB
C#
139 lines
4.7 KiB
C#
using cs_mic_cli.Functions;
|
|
using CSMic;
|
|
using CSMic.StandardLibrary;
|
|
using System.CommandLine;
|
|
|
|
namespace cs_mic_cli
|
|
{
|
|
internal class Program
|
|
{
|
|
|
|
static int Main(string[] args)
|
|
{
|
|
var verboseOption = new Option<bool>("--verbose", "-v")
|
|
{
|
|
Description = "Displays error messages from the parser if a soft error occurs."
|
|
};
|
|
var interactiveOption = new Option<bool>("--interactive", "-i")
|
|
{
|
|
Description = "Starts an interactive session."
|
|
};
|
|
var versionOption = new Option<bool>("--version", "-v")
|
|
{
|
|
Description = "Prints the version number of CS-MIC."
|
|
};
|
|
var expressionArgument = new Argument<string[]>("expression")
|
|
{
|
|
Arity = ArgumentArity.ZeroOrMore,
|
|
Description = "Expression to evaluate."
|
|
};
|
|
expressionArgument.Validators.Add(result =>
|
|
{
|
|
foreach (var token in result.Tokens)
|
|
{
|
|
if (token.Value.StartsWith("--", StringComparison.Ordinal))
|
|
{
|
|
result.AddError($"Unrecognized command or argument '{token.Value}'.");
|
|
}
|
|
}
|
|
});
|
|
var rootCommand = new RootCommand("CSMic command line client")
|
|
{
|
|
verboseOption,
|
|
interactiveOption,
|
|
expressionArgument
|
|
};
|
|
|
|
rootCommand.SetAction(parseResult =>
|
|
{
|
|
var options = new Options
|
|
{
|
|
Verbose = parseResult.GetValue(verboseOption),
|
|
Interactive = parseResult.GetValue(interactiveOption),
|
|
Version = parseResult.GetValue(versionOption),
|
|
Expression = parseResult.GetValue(expressionArgument) ?? Array.Empty<string>()
|
|
};
|
|
|
|
return Run(options);
|
|
});
|
|
|
|
return rootCommand.Parse(args).Invoke();
|
|
}
|
|
|
|
private static int Run(Options options)
|
|
{
|
|
var fullExpression = string.Join(' ', options.Expression);
|
|
var inputInterpreter = new InputInterpreter();
|
|
|
|
Initializer.InitializeAll(inputInterpreter);
|
|
inputInterpreter.RegisterFunction(new PrintVariables(inputInterpreter));
|
|
inputInterpreter.RegisterFunction(new Functions.Version());
|
|
|
|
if(!options.Verbose)
|
|
{
|
|
CSMic.Interpreter.Errors.errorStream = TextWriter.Null;
|
|
}
|
|
|
|
if(options.Version)
|
|
{
|
|
Console.WriteLine(Functions.Version.VersionCLI);
|
|
Console.WriteLine(Functions.Version.VersionCSMIC);
|
|
Console.WriteLine(Functions.Version.VersionStandardLibrary);
|
|
|
|
return 0;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(fullExpression) || options.Interactive)
|
|
{
|
|
// console mode
|
|
var exitRequestedState = new ExitRequestedState();
|
|
inputInterpreter.RegisterFunction(new Exit(exitRequestedState));
|
|
|
|
Console.CancelKeyPress += (_, e) =>
|
|
{
|
|
e.Cancel = false;
|
|
exitRequestedState.Requested = true;
|
|
Console.WriteLine();
|
|
};
|
|
|
|
while (!exitRequestedState.Requested)
|
|
{
|
|
Console.Write("csmic> ");
|
|
|
|
fullExpression = Console.ReadLine();
|
|
|
|
if (string.IsNullOrWhiteSpace(fullExpression))
|
|
{
|
|
Console.WriteLine();
|
|
break;
|
|
}
|
|
|
|
Calculate(inputInterpreter, options, fullExpression, exitRequestedState);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// one-shot mode
|
|
Calculate(inputInterpreter, options, fullExpression);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
private static void Calculate(InputInterpreter inputInterpreter, Options options, string expression, ExitRequestedState? exitRequestedState = null)
|
|
{
|
|
inputInterpreter.Interpret(expression);
|
|
|
|
if (exitRequestedState?.Requested == true)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Console.WriteLine(inputInterpreter.NumericValue);
|
|
if(options.Verbose)
|
|
{
|
|
Console.WriteLine("Executed in {0:N2} ms | Variables: {1:N0}", inputInterpreter.LastExecutionTime.TotalMilliseconds, inputInterpreter.Variables.Count());
|
|
}
|
|
}
|
|
}
|
|
}
|