Version 1

This commit is contained in:
Jordan Wages 2026-06-30 02:28:48 -05:00
commit f467d6eeae
6 changed files with 208 additions and 0 deletions

1
.gitignore vendored
View file

@ -428,3 +428,4 @@ FodyWeavers.xsd
*.msm *.msm
*.msp *.msp
src/.github/copilot-instructions.md

25
src/cs-mic-cli.sln Normal file
View file

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.37328.6 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "cs-mic-cli", "cs-mic-cli\cs-mic-cli.csproj", "{D241250D-97BC-4D1F-A3B4-8A084D72DA7C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D241250D-97BC-4D1F-A3B4-8A084D72DA7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D241250D-97BC-4D1F-A3B4-8A084D72DA7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D241250D-97BC-4D1F-A3B4-8A084D72DA7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D241250D-97BC-4D1F-A3B4-8A084D72DA7C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {198FC807-9454-4AF0-BA73-30DFE9A7F7E4}
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,65 @@
using CSMic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cs_mic_cli.Functions
{
internal class PrintVariables : ICodedFunction
{
public InputInterpreter Parent
{
get;
private set;
}
public string Name
{
get
{
return "printvariables";
}
}
public IEnumerable<FunctionArgument> ExpectedArguments
{
get
{
yield break;
}
}
public FunctionValue ReturnValue
{
get
{
return FunctionValue.NONE;
}
}
public PrintVariables(InputInterpreter parent)
{
this.Parent = parent;
}
public FunctionValue Execute(params FunctionArgument[] args)
{
int variableIndex = 1;
foreach(var variable in this.Parent.Variables)
{
var displayValue = variable.Value ?? string.Empty;
if(variable.Type == VariableType.NumericArray && variable.Value != null)
{
displayValue = string.Format("[{0}]", string.Join(", ", (decimal[])variable.Value));
}
Console.WriteLine("{0}. {1} {2} = {3}", variableIndex++, variable.Type, variable.Name, displayValue);
}
return FunctionValue.NONE;
}
}
}

21
src/cs-mic-cli/Options.cs Normal file
View file

@ -0,0 +1,21 @@
using CommandLine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cs_mic_cli
{
internal class Options
{
[Option('v', "verbose", HelpText = "Displays error messages from the parser if a soft error occurs.")]
public bool Verbose { get; set; }
[Option('i', "interactive", HelpText = "Starts an interactive session.")]
public bool Interactive { get; set; }
[Value(0)]
public required IEnumerable<string> Expression { get; set; }
}
}

77
src/cs-mic-cli/Program.cs Normal file
View file

@ -0,0 +1,77 @@
using CommandLine;
using cs_mic_cli.Functions;
using CSMic;
using CSMic.StandardLibrary;
namespace cs_mic_cli
{
internal class Program
{
static void Main(string[] args)
{
CommandLine.Parser.Default.ParseArguments<Options>(args)
.WithParsed(options =>
{
var fullExpression = string.Join(' ', options.Expression);
var inputInterpreter = new InputInterpreter();
Initializer.InitializeAll(inputInterpreter);
inputInterpreter.RegisterFunction(new PrintVariables(inputInterpreter));
if(!options.Verbose)
{
CSMic.Interpreter.Errors.errorStream = TextWriter.Null;
}
if (string.IsNullOrWhiteSpace(fullExpression) || options.Interactive)
{
// console mode
bool exitRequested = false;
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
exitRequested = true;
Console.WriteLine();
};
while (!exitRequested)
{
Console.Write("csmic> ");
fullExpression = Console.ReadLine();
if (string.IsNullOrWhiteSpace(fullExpression))
{
Console.WriteLine();
break;
}
Calculate(inputInterpreter, options, fullExpression);
}
}
else
{
// one-shot mode
Calculate(inputInterpreter, options, fullExpression);
}
})
.WithNotParsed<Options>(options =>
{
});
}
private static void Calculate(InputInterpreter inputInterpreter, Options options, string expression)
{
inputInterpreter.Interpret(expression);
Console.WriteLine(inputInterpreter.NumericValue);
if(options.Verbose)
{
Console.WriteLine("Executed in {0:N2} ms | Variables: {1:N0}", inputInterpreter.LastExecutionTime.TotalMilliseconds, inputInterpreter.Variables.Count());
}
}
}
}

View file

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>cs_mic_cli</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="CSMic" Version="2.1.0" />
<PackageReference Include="CSMic.StandardLibrary" Version="2.1.1" />
</ItemGroup>
</Project>