uCalc API Version: 2.1.3-preview.2 Released: 6/16/2026
Warning
uCalc API Preview Release Notice:The documentation describes the intended behavior of the API. The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.
How Expressions Are Processed
Product:
Class:
Explains uCalc's two-stage process for handling expressions: parsing (compilation) and evaluation (execution).
Remarks
📜 Expressions and Parsing: The Core of uCalc
At its heart, uCalc is an engine designed to understand and compute expressions. An expression is simply a string of text that contains instructions, which can range from simple arithmetic to complex logical and string-based operations.
"5 * (10 + 2)""'Hello' + ' World!'""IIf(x > 0, Sin(x), 0)"
Understanding how uCalc processes these strings is the key to writing efficient and powerful code. The entire process is a two-stage pipeline: Parsing and Evaluation.
1. Stage One: Parsing (The Blueprint)
Parsing is the process of converting a human-readable string into a structured, machine-executable format. This is the most computationally intensive part of the process and is what makes uCalc intelligent. It involves two sub-steps:
Lexical Analysis (Tokenization): The engine first scans the string and breaks it into a sequence of meaningful units called tokens. For the expression
"x = 10.5", the tokens would bex(Identifier),=(Operator), and10.5(Number). This process is configured by the Tokens collection.Syntactic Analysis (AST Building): The stream of tokens is then analyzed to build a hierarchical structure known as an Abstract Syntax Tree (AST). This tree represents the order of operations, respecting operator precedence and parentheses. This tree is the final, compiled blueprint of the expression.
The Parse method is the function dedicated to performing this stage. It takes a string and returns a compiled Expression object, which contains this blueprint.
2. Stage Two: Evaluation (The Execution)
Evaluation is the process of walking through the AST created during parsing and performing the specified operations to produce a final result. This step is extremely fast because all the hard analytical work has already been done.
The Evaluate method, called on a parsed Expression object, performs this stage.
🚀 The Performance Strategy: Parse-Once, Evaluate-Many
The single most important concept for writing high-performance code in uCalc is to separate these two stages. While convenience methods like EvalStr are useful, they perform both parsing and evaluation on every call. In a loop, this is highly inefficient.
The optimal pattern is to parse once, evaluate many:
// Parse the expression ONCE, before the loop begins.var expr = uc.Parse("x * x + 2");// Inside the loop, only the fast evaluation step is performed.for ( i = 1; i <= 1000; i++) { // Update variable value x.Value(i); // Evaluate the pre-parsed object Console.WriteLine(expr.Evaluate()); }This pattern allows calculations to run at speeds that approach native compiled code, making it suitable for real-time simulations, data processing, and other performance-critical tasks. For a detailed guide, see the Optimizing Performance tutorial.