Usage

The main API is InputInterpreter.

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

Basic Evaluation

using CSMic;

var interpreter = new InputInterpreter();

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

Interpret returns the numeric result and updates the interpreter state:

  • NumericValue: numeric result of the latest successful expression, or 0 for a soft error.
  • StringValue: empty for a successful numeric expression, or an error message for a soft error.
  • LastExecutionTime: elapsed time for the latest interpretation.
  • Variables: current numeric variables, expression variables, and numeric arrays.

Error Handling

Normal parse and evaluation failures are soft errors. CS-MIC returns 0 and writes the error message to StringValue.

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

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

Expressions

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

InputResult
5 + 510
1 + 2 * 37
(1 + 2) * 39
2 ^ 8256
7 % 43
2(3 + 1)8

Comparisons

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

InputResult
2 == 21
2 < 31
3 < 20
2 >= 21
2 <= 10

Numeric Literals

Numbers are decimal by default. Hexadecimal values use a 0x prefix. Binary values use a b suffix.

InputResult
100100
0xFF255
1010b10
0xFF * 1010b2550

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

Variables

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

Interpreter State

Interpreter state is intentionally persistent. This makes repeated evaluation useful for applications that expose a stable set of named values.

interpreter.Interpret("taxRate :: 0.0825");
interpreter.Interpret("subtotal :: 120");

decimal total = interpreter.Interpret("subtotal * (1 + taxRate)");

Create a new InputInterpreter when state should not be shared between evaluations or users.