CSMic 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CSMic --version 2.0.0
                    
NuGet\Install-Package CSMic -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CSMic" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSMic" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CSMic" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CSMic --version 2.0.0
                    
#r "nuget: CSMic, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CSMic@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CSMic&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CSMic&version=2.0.0
                    
Install as a Cake Tool

CS-MIC

CS-MIC is a small, embeddable expression interpreter for .NET applications. It is designed for places where users need to enter flexible numeric input, while the host application needs a deterministic decimal result and a controlled extension surface.

The 2.0 release separates the project into two NuGet packages:

  • CSMic: the core parser, interpreter, variable store, and custom function API.
  • CSMic.StandardLibrary: optional constants and common math functions built on top of the core package.

CS-MIC targets netstandard2.1.

Installation

Install the core interpreter when you want to parse expressions and provide your own functions:

dotnet add package CSMic

Install the standard library when you also want built-in constants and math helpers:

dotnet add package CSMic.StandardLibrary

CSMic.StandardLibrary references CSMic, so applications that use the standard library do not need to install both packages explicitly.

Basic Usage

using CSMic;

var interpreter = new InputInterpreter();

decimal result = interpreter.Interpret("2 + 3 * 4");
// result == 14
// interpreter.NumericValue == 14

Interpret returns the numeric result and also stores the last output on the interpreter. Parse and runtime errors are soft errors: the interpreter returns 0 and writes the error message to StringValue.

decimal result = interpreter.Interpret("1 / 0");

if (!string.IsNullOrEmpty(interpreter.StringValue))
{
    Console.WriteLine(interpreter.StringValue);
}

Create a new interpreter for an isolated evaluation context. Reuse an interpreter when variables, arrays, expression bindings, and registered functions should persist across calls.

Expressions

CS-MIC evaluates numeric expressions with the usual precedence rules for parentheses, powers, multiplication, division, modulus, addition, and subtraction.

Input Result
5 + 5 10
1 + 2 * 3 7
(1 + 2) * 3 9
2 ^ 8 256
7 % 4 3
2(3 + 1) 8

Comparison operators return numeric booleans: 1 for true and 0 for false.

Input Result
2 == 2 1
2 < 3 1
3 < 2 0
2 >= 2 1
2 <= 1 0

Literals

Numbers are decimal by default. Hexadecimal values use a 0x prefix, and binary values use a b suffix.

Input Result
100 100
0xFF 255
1010b 10
0xFF * 1010b 2550

String literals are accepted only as function arguments. They are not standalone expression values, variables, or arithmetic operands.

Variables And Arrays

Use :: to assign a numeric value. Numeric variables are evaluated immediately and persist on the interpreter.

interpreter.Interpret("x :: 4");  // 4
interpreter.Interpret("x + 6");   // 10

Use := to assign an expression binding. Expression bindings are evaluated when referenced, so they can reflect later changes to other variables.

interpreter.Interpret("x :: 2");
interpreter.Interpret("doubleX := 2 * x");

interpreter.Interpret("doubleX"); // 4

interpreter.Interpret("x :: 5");
interpreter.Interpret("doubleX"); // 10

Use -> to assign a numeric array, then index it with zero-based indexes.

interpreter.Interpret("values -> [10, 20, 30]");
interpreter.Interpret("values[1]"); // 20

Standard Library

Add CSMic.StandardLibrary and initialize the interpreter to register the standard functions and constants:

using CSMic;
using CSMic.StandardLibrary;

var interpreter = new InputInterpreter();
Initializer.InitializeAll(interpreter);

decimal area = interpreter.Interpret("pi * 10^2");
decimal angle = interpreter.Interpret("degrees(pi / 2)");

InitializeAll registers all functions and constants. You can also opt into smaller groups with InitializeAllFunctions, InitializeConstants, InitializeBaseFunctions, InitializeAngleFunctions, InitializeRoundingFunctions, InitializeTrigonometryFunctions, InitializeNumberTheoryFunctions, and InitializeRandomFunctions.

The standard library includes:

  • Base functions: abs, sign, min, max
  • Angle helpers: degrees, radians, wrapangle
  • Rounding helpers: floor, ceiling, truncate, frac, round, clamp
  • Trigonometry: sin, cos, tan, asin, acos, atan, atan2
  • Hyperbolic trigonometry: sinh, cosh, tanh, asinh, acosh, atanh
  • Number theory: fac, ncr, npr, gcd, lcm
  • Random helpers: flip, bern, rand, rands, randn, randns
  • Constants: pi, e, tau, phi, goldenratio, eurler, omega

Custom Functions

Register custom functions by implementing ICodedFunction.

using CSMic;

public sealed class Square : ICodedFunction
{
    public string Name => "square";

    public IEnumerable<FunctionArgument> ExpectedArguments =>
        new[] { new FunctionArgument("value", FunctionValue.NUMBER) };

    public FunctionValue ReturnValue => FunctionValue.NUMBER;

    public FunctionValue Execute(params FunctionArgument[] args)
    {
        var value = (decimal)args[0].Value.Value!;
        return new FunctionValue(FunctionValueType.Numeric, value * value);
    }
}

var interpreter = new InputInterpreter();
interpreter.RegisterFunction(new Square());

decimal result = interpreter.Interpret("square(12)");
// result == 144

Functions can accept numeric or string arguments. String arguments are useful for host-defined keys, modes, or labels while preserving CS-MIC's numeric-first expression model.

Building From Source

dotnet restore src/CsMic.sln
dotnet test src/CsMic.sln
dotnet pack src/Core/CSMic.Core.csproj -c Release
dotnet pack src/StandardLibrary/CSMic.StandardLibrary.csproj -c Release

The core project uses Coco/R during build to generate parser and scanner code from src/Core/cocor/Interpreter.atg.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on CSMic:

Package Downloads
CSMic.StandardLibrary

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.